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;
}