import java.util.ArrayList;
public class SimpleDotComTester
{
public static void main(String [] args)
{
int numOfGuesses =0;
GameHelper helper=new GameHelper();
DotCom theDotCom=new DotCom();
//int randomNum=(int)(Math.random()*5);
//int [] locations={randomNum,randomNum+1,randomNum+2};
ArrayList<String> locations=new ArrayList<String>();
locations.add("5");
locations.add("4");
locations.add("3");
theDotCom.setLocationCells(locations);
boolean isAlive=true;
while(isAlive==true)
{
String guess=helper.getUserInput("enter a number");
String result=theDotCom.checkYourself(guess);
numOfGuesses++;
if(result.equals("kill"))
{
isAlive=false;
System.out.println("You took "+numOfGuesses+" guesses");
}
}
}
}
import java.util.ArrayList;
public class DotCom
{
private ArrayList<String> locationCells;
public void setLocationCells(ArrayList<String> locs)
{
locationCells=locs;
}
public String checkYourself(String stringGuess)
{
//int guess=Integer.parseInt(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;
}
}
import java.io.*;
public 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("IOExpection: "+e);
}
return inputLine;
}
}