Hello. I am trying to make a checkers game and I want touse a Stack to keep track of the game but I am having trouble withit. I am able to get the user to enter a space they wouldlike to move their piece but when I want to capture a piece, thepieces just swap. The game is supposed to indicate when it iseach players turn.
Can anyone look over my code and see if there is a way toimplement a method or maybe adjust my code to do so? Thankyou!
Here is my code:
import java.util.Scanner;
import java.util.Stack;
public class Checkers
{
private staticfinal charEMPTY = '_';
private staticfinal intWIDTH = 8;
private staticfinal intHEIGHT = 8;
private char[][] board =new char[WIDTH][HEIGHT];
private Stack<Move> moves =new Stack<Move>();//encapsulates theboard
private class Move
{
int fromRow;
int fromColumn;
int toRow;
int toColumn;
public Move(int fc,int fr, int tc,int tr)
{
fromRow = fr;
fromColumn = fc;
toRow = tr;
toColumn = tc;
}// end Move constructor
}//end of Move class
public staticboolean isEmpty(char[][]board)
{
if(board.equals('_'));
{
return true;
}
}//end isEmpty method
private void makeMove(Movem)//updates board and saves the move
{
board[m.toRow][m.toColumn] =board[m.fromRow][m.fromColumn];//takes piece from current positionto new position
board[m.fromRow][m.fromColumn] =EMPTY; //updates board to show space isempty
moves.push(m);//pushes move onto stack
}//end makeMove Method
private voidtakebackMove()
{
if(moves.isEmpty())
{
return; //does nothing since no piece isavailable
}
Move takeback = moves.pop();//removes move from stack
board[takeback.fromRow][takeback.fromColumn] =board[takeback.toRow][takeback.toColumn];//takes piece from currentposition to new position
board[takeback.toRow][takeback.toColumn] =EMPTY; //updates board to show space isempty
}// end of takebackMove method
public staticvoid printGameBoard(char[][]gameBoard)
{
for(char[] row :gameBoard)
{
for(char c : row)
{
System.out.print(c);
}
System.out.println();
}
}
public staticvoid main(String[] args)
{
char[][] gameBoard =
{{'8','|','_','|','o','|','_','|','o','|','_','|','o','|','_','|','o','|'},
{'7','|','o','|','_','|','o','|','_','|','o','|','_','|','o','|','_','|'},
{'6','|','_','|','o','|','_','|','o','|','_','|','o','|','_','|','o','|'},
{'5','|','_','|','_','|','_','|','_','|','_','|','_','|','_','|','_','|'},
{'4','|','_','|','_','|','_','|','_','|','_','|','_','|','_','|','_','|'},
{'3','|','x','|','_','|','x','|','_','|','x','|','_','|','x','|','_','|'},
{'2','|','_','|','x','|','_','|','x','|','_','|','x','|','_','|','x','|'},
{'1','|','x','|','_','|','x','|','_','|','x','|','_','|','x','|','_','|'},
{' ',' ','a',' ','b',' ','c',' ','d',' ','e',' ','f',' ','g','','h'},};
printGameBoard(gameBoard);
Scanner scan = newScanner(System.in);
System.out.println("Begin Game. PlayerX - your turn.\nChoose a cell"
+ "position of piece to be moved and the new position. e.g.3a-4b");
while(true)
{
String [] positions = scan.next().split ("-");
int fromY =(positions [0].charAt (1) - 'a' + 1) * 2;
int fromX = 8 -(positions [0].charAt (0) - '0');
int toY = (positions[1].charAt (1) - 'a' + 1) * 2;
int toX = 8 -(positions [1].charAt (0) - '0');
System.out.printf ("%d %d %d %d\n",fromX, fromY, toX, toY);
char tmp = gameBoard[toX][toY];
gameBoard [toX][toY] = gameBoard[fromX][fromY];
gameBoard [fromX][fromY] = tmp;
printGameBoard(gameBoard);
}
}//end of main method
}// end of Checkers Class
Hello. I am trying to make a checkers game and I want to use a Stack to keep track of the game but I am having trouble
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am