C++ coding for Checkers. Set Up • Two players, one has red tokens, the other has
ID: 3727394 • Letter: C
Question
C++ coding for Checkers.
Set Up
• Two players, one has red tokens, the other has white tokens.
• Checkers may be played on an 8x8, 10x10 or 12x12 board.
• No matter the size of the board, pieces are placed on the dark squares of the board, leaving the
middle two rows blank.
How to Play (Recommended: https://www.youtube.com/watch?v=m0drB0cx8pQ)
• Players cannot move an opponent’s piece.
• Turns alternate between players until the game is won.
• The player who runs out of pieces first or cannot make a valid move loses the game.
• Pieces may only move diagonally into unoccupied squares and only by one square if they are not
capturing a piece.
• A piece may capture opponent’s pieces by jumping over them diagonally. So long as there are
successive pieces to be captured, the piece may continue moving. The line the piece travels does
not have to be straight along one diagonal, it may change directions so long as it continues to
progress the board and capture pieces.
• When a piece has reached the King’s Row (furthest row from it’s originating side) then it is
“kinged” by placing a second token on top of it. This piece is now permitted to move backwards
on the board and allowed to capture backward.
Implementation
• Establish the size of the board via command line arguments, must include error handling for too
many and too few arguments as well as incorrect input.
• The board must be created using two-dimensional, dynamic memory.
• The board must be correctly colored black and white using the following code as a base. The
following code fragment colors a two-dimensional board. It is expected that you will adjust it as
needed to provide the best user interface possible for your program.
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++) {
if (i%2 == 0 && j%2 == 0)
cout << “|[30;47m “ << board[i][j] << “ “;
else if (i%2 == 1 && j%2 == 1)
cout << “|[30;47m “ << board[i][j] << “ “;
else
cout << “|[0m “ << board[i][j] << “ “;
cout << “[0m”;
}
cout << endl;
}
• The game must be correctly set up and played as described in the “Set Up” and “How to Play”
sections of this document.
• The path of travel shall be received as a single, c-style string and correctly parsed and error
handled to move the pieces in a correct fashion on the board. The string should be a list of square
coordinates. It is up to you as the programmer to determine the best delimiter and coordinate
system to use. Whatever method that is chosen should be clearly communicated to the user. The
board should be printed after each move of a piece and include the defined coordinate system.
This is especially important for a piece that may move multiple squares in a turn. After each
move, captured pieces should be removed from the board. Example board below:
• When the move sequence of a turn has been completed, the total number of captured pieces for
each player should be displayed.
• If a piece makes the King’s Row then it should be “kinged” in some way. Kinged pieces are the
only pieces which may move forwards and backwards on the board.
• The game should end when only one color of piece remains on the board or if a player has run out
of valid moves.
• You will submit a README.txt that outlines how your program is compiled and run.
Grading
Design to be submitted to recitation.
Implementation – 80 pts
Automatic Deductions
Memory Leaks: -10 pts
Segmentation Faults: -10 pts
Global Variables (including constants): -10 pts
Missing Error Handling: -10 pts
Functions More Than 20 Lines: -10 pts
Missing README File: -10 pts
Style and Code Quality – 20 pts
Please post a copyable code for the program.
Explanation / Answer
#include <iostream.h>
#include <stdlib.h>
#include <ctype.h>
//REGULAR PROTOTYPES
int getxcoord(void);
int getycoord(void);
void showboard(int matrix[8][8]);
char convert(int number);
//Checker Piece Class for Inheritance
class checkerpiece
{
protected:
int x;
int y;
bool king;
bool selected;
public:
int givexposition(void);
int giveyposition(void);
void select(void);
void unselect(void);
bool whetherselected(void);
//bool alljump(void); <- Implement This!
checkerpiece(int startx, int starty, bool kingship = 0);
};
int checkerpiece::givexposition(void)
{
return(x);
}
int checkerpiece::giveyposition(void)
{
return(y);
}
void checkerpiece::select(void)
{
selected = 1;
}
void checkerpiece::unselect(void)
{
selected = 0;
}
bool checkerpiece::whetherselected(void)
{
return(selected);
}
checkerpiece::checkerpiece(int startx, int starty, bool kingship)
{
x = startx;
y = starty;
king = kingship;
}
//White Piece
class wpiece : public checkerpiece
{
public:
bool moveup(int nextx, int nexty, int matrix[8][8]);
bool jumpup(int nextx, int nexty, int matrix[8][8]);
wpiece(int startx = -1, int starty = -1, bool kingship = 0);
};
bool wpiece::jumpup(int nextx, int nexty, int matrix[8][8])
{
if(nextx == x + 2 && nexty == y + 2 && matrix[x+1][y+1] == 2)
{
matrix[x][y] = 0;
matrix[x+1][y+1] = 0;
matrix[x+2][y+2] = 1;
x = x+2;
y = y+2;
cout << "jumped up right" << endl;
return(1);
}
if(nextx == x - 2 && nexty == y + 2 && matrix[x-1][y+1] == 2)
{
matrix[x][y] = 0;
matrix[x-1][y+1] = 0;
matrix[x-2][y+2] = 1;
x = x-2;
y = y+2;
cout << "jumped up left" << endl;
return(1);
}
cout << "No Jump - sorry" << endl;
return(0);
}
bool wpiece::moveup(int nextx, int nexty, int matrix[8][8])
{
if(nextx == x + 1 && nexty == y + 1 && matrix[x][y] == 0)
{
cout << matrix[x-1][y-1] << endl;
matrix[x-1][y-1] = 0;
matrix[x][y] = 1;
cout << matrix[x][y] << endl;
x++;
y++;
cout << matrix[x-1][y-1] << endl;
cout << "moved up right" << endl;
return(1);
}
if(nextx == x - 1 && nexty == y + 1 && matrix[x-2][y] == 0)
{
matrix[x-1][y-1] = 0;
matrix[x-2][y] = 1;
x--;
y++;
cout << "moved up left" << endl;
return(1);
}
cout << "no move - sorry" << endl;
return(0);
}
wpiece::wpiece(int startx, int starty, bool kingship):checkerpiece(startx, starty, king)
{
x = startx;
y = starty;
king = kingship;
}
//CLASS-RELATED PROTOTYPES
void printpieces(wpiece whitepiece[12]);
int findpiece(wpiece whitepiece[12], int x, int y);
////////
//MAIN//
////////
main()
{
int matrix[8][8] = { 1,0,1,0,1,0,1,0,
0,1,0,1,0,1,0,1,
1,0,1,0,1,0,1,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,2,0,2,0,2,0,2,
2,0,2,0,2,0,2,0,
0,2,0,2,0,2,0,2};
int xposition;
int yposition;
int xposition2;
int yposition2;
int index;
wpiece whitepiece[12] = {wpiece(1,1,0),wpiece(3,1,0),wpiece(5,1,0),wpiece(7,1,0),wpiece(2,2,0),wpiece(4,2,0),wpiece(6,2,0),wpiece(8,2,0),wpiece(1,3,0),wpiece(3,3,0),wpiece(5,3,0),wpiece(7,3,0)};
printpieces(whitepiece);
bool turn = 0;
showboard(matrix);
while(turn == 0)
{
cout << "=-WHITE'S TURN-=" << endl;
cout << "Enter coordinates of piece to move" << endl;
xposition = getxcoord();
yposition = getycoord();
while(findpiece(whitepiece, xposition, yposition) == 12)
{
cout << "No White Piece found - Enter coordinates of piece to move" << endl;
xposition = getxcoord();
yposition = getycoord();
}
index = findpiece(whitepiece, xposition, yposition);
cout << "Where do you want to move it?" << endl;
xposition2 = getxcoord();
yposition2 = getycoord();
cout << xposition2 << ", " << yposition2 << endl;
while(findpiece(whitepiece, xposition2, yposition2) != 12)
{
cout << findpiece(whitepiece, xposition2, yposition2) << endl;
cout << "Already a Piece there - Enter different place to move" << endl;
xposition2 = getxcoord();
yposition2 = getycoord();
}
if(whitepiece[index].jumpup(xposition2, yposition2, matrix) == 0 && whitepiece[index].moveup(xposition2, yposition2, matrix) == 0)
{
cout<< "Not a valid move - Enter different place to move" << endl;
}
showboard(matrix);
}
return (0);
}
//FUNCTIONS
int getxcoord(void)
{
char letter;
int x = 9;
cout << "A - H: ";
while(x == 9)
{
cin >> letter;
letter = toupper(letter);
switch (letter)
{
case 'A': x = 1;
break;
case 'B': x = 2;
break;
case 'C': x = 3;
break;
case 'D': x = 4;
break;
case 'E': x = 5;
break;
case 'F': x = 6;
break;
case 'G': x = 7;
break;
case 'H': x = 8;
break;
default : cout << "Please give a letter from A to H: ";
}
}
return(x);
}
int getycoord(void)
{
int y = 9;
cout << "1 - 8: ";
cin >> y;
while(y < 1 || y > 8)
{
cout << "Please give a number from 1 to 8: ";
cin >> y;
}
return(y);
}
int findpiece(wpiece whitepiece[12], int x, int y)
{
int finder;
for (finder = 0; finder < 12; finder++)
{
if (whitepiece[finder].givexposition() == x && whitepiece[finder].giveyposition() == y)
{
whitepiece[finder].select();
return(finder);
}
}
return(finder);
}
void showboard(int matrix[8][8])
{
int bigcount;
int counter;
cout << "|----|----|----|----|----|----|----|----|" << endl;
for(bigcount = 7; bigcount >= 0; bigcount--)
{
for(counter = 0; counter < 8; counter++)
{
cout << "| " << convert(matrix[bigcount][counter]) << " ";
}
cout << "|" << endl << "|----|----|----|----|----|----|----|----|" << endl;
}
}
char convert(int number)
{
switch(number)
{
case 0 : return(' ');
break;
case 1 : return('w');
break;
case 2 : return('b');
break;
case 3 : return('W');
break;
case 4 : return('B');
break;
default : return('!');
}
}
void printpieces(wpiece whitepiece[12])
{
int count;
for(count = 0; count < 12; count++)
{
cout << "White Piece Index Number " << count << " has coordinates: " << whitepiece[count].givexposition() << ", " << whitepiece[count].giveyposition() << endl;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.