how to make it another player's turn java code example

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

g.fillOval(3 + 13*col, 3 + 13*row, 10, 10);

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

int ct = 1;  // Number of pieces in a row belonging to the player.
      
      int r, c;    // A row and column to be examined
      
      r = row + dirX;  // Look at square in specified direction.
      c = col + dirY;
      while ( r >= 0 && r < 13 && c >= 0 && c < 13 
                                        && board[r][c] == player ) {
              // Square is on the board, and it 
              // contains one of the players's pieces.
         ct++;
         r += dirX;  // Go on to next square in this direction.
         c += dirY;
      }

      r = row - dirX;  // Now, look in the opposite direction.
      c = col - dirY;
      while ( r >= 0 && r < 13 && c >= 0 && c < 13 
                                       && board[r][c] == player ) {
         ct++;
         r -= dirX;   // Go on to next square in this direction.
         c -= dirY;
      }

Example 3: 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()

Example 4: 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 5: how to make it another player's turn java

public void paint(Graphics g) {
          
          /* Draw grid lines in darkGray.  */
          
          g.setColor(Color.darkGray);
          for (int i = 1; i < 13; i++) {
             g.drawLine(1 + 13*i, 0, 1 + 13*i, getSize().height);
             g.drawLine(0, 1 + 13*i, getSize().width, 1 + 13*i);
          }
          
          /* Draw a two-pixel black border around the edges of the board. */
  
          g.setColor(Color.black);
          g.drawRect(0,0,getSize().width-1,getSize().height-1);
          g.drawRect(1,1,getSize().width-3,getSize().height-3);
          
          /* Draw the pieces that are on the board. */
          
          for (int row = 0; row < 13; row++)
             for (int col = 0; col < 13; col++)
                if (board[row][col] != EMPTY)
                   drawPiece(g, board[row][col], row, col);
                   
        }  // end paint()

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

private void drawPiece(Graphics g, int piece, int row, int col) {
              // Draw a piece in the square at (row,col).  The color 
              // is specified by the "piece" parameter, which should be 
              // either BLACK or WHITE.
          if (piece == WHITE)
             g.setColor(Color.white);
          else
             g.setColor(Color.black);
          g.fillOval(3 + 13*col, 3 + 13*row, 10, 10);
       }

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

dirX    dirY    Why?
                           ----    ----    --------------------------------
     horizontal direction    1       0       Only x changes.
     vertical direction      0       1       Only y changes.
     first diagonal          1       1       Both x and y change.
     second diagonal         1      -1       Change in opposing directions.

Tags:

Misc Example