how to create a turn system java code example

Example 1: how to make it another player's turn java

void doClickSquare(int row, int col) {
             // This is called by mousePressed() when a player clicks 
             // on the square in the specified row and col.  It has already 
             // been checked that a game is, in fact, in progress.
             
          /* Check that the user clicked an empty square.  If not, show an
             error message and exit. */
             
          if ( board[row][col] != EMPTY ) {
             if (currentPlayer == BLACK)
                message.setText("BLACK:  Please click an empty square.");
             else
                message.setText("WHITE:  Please click an empty square.");
             return;
          }
          
          /* Make the move.  Check if the board is full or if the move
             is a winning move.  If so, the game ends.  If not, then it's
             the other user's turn. */
             
          board[row][col] = currentPlayer;  // Make the move.
          Graphics g = getGraphics();
          drawPiece(g, currentPlayer, row, col);  // Draw the new piece.
          g.dispose();
          
          if (winner(row,col)) {  // First, check for a winner.
             if (currentPlayer == WHITE)
                gameOver("WHITE wins the game!");
             else
                gameOver("BLACK wins the game!");
             return;
          }
          
          boolean emptySpace = false;     // Check if the board is full.
          for (int i = 0; i < 13; i++)
             for (int j = 0; j < 13; j++)
                if (board[i][j] == EMPTY)
                   emptySpace = true;  // The board contains an empty space.
          if (emptySpace == false) {
             gameOver("The game ends in a draw.");
             return;
          }
          
          /* Continue the game.  It's the other player's turn. */
          
          if (currentPlayer == BLACK) {
             currentPlayer = WHITE;
             message.setText("WHITE:  Make your move.");
          }
          else {  
             currentPlayer = BLACK;
             message.setText("BLACK:  Make your move.");
          }
    
       }  // end doClickSquare()

Example 2: how to make it another player's turn java

private boolean winner(int row, int col) {
            // This is called just after a piece has been played on the
            // square in the specified row and column.  It determines
            // whether that was a winning move by counting the number
            // of squares in a line in each of the four possible
            // directions from (row,col).  If there are 5 squares (or more)
            // in a row in any direction, then the game is won.
            
          if (count( board[row][col], row, col, 1, 0 ) >= 5)
             return true;
          if (count( board[row][col], row, col, 0, 1 ) >= 5)
             return true;
          if (count( board[row][col], row, col, 1, -1 ) >= 5)
             return true;
          if (count( board[row][col], row, col, 1, 1 ) >= 5)
             return true;
             
          /* When we get to this point, we know that the game is not won. */

          return false;
          
       }  // end winner()

Tags:

Java Example