468,539 Members | 1,690 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

How to stop king to jump in checker game with Java

132 100+
I have been creating English checker game by using Java, everything is working fine, my king is jumping and capture opponents pieces as it should have been. My problem is that when the king is crowned after capturing the opponent pieces it continue to jump back and capture other pieces available. Actually it is supposed to stop right after being crowned as king.

Here is my code:
Expand|Select|Wrap|Line Numbers
  1. if (board[row][col] == playerKing)
  2.             {
  3.  
  4.                 // row/col is the current row/col of the Current Player
  5.  
  6.                 if (canJumpKing(player, row, col, row+1, col+1, row+2, col+2))
  7.                     moves.add(new CheckersMove(row, col, row+2, col+2));
  8.                 if (canJumpKing(player, row, col, row-1, col+1, row-2, col+2))
  9.                     moves.add(new CheckersMove(row, col, row-2, col+2));
  10.                 if (canJumpKing(player, row, col, row+1, col-1, row+2, col-2))
  11.                     moves.add(new CheckersMove(row, col, row+2, col-2));
  12.                 if (canJumpKing(player, row, col, row-1, col-1, row-2, col-2))
  13.                     moves.add(new CheckersMove(row, col, row-2, col-2));
  14.  
  15.  
  16.                 if (row<col)
  17.                     for (int i=1; i<(7-col); i++) {
  18.                         if (canJumpKing(player, row, col, row+i, col+i, row+1+i, col+1+i))
  19.                          moves.add(new CheckersMove(row, col, row+1+i, col+1+i)); }
  20.  
  21.                  if (row>col) 
  22.                         for (int i=1; i<Math.min(7-row,7-col); i++)  {
  23.                              if (canJumpKing(player, row, col, row+i, col+i, row+1+i, col+1+i))
  24.                                    moves.add(new CheckersMove(row, col, row+1+i, col+1+i));  }
  25.  
  26.                   if (row<col)
  27.                           for (int i=1; i<row; i++) {
  28.                             if (canJumpKing(player, row, col, row-i, col-i, row-1-i, col-1-i))
  29.                                 moves.add(new CheckersMove(row, col, row-1-i, col-1-i));  }
  30.  
  31.                   if (row>col)
  32.                          for (int i=1; i<col; i++) {
  33.                             if (canJumpKing(player, row, col, row-i, col-i, row-1-i, col-1-i))
  34.                                 moves.add(new CheckersMove(row, col, row-1-i, col-1-i)); }
  35.  
  36.                     for (int i=1; i<Math.min(row,7-col); i++) {
  37.                             if (canJumpKing(player, row, col, row-i, col+i, row-1-i, col+1+i))
  38.                                    moves.add(new CheckersMove(row, col, row-1-i, col+1+i)); }
  39.  
  40.                     for (int i=1; i<Math.min(7-row,col); i++) {
  41.                             if (canJumpKing(player, row, col, row+i, col-i, row+1+i, col-1-i))
  42.                                    moves.add(new CheckersMove(row, col, row+1+i, col-1-i)); }
  43.  
  44.               }
This is my canjumpKing boolean function which gives permission whether to jump and capture or not.
Expand|Select|Wrap|Line Numbers
  1.  private boolean canJumpKing(int player, int r1, int c1, int r2, int c2, int r3, int c3) {
  2.  
  3.             if (r3 < 0 || r3 >= 8 || c3 < 0 || c3 >= 8)
  4.                 return false;
  5.  
  6.             if (board[r3][c3] != EMPTY)
  7.                 return false;
  8.  
  9.             for (int i = 1; i < r3 - r1 - 1; i++) {
  10.                 if (((r3 > r2) && (c3 > c2)))
  11.                     if ((board[r1 + i][c1 + i] != EMPTY) && (board[r1 + i + 1][c1 + i + 1] != EMPTY))
  12.                         return false; }
  13.  
  14.             for (int i = 1; i < c1 - c3 - 1; i++) {
  15.                 if (((r3 < r2) && (c3 < c2)))
  16.  
  17.                     if ((board[r1 - i][c1 - i] != EMPTY) && (board[r1 - i - 1][c1 - i - 1] != EMPTY))
  18.                         return false; }
  19.  
  20.             for (int i = 1; i < (r3 - r1 - 1); i++) {
  21.                 if (((r3 > r2) && (c3 < c2)))
  22.                     if ((board[r1 + i][c1 - i] != EMPTY) && (board[r1 + i + 1][c1 - i - 1] != EMPTY))
  23.                         return false; }
  24.  
  25.             for (int i = 1; i <= (c3 - c1 - 2); i++) {
  26.                 if (((r3 < r2) && (c3 > c2)))
  27.                     if ((board[r1 - i][c1 + i] != EMPTY) && (board[r1 - i - 1][c1 + i + 1] != EMPTY))
  28.                         return false; }
  29.  
  30.             for (int i = 1; i < r3 - r1 - 1; i++) {
  31.                 if (((r3 > r2) && (c3 > c2)))
  32.                     if (board[r1][c1] == RED_KING)
  33.                         if ((board[r1 + i][c1 + i] == RED))
  34.                             return false; }
  35.  
  36.             for (int i = 1; i < c1 - c3 - 1; i++) {
  37.                 if (((r3 < r2) && (c3 < c2)))
  38.                     if (board[r1][c1] == RED_KING)
  39.                         if ((board[r1 - i][c1 - i] == RED))
  40.                             return false; }
  41.  
  42.             for (int i = 1; i < (r3 - r1 - 1); i++) {
  43.                 if (((r3 > r2) && (c3 < c2)))
  44.                     if (board[r1][c1] == RED_KING)
  45.                         if ((board[r1 + i][c1 - i] == RED))
  46.                             return false; }
  47.  
  48.             for (int i = 1; i <= (c3 - c1 - 2); i++) {
  49.                 if (((r3 < r2) && (c3 > c2)))
  50.                     if (board[r1][c1] == RED_KING)
  51.                         if ((board[r1 - i][c1 + i] == RED))
  52.                             return false; }
  53.  
  54.             for (int i = 1; i < r3 - r1 - 1; i++) {
  55.                 if (((r3 > r2) && (c3 > c2)))
  56.                     if (board[r1][c1] == BLACK_KING)
  57.                         if ((board[r1 + i][c1 + i] == BLACK))
  58.                             return false; }
  59.  
  60.             for (int i = 1; i < c1 - c3 - 1; i++) {
  61.                 if (((r3 < r2) && (c3 < c2)))
  62.                     if (board[r1][c1] == BLACK_KING)
  63.  
  64.                         if ((board[r1 - i][c1 - i] == BLACK))
  65.                             return false; }
  66.  
  67.             for (int i = 1; i < (r3 - r1 - 1); i++) {
  68.                 if (((r3 > r2) && (c3 < c2)))
  69.                     if (board[r1][c1] == BLACK_KING)
  70.  
  71.                         if ((board[r1 + i][c1 - i] == BLACK))
  72.                             return false; }
  73.  
  74.             for (int i = 1; i <= (c3 - c1 - 2); i++) {
  75.                 if (((r3 < r2) && (c3 > c2)))
  76.                     if (board[r1][c1] == BLACK_KING)
  77.                         if ((board[r1 - i][c1 + i] == BLACK))
  78.                             return false; }
  79.  
  80.             if (board[r1][c1] == RED_KING) {
  81.                 if (board[r2][c2] == RED)
  82.                     return false;
  83.                 if (board[r2][c2] != BLACK && board[r2][c2] != BLACK_KING)
  84.                     return false;
  85.                 return true; }
  86.              else {
  87.                 if (board[r1][c1] == BLACK)
  88.                     return false;
  89.                 if (board[r2][c2] != RED && board[r2][c2] != RED_KING)
  90.                     return false;
  91.                 return true; }
  92.             }
Lastly the code where the king is crowned


Expand|Select|Wrap|Line Numbers
  1. if (toRow == 0 && board[toRow][toCol] == RED)
  2.                 board[toRow][toCol] = RED_KING;
  3.             if (toRow == 7 && board[toRow][toCol] == BLACK)
  4.                 board[toRow][toCol] = BLACK_KING;
After being crowned as a king it supposed to stop jumping and capture opponent pieces and the other player should play first before the king can move.
May 1 '21 #1
0 749

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by BlackHawke | last post: by
2 posts views Thread by Mike Bennett | last post: by
10 posts views Thread by sam_cit | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.