Estou começando em Java.
[color=darkred][/color]
Primeiro estou brincando com o Livro Use a Cabeça Java. Nele tem um jogo de batalha naval, onde revisa todos os conceitos vistos nos capítulos anteriores.
Tentei compilar mas não deu certo.
[color=darkred]Há alguma coisa errada que não está certa![/color]
Alguém poderia me dá uma dica. Obrigado.
import java.io.*;
import java.util.*;
public class DotComBust {
private GameHelper helper = new GameHelper();
private ArrayList<DotCom> dotComsList = new ArrayList<DotCom>();
private int numOfGuess = 0;
private void setUpGame() {
//primeiro cria alguns objetos Dotcom e fornece seus locais
DotCom one = new DotCom();
one.setName("Pets.com");
DotCom two = new DotCom();
two.setName("eToys.com");
DotCom three = new DotCom();
three.setName("Go2.com");
dotComsList.add(one);
dotComsList.add(two);
dotComsList.add(three);
System.out.println("Seu obejtivo é eliminar três dot coms.");
System.out.println("Pets.com, eToys.com, Go2.com");
System.out.println("Tente eliminar todas com o menor número de palpites");
for (DotCom dotComToSet : dotComsList) {
ArrayList<String> newLocation = helper.placeDotCom(3);
dotComToSet.setLocationCells(newLocation);
}
}
private void startPlaying() {
while (!dotComsList.isEmpty()) {
String userGuess = helper.getUserInput("Insira um palpite");
checkUserGuess (userGuess);
} // encerra while
finishGame();
} // encerra metodo startPlaying
private void checkUserGuess (String userGuess) {
numOfGuesses++;
String result = "errado";
for (DotCom dotComToTest : dotComsList) {
result = dotComToTest.checkYourself (userGuess);
if (result.equals("correto")) {
break;
}
if (result.equals("eliminar")) {
dotComsList.remove(dotComToTest);
break;
}
} // encerra for
System.out.println(result);
}// encerra o método
private void finishGame() {
System.out.println("Todas as Dot Comns foram eliminadas! Agora seu conjunto está vazio.");
if (numOfGuess <= 18) {
System.out.println("Você só usou " + numOfGuess + " palpites.");
System.out.println("Você saiu antes de eliminar suas opções.");
} else {
System.out.println("Demorou demais. " + numOfGuess + "palpites");
system.out.println("Nao havera pesca com essas opcoes");
}
} // encerra o metodo
public static void main (String[] args) {
DotComBust game = new DotComBust();
game.setUpGame();
game.startPlaying();
}
}
public class DotCom {
private ArrayList<String> locationCells;
private String name;
public void setLocationCells(ArrayList<String> loc) {
locationCells = loc;
}
public void SetName(String n) {
name = n;
}
public String checkYourself (String userInput) {
String result = "errado";
int index = locationCells.indexOf(userInput);
if (index >= 0) {
locationCells.remove(index);
if (locationCells.isEmpty()) {
result = "eliminar";
System.out.println("Ora! Voce afundou " + name);
} else {
result = "correto";
}
return result;
}
}
public class GameHelper {
private static final String alphabet = "abcdefg";
private int gridLength = 7;
private int gridSize = 49;
private int[] grid = new int[gridSize];
private int comCount = 0;
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BuffereReader is = new BufferedReader (new InputStreamReader(System.in));
inputLine = is.readLine();
if ( inputLine.length() == 0 ) return null;
} catch (IOException e) {
system.out.println("IOException: " + e);
}
return inputLine.toLowerCase();
}
public ArrayList<String> placeDotCom (int comSize) {
ArrayList<String> alphaCells = new ArrayList<String>();
String[] alphacoords = new String [comSize];
String temp = null;
int attempts = 0;
comCount++;
int incr = 1;
if ( (comCount % 2) == 1) {
incr = gridLength;
}
while (!success & attempts++ < 200) {
location = (int) (Math.random() * gridSize);
//System.out.print("try " + location);
int x = 0;
success = true;
while (sucess && x < comSize) {
if (grid[location] == 0) {
coords[x++] = location;
location += incr;
if (location >= gridSize) {
success = false;
}
if (x > 0 && (location % gridLength == 0)) {
success = false;
}
} else {
// system.out.print(" used " + location);
success = false;
}
}
}
int x = 0;
int row = 0;
int column = 0;
//System.out.println("\n");
while (x < comSize) {
grid[coords[x]] = 1;
row = (int) (coords[x] / gridLength);
column = coords[x] % gridLength;
temp = String.valueOf(alphabet.charAt(column));
alphaCells.add(temp.concat(Integer.toString(row)));
x++;
//System.out.print(" coord " +x+ " +alphaCells.get(x-1));
}
//System.out.println("\n");
return alphaCells;
}
}
Não uso nenhuma IDE, por enquanto.
Nome do arquivo salvo: "DotComBust.java" o mesmo da class.
Compilado: javac DotComBust.java (criando arquivo DotComBust.class).
Não executa: java DotComBust (gera erro).
Att., Hugo.