Javatpoint Logo
Javatpoint Logo

Capture the Pawns Problem in Java

"Capture the Pawns" is a classic chessboard problem that challenges programmers to develop a solution for finding the minimum number of moves required to capture all pawns on a given chessboard. In this problem, a chessboard of size N x N is considered, and the task is to determine the optimal sequence of moves to capture each pawn while adhering to the rules of chess movement.

Capture the Pawns Problem in Java


When it's the white pawn's turn, the game checks if it's located on the 8th row. If so, it signifies a win for Black, since the white pawn cannot make any more moves. Similarly, if the black pawn's turn comes and it's on the 1st row, this results in a victory for White, as the black pawn has no remaining moves.

A check is made for adjacency with the black pawn diagonally during the white pawn's turn. If adjacent, the white pawn captures the black pawn, resulting in a win for the White; otherwise, the white pawn moves forward one step if the destination is unoccupied. A similar process is applied during the black pawn's turn, with adjacency checks and forward movement, potentially leading to a win for the black or a loss if no valid moves are available.


Step 1: Set the initial positions of the white pawn (rowWhite, colWhite) and the black pawn (rowBlack, colBlack).

Step 2: Initialize counters for the number of moves for white and black (whiteMoves, blackMoves).

Step 3: Set a boolean variable to control the turn-based moves (isWhiteTurn = true).

Step 4: Enter a loop that alternates between white and black moves until a winner is determined.

Step 5: Check if it's the white player's turn (isWhiteTurn is true). Check if the white pawn can move forward (rowWhite != 8).

  • If yes, check if the white pawn can capture the black pawn (rowBlack == rowWhite + 1 and colBlack is adjacent), then return that white wins.
  • If not, move the white pawn forward (increment rowWhite). If the white pawn reaches the end of the board (rowWhite == 8), return that white loses.

Step 6: Check if it's the black player's turn (isWhiteTurn is false). Check if the black pawn can move forward (rowBlack != 1).

  • If yes, check if the black pawn can capture the white pawn (rowBlack == rowWhite + 1 and colBlack is adjacent). If true, return that white loses.
  • If not, move the black pawn forward (decrement rowBlack). If the black pawn reaches the end of the board (rowBlack == 1), return that white wins.

Step 7: After each complete turn (white and black move), toggle the turn (isWhiteTurn = !isWhiteTurn).

Step 8: After the Loop, check if whiteMoves are greater than blackMoves.

  • If true, return that white wins.
  • If false, return that it's a draw or some other outcome.




White wins by capturing Black's pawn!

Time Complexity: The code has a time complexity of O(n) due to the Loop, where n represents the number of rows on the chessboard.

Space Complexity: The space complexity is O(1) as the amount of extra space used by the algorithm remains constant regardless of the input size.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA