The problem is on line 85. The pieces move fine, but can't jump. I'm pretty sure the problem is somewhere in the conditions on my 'if' statement in there. If you want to see the problem in runtime, just compile, and move pieces by using the coordinates on the sides of the board. moving a piece from square (3, 4) to (4, 5) gets inputted to the program as '3445'.
Code:
/*
A checkers engine, for future use in programs
by Alex Borland
*/
#include <iostream>
using namespace std;
struct square{
int status;
};
square board[12][12];//global multidimensional array of squares
void checkForWin(){
int blackPieces = 0;
int redPieces = 0;
int score = 0;
int m;
int n;
for (m=0; m<12; m++){
for (n=0; n<12; n++){
if (board[m][n].status==1){
blackPieces ++;
score += 1;
} else if (board[m][n].status==2){
blackPieces ++;
score += 2;
} else if (board[m][n].status==-1){
redPieces ++;
score -= 1;
} else if (board[m][n].status==-1){
redPieces ++;
score -= 2;
}
}
}
cout << "score: " << score << endl;
cout << "black pieces: " << blackPieces << endl;
cout << "red pieces: " << redPieces << endl;
if (blackPieces==0){
cout << "\n!!!!!RED WINS!!!!!" << endl;
}
if (redPieces==0){
cout << "\n!!!!!BLACK WINS!!!!!" << endl;
}
}
void printBoard(){
system("cls");
cout << "\n";
checkForWin();
cout << "\n";
int q;
int r;
for (q=9; q>1; q--){
cout << q << " ";
if (q<10){
cout << " ";
}
for (r=2; r<10; r++){
if (board[r][q].status>=0){
cout << " ";
}
cout << board[r][q].status << " ";
}
cout << "\n\n";
}
cout << "\n 2 3 4 5 6 7 8 9";
}
bool moveBlackPiece(string moveCode){
bool isJump;
int x1 = (int)moveCode[0]-48;
int y1 = (int)moveCode[1]-48;
int x2 = (int)moveCode[2]-48;
int y2 = (int)moveCode[3]-48;
switch (board[x1][y1].status){
case(1):
if ((x1-x2==1||x1-x2==-1) && (y1-y2==-1) && (board[x2][y2].status==0)){
board[x1][y1].status = 0;
board[x2][y2].status = 1;
return true;
/*the problem is in the below if statement, and all of the similar ones, it marks an attempted jump as illegal*/
} else if ((x1-x2==2||x1-x2==-2) && (y1-y2==-2) && (board[x2][y2].status==0) && (board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==-1||board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==-2)){
board[x1][y1].status = 0;
board[x2][y2].status = 1;
board[x1-(x1-x2)][y1-(y1-y1)].status = 0;
isJump = true;
return true;
}
cout << board[x1-(x1-x2)/2][y1-(y1-y2)/2].status << endl;
break;
case(2):
if ((x1-x2==1||x1-x2==-1) && (y1-y2==1||y1-y2==-1) && (board[x2][y2].status==0)){
board[x1][y1].status = 0;
board[x2][y2].status = 2;
return true;
} else if ((x1-x2==2||x1-x2==-2) && (y1-y2==2||x1-y1==-2) && (board[x2][y2].status==0) && (board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==1||board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==2)){
board[x1][y1].status = 0;
board[x2][y2].status = 2;
board[x1-(x1-x2)][y1-(y1-y1)].status = 0;
isJump = true;
return true;
}
break;
default:
return false;
break;
}
}
bool moveRedPiece(string moveCode){
bool isJump;
int x1 = (int)moveCode[0]-48;
int y1 = (int)moveCode[1]-48;
int x2 = (int)moveCode[2]-48;
int y2 = (int)moveCode[3]-48;
switch (board[x1][y1].status){
case(-1):
if ((x1-x2==1||x1-x2==-1) && (y1-y2==1) && (board[x2][y2].status==0)){
board[x1][y1].status = 0;
board[x2][y2].status = -1;
return true;
} else if ((x1-x2==2||x1-x2==-2) && (y1-y2==2) && (board[x2][y2].status==0) && (board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==1||board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==2)){
board[x1][y1].status = 0;
board[x2][y2].status = -1;
board[x1-(x1-x2)][y1-(y1-y1)].status = 0;
isJump = true;
return true;
}
break;
case(-2):
if ((x1-x2==1||x1-x2==-1) && (y1-y2==1||y1-y2==-1) && (board[x2][y2].status==0)){
board[x1][y1].status = 0;
board[x2][y2].status = -2;
return true;
} else if ((x1-x2==2||x1-x2==-2) && (y1-y2==2||y1-y2==-2) && (board[x2][y2].status==0) && (board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==1||board[x1-(x1-x2)/2][y1-(y1-y1)/2].status==2)){
board[x1][y1].status = 0;
board[x2][y2].status = -2;
board[x1-(x1-x2)][y1-(y1-y1)].status = 0;
isJump = true;
return true;
}
break;
default:
return false;
break;
}
}
int main(){
//--------------declare board
int a;
int b;
for (a=0; a<12; a++){
if (1<a && a<10){
board[a][0].status = 9;
board[a][1].status = 9;
board[a][2].status = 0;
board[a][3].status = 0;
board[a][4].status = 0;
board[a][5].status = 0;
board[a][6].status = 0;
board[a][7].status = 0;
board[a][8].status = 0;
board[a][9].status = 0;
board[a][10].status = 9;
board[a][11].status = 9;
} else {
board[a][0].status = 9;
board[a][1].status = 9;
board[a][2].status = 9;
board[a][3].status = 9;
board[a][4].status = 9;
board[a][5].status = 9;
board[a][6].status = 9;
board[a][7].status = 9;
board[a][8].status = 9;
board[a][9].status = 9;
board[a][10].status = 9;
board[a][11].status = 9;
}
}
cout << "board initialized successfully" << endl;
//-------------------done declaring board, start placing pieces
/*this section of the program can be edited to manipulate the position the pieces start at
~a black piece is a 1, black kings are 2
~a red piece is -1, red kings are -2
*/
board[2][3].status = 1;
board[3][2].status = 1;
board[3][4].status = 1;
board[4][3].status = 1;
board[5][2].status = 1;
board[5][4].status = 1;
board[6][3].status = 1;
board[7][2].status = 1;
board[7][4].status = 1;
board[8][3].status = 1;
board[9][2].status = 1;
board[9][4].status = 1;
board[2][9].status = -1;
board[2][7].status = -1;
board[3][8].status = -1;
board[4][9].status = -1;
board[4][7].status = -1;
board[5][8].status = -1;
board[6][9].status = -1;
board[6][7].status = -1;
board[7][8].status = -1;
board[8][9].status = -1;
board[8][7].status = -1;
board[9][8].status = -1;
//----------------------------------done setting up board
cout << "board set up\n\n" << endl;
printBoard();
cout << "\n\n";
int h = 2;
while (1){
string i;
if (h%2==1){
cout << "red\'s move" << endl;
} else {
cout << "black\'s move" << endl;
}
cout << "please enter your desired move's code" << endl;
cin >> i;
if (h%2==0){
while (!moveBlackPiece(i)){
cout << "illegal move, please try again" << endl;
cin >> i;
}
} else {
while (!moveRedPiece(i)){
cout << "illegal move, please try again" << endl;
cin >> i;
}
}
h ++;
cout << "\n\n";
printBoard();
cout << "\n\n";
}
return 0;
}
I know my indentation is a little sloppy and my commenting is almost nonexistant, but any help is appreciated .
EDIT: to the guy below me, The first sentance in my post narrows it to one line. I posted the whole program for completeness. If I have to be completely specific, the same issue happens on lines 85, 100, 127, and 140.