//problem E - Checkers #include #include #include char P; //player int Board[33], move[14], moveLen; int RedMoves[33][33], RedJumps[33][33]; char movetype; //indicates which moves can make from each spot void BuildMoves() { int i,j; for(i=0;i<33;i++) for(j=0;j<33;j++) RedMoves[i][j]=RedJumps[i][j]=0; for(i=1;i<=28;i++) //build RedMoves { switch(i) { case 1: case 2: case 3: case 9: case 10: case 11: case 17: case 18: case 19: case 25: case 26: case 27: RedMoves[i][i+4]=RedMoves[i][i+5]=1; break; case 6: case 7: case 8: case 14: case 15: case 16: case 22: case 23: case 24: RedMoves[i][i+3]=RedMoves[i][i+4]=1; break; case 5: case 13: case 21: case 4: case 12: case 20: case 28: RedMoves[i][i+4]=1; break; } } for(i=1;i<=24;i++) //build RedJumps { switch(i) { case 2: case 10: case 18: case 3: case 11: case 19: RedJumps[i][i+7]=i+4; RedJumps[i][i+9]=i+5; break; case 1: case 9: case 17: RedJumps[i][i+9]=i+5; break; case 4: case 12: case 20: RedJumps[i][i+7]=i+4; break; case 5: case 13: case 21: RedJumps[i][i+9]=i+4; break; case 8: case 16: case 24: RedJumps[i][i+7]=i+3; break; case 6: case 7: case 14: case 15: case 22: case 23: RedJumps[i][i+7]=i+3; RedJumps[i][i+9]=i+4; break; } } } //Can we make a legal red move from i to j? int IsRedMove(int i, int j) { if(Board[i]<0) //king return(RedMoves[i][j] || RedMoves[33-i][33-j]); else if(Board[i]>0) return RedMoves[i][j]; else return 0; } //Can we make a legal red jump from i to j? int IsRedJump(int i, int j) { if(Board[i]<0) //king return(RedJumps[i][j] ||RedJumps[33-i][33-j]); else if(Board[i]>0) return (RedJumps[i][j]>0); else return 0; } //can we make a legal white move from i to j? //note we turn red's board "upside down" int IsWMove(int i, int j) { if(Board[i]<0) //king return(RedMoves[i][j] || RedMoves[33-i][33-j]); else if(Board[i]>0) return RedMoves[33-i][33-j]; else return 0; } //can we make a legal white move from i to j? //note we turn red's board "upside down" int IsWJump(int i, int j) { if(Board[i]<0) //king return(RedJumps[i][j] ||RedJumps[33-i][33-j]); else if(Board[i]>0) return (RedJumps[33-i][33-j]>0); else return 0; } void getRedPieces(int n) { int i,p; for(i=0;i>p; if(p>0) Board[p]=1; else Board[abs(p)]=-1; } } void getWhPieces(int n) { int i,p; for(i=0;i>p; if(p>0) Board[p]=2; else Board[abs(p)]=-2; } } //figure what type of move we've got here char gettype(int * move) { int first, second; if(moveLen>2) return 'J'; first=move[0]; second=move[1]; if (P=='R') { if(IsRedMove(first,second)) return 'R'; if(IsRedJump(first,second)) return 'J'; return 'I'; } else //P=='W' { if (IsWMove(first,second)) return 'R'; if(IsWJump(first,second)) return 'J'; return 'I'; } } //is there a red jump possible? int canRedJump(int i) { for(int j=1;j<33;j++) if(RedJumps[i][j]>0 && abs(Board[j])==0 && abs(Board[RedJumps[i][j]])==2) return 1; return 0; } //ditto int canRedKingJump(int i) { int j; for(j=1;j<33;j++) if(RedJumps[i][j]>0 && (abs(Board[j])==0) && abs(Board[RedJumps[i][j]])==2) return 1; for(j=1;j<33;j++) if(RedJumps[33-i][33-j]>0 && (abs(Board[j])==0) && abs(Board[33-RedJumps[33-i][33-j]])==2) return 1; return 0; } int canWJump(int i) { for(int j=1;j<33;j++) if(RedJumps[33-i][33-j]>0 && abs(Board[j])==0 && abs(Board[33-RedJumps[33-i][33-j]])==1) return 1; return 0; } int canWKingJump(int i) { int j; for(j=1;j<33;j++) if(RedJumps[33-i][33-j]>0 && (abs(Board[j])==0) && abs(Board[33-RedJumps[33-i][33-j]])==1) return 1; for(j=1;j<33;j++) if(RedJumps[i][j]>0 && (abs(Board[j])==0) && abs(Board[RedJumps[i][j]])==1) return 1; return 0; } //is there a jump on the board? int jumpexists() { int i,j; if(P=='R') { for(i=1;i<33;i++) for(j=1;j<33;j++) { if(RedJumps[i][j]>0 && abs(Board[i])==1 && abs(Board[j])==0 && abs(Board[RedJumps[i][j]])==2) return 1; //red king jump exists? if(RedJumps[33-i][33-j]>0 && (Board[i])==-1 && abs(Board[j])==0 && abs(Board[33-RedJumps[33-i][33-j]])==2) return 1; } } else //white { for(i=1;i<33;i++) for(j=1;j<33;j++) { if(RedJumps[33-i][33-j]>0 && abs(Board[i])==2 && abs(Board[j])==0 && abs(Board[33-RedJumps[33-i][33-j]])==1) return 1; if(RedJumps[i][j]>0 && (Board[i])==-2 && abs(Board[j])==0 && abs(Board[RedJumps[i][j]])==1) return 1; } } return 0; } //make the move int makemove(int * move) { //we know it's legal int piece,i,S,E; piece=Board[move[0]]; if((P=='R' && abs(piece)!=1) ||(P=='W' && abs(piece)!=2)) return 0; if (movetype=='R') //it's a simple move { Board[move[0]]=0; if(Board[move[1]]!=0) return 0; Board[move[1]]=piece; S=move[1]; } else //it's a jump { i=1; S=move[0];E=move[1]; while(E>0) { if(Board[E]!=0) return 0; Board[S]=0; if(P=='R'){ if(piece==1){ if(abs(Board[RedJumps[S][E]])!=2) return 0; Board[RedJumps[S][E]]=0;} else if(piece ==-1) { if(ES){ if(abs(Board[RedJumps[S][E]])!=1) return 0; Board[RedJumps[S][E]]=0;} else{ if(abs(Board[33-RedJumps[33-S][33-E]])!=1) return 0; Board[33-RedJumps[33-S][33-E]]=0;} } } Board[E]=piece; S=E; i++; E=move[i]; } if(piece==1){ if(canRedJump(S)) return 0;} else if(piece==-1){ if(canRedKingJump(S)) return 0;} else if(piece==2){ if(canWJump(S)) return 0;} else if(piece==-2){ if(canWKingJump(S)) return 0;} } //promote to king if(P=='R' && S >28) Board[S]=-1; if(P=='W' && S<5) Board[S]=-2; if(P=='R') P='W'; else P='R'; return 1; } void clearboard() { int i; for(i=1;i<33;i++) Board[i]=0; } void getmove(int * move) { int i=0, j, k; char m[3], S[41]; cin>>S; j=k=0; while(S[j]!='\0') { if(S[j]=='-') { m[k]='\0'; move[i++]=atoi(m); k=0; j++; } else { m[k++]=S[j++]; } } m[k]='\0'; move[i++]=atoi(m); move[i]=0; moveLen=i; } void main() { int r,w,i,m,ok; BuildMoves(); cin>>r>>w; while(r>0) { clearboard(); getRedPieces(r); getWhPieces(w); cin>>m>>P; ok=1; for(i=0;i>r>>w; } }