473,395 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 1543

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

Similar topics

13
by: BlackHawke | last post by:
Our program, game program Andromeda Online (www.andromedaonline.net) uses two programs- one to play the game, another to patch the game as updates come out. Players actually launch the updater...
2
by: Mike Bennett | last post by:
Does the .NET framework (using VB.NET) support the ability to programmatically STOP a device prior to its removal from the system? I have need to move data from a computer on one network to a...
11
by: DrUg13 | last post by:
In java, this seems so easy. You need a new object Object test = new Object() gives me exactly what I want. could someone please help me understand the different ways to do the same thing in...
1
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
10
by: sam_cit | last post by:
Hi Everyone, I'm working on developing a chess game and i decided to use c++ for its object oriented approach. I have a bass class unit and is inherited to distinct number of units (like king,...
2
by: slapsh0t11 | last post by:
So, I've been working on this Game of Life (http://www.bitstorm.org/gameoflife/) project, and all the code has been written. However, it will not run. First, I will post the error message and the...
4
by: MyRedz | last post by:
hi i just got into my first class and the prof already ask me to suggest a project for database structure and algorithm design can anyone suggest me some topics that i can make into a project like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.