#include #include using namespace std; char board[33]; int squares[32]; // rMoves[i][0], rMoves[i][1] - legal move-to squares for Red from square i // rJumps[i][0], rJumps[i][1] - legal jump-to squares for Red from square i // // ditto for wMoves and wJumps // int rMoves[33][2] = {-1,-1, 5, 6, 6, 7, 7, 8, 8,-1,-1, 9, 9,10,10,11,11,12, 13,14,14,15,15,16,16,-1,-1,17,17,18,18,19,19,20, 21,22,22,23,23,24,24,-1,-1,25,25,26,26,27,27,28, 29,30,30,31,31,32,32,-1,-1,-1,-1,-1,-1,-1,-1,-1}; int wMoves[33][2] = {-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1, 1, 1, 2, 2, 3, 3, 4, 5, 6, 6, 7, 7, 8, 8,-1,-1, 9, 9,10,10,11,11,12, 13,14,14,15,15,16,16,-1,-1,17,17,18,18,19,19,20, 21,22,22,23,23,24,24,-1,-1,25,25,26,26,27,27,28}; int rJumps[33][2] = {-1,-1, -1,10, 9,11,10,12,11,-1,-1,14,13,15,14,16,15,-1, -1,18,17,19,18,20,19,-1,-1,22,21,23,22,24,23,-1, -1,26,25,27,26,28,27,-1,-1,30,29,31,30,32,31,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}; int wJumps[33][2] = {-1,-1, -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1, -1, 2, 1, 3, 2, 4, 3,-1,-1, 6, 5, 7, 6, 8, 7,-1, -1,10, 9,11,10,12,11,-1,-1,14,13,15,14,16,15,-1, -1,18,17,19,18,20,19,-1,-1,22,21,23,22,24,23,-1}; void readBoard(int r, int w) // // read in read positions and white positions // { int i; for(i=1; i<=32; i++) board[i] = ' '; for(i=1; i<=r; i++) { int val; cin >> val; if (val > 0) board[val] = 'r'; else board[-val] = 'R'; // promoted piece } for(i=1; i<=w; i++) { int val; cin >> val; if (val > 0) board[val] = 'w'; else board[-val] = 'W'; } } void printBoard() // // for debugging purposes // { for(int i=1; i<=32; i++) { if (i%8 != 5) cout << ' '; cout << board[i]; if (i%8 == 0) cout << ' '; if (i%4 == 0) cout << endl; } } int getMove(int squares[]) // // read next move sequence, and return number of moves // { int sq, nmove = 0; char ch; do { cin >> sq; cin.get(ch); squares[nmove++] = sq; } while (ch == '-'); return nmove-1; } bool incorrectPlayer(int sq, char pl) // // check is player at square is // { if (pl == 'R') return !(board[sq] == 'R' || board[sq] == 'r'); else return !(board[sq] == 'W' || board[sq] == 'w'); } bool notAJump(int sqs[], int nm) // // check if move sequence is a regular move, not a jump // { if (nm > 1) return false; int sq0 = sqs[0]; int sq1 = sqs[1]; return (rMoves[sq0][0] == sq1 || rMoves[sq0][1] == sq1 || wMoves[sq0][0] == sq1 || wMoves[sq0][1] == sq1); } bool legalMove(int sq0, int sq1, char pl) // // check if player can move from square to square // { if (board[sq1] != ' ') return false; if (pl == 'R' && board[sq0] == 'r') return (rMoves[sq0][0] == sq1 || rMoves[sq0][1] == sq1); else if (pl == 'W' && board[sq0] == 'w') return (wMoves[sq0][0] == sq1 || wMoves[sq0][1] == sq1); else // kings can move in either direction return (rMoves[sq0][0] == sq1 || rMoves[sq0][1] == sq1 || wMoves[sq0][0] == sq1 || wMoves[sq0][1] == sq1); } bool legalJump(int sq0, int sq1, char pl, char piece) // // check if player can jump from square to square // { if (board[sq1] != ' ') return false; if (pl == 'R' && piece == 'r') return (rJumps[sq0][0] == sq1 || rJumps[sq0][1] == sq1); else if (pl == 'W' && piece == 'w') return (wJumps[sq0][0] == sq1 || wJumps[sq0][1] == sq1); else // kings can move in either direction return (rJumps[sq0][0] == sq1 || rJumps[sq0][1] == sq1 || wJumps[sq0][0] == sq1 || wJumps[sq0][1] == sq1); } int getInbetweenSquare(int sq1, int sq2) // // get square in between jump from to // { if (sq1 > sq2) { int temp = sq1; sq1 = sq2; sq2 = temp; } int sq = (sq1+sq2)/2; if ((sq1-1)%8 < 4) sq++; return sq; } bool noPiece(int sq0, int sq1, char pl) // // check if a valid piece to jump lies between square and // square // { int inb = getInbetweenSquare(sq0, sq1); if (pl == 'R') return !(board[inb] == 'w' || board[inb] == 'W'); else return !(board[inb] == 'r' || board[inb] == 'R'); } void makeMove(int sq0, int sq1) // // modify the board to reflect a move // { board[sq1] = board[sq0]; board[sq0] = ' '; } void makeJump(int sq0, int sq1) // // modify the board to reflect a jump // { makeMove(sq0, sq1); board[getInbetweenSquare(sq0, sq1)] = ' '; } void convertToKings() // // promote anything on a promotion row // { int sq; for(sq=29; sq <=32; sq++) if (board[sq] == 'r') board[sq] = 'R'; for(sq=1; sq <=4; sq++) if (board[sq] == 'w') board[sq] = 'W'; } bool canJump(int sq, char pl) // // determine if player can jump from square // { if (pl == 'r' || pl == 'R') { int sq2 = rJumps[sq][0]; int inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; sq2 = rJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; } if (pl == 'w' || pl == 'W') { int sq2 = wJumps[sq][0]; int inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; sq2 = wJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; } if (pl == 'R') { int sq2 = rJumps[sq][0]; int inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; sq2 = rJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; sq2 = wJumps[sq][0]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; sq2 = wJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'w' || board[inb] == 'W')) return true; } if (pl == 'W') { int sq2 = rJumps[sq][0]; int inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; sq2 = rJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; sq2 = wJumps[sq][0]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; sq2 = wJumps[sq][1]; inb = getInbetweenSquare(sq, sq2); if (sq2 != -1 && board[sq2] == ' ' && (board[inb] == 'r' || board[inb] == 'R')) return true; } return false; } char toLower(char ch) // // don't know why I wrote this // { return (char)(ch - 'A' + 'a'); } bool jumpExists(char pl) // // check if a jump exists for player // { for(int sq=1; sq<=32; sq++) { if ((pl == board[sq] || toLower(pl) == board[sq]) && canJump(sq, board[sq])) return true; } return false; } void main() { int r, w; string move; cin >> r; while (r != 0) { cin >> w; readBoard(r, w); int m; char player; cin >> m >> player; int valid = 0; int nMove; for(nMove=1; nMove<=m; nMove++) { int numMove = getMove(squares); if (incorrectPlayer(squares[0], player)) { valid = 1; // give a specific value for debugging purposes break; } if (notAJump(squares, numMove)) { if (jumpExists(player)) { valid = 2; break; } else if (!legalMove(squares[0], squares[1], player)) { valid = 3; break; } makeMove(squares[0], squares[1]); } else { char piece = board[squares[0]]; for(int i=1; i<=numMove; i++) { if (!legalJump(squares[i-1], squares[i], player, piece)) { valid = 4; break; } else if (noPiece(squares[i-1], squares[i], player)) { valid = 5; break; } makeJump(squares[i-1], squares[i]); } if (valid != 0) break; if (canJump(squares[numMove], piece)) { // check if you can still jump valid = 6; break; } } convertToKings(); player = (player == 'R') ? 'W' : 'R'; } if (valid != 0) { cout << "Move " << nMove << " is invalid" << endl; nMove++; for(;nMove <= m; nMove++) { cin >> move; } } else cout << "All moves valid" << endl; cin >> r; } }