src/com/hamish/battleships/Game.java
private void playGame() {
        int[] attackCo;
        boolean bothPlayersIn = true;
        while (bothPlayersIn) {
            for (int i = 0; i < 2; i++) {
                if (bothPlayersIn) {
                    attackCo = players[i].playerGo();
                    if (players[i == 1 ? 0 : 1].positionTaken(attackCo[0], attackCo[1])) {
                        System.out.println("Ship Hit!");
                        players[i].attackHit(attackCo[0], attackCo[1]);
                    } else {
                        System.out.println("No ships hit");
                        players[i].attackMissed(attackCo[0], attackCo[1]);
                    }
                    if (i == 0) {
                        if (!players[1].shipLeft()) {
                            bothPlayersIn = false;
                            System.out.println("Congratulations " + players[0].getName() + " won!");
                        }
                    } else {
                        if (!players[0].shipLeft()) {
                            bothPlayersIn = false;
                            System.out.println("Congratulations " + players[1].getName() + " won!");
                        }
                    }
                    System.out.println("Enter any key to continue");
                    systemIn.nextLine();
                    systemIn.nextLine();
                    clearBoard();
                }
            }
        }
    }
/**
     * Runs the game while both players have ships left. Once done prints a congratulations message
     */
Runs the game while both players have ships left. Once done prints a congratulations message
Runs the game while both players have ships left . Once done prints a congratulations message
private void playGame() {
        int[] attackCo;
        boolean bothPlayersIn = true;
        while (bothPlayersIn) {
            for (int i = 0; i < 2; i++) {
                if (bothPlayersIn) {
                    attackCo = players[i].playerGo();
                    if (players[i == 1 ? 0 : 1].positionTaken(attackCo[0], attackCo[1])) {
                        System.out.println("Ship Hit!");
                        players[i].attackHit(attackCo[0], attackCo[1]);
                    } else {
                        System.out.println("No ships hit");
                        players[i].attackMissed(attackCo[0], attackCo[1]);
                    }
                    if (i == 0) {
                        if (!players[1].shipLeft()) {
                            bothPlayersIn = false;
                            System.out.println("Congratulations " + players[0].getName() + " won!");
                        }
                    } else {
                        if (!players[0].shipLeft()) {
                            bothPlayersIn = false;
                            System.out.println("Congratulations " + players[1].getName() + " won!");
                        }
                    }
                    System.out.println("Enter any key to continue");
                    systemIn.nextLine();
                    systemIn.nextLine();
                    clearBoard();
                }
            }
        }
    }
private void playGame ( ) { int [ ] attackCo ; boolean bothPlayersIn = true ; while ( bothPlayersIn ) { for ( int i = 0 ; i < 2 ; i ++ ) { if ( bothPlayersIn ) { attackCo = players [ i ] . playerGo ( ) ; if ( players [ i == 1 ? 0 : 1 ] . positionTaken ( attackCo [ 0 ] , attackCo [ 1 ] ) ) { System . out . println ( "Ship Hit!" ) ; players [ i ] . attackHit ( attackCo [ 0 ] , attackCo [ 1 ] ) ; } else { System . out . println ( "No ships hit" ) ; players [ i ] . attackMissed ( attackCo [ 0 ] , attackCo [ 1 ] ) ; } if ( i == 0 ) { if ( ! players [ 1 ] . shipLeft ( ) ) { bothPlayersIn = false ; System . out . println ( "Congratulations " + players [ 0 ] . getName ( ) + " won!" ) ; } } else { if ( ! players [ 0 ] . shipLeft ( ) ) { bothPlayersIn = false ; System . out . println ( "Congratulations " + players [ 1 ] . getName ( ) + " won!" ) ; } } System . out . println ( "Enter any key to continue" ) ; systemIn . nextLine ( ) ; systemIn . nextLine ( ) ; clearBoard ( ) ; } } } }
Runs the game while both players have ships left.
Runs the game while both players have ships left .