I'm learning Java with Head First Java.
I've built a program that creates a 7x7 grid (A0 to G6) and places an object of 3 consecutive values inside it. The user has to guess the location and when the 3 values are found, the program will confirm that the object is destroyed.
My problem right now is that I'm not satisfied with my current implementation, even if it works.
In the DotComBust class, where the main sits, I had created those 3 consecutive values on a 7x7 grid and then I assigned them to an ArrayList field in DotCom. Then, I had asked the user for an input and his input is sent to a method, which traverses this ArrayList field and removes this value from the list if it's found there. Once all 3 values are knocked out, the program concludes that the object has been killed and outputs the number of guesses.
There are 96 lines of code that correspond to the creation of this grid (lines 14 to 115). I think that this is too much and I'm not sure if my logic is wrong, the choice of statements or both.
DotCom.java
import java.util.ArrayList;
public class DotCom {
private ArrayList<String> locationCells;
private int numOfHits = 0;
public DotCom() {
}
public void setLocationCells(ArrayList<String> locs) {
locationCells = locs;
}
public String checkYourself (String stringGuess) {
String result = "miss";
int index = locationCells.indexOf(stringGuess);
if (index >= 0) {
locationCells.remove(index);
if (locationCells.isEmpty()) {
result = "kill";
} else {
result = "hit";
}
}
System.out.println(result);
return result;
}
}
DotComBust.java (the main and where the "grid" is created)
import java.util.ArrayList;
import java.util.Scanner;
public class DotComBust {
public static void main(String[] args) {
int numOfGuesses = 0;
DotCom dot = new DotCom();
int firstLocationVInteger = (int) (Math.random() * 7);
int firstLocationH = (int) (Math.random() * 7);
int secondLocationVInteger = 0;
int thirdLocationVInteger = 0;
int secondLocationH = 0;
int thirdLocationH = 0;
int directionChoice;
if (((firstLocationVInteger >= 0) && (firstLocationVInteger <= 4)) && ((firstLocationH >= 0) && (firstLocationH <= 4))) {
// 0 for down, 1 for right
directionChoice = (int) (Math.random() * 2);
if (directionChoice == 0) {
secondLocationVInteger = firstLocationVInteger + 1;
thirdLocationVInteger = firstLocationVInteger + 2;
secondLocationH = firstLocationH;
thirdLocationH = firstLocationH;
} else {
secondLocationH = firstLocationH + 1;
thirdLocationH = firstLocationH + 2;
secondLocationVInteger = firstLocationVInteger;
thirdLocationVInteger = firstLocationVInteger;
}
}
else {
// 0 for up, 1 for left
directionChoice = (int) (Math.random() * 2);
if (directionChoice == 0) {
secondLocationVInteger = firstLocationVInteger - 1;
thirdLocationVInteger = firstLocationVInteger - 2;
secondLocationH = firstLocationH;
thirdLocationH = firstLocationH;
} else {
secondLocationH = firstLocationH - 1;
thirdLocationH = firstLocationH - 2;
secondLocationVInteger = firstLocationVInteger;
thirdLocationVInteger = firstLocationVInteger;
}
}
String firstLocationV;
switch (firstLocationVInteger) {
case 0: firstLocationV = "A";
break;
case 1: firstLocationV = "B";
break;
case 2: firstLocationV = "C";
break;
case 3: firstLocationV = "D";
break;
case 4: firstLocationV = "E";
break;
case 5: firstLocationV = "F";
break;
case 6: firstLocationV = "G";
break;
default: firstLocationV = "Invalid range";
break;
}
String secondLocationV;
switch (secondLocationVInteger) {
case 0: secondLocationV = "A";
break;
case 1: secondLocationV = "B";
break;
case 2: secondLocationV = "C";
break;
case 3: secondLocationV = "D";
break;
case 4: secondLocationV = "E";
break;
case 5: secondLocationV = "F";
break;
case 6: secondLocationV = "G";
break;
default: secondLocationV = "Invalid range";
break;
}
String thirdLocationV;
switch (thirdLocationVInteger) {
case 0: thirdLocationV = "A";
break;
case 1: thirdLocationV = "B";
break;
case 2: thirdLocationV = "C";
break;
case 3: thirdLocationV = "D";
break;
case 4: thirdLocationV = "E";
break;
case 5: thirdLocationV = "F";
break;
case 6: thirdLocationV = "G";
break;
default: thirdLocationV = "Invalid range";
break;
}
//concatenate
String firstLocation = firstLocationV + Integer.toString(firstLocationH);
String secondLocation = secondLocationV + Integer.toString(secondLocationH);
String thirdLocation = thirdLocationV + Integer.toString(thirdLocationH);
ArrayList<String> locations = new ArrayList<>();
locations.add(firstLocation);
locations.add(secondLocation);
locations.add(thirdLocation);
dot.setLocationCells(locations);
Scanner sc = new Scanner(System.in);
boolean isAlive = true;
while (isAlive) {
System.out.print("Enter your guess: ");
String userGuess = sc.next();
String result = dot.checkYourself(userGuess);
numOfGuesses++;
if (result.equals("kill")) {
isAlive = false;
System.out.println("You took " + numOfGuesses + " guesses.");
}
}
}
}
Output sample
...
Enter your guess: F0
miss
Enter your guess: F1
miss
Enter your guess: F2
miss
Enter your guess: F3
miss
Enter your guess: F4
miss
Enter your guess: F5
miss
Enter your guess: F6
miss
Enter your guess: G0
miss
Enter your guess: G1
hit
Enter your guess: G2
hit
Enter your guess: G3
kill
You took 47 guesses.