import java.awt.*; import java.util.*; /** * The class which initiates the game. */ public class Game { public static final int DEFAULT_NUMBER_OF_SHIPS = 5; public static final int BOARD_X_COORDINATE = 15; public static final int BOARD_Y_COORDINATE = 15; public static Random rand = new Random(); /** * The main program. * * @param args the command line inputs. **/ public static void main(String args[]) { Board board1, board2; // = new Board(BOARD_X_COORDINATE, BOARD_Y_COORDINATE); //Human player1 = new Human(); AI player1 = new AI(0); AI player2 = new AI(1); // Setup, display and check board board1 = player1.setupBoard(BOARD_X_COORDINATE, BOARD_Y_COORDINATE); MapPrinter.printBoardForPlayer(player1.getPronoun() + " board:", board1); if ( !board1.isValidBoard() ) { System.out.println (player1.getPronoun() + " board is invalid!"); System.exit(0); } // Setup AI's board board2 = player2.setupBoard(BOARD_X_COORDINATE, BOARD_Y_COORDINATE); MapPrinter.printBoardForPlayer(player2.getPronoun() + " board:", board2); if ( !board2.isValidBoard() ) { System.out.println (player2.getPronoun() + " board is invalid!"); System.exit(0); } // now play the Game int round = 0; while ( true ) { // Print round number and boards System.out.println ("Round " + round); player1.print(board1); player2.printForOpponent(board2); // Player goes first Point location = player1.getNextShot(); System.out.print (player1.getIntro() + " (" + ((int) location.getX()) + "," + ((int) location.getY()) + ")..."); int retStatus = board2.processShot((int) location.getX(), (int) location.getY()); player1.reportStatus (retStatus); if ( board2.allShipsSunk() ) break; // Computer goes second location = player2.getNextShot(); System.out.print (player2.getIntro() + " (" + ((int) location.getX()) + "," + ((int) location.getY()) + ")..."); retStatus = board1.processShot((int) location.getX(), (int) location.getY()); player1.reportStatus (retStatus); if ( board1.allShipsSunk() ) break; round++; } System.out.println ("\nGame over!"); if ( board1.allShipsSunk() ) System.out.println (player2.getName() + " wins!\n"); else System.out.println (player1.getName() + " wins!\n"); player1.print(board1); player2.print(board2); System.out.println ("It took " + round + " rounds"); } }