Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- char pieces[12][12];
- char gameBoard [12][12];
- char deadPieces[37];
- int undo[500][5];
- int redo[500][4];
- int pUndo;
- int pRedo;
- char play[5];
- int index[4];
- int died=1;
- int diedLast;
- int i,j,k,l;
- int valid;
- int turn;
- int king1x, king1y, king2x, king2y;
- int checkWhite, checkBlack;
- int checkMateBlack,checkMateWhite;
- void readLine(char s[],int n)
- {
- int ch, i=0;
- while((ch=getchar()) != '\n'){
- if(i<n)
- {
- s[i++]=ch;
- }
- }
- s[i]='\0';
- }
- void gameBoardBorders();
- void checkerBoard();
- void startingChessBoard();
- void printGame ();
- void scanAndConvert();
- void checkValidMove();
- void swapElements ();
- void reverseSwapElement();
- void rook();
- void knight();
- void king();
- void king();
- void bishop();
- void queen();
- void pawn();
- void moveSet();
- void fullKingCheckWhite(int x,int y);
- void fullKingCheckBlack(int x,int y);
- void kingWhiteCheckMate();
- void kingBlackCheckMate();
- void checkMateBlackPath(int x,int y);
- void checkMateWhitePath(int x,int y);
- void kingWhiteStaleMate();
- void kingBlackStaleMate();
- void fullPieceCheckBlack(int x,int y);
- void fullPieceCheckWhite(int x,int y);
- void saveRedo();
- void saveUndo();
- int main(){
- startingChessBoard();
- gameBoardBorders();
- checkerBoard();
- printGame();
- king1x=9, king1y=6, king2x=2, king2y=6;
- turn=0;
- diedLast=0;
- checkWhite=0;
- checkBlack=0;
- pUndo=0;
- pRedo=0;
- int r, u;
- FILE *fPointer;
- while (1){
- valid = 0;
- checkMateBlack=-1;checkMateWhite=-1;
- if (turn%2==0){
- while ((valid==0)||(checkWhite==1)){
- pUndo=0;
- printf("\nWhite's turn:\t (for undo press \"U\" / for redo press \"R\" / for save press \"S\" / for load press \"L\")\n");
- scanAndConvert();
- while ((play[0]=='S')&&(play[1]=='\0')){
- printf("Enter the file name.\n");
- char saveName[100];
- readLine(saveName,100);
- fPointer = fopen(saveName,"w");
- fputc(turn,fPointer);
- for (i=2;i<=9;i++){
- for (j=2;j<=9;j++){
- fputc(pieces[i][j],fPointer);
- }
- }
- for (i=0;i<37;i++){
- fputc(deadPieces[i],fPointer);
- }
- for (i=0;i<500;i++){
- for (j=0;j<5;j++){
- fputc(undo[i][j],fPointer);
- }
- }
- for (i=0;i<500;i++){
- for (j=0;j<4;j++){
- fputc(redo[i][j],fPointer);
- }
- }
- fputc(died,fPointer);
- fclose(fPointer);
- printf("Game successfully saved.\n");
- printf("\nWhite's turn:\t (for undo press \"U\" / for redo press \"R\" / for save press \"S\" / for load press \"L\")\n");
- scanAndConvert();
- }
- if ((play[0]=='L')&&(play[1]=='\0')){
- printf("Enter the file name to be loaded.\n");
- char saveName[100];
- readLine(saveName,100);
- fPointer = fopen(saveName,"r");
- if (fPointer){
- turn=fgetc(fPointer);
- for (i=2;i<=9;i++){
- for (j=2;j<=9;j++){
- pieces[i][j]=fgetc(fPointer);
- }
- }
- for (i=0;i<37;i++){
- deadPieces[i]=fgetc(fPointer);
- }
- for (i=0;i<500;i++){
- for (j=0;j<5;j++){
- undo[i][j]=fgetc(fPointer);
- }
- }
- for (i=0;i<500;i++){
- for (j=0;j<4;j++){
- redo[i][j]=fgetc(fPointer);
- }
- }
- died=fgetc(fPointer);
- fclose(fPointer);
- printGame();
- printf("Game loaded successfully.\n");
- break;
- }
- else {
- printf("File doesn't exist.\n");
- }
- }
- if((play[0]=='U'&&turn>0)&&(play[1]=='\0')){
- for(u=0;u<4;u++){
- index[u]=undo[turn-1][u];
- }
- diedLast=undo[turn-1][4];
- saveRedo();
- reverseSwapElement();
- turn--;
- pUndo=1;
- break;
- }
- else if((play[0]=='R'&&pRedo-1>=0)&&(play[1]=='\0')){
- for(r=0;r<4;r++){
- index[r]=redo[pRedo-1][r];
- }
- saveUndo();
- swapElements();
- undo[turn][4]=diedLast;
- pRedo--;
- break;
- }
- else{
- if(valid==1){
- checkValidMove();
- }
- if(valid==1){
- moveSet();
- }
- if (valid==1){
- saveUndo();
- swapElements();
- undo[turn][4]=diedLast;
- }
- if(valid==1){
- fullKingCheckWhite(king1x,king1y);
- }
- if ((valid==1)&&(checkWhite==1)){
- printf("Invalid move.\n");
- reverseSwapElement();
- }
- }
- }
- if (play[0]=='L'){
- continue;
- }
- printf("\n");
- printGame();
- kingWhiteStaleMate();
- if (checkMateBlack==0){
- printf("Stalemate.\n");
- exit(EXIT_SUCCESS);
- }
- fullKingCheckBlack(king2x,king2y);
- if (checkMateBlack==1){
- kingBlackCheckMate();
- if (checkMateWhite==0){
- printf("Check mate.\n");
- exit(EXIT_SUCCESS);
- }
- }
- fullKingCheckBlack(king2x,king2y);
- if (checkBlack==0){
- kingBlackStaleMate();
- if (checkMateWhite==0){
- printf("Stalemate.\n");
- exit(EXIT_SUCCESS);
- }
- }
- fullKingCheckBlack(king2x,king2y);
- if (checkBlack==1){
- printf("Check black.\n");
- }
- if(pUndo==0){
- turn++;
- }
- }
- else {
- while ((valid==0)||(checkBlack==1)){
- pUndo=0;
- printf("\nBlack's turn:\t (for undo press \"U\" / for redo press \"R\" / for save press \"S\" / for load press \"L\")\n");
- scanAndConvert();
- while ((play[0]=='S')&&(play[1]=='\0')){
- printf("Enter the file name.\n");
- char saveName[100];
- readLine(saveName,100);
- fPointer = fopen(saveName,"w");
- fputc(turn,fPointer);
- for (i=2;i<=9;i++){
- for (j=2;j<=9;j++){
- fputc(pieces[i][j],fPointer);
- }
- }
- for (i=0;i<37;i++){
- fputc(deadPieces[i],fPointer);
- }
- for (i=0;i<500;i++){
- for (j=0;j<5;j++){
- fputc(undo[i][j],fPointer);
- }
- }
- for (i=0;i<500;i++){
- for (j=0;j<4;j++){
- fputc(redo[i][j],fPointer);
- }
- }
- fputc(died,fPointer);
- fclose(fPointer);
- printf("Game successfully saved.\n");
- printf("\nBlack's turn:\t (for undo press \"U\" / for redo press \"R\" / for save press \"S\" / for load press \"L\")\n");
- scanAndConvert();
- }
- if ((play[0]=='L')&&(play[1]=='\0')){
- printf("Enter the file name to be loaded.\n");
- char saveName[100];
- readLine(saveName,100);
- fPointer = fopen(saveName,"r");
- if (fPointer){
- turn=fgetc(fPointer);
- for (i=2;i<=9;i++){
- for (j=2;j<=9;j++){
- pieces[i][j]=fgetc(fPointer);
- }
- }
- for (i=0;i<37;i++){
- deadPieces[i]=fgetc(fPointer);
- }
- for (i=0;i<500;i++){
- for (j=0;j<5;j++){
- undo[i][j]=fgetc(fPointer);
- }
- }
- for (i=0;i<500;i++){
- for (j=0;j<4;j++){
- redo[i][j]=fgetc(fPointer);
- }
- }
- died=fgetc(fPointer);
- fclose(fPointer);
- printGame();
- printf("Game loaded successfully.\n");
- break;
- }
- else {
- printf("File doesn't exist.\n");
- }
- }
- if((play[0]=='U'&&turn>0)&&(play[1]=='\0')){
- for(u=0;u<4;u++){
- index[u]=undo[turn-1][u];
- }
- diedLast=undo[turn-1][4];
- saveRedo();
- reverseSwapElement();
- turn--;
- pUndo=1;
- break;
- }
- else if((play[0]=='R'&&pRedo-1>=0)&&(play[1]=='\0')){
- for(r=0;r<4;r++){
- index[r]=redo[pRedo-1][r];
- }
- saveUndo();
- swapElements();
- undo[turn][4]=diedLast;
- pRedo--;
- break;
- }
- else{
- if(valid==1){
- checkValidMove();
- }
- if(valid==1){
- moveSet();
- }
- if(valid==1){
- saveUndo();
- swapElements();
- undo[turn][4]=diedLast;
- }
- if(valid==1){
- fullKingCheckBlack(king2x,king2y);
- }
- if(checkBlack==1 && valid==1){
- printf("Invalid move.\n");
- reverseSwapElement();
- }
- }
- }
- if (play[0]=='L'){
- continue;
- }
- printf("\n");
- printGame();
- kingBlackStaleMate();
- if (checkMateWhite==0){
- printf("Stalemate.\n");
- exit(EXIT_SUCCESS);
- }
- fullKingCheckWhite(king1x,king1y);
- if (checkMateWhite==1){
- kingWhiteCheckMate();
- if (checkMateBlack==0){
- printf("Check mate.\n");
- exit(EXIT_SUCCESS);
- }
- }
- fullKingCheckWhite(king1x,king1y);
- if (checkWhite==0){
- kingWhiteStaleMate();
- if (checkMateBlack==0){
- printf("Stalemate.\n");
- exit(EXIT_SUCCESS);
- }
- }
- fullKingCheckWhite(king1x,king1y);
- if (checkWhite==1){
- printf("Check white.\n");
- }
- if(pUndo==0){
- turn++;
- }
- }
- }
- return 0;
- }
- void gameBoardBorders(){
- for (i=0;i<12;i++){
- for (j=0;j<12;j++){
- gameBoard[i][j]='\0';
- }
- }
- for (i=0;i<12;i+=11){
- for (j=2;j<10;j++){
- gameBoard[i][j]='A'+(j-2);
- }
- }
- for (j=2;j<10;j++){
- gameBoard[1][j]='-';
- gameBoard[10][j]='-';
- }
- for (i=2;i<10;i++){
- for (j=0;j<12;j+=11){
- gameBoard[i][j]='8'-(i-2);
- }
- }
- for (i=2;i<10;i++){
- gameBoard[i][1]='|';
- gameBoard[i][10]='|';
- }
- }
- void checkerBoard(){
- for (i=2;i<10;i++){
- for (j=2;j<10;j+=2){
- if (i%2==0){
- gameBoard[i][j]='.';
- }
- else {
- gameBoard[i][j]='_';
- }
- }
- }
- for (i=2;i<10;i++){
- for (j=3;j<10;j+=2){
- if (i%2==0){
- gameBoard[i][j]='_';
- }
- else {
- gameBoard[i][j]='.';
- }
- }
- }
- }
- void startingChessBoard(){
- for (i=0;i<12;i++){
- for(j=0;j<12;j++){
- pieces[i][j]='\0';
- }
- }
- pieces[2][2]= 'R';
- pieces[2][3]= 'N';
- pieces[2][4]= 'B';
- pieces[2][5]= 'Q';
- pieces[2][6]= 'K';
- pieces[2][7]= 'B';
- pieces[2][8]= 'N';
- pieces[2][9]= 'R';
- pieces[9][2]= 'r';
- pieces[9][3]= 'n';
- pieces[9][4]= 'b';
- pieces[9][5]= 'q';
- pieces[9][6]= 'k';
- pieces[9][7]= 'b';
- pieces[9][8]= 'n';
- pieces[9][9]= 'r';
- for(j=2;j<10;j++){
- pieces[3][j]= 'P';
- }
- for(j=2;j<10;j++){
- pieces[8][j]= 'p';
- }
- for(i=0;i<36;i++){
- deadPieces[i]='\0';
- }
- }
- void printGame (){
- system("cls");
- printf("\n\t\t\t\t\t\tDead\n\n");
- for (i=0;i<12;i++){
- for (j=0;j<12;j++){
- printf("%c",gameBoard[i][j]);
- printf("%c ", pieces[i][j]);
- }
- printf("\t\t%c %c %c", deadPieces[i+1], deadPieces[i+13], deadPieces[i+25]);
- printf("\n\n");
- }
- printf("\n");
- }
- void scanAndConvert(){
- readLine(play,5);
- if((play[0]=='U')&&(play[1]=='\0')){
- return;
- }
- else if((play[0]=='R')&&(play[1]=='\0')){
- return;
- }
- else if ((play[0]=='S')&&(play[1]=='\0')){
- return;
- }
- else if ((play[0]=='L')&&(play[1]=='\0')){
- return;
- }
- else{
- if (play[4]!='\0'){
- printf("Invalid move.\n");
- valid=0;
- return;
- }
- if ((play[0]<'A')||(play[0]>'H')||(play[1]<'1')||(play[1]>'8')||(play[2]<'A')||(play[2]>'H')||(play[3]<'1')||(play[3]>'8')){
- printf("Invalid move.\n");
- valid=0;
- return;
- }
- }
- index[0]=58-play[1];
- index[1]=play[0]-63;
- index[2]=58-play[3];
- index[3]=play[2]-63;
- valid=1;
- }
- void checkValidMove(){
- if (turn%2==0){
- if ((pieces[index[0]][index[1]]<'a')||(pieces[index[0]][index[1]]>'z')){
- printf("Invalid move.\n");
- valid=0;
- return;
- }
- if ((pieces[index[2]][index[3]]>='a')&&(pieces[index[2]][index[3]]<='z')){
- printf("Invalid move.\n");
- valid=0;
- return;
- }
- }
- else if (turn%2==1){
- if ((pieces[index[0]][index[1]]<'A')||(pieces[index[0]][index[1]]>'Z')){
- printf("Invalid move.\n");
- valid=0;
- return;
- }
- if((pieces[index[2]][index[3]]>='A')&&(pieces[index[2]][index[3]]<='Z')){
- printf("Invalid move.\n");
- valid=0;
- return;
- }
- }
- valid=1;
- }
- void swapElements (){
- if ((pieces[index[2]][index[3]])!='\0'){
- deadPieces[died]= pieces[index[2]][index[3]];
- died++;
- pieces[index[2]][index[3]]='\0';
- diedLast=1;
- }else{
- diedLast=0;
- }
- char temp=pieces[index[0]][index[1]];
- pieces[index[0]][index[1]]=pieces[index[2]][index[3]];
- pieces[index[2]][index[3]]=temp;
- }
- void saveUndo(){
- int u;
- for(u=0;u<4;u++){
- undo[turn][u]=index[u];
- }
- }
- void saveRedo(){
- int r;
- for(r=0;r<4;r++){
- redo[pRedo][r]=undo[turn-1][r];
- }
- pRedo++;
- }
- void reverseSwapElement(){
- if(diedLast==1){
- if(pieces[index[0]][index[1]]=='\0' || deadPieces[died-1]!='\0'){
- pieces[index[0]][index[1]]=deadPieces[died-1];
- died--;
- deadPieces[died]='\0';
- }
- }
- char temp=pieces[index[2]][index[3]];
- pieces[index[2]][index[3]]=pieces[index[0]][index[1]];
- pieces[index[0]][index[1]]=temp;
- }
- void rook(){
- if((index[3]-index[1])==0 && (index[2]-index[0]>0)){
- for (i=1;i<index[2]-index[0];i++){
- if (pieces[index[0]+i][index[1]]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[0]+i==index[2]){
- valid=1;
- }
- }
- else if ((index[3]-index[1])==0 && (index[0]-index[2]>0)){
- for (i=1;i<index[0]-index[2];i++){
- if (pieces[index[0]-i][index[1]]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if (index[0]-i==index[2]){
- valid=1;
- }
- }
- else if ((index[3]-index[1])>0 && (index[2]-index[0]==0)){
- for (i=1;i<index[3]-index[1];i++){
- if (pieces[index[0]][index[1]+i]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[1]+i==index[3]){
- valid=1;
- }
- }
- else if ((index[1]-index[3])>0 && (index[2]-index[0]==0)){
- for (i=1;i<index[1]-index[3];i++){
- if (pieces[index[0]][index[1]-i]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[1]-i==index[3]){
- valid=1;
- }
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- void knight(){
- if ((((abs(index[3]-index[1]))==1)&&((abs(index[2]-index[0]))==2)) || (((abs(index[3]-index[1]))==2)&&((abs(index[2]-index[0]))==1)))
- {
- valid=1;
- }else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- void king(){
- if((((abs(index[3]-index[1]))==1) && ((abs(index[2]-index[0]))==1)) || (((abs(index[3]-index[1]))==1) && ((abs(index[2]-index[0]))==0)) || (((abs(index[3]-index[1]))==0) && ((abs(index[2]-index[0]))==1)))
- {
- if (turn%2==0){
- king1x=index[2];
- king1y=index[3];
- }
- else{
- king2x=index[2];
- king2y=index[3];
- }
- valid=1;
- }else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- void bishop(){
- if (((index[0]-index[2])==(index[1]-index[3]))&&((index[0]-index[2])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]-i][index[1]-i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]-i==index[2])&&(index[1]-i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[2]-index[0])>0)){
- for (i=1;i<index[2]-index[0];i++){
- if ((pieces[index[0]+i][index[1]+i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]+i==index[2])&&(index[1]+i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[0]-index[2])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]-i][index[1]+i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]-i==index[2])&&(index[1]+i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[2]-index[0])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]+i][index[1]-i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]+i==index[2])&&(index[1]-i==index[3])){
- valid=1;
- }
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- void queen(){
- if((index[3]-index[1])==0 && (index[2]-index[0]>0)){
- for (i=1;i<index[2]-index[0];i++){
- if (pieces[index[0]+i][index[1]]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[0]+i==index[2]){
- valid=1;
- }
- }
- else if ((index[3]-index[1])==0 && (index[0]-index[2]>0)){
- for (i=1;i<index[0]-index[2];i++){
- if (pieces[index[0]-i][index[1]]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if (index[0]-i==index[2]){
- valid=1;
- }
- }
- else if ((index[3]-index[1])>0 && (index[2]-index[0]==0)){
- for (i=1;i<index[3]-index[1];i++){
- if (pieces[index[0]][index[1]+i]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[1]+i==index[3]){
- valid=1;
- }
- }
- else if ((index[1]-index[3])>0 && (index[2]-index[0]==0)){
- for (i=1;i<index[1]-index[3];i++){
- if (pieces[index[0]][index[1]-i]!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if(index[1]-i==index[3]){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[0]-index[2])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]-i][index[1]-i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]-i==index[2])&&(index[1]-i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[2]-index[0])>0)){
- for (i=1;i<index[2]-index[0];i++){
- if ((pieces[index[0]+i][index[1]+i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]+i==index[2])&&(index[1]+i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[0]-index[2])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]-i][index[1]+i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]-i==index[2])&&(index[1]+i==index[3])){
- valid=1;
- }
- }
- else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[2]-index[0])>0)){
- for (i=1;i<index[0]-index[2];i++){
- if ((pieces[index[0]+i][index[1]-i])!='\0'){
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if ((index[0]+i==index[2])&&(index[1]-i==index[3])){
- valid=1;
- }
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- void pawn(){
- char promote[2];
- if (pieces[index[0]][index[1]]=='p'){
- if(index[0]==8){
- if(((index[2]-index[0]==-1)||(index[2]-index[0]==-2)) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
- valid = 1;
- }
- else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
- valid = 1;
- }
- else if ((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
- valid=1;
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }else{
- if((index[2]-index[0]==-1) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
- valid = 1;
- }
- else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
- valid = 1;
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if (index[2]==2){
- printf("Promote to: ");
- readLine(promote,2);
- while ((promote[1]!='\0')||((promote[0] != 'R')&&(promote[0] != 'N')&&(promote[0] != 'B')&&(promote[0] != 'Q')&&(promote[0] != 'P'))){
- printf("Wrong promotion\n");
- printf("Promote to: ");
- readLine(promote,2);
- }
- if(promote[0] == 'R'){
- pieces[index[0]][index[1]]='r';
- valid=1;
- }
- else if(promote[0] == 'N'){
- pieces[index[0]][index[1]]='n';
- valid=1;
- }
- else if(promote[0] == 'B'){
- pieces[index[0]][index[1]]='b';
- valid=1;
- }
- else if(promote[0] == 'Q'){
- pieces[index[0]][index[1]]='q';
- valid=1;
- }else if(promote[0] == 'P'){
- pieces[index[0]][index[1]]='p';
- valid=1;
- }
- }
- }
- else{
- if(index[0]==3){
- if(((index[2]-index[0]==1)||(index[2]-index[0]==2)) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
- valid = 1;
- }
- else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
- valid = 1;
- }
- else if ((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==1)&& (abs(index[3]-index[1])==1))){
- valid=1;
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }else{
- if((index[2]-index[0]==1) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
- valid = 1;
- }
- else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==1)&& (abs(index[3]-index[1])==1))){
- valid = 1;
- }
- else{
- printf("Invalid move.");
- valid=0;
- return;
- }
- }
- if (index[2]==9){
- printf("Promote to: ");
- readLine(promote,2);
- while((promote[0] != 'R' && promote[0] != 'N' && promote[0] != 'B' && promote[0] != 'Q' && promote[0] != 'P')||(promote[1]!='\0')){
- printf("Wrong promotion\n");
- printf("Promote to: ");
- readLine(promote,2);
- }
- if(promote[0] == 'R'){
- pieces[index[0]][index[1]]='R';
- valid=1;
- }
- else if(promote[0] == 'N'){
- pieces[index[0]][index[1]]='N';
- valid=1;
- }
- else if(promote[0] == 'B'){
- pieces[index[0]][index[1]]='B';
- valid=1;
- }
- else if(promote[0] == 'Q'){
- pieces[index[0]][index[1]]='Q';
- valid=1;
- }else if(promote[0] == 'P'){
- pieces[index[0]][index[1]]='P';
- valid=1;
- }
- }
- }
- }
- void moveSet(){
- if ((pieces[index[0]][index[1]]=='p')||(pieces[index[0]][index[1]]=='P')){
- pawn();
- }
- else if ((pieces[index[0]][index[1]]=='r')||(pieces[index[0]][index[1]]=='R')){
- rook();
- }
- else if ((pieces[index[0]][index[1]]=='n')||(pieces[index[0]][index[1]]=='N')){
- knight();
- }
- else if ((pieces[index[0]][index[1]]=='b')||(pieces[index[0]][index[1]]=='B')){
- bishop();
- }
- else if ((pieces[index[0]][index[1]]=='q')||(pieces[index[0]][index[1]]=='Q')){
- queen();
- }
- else if ((pieces[index[0]][index[1]]=='k')||(pieces[index[0]][index[1]]=='K')){
- king();
- }
- }
- void fullKingCheckWhite(int x,int y){
- if ((pieces[x-1][y+2]=='N')||(pieces[x+1][y+2]=='N')||(pieces[x-1][y-2]=='N')||(pieces[x+1][y-2]=='N')||(pieces[x+2][y-1]=='N')||(pieces[x+2][y+1]=='N')||(pieces[x-2][y-1]=='N')||(pieces[x-2][y+1]=='N')){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- else if ((pieces[x-1][y-1]=='P')||(pieces[x-1][y+1]=='P')){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- else if ((pieces[x-1][y]=='P')&&(pieces[x][y]=='\0')){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- else if ((pieces[x+1][y]=='K')||(pieces[x-1][y]=='K')||(pieces[x][y+1]=='K')||(pieces[x][y-1]=='K')||(pieces[x+1][y+1]=='K')||(pieces[x-1][y-1]=='K')||(pieces[x+1][y-1]=='K')||(pieces[x-1][y+1]=='K')){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- else{
- for(i=1; i<=7;i++){
- if(x+i>9){
- break;
- }
- else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='R' && pieces[x+i][y]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x+i][y]=='R'||pieces[x+i][y]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2){
- break;
- }
- else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='R' && pieces[x-i][y]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x-i][y]=='R'||pieces[x-i][y]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y+i>9){
- break;
- }
- else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='R' && pieces[x][y+i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x][y+i]=='R'||pieces[x][y+i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y-i<2){
- break;
- }
- else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='R' && pieces[x][y-i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x][y-i]=='R'||pieces[x][y-i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y+i>9){
- break;
- }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='B' && pieces[x+i][y+i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x+i][y+i]=='B'||pieces[x+i][y+i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y+i>9){
- break;
- }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='B' && pieces[x-i][y+i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x-i][y+i]=='B'||pieces[x-i][y+i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y-i<2){
- break;
- }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='B' && pieces[x-i][y-i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x-i][y-i]=='B'||pieces[x-i][y-i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y-i<2){
- break;
- }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='B' && pieces[x+i][y-i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x+i][y-i]=='B'||pieces[x+i][y-i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- }
- }
- void fullKingCheckBlack(int x,int y){
- if ((pieces[x-1][y+2]=='n')||(pieces[x+1][y+2]=='n')||(pieces[x-1][y-2]=='n')||(pieces[x+1][y-2]=='n')||(pieces[x+2][y-1]=='n')||(pieces[x+2][y+1]=='n')||(pieces[x-2][y-1]=='n')||(pieces[x-2][y+1]=='n')){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- else if ((pieces[x+1][y-1]=='p')||(pieces[x+1][y+1]=='p')){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- else if ((pieces[x+1][y]=='k')||(pieces[x-1][y]=='k')||(pieces[x][y+1]=='k')||(pieces[x][y-1]=='k')||(pieces[x+1][y+1]=='k')||(pieces[x-1][y-1]=='k')||(pieces[x+1][y-1]=='k')||(pieces[x-1][y+1]=='k')){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- else{
- for(i=1; i<=7;i++){
- if(x+i>9){
- break;
- }
- else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='r' && pieces[x+i][y]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x+i][y]=='r'||pieces[x+i][y]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2){
- break;
- }
- else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='r' && pieces[x-i][y]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x-i][y]=='r'||pieces[x-i][y]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y+i>9){
- break;
- }
- else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='r' && pieces[x][y+i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x][y+i]=='r'||pieces[x][y+i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y-i<2){
- break;
- }
- else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='r' && pieces[x][y-i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x][y-i]=='r'||pieces[x][y-i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y+i>9){
- break;
- }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='b' && pieces[x+i][y+i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x+i][y+i]=='b'||pieces[x+i][y+i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y+i>9){
- break;
- }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='b' && pieces[x-i][y+i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x-i][y+i]=='b'||pieces[x-i][y+i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y-i<2){
- break;
- }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='b' && pieces[x-i][y-i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x-i][y-i]=='b'||pieces[x-i][y-i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y-i<2){
- break;
- }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='b' && pieces[x+i][y-i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x+i][y-i]=='b'||pieces[x+i][y-i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- }
- }
- void kingWhiteCheckMate(){
- checkMateWhite=1;
- if ((pieces[king1x+1][king1y]<'a')&&(king1x+1>1)&&(king1x+1<10)){
- fullKingCheckWhite(king1x+1,king1y);
- }
- if ((checkMateWhite==1)&&(pieces[king1x][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)){
- fullKingCheckWhite(king1x,king1y+1);
- }
- if ((checkMateWhite==1)&&(pieces[king1x+1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x+1>1)&&(king1x+1<10)){
- fullKingCheckWhite(king1x+1,king1y+1);
- }
- if ((checkMateWhite==1)&&(pieces[king1x-1][king1y]<'a')&&(king1x-1>1)&&(king1x-1<10)){
- fullKingCheckWhite(king1x-1,king1y);
- }
- if ((checkMateWhite==1)&&(pieces[king1x][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)){
- fullKingCheckWhite(king1x,king1y-1);
- }
- if ((checkMateWhite==1)&&(pieces[king1x-1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x-1>1)&&(king1x-1<10)){
- fullKingCheckWhite(king1x-1,king1y-1);
- }
- if ((checkMateWhite==1)&&(pieces[king1x+1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x+1>1)&&(king1x+1<10)){
- fullKingCheckWhite(king1x+1,king1y-1);
- }
- if ((checkMateWhite==1)&&(pieces[king1x-1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x-1>1)&&(king1x-1<10)){
- fullKingCheckWhite(king1x-1,king1y+1);
- }
- if (checkMateWhite==1){
- fullPieceCheckBlack(index[2],index[3]);
- if (checkMateBlack==0){
- checkMateBlackPath(king1x,king1y);
- }
- }
- }
- void kingBlackCheckMate(){
- checkMateWhite=1;
- if (((pieces[king2x+1][king2y]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2x+1>1)&&(king2x+1<10)){
- fullKingCheckBlack(king2x+1,king2y);
- }
- if ((checkMateBlack==1)&&((pieces[king2x][king2y+1]>='a')||(pieces[king2x][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)){
- fullKingCheckBlack(king2x,king2y+1);
- }
- if ((checkMateBlack==1)&&((pieces[king2x+1][king2y+1]>='a')||(pieces[king2x+1][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x+1>1)&&(king2x+1<10)){
- fullKingCheckBlack(king2x+1,king2y+1);
- }
- if ((checkMateBlack==1)&&((pieces[king2x-1][king2y]>='a')||(pieces[king2x-1][king2y]=='\0'))&&(king2x-1>1)&&(king2x-1<10)){
- fullKingCheckBlack(king2x-1,king2y);
- }
- if ((checkMateBlack==1)&&((pieces[king2x][king2y-1]>='a')||(pieces[king2x][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)){
- fullKingCheckBlack(king2x,king2y-1);
- }
- if ((checkMateBlack==1)&&((pieces[king2x-1][king2y-1]>='a')||(pieces[king2x-1][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x-1>1)&&(king2x-1<10)){
- fullKingCheckBlack(king2x-1,king2y-1);
- }
- if ((checkMateBlack==1)&&((pieces[king2x+1][king2y-1]>='a')||(pieces[king2x+1][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x+1>1)&&(king2x+1<10)){
- fullKingCheckBlack(king2x+1,king2y-1);
- }
- if ((checkMateBlack==1)&&((pieces[king2x-1][king2y+1]>='a')||(pieces[king2x-1][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x-1>1)&&(king2x-1<10)){
- fullKingCheckBlack(king2x-1,king2y+1);
- }
- if (checkMateBlack==1){
- fullPieceCheckWhite(index[2],index[3]);
- if (checkMateWhite==0){
- checkMateWhitePath(king2x,king2y);
- }
- }
- }
- void checkMateWhitePath(int x,int y){
- if((index[3]-y)==0 && (index[2]-x>0)){
- for (k=1;k<index[2]-x;k++){
- fullKingCheckBlack(x+k,y);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((index[3]-y)==0 && (x-index[2]>0)){
- for (k=1;k<x-index[2];k++){
- fullKingCheckBlack(x-k,y);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((index[3]-y)>0 && (index[2]-x==0)){
- for (k=1;k<index[3]-y;k++){
- fullKingCheckBlack(x,y+k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((y-index[3])>0 && (index[2]-x==0)){
- for (k=1;k<y-index[3];k++){
- fullKingCheckBlack(x,y-k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(y-index[3]))&&((x-index[2])>0)){
- for (k=1;k<x-index[2];k++){
- fullKingCheckBlack(x-k,y-k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(y-index[3]))&&((index[2]-x)>0)){
- for (k=1;k<index[2]-x;k++){
- fullKingCheckBlack(x+k,y+k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(index[3]-y))&&((x-index[2])>0)){
- for (k=1;k<x-index[2];k++){
- fullKingCheckBlack(x-k,y+k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(index[3]-y))&&((index[2]-x)>0)){
- for (k=1;k<index[0]-index[2];k++){
- fullKingCheckBlack(x+k,y-k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- }
- void checkMateBlackPath(int x,int y){
- if((index[3]-y)==0 && (index[2]-x>0)){
- for (k=1;k<index[2]-x;k++){
- fullKingCheckBlack(x+k,y);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((index[3]-y)==0 && (x-index[2]>0)){
- for (k=1;k<x-index[2];k++){
- fullKingCheckBlack(x-k,y);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((index[3]-y)>0 && (index[2]-x==0)){
- for (k=1;k<index[3]-y;k++){
- fullKingCheckBlack(x,y+k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if ((y-index[3])>0 && (index[2]-x==0)){
- for (k=1;k<y-index[3];k++){
- fullKingCheckBlack(x,y-k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(y-index[3]))&&((x-index[2])>0)){
- for (k=1;k<x-index[2];k++){
- fullKingCheckBlack(x-k,y-k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(y-index[3]))&&((index[2]-x)>0)){
- for (k=1;k<index[2]-x;k++){
- fullKingCheckBlack(x+k,y+k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(index[3]-y))&&((x-index[2])>0)){
- for (k=1;k<x-index[2];k++){
- fullKingCheckBlack(x-k,y+k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- else if (((x-index[2])==(index[3]-y))&&((index[2]-x)>0)){
- for (k=1;k<index[0]-index[2];k++){
- fullKingCheckBlack(x+k,y-k);
- if (checkMateBlack==1){
- break;
- }
- }
- }
- }
- void kingWhiteStaleMate(){
- checkWhite=1;
- checkMateBlack=1;
- if ((pieces[king1x+1][king1y]<'a')&&(king1x+1>1)&&(king1x+1<10)){
- fullKingCheckWhite(king1x+1,king1y);
- }
- if ((checkWhite==1)&&(pieces[king1x][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)){
- fullKingCheckWhite(king1x,king1y+1);
- }
- if ((checkWhite==1)&&(pieces[king1x+1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x+1>1)&&(king1x+1<10)){
- fullKingCheckWhite(king1x+1,king1y+1);
- }
- if ((checkWhite==1)&&(pieces[king1x-1][king1y]<'a')&&(king1x-1>1)&&(king1x-1<10)){
- fullKingCheckWhite(king1x-1,king1y);
- }
- if ((checkWhite==1)&&(pieces[king1x][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)){
- fullKingCheckWhite(king1x,king1y-1);
- }
- if ((checkWhite==1)&&(pieces[king1x-1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x-1>1)&&(king1x-1<10)){
- fullKingCheckWhite(king1x-1,king1y-1);
- }
- if ((checkWhite==1)&&(pieces[king1x+1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x+1>1)&&(king1x+1<10)){
- fullKingCheckWhite(king1x+1,king1y-1);
- }
- if ((checkWhite==1)&&(pieces[king1x-1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x-1>1)&&(king1x-1<10)){
- fullKingCheckWhite(king1x-1,king1y+1);
- }
- if (checkWhite==1){
- checkMateBlack=0;
- for (k=2;k<=9;k++){
- for (l=2;l<=9;l++){
- if (pieces[k][l]<'a'){
- fullPieceCheckBlack(k,l);
- if (checkMateBlack==1){
- return;
- }
- }
- }
- }
- }
- }
- void kingBlackStaleMate(){
- checkBlack=1;
- checkMateWhite=1;
- if (((pieces[king2x+1][king2y]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2x+1>1)&&(king2x+1<10)){
- fullKingCheckBlack(king2x+1,king2y);
- }
- if ((checkBlack==1)&&((pieces[king2x][king2y+1]>='a')||(pieces[king2x][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)){
- fullKingCheckBlack(king2x,king2y+1);
- }
- if ((checkBlack==1)&&((pieces[king2x+1][king2y+1]>='a')||(pieces[king2x+1][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x+1>1)&&(king2x+1<10)){
- fullKingCheckBlack(king2x+1,king2y+1);
- }
- if ((checkBlack==1)&&((pieces[king2x-1][king2y]>='a')||(pieces[king2x-1][king2y]=='\0'))&&(king2x-1>1)&&(king2x-1<10)){
- fullKingCheckBlack(king2x-1,king2y);
- }
- if ((checkBlack==1)&&((pieces[king2x][king2y-1]>='a')||(pieces[king2x][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)){
- fullKingCheckBlack(king2x,king2y-1);
- }
- if ((checkBlack==1)&&((pieces[king2x-1][king2y-1]>='a')||(pieces[king2x-1][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x-1>1)&&(king2x-1<10)){
- fullKingCheckBlack(king2x-1,king2y-1);
- }
- if ((checkBlack==1)&&((pieces[king2x+1][king2y-1]>='a')||(pieces[king2x+1][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x+1>1)&&(king2x+1<10)){
- fullKingCheckBlack(king2x+1,king2y-1);
- }
- if ((checkBlack==1)&&((pieces[king2x-1][king2y+1]>='a')||(pieces[king2x-1][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x-1>1)&&(king2x-1<10)){
- fullKingCheckBlack(king2x-1,king2y+1);
- }
- if (checkBlack==1){
- checkMateWhite=0;
- for (k=2;k<=9;k++){
- for (l=2;l<=9;l++){
- if ((pieces[k][l]=='\0')||(pieces[k][l]>='a')){
- fullPieceCheckWhite(k,l);
- if (checkMateWhite==1){
- return;
- }
- }
- }
- }
- }
- }
- void fullPieceCheckBlack(int x,int y){
- if ((pieces[x-1][y+2]=='n')||(pieces[x+1][y+2]=='n')||(pieces[x-1][y-2]=='n')||(pieces[x+1][y-2]=='n')||(pieces[x+2][y-1]=='n')||(pieces[x+2][y+1]=='n')||(pieces[x-2][y-1]=='n')||(pieces[x-2][y+1]=='n')){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- else if (((pieces[x+1][y-1]=='p')||(pieces[x+1][y+1]=='p'))&&(pieces[x][y]>='A')&&(pieces[x][y]<='Z')){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- else if ((pieces[x+1][y]=='p')&&(pieces[x][y]=='\0')){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- else{
- for(i=1; i<=7;i++){
- if(x+i>9){
- break;
- }
- else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='r' && pieces[x+i][y]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x+i][y]=='r'||pieces[x+i][y]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2){
- break;
- }
- else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='r' && pieces[x-i][y]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x-i][y]=='r'||pieces[x-i][y]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y+i>9){
- break;
- }
- else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='r' && pieces[x][y+i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x][y+i]=='r'||pieces[x][y+i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y-i<2){
- break;
- }
- else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='r' && pieces[x][y-i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x][y-i]=='r'||pieces[x][y-i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y+i>9){
- break;
- }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='b' && pieces[x+i][y+i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x+i][y+i]=='b'||pieces[x+i][y+i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y+i>9){
- break;
- }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='b' && pieces[x-i][y+i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x-i][y+i]=='b'||pieces[x-i][y+i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y-i<2){
- break;
- }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='b' && pieces[x-i][y-i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x-i][y-i]=='b'||pieces[x-i][y-i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y-i<2){
- break;
- }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='b' && pieces[x+i][y-i]!='q')){
- checkBlack=0;
- checkMateBlack=0;
- break;
- }
- else if(pieces[x+i][y-i]=='b'||pieces[x+i][y-i]=='q'){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- }
- }
- }
- void fullPieceCheckWhite(int x,int y){
- if ((pieces[x-1][y+2]=='N')||(pieces[x+1][y+2]=='N')||(pieces[x-1][y-2]=='N')||(pieces[x+1][y-2]=='N')||(pieces[x+2][y-1]=='N')||(pieces[x+2][y+1]=='N')||(pieces[x-2][y-1]=='N')||(pieces[x-2][y+1]=='N')){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- else if (((pieces[x-1][y-1]=='P')||(pieces[x-1][y+1]=='p'))&&(pieces[x][y]>='a')&&(pieces[x][y]<='z')){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- else if ((pieces[x-1][y]=='P')&&(pieces[x][y]=='\0')){
- checkBlack=1;
- checkMateBlack=1;
- return;
- }
- else{
- for(i=1; i<=7;i++){
- if(x+i>9){
- break;
- }
- else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='R' && pieces[x+i][y]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x+i][y]=='R'||pieces[x+i][y]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2){
- break;
- }
- else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='R' && pieces[x-i][y]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x-i][y]=='R'||pieces[x-i][y]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y+i>9){
- break;
- }
- else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='R' && pieces[x][y+i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x][y+i]=='R'||pieces[x][y+i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(y-i<2){
- break;
- }
- else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='R' && pieces[x][y-i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x][y-i]=='R'||pieces[x][y-i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y+i>9){
- break;
- }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='B' && pieces[x+i][y+i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x+i][y+i]=='B'||pieces[x+i][y+i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y+i>9){
- break;
- }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='B' && pieces[x-i][y+i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x-i][y+i]=='B'||pieces[x-i][y+i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x-i<2 && y-i<2){
- break;
- }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='B' && pieces[x-i][y-i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x-i][y-i]=='B'||pieces[x-i][y-i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- for(i=1; i<=7;i++){
- if(x+i>9 && y-i<2){
- break;
- }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='B' && pieces[x+i][y-i]!='Q')){
- checkWhite=0;
- checkMateWhite=0;
- break;
- }
- else if(pieces[x+i][y-i]=='B'||pieces[x+i][y-i]=='Q'){
- checkWhite=1;
- checkMateWhite=1;
- return;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement