# Checkers movement and capture logic in java

I am making a checkers game.

I implemented a 2D array to store the board position, and made a grid layout with all my squares as panels. I also have my pieces drawn.

I wanted some info on how I would make the movement methods or the capture method. My main issue is finding a mathematical way to code the jump and what I need to do in order to move the pieces properly.

I was thinking of having a method to check if the move is legal and a method to actually move.

• What have you tried so far, and what's giving you trouble? If you're looking for a step-by-step guide, searching for checkers tutorials will give you better results. Here we specialise in individualized problem-solving, so you'll need to narrow down what specifically you need help with. – DMGregory Jan 12 '18 at 20:58

Working with a grid is fairly easy because you can directly check adjacent tiles.

You can check for valid moves during the attempt to move a piece or pre-check when you select a piece. So let's get one thing out of the way:

You can not move head on, you're only interested in tiles that are diagonal to the current tile. Checking adjacent tiles is as simple as

 boardgrid[currentY-1][currentX-1] // top left
boardgrid[currentY-1][currentX+1] // top Right
etc


Your tiles probably have some sort of state. Whether it's occupied and by whom. Based on this information it's really easy to check for validity.

pseudo code:

if adjacent tile is not occupied:
move current piece to adjacent tile

else:
do:
if current adjacent tile is occupied by the enemy AND
check if a tile is free in the same direction:
mark enemy piece for removal // queue it up to process after
additional logic goes here, like keeping track of where the piece moves to
valid move has been found
if not:
move is invalid

while( valid move has been found )


Once your piece jumps, the rules state that you must continue jumping if any adjacent tile is occupied by the enemy. So when you jump, at the new position you check the diagonal tiles again and repeat the process.

If you want to animate your pieces you can queue up the valid moves and perform them in a sequence. Hope this helps.

i create a game like this before and other method you need is a game over. I used this one and it work good.

public boolean gameOver() {
return gameOverInternal(0, 0, 0, 0); }

public boolean gameOverInternal(int c, int r, int white, int black){
if(data[c][r] == white|| data[c][r] == white king)
white+= 1;
if(data[c][r] == black|| data[c][r] == black king)
black+= 1;
if(c== tiles-1 && row == tiles-1){
if(white== 0 || black== 0)
return true;
else return false;
}
if(c== tiles-1){
r+= 1;
c= -1;
}
return gameOverInternal(c+1, r, white, black);
}


Sorry if this answer wasnt your main question, iam new to this website. sorry for bad english

• This doesn't answer the main question about movement & capture logic. Although the original phrasing of the question did ask for "any other" code suggestions, questions inviting open-ended lists like "name everything I haven't thought of yet" are considered too broad to be on-topic for this site. – DMGregory Jan 15 '18 at 1:15