Use this class the create a program that moves a single checker piece across a b
ID: 3813874 • Letter: U
Question
Use this class the create a program that moves a single checker piece across a board and then have it return to its starting square. Your program should output the game-board after every move it makes to show its progress.
Need help finishing it.
#include <iostream>
using namespace std;
class checkerPiece
{
private:
int xPosition = 0;//from 0 to 7
int yPosition = 0;//from 0 to 7
bool king = 0;//0 is not king, 1 is king
public:
char color;//b for black and r for red
void upRight();
void upLeft();
void downRight();
void downLeft();
int xPositionReturn();
int yPositionReturn();
};
void checkerPiece::upRight()
{
if (color == 'b')
{
if ((xPosition + 1 <= 7) && (yPosition + 1 <= 7))
{
xPosition++;
yPosition++;
}
}
else if (color == 'r')
{
if ((king == 1) && (xPosition + 1 <= 7) && (yPosition + 1 <= 7))
{
xPosition++;
yPosition++;
}
}
if (king == 0 && color == 'b'&&yPosition == 7)
{
king = 1;
}
}
void checkerPiece::upLeft()
{
if (color == 'b')
{
if ((xPosition - 1 >= 0) && (yPosition + 1 <= 7))
{
xPosition--;
yPosition++;
}
}
else if (color == 'r')
{
if (king == 1 && (xPosition - 1 >= 0) && (yPosition + 1 <= 7))
{
xPosition--;
yPosition++;
}
}
if (king == 0 && color == 'b'&&yPosition == 7)
{
king = 1;
}
}
void checkerPiece::downRight()
{
if (color == 'r')
{
if ((xPosition + 1 <= 7) && (yPosition - 1 >= 0))
{
xPosition++;
yPosition--;
}
}
else if (color == 'b')
{
if ((king == 1) && (xPosition + 1 <= 7) && (yPosition - 1 >= 0))
{
xPosition++;
yPosition--;
}
}
if (king == 0 && color == 'r'&&yPosition == 0)
{
king = 1;
}
}
void checkerPiece::downLeft()
{
if (color == 'r')
{
if ((xPosition - 1 >= 0) && (yPosition - 1 >= 0))
{
xPosition--;
yPosition--;
}
}
else if (color == 'b')
{
if ((king == 1) && (xPosition + 1 >= 0) && (yPosition - 1 >= 0))
{
xPosition--;
yPosition--;
}
}
if (king == 0 && color == 'r'&&yPosition == 0)
{
king = 1;
}
}
int checkerPiece::xPositionReturn()
{
return xPosition;
}
int checkerPiece::yPositionReturn()
{
return yPosition;
}
void display(checkerPiece testPiece)
{
cout << endl;
for (int y = 7; y >= 0; y--)
{
for (int x = 0; x < 8; x++)
{
if (x == testPiece.xPositionReturn() && y == testPiece.yPositionReturn())
{
cout << "O";
}
else
{
cout << "X";
}
}
cout << endl;
}
cout << endl;
}
int main()
{
checkerPiece testPiece;
testPiece.color = 'b';
display(testPiece);
testPiece.upRight();//starting point
//how to make it across the board (to the top) and then have it return to its starting point
return 0;
}
Explanation / Answer
void doClickSquare(int row, int col) { // This is called by mousePressed() when a player clicks // on the square in the specified row and col. It has already // been checked that a game is, in fact, in progress. /* Check that the user clicked an empty square. If not, show an error message and exit. */ if ( board[row][col] != EMPTY ) { if (currentPlayer == BLACK) message.setText("BLACK: Please click an empty square."); else message.setText("WHITE: Please click an empty square."); return; } /* Make the move. Check if the board is full or if the move is a winning move. If so, the game ends. If not, then it's the other user's turn. */ board[row][col] = currentPlayer; // Make the move. Graphics g = getGraphics(); drawPiece(g, currentPlayer, row, col); // Draw the new piece. g.dispose(); if (winner(row,col)) { // First, check for a winner. if (currentPlayer == WHITE) gameOver("WHITE wins the game!"); else gameOver("BLACK wins the game!"); return; } boolean emptySpace = false; // Check if the board is full. for (int i = 0; i < 13; i++) for (int j = 0; j < 13; j++) if (board[i][j] == EMPTY) emptySpace = true; // The board contains an empty space. if (emptySpace == false) { gameOver("The game ends in a draw."); return; } /* Continue the game. It's the other player's turn. */ if (currentPlayer == BLACK) { currentPlayer = WHITE; message.setText("WHITE: Make your move."); } else { currentPlayer = BLACK; message.setText("BLACK: Make your move."); } } // end doClickSquare()Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.