Pessoal, gostaria de fazer uma classe para testar a classe DotComBust do exercício Sink a Dot Com do livro Use a Cabeça Java.
Meu código preparatório possue 5 passos mas não consigo sair do 2°.
1° Instanciar um objeto DotComBust
2° Chamar o método Set UpGame() e exibir os nomes e os locais dos objetos DotCom.
Aí fica difícil, porque não consigo escrever um código que exiba os nomes e os locais dos objetos DotCom.
O jogo possue 3 classes:
Classe DotCom
Classe DotComBust (o jogo) - Está é a classe que quero testar.
Classe GameHelper (auxiliar)
DotCom
import java.util.*;
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, vc acertou na agua!";
int index = locationCells.indexOf(userInput);
if (index >= 0 ) {
locationCells.remove(index);
if (locationCells.isEmpty()){
result = "eliminado";
} else {
result = "correto";
}
}
return result;
}
}
DotComBust - Classe que quero testar
import java.util.*;
class DotComBust {
private GameHelper helper = new GameHelper();
private ArrayList<DotCom> dotComsList = new ArrayList<DotCom>();
private int numOfGuesses = 0;
public 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 objetivo e eliminar tres dot coms.");
System.out.println("Pets.com, eToys.com, Go2.com");
System.out.println("Tente eliminar todas com o menor numero de palpites");
for (DotCom dotComToSet : dotComsList) {
ArrayList<String> newLocation = helper.placeDotCom(3);
dotComToSet.setLocationCells(newLocation);
} // encerra o loop for
} // encerra o método setUpGame
public void startPlaying() {
while(!dotComsList.isEmpty()) {
String userGuess = helper.getUserInput("Insira um palpite");
checkUserGuess(userGuess);
} // encerra o while
} // encerra o metodo startPlaying
public void checkUserGuess(String userGuess) {
numOfGuesses++;
String result = "errado";
for (DotCom dotComtoTest : dotComsList) {
result = dotComtoTest.checkYourself(userGuess);
if (result.equals("correto")) {
break;
}
if (result.equals("eliminado")) {
dotComsList.remove(dotComtoTest);
break;
}
} // encerra for
System.out.println(result);
} // encerra o método
public void finishGame () {
System.out.println("Todas as Dots Com foram mortas, agora o seu conjunto está vazio");
if (numOfGuesses <= 18) {
System.out.println(" Voce soh usou" + numOfGuesses + " palpites ") ;
System.out.println(" Voce saiu antes de elomonar suas opcoes.");
} else {
System.out.println("Demorou demais " + numOfGuesses + " palpites ");
System.out.println(" Vc usou muitas opcoes! " ) ;
}
}
public static void main (String[] args) {
DotComBust game = new DotComBust();
game.setUpGame();
game.startPlaying();
} // encerra o método
} // encerra a classe
GameHelper - Classe Auxiliar
import java.io.*;
import java.util.*;
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 {
BufferedReader 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) { // line 19
ArrayList<String> alphaCells = new ArrayList<String>();
String [] alphacoords = new String [comSize]; // holds 'f6' type coords
String temp = null; // temporary String for concat
int [] coords = new int[comSize]; // current candidate coords
int attempts = 0; // current attempts counter
boolean success = false; // flag = found a good location ?
int location = 0; // current starting location
comCount++; // nth dot com to place
int incr = 1; // set horizontal increment
if ((comCount % 2) == 1) { // if odd dot com (place vertically)
incr = gridLength; // set vertical increment
}
while ( !success & attempts++ < 200 ) { // main search loop (32)
location = (int) (Math.random() * gridSize); // get random starting point
//System.out.print(" try " + location);
int x = 0; // nth position in dotcom to place
success = true; // assume success
while (success && x < comSize) { // look for adjacent unused spots
if (grid[location] == 0) { // if not already used
coords[x++] = location; // save location
location += incr; // try 'next' adjacent
if (location >= gridSize){ // out of bounds - 'bottom'
success = false; // failure
}
if (x>0 & (location % gridLength == 0)) { // out of bounds - right edge
success = false; // failure
}
} else { // found already used location
// System.out.print(" used " + location);
success = false; // failure
}
}
} // end while
int x = 0; // turn good location into alpha coords
int row = 0;
int column = 0;
// System.out.println("\n");
while (x < comSize) {
grid[coords[x]] = 1; // mark master grid pts. as 'used'
row = (int) (coords[x] / gridLength); // get row value
column = coords[x] % gridLength; // get numeric column value
temp = String.valueOf(alphabet.charAt(column)); // convert to alpha
alphaCells.add(temp.concat(Integer.toString(row)));
x++;
// System.out.print(" coord "+x+" = " + alphaCells.get(x-1));
}
// System.out.println("\n");
return alphaCells;
}
}
DotComBustTestDrive - Classe que quero fazer para testar a classe DotComBust
import java.util.*;
public class DotComBustTestDrive {
public static void main (String[] args) {
DotComBust dot = new DotComBust(); //instancia um objeto DotComBust
// Chamar metodo setUpGame
dot.setUpGame();
// Exibir DotComList
}
}
Quem puder me ajudar, será de grande valia!!!