import java.util.ArrayList; import java.util.Scanner; public class game { private ArrayList<String> ship = new ArrayList<Ship>(); int numGuess = 0; public static void main(String[] args){ Game game = new Game(); game.setup(); } private void setup(){ ship.add(new Ship("sub",3)); ship.add(new Ship("Battle Ship",3)); ship.add(new Ship("War Machine",3)); setLocations(); System.out.println("welcome"); play(); } private void play(){ String guess, result; Scanner input = new Scanner(System.in); while(!ship.isEmpty()){ result = "miss"; numGuess++; guess = input.nextLine(); guess = guess.toUpperCase(); for(int i=0; i<ship.size();i++){ result = ship.get(i).check(guess); if(result.equals("kill")){ System.out.print("you sunk" + ship.get(i).getName()); ship.remove(i); break; }else if(result.equals("hit")){ break; } System.out.println(result); } input.close(); finish(); } } }
import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class SimpleShipTest{ Scanner input = new Scanner(System.in); Random rand = new Random(); SimpleShip ship = new SimpleShip(); String userGuess; int numOfGuess = 0; boolean isAlive = true; int temp = rand.nextint(5) + 1; int [] locations = {temp,++temp,++temp}; ship.setLocations(locations); while(isAlive){ String result; System.out.println("Enter a guess"); userGuess = input.nextLine(); result = ship.checkGuess(userGuess); numOfGuess++; if(result.equals("kill")){ isAlive = false; } } System.out.println("you took" + numofguess+ "to sink the ship"); }