FZarsaz / TTT

TicTacToe-Backend
MIT License
1 stars 0 forks source link

Winner Method #45

Closed FZarsaz closed 2 years ago

lara-ranta commented 2 years ago

public static char hasWon(char[][]board) { // Row for(int i = 0; i < 3; i++) { if(board[i][0] == board[i][1] && board[i][1] == board [i][2] && board [i][0] != '-') { return board [i][0]; } } // Col for(int j = 0; j < 3; i++) { if(board[0][j] == board[1][j] && board[1][j] == board [2][j] && board [0][j] != '-') { return board [0][j]; } } // Diagonal if(board[0][0] == board[1][1] && board[1][1] == board [2][2] && board [0][0] != '-') { return board [0][0]; }
if(board[2][0] == board[1][1] && board[i][1] == board [0][2] && board [2][0] != '-') { return board [i2[0];
} // Nobody return '-';
}

// check if a player has won

if (hasWon(board) == 'X') {
    //Player 1 has won
    System.println.out(p1 + "has won!");
}
else if (hasWon(board) == '0') {
    //Player 2 has won
    System.println.out(p2 + "has won!");
}
    else{
      //Nobody
    if (hasTied(board)) {
        System.printl.out("Its a tie!");
    }
}
lara-ranta commented 2 years ago

public boolean isWin(Player player) { if (this.arr[0][0] == player.getOperator() && this.arr[0][1] == player.getOperator() && this.arr[0][2] == player.getOperator()) return true; if (this.arr[1][0] == player.getOperator() && this.arr[1][1] == player.getOperator() && this.arr[1][2] == player.getOperator()) return true; if (this.arr[2][0] == player.getOperator() && this.arr[2][1] == player.getOperator() && this.arr[2][2] == player.getOperator()) return true; if (this.arr[0][0] == player.getOperator() && this.arr[1][0] == player.getOperator() && this.arr[2][0] == player.getOperator()) return true; if (this.arr[0][1] == player.getOperator() && this.arr[1][1] == player.getOperator() && this.arr[2][1] == player.getOperator()) return true; if (this.arr[0][2] == player.getOperator() && this.arr[1][2] == player.getOperator() && this.arr[2][2] == player.getOperator()) return true; if (this.arr[0][0] == player.getOperator() && this.arr[1][1] == player.getOperator() && this.arr[2][2] == player.getOperator()) return true; if (this.arr[0][2] == player.getOperator() && this.arr[1][1] == player.getOperator() && this.arr[2][0] == player.getOperator()) return true; return false; }