Starlight-30036225 / ChessTCP

FILLMELATER
0 stars 0 forks source link

Win condition #37

Open Starlight-30036225 opened 7 months ago

Starlight-30036225 commented 7 months ago

Players can now win the game.

After a move has been made, the Gamemaster checks all pieces to see if both players have legal moves. If either player has no legal moves, the opponent wins.

``

    for(int file = 0; file < 8; file++) {
        for (int rank = 0; rank < 8; rank++) {

            Piece temp = board[rank][file]; //Gets piece at location

            if (temp == null) {continue;}   //No piece here

            //Stops redundant checks, only one legal move needs to be found
            if (temp.white && whiteLegalMove) {continue;}
            if (!temp.white && blackLegalMove) {continue;}

            if (temp.white) {   whiteLegalMove  = !temp.getLegalMoves(board).isEmpty();}
            else {blackLegalMove  = !temp.getLegalMoves(board).isEmpty();}

            if (whiteLegalMove && blackLegalMove) {break;}  //stops searching when legal moves have been found
        }
        if (whiteLegalMove && blackLegalMove) {break;}  //Can only break one loop at a time, and there is two. Forgive the repetition
    }

``

There are plenty of checks to ensure this only repeats as far as it needs to.

When a player is found to have won, all connected clients are sent the WIN packet. This packet contains the winning player.

image