Compiles but doesn't play right
Hi, this code compiles, but it doesn't play right at all.
The following is what I try to do with this simple command prompt game:
- Have 2 cats hide in a random location, both stored in the ArrayList<String> called kittyLocation indicated in the 6 possible strings in the string[] possibilities
- Have the user input a guess
- Check the guess to see if it matches one of the possible hiding places
- If it does, take away the location it was in from the ArrayList
- When both locations are found, the game ends.
The following is what happens
No matter what, it returns that no cat was found, and it increases the int KittehsFound by 1 regardless of if the guess matched or not.
Thanks in advance for your help!
Heres the code:
Code :
import java.util.ArrayList;
import java.io.*;
class GameHelper {
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();
}
}
class LostCats {
private ArrayList<String> kittyLocation;
int KittehsFound=0;
public void setLocation(ArrayList<String> loc) {
kittyLocation=loc;
}
public String checkGuess(String userInput) {
String k="Neither of them are there. :(";
int index=kittyLocation.indexOf(userInput);
if (index>=0) {
KittehsFound++;
k="You found one! :D";
kittyLocation.remove(index);
if (kittyLocation.isEmpty()) {
k="You found them both!! :DD";
}
}
System.out.println(k);
System.out.println("You have found "+KittehsFound+" of them.");
return k;
}
}
public class FindTheCats {
public static void main(String[] args) {
GameHelper g=new GameHelper();
System.out.println("HuskehGarrett and KaffehKitteh are missing!");
LostCats l1=new LostCats();
try {
Thread.sleep(1500);
}
catch (InterruptedException ie) {
}
System.out.println("Can you find them?");
System.out.println("-----------------------------------------------------------------");
ArrayList<String> location=new ArrayList<String>();
String[] possibilities={"Attic","Bedroom","Refrigerator","Basement","Yard","Pantry"};
int x=0;
int y=0;
boolean GameGoing=true;
int Guesses=0;
x=(int)(Math.random()*possibilities.length);
y=(int)(Math.random()*possibilities.length);
while(x==y) {
y=(int)(Math.random()*possibilities.length);
}
location.add(possibilities[x]);
location.add(possibilities[y]);
l1.setLocation(location);
String[] q=new String[possibilities.length];
String guess=null;
String result=null;
while(GameGoing==true) {
System.out.println();
for(int i=0;i<possibilities.length;i++) {
q[i]=possibilities[i];
if (i<possibilities.length-1) {
System.out.print(q[i]+", ");
} else {
System.out.print(q[i]);
}
}
guess=g.getUserInput("?");
result=l1.checkGuess(guess);
if (result.equals("You found them both!! :DD")) {
GameGoing=false;
}
}
}
}
And regarding creating a game involving finding a kitten and a husky, it's for my girlfriend.