hw1/src/main/java/models/GameBoard.java
/**
   * Updates the Gameboard with the winner and whether or not there is a draw.
   */
Updates the Gameboard with the winner and whether or not there is a draw.
Updates the Gameboard with the winner and whether or not there is a draw .
private void updateWinner() {
    boolean p1Wins = false;
    boolean p2Wins = false;
    boolean containsEmpty = false;
    int countP1 = 0;
    int countP2 = 0;
    char move = '\0';
    for (int i = 0; i < 3; i++) {
      countP1 = 0;
      countP2 = 0;
      for (int j = 0; j < 3; j++) {
        move = boardState[i][j];
        countP1 += move == p1.getType() ? 1 : 0;
        countP2 += move == p2.getType() ? 1 : 0;
        containsEmpty = containsEmpty || move == '\0';
      }
      if (countP1 == 3) {
        p1Wins = true;
      } else if (countP2 == 3) {
        p2Wins = true;
      }
    }
    for (int i = 0; i < 3; i++) {
      countP1 = 0;
      countP2 = 0;
      for (int j = 0; j < 3; j++) {
        move = boardState[j][i];
        countP1 += move == p1.getType() ? 1 : 0;
        countP2 += move == p2.getType() ? 1 : 0;
      }
      if (countP1 == 3) {
        p1Wins = true;
      } else if (countP2 == 3) {
        p2Wins = true;
      }
    }
    countP1 = 0;
    countP2 = 0;
    for (int i = 0; i < 3; i++) {
      move = boardState[i][i];
      countP1 += move == p1.getType() ? 1 : 0;
      countP2 += move == p2.getType() ? 1 : 0;
    }
    if (countP1 == 3) {
      p1Wins = true;
    } else if (countP2 == 3) {
      p2Wins = true;
    }
    countP1 = 0;
    countP2 = 0;
    for (int i = 0; i < 3; i++) {
      move = boardState[i][2 - i];
      countP1 += move == p1.getType() ? 1 : 0;
      countP2 += move == p2.getType() ? 1 : 0;
    }
    if (countP1 == 3) {
      p1Wins = true;
    } else if (countP2 == 3) {
      p2Wins = true;
    }
    if (!p1Wins && !p2Wins && !containsEmpty) {
      setDraw(true);
    } else if (p1Wins) {
      setWinner(1);
    } else if (p2Wins) {
      setWinner(2);
    } else {
      setTurn(turn == 1 ? 2 : 1);
    }
  }
private void updateWinner ( ) { boolean p1Wins = false ; boolean p2Wins = false ; boolean containsEmpty = false ; int countP1 = 0 ; int countP2 = 0 ; char move = '\0' ; for ( int i = 0 ; i < 3 ; i ++ ) { countP1 = 0 ; countP2 = 0 ; for ( int j = 0 ; j < 3 ; j ++ ) { move = boardState [ i ] [ j ] ; countP1 += move == p1 . getType ( ) ? 1 : 0 ; countP2 += move == p2 . getType ( ) ? 1 : 0 ; containsEmpty = containsEmpty || move == '\0' ; } if ( countP1 == 3 ) { p1Wins = true ; } else if ( countP2 == 3 ) { p2Wins = true ; } } for ( int i = 0 ; i < 3 ; i ++ ) { countP1 = 0 ; countP2 = 0 ; for ( int j = 0 ; j < 3 ; j ++ ) { move = boardState [ j ] [ i ] ; countP1 += move == p1 . getType ( ) ? 1 : 0 ; countP2 += move == p2 . getType ( ) ? 1 : 0 ; } if ( countP1 == 3 ) { p1Wins = true ; } else if ( countP2 == 3 ) { p2Wins = true ; } } countP1 = 0 ; countP2 = 0 ; for ( int i = 0 ; i < 3 ; i ++ ) { move = boardState [ i ] [ i ] ; countP1 += move == p1 . getType ( ) ? 1 : 0 ; countP2 += move == p2 . getType ( ) ? 1 : 0 ; } if ( countP1 == 3 ) { p1Wins = true ; } else if ( countP2 == 3 ) { p2Wins = true ; } countP1 = 0 ; countP2 = 0 ; for ( int i = 0 ; i < 3 ; i ++ ) { move = boardState [ i ] [ 2 - i ] ; countP1 += move == p1 . getType ( ) ? 1 : 0 ; countP2 += move == p2 . getType ( ) ? 1 : 0 ; } if ( countP1 == 3 ) { p1Wins = true ; } else if ( countP2 == 3 ) { p2Wins = true ; } if ( ! p1Wins && ! p2Wins && ! containsEmpty ) { setDraw ( true ) ; } else if ( p1Wins ) { setWinner ( 1 ) ; } else if ( p2Wins ) { setWinner ( 2 ) ; } else { setTurn ( turn == 1 ? 2 : 1 ) ; } }
Updates the Gameboard with the winner and whether or not there is a draw.
Updates the Gameboard with the winner and whether or not there is a draw .
// True if P1 wins // True if P2 wins // True if the board contains an empty spot // Counts the number of P1 moves (reused) // Counts the number of P2 moves (reused) // Stores the currently located move in each iteration // Check horizontally // Check vertically // Check diagonally from top-left // Check diagonally from top-right // Draw situation // P1 wins // P2 wins // Increment the turn