Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- char BWBoard[8][8];
- char piecesBoard[8][8] = {{'R','N','B','Q','K','B','N','R'}, {'P','P','P','P','P','P','P','P'},{},{},{},{},{'p','p','p','p','p','p','p','p'},{'r','n','b','q','k','b','n','r'}};
- char prototypeBoard[8][8] = {{'R','N','B','Q','K','B','N','R'}, {'P','P','P','P','P','P','P','P'},{},{},{},{},{'p','p','p','p','p','p','p','p'},{'r','n','b','q','k','b','n','r'}};
- char board[10][10];
- int turn=0, countBlack=0, countWhite=0;
- char capturedBlack[16], capturedWhite[16];
- void initializeBW();
- void userInput();
- void move(int iCurrent, int iDestination, int jCurrent, int jDestination);
- void printBoard();
- void capture(int iCurrent, int iDestination, int jCurrent, int jDestination);
- int validate(int iCurrent, int iDestination, int jCurrent, int jDestination);
- int printErrors(int iCurrent, int iDestination, int jCurrent, int jDestination, char inp[1000]);
- int pawn(int iCurrent, int iDestination, int jCurrent, int jDestination);
- int knight(int iCurrent, int iDestination, int jCurrent, int jDestination);
- int rook(int iCurrent, int iDestination, int jCurrent, int jDestination);
- int bishop(int iCurrent, int iDestination, int jCurrent, int jDestination);
- int queen(int iCurrent, int iDestination, int jCurrent, int jDestination);
- int king(int iCurrent, int iDestination, int jCurrent, int jDestination);
- int iBlackKing=0, iWhiteKing=7, jBlackKing=4, jWhiteKing=4;
- int check();
- int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination);
- void checkmate();
- int checkmateFlag=0;
- void correspondPrototypeBoard();
- int validatePrototype(int iCurrent, int iDestination, int jCurrent, int jDestination);
- int checkPrototype();
- void processUndo();
- int undo[500] = {0};
- int movesCount=0;
- int capturingTrack[500], captureTurn=0;
- int promotionTrack[500], promoteTurn=0;
- char promotionPieces[500];
- void getInput();
- void processRedo();
- int promote(int iCurrent, int jCurrent, char type);
- int predictPromotion();
- int main()
- {
- initializeBW();
- while(!checkmateFlag)
- {
- turn++;
- printf("\nCaptured White Pieces: ");
- for(int i=0; i<countWhite; i++)
- printf("%c ", capturedWhite[i]);
- printf("\nCaptured Black Pieces: ");
- for(int i=0; i<countBlack; i++)
- printf("%c ", capturedBlack[i]);
- printf("\n\n");
- printBoard();
- turn++;
- if(check()==1)
- printf("CHECK!\n");
- turn--;
- userInput();
- system("cls");
- }
- printBoard();
- if(checkmateFlag==1)
- printf("CHECKMATE!\n");
- else if(checkmateFlag==2)
- printf("STALEMATE!\n");
- }
- void initializeBW ()
- {
- char c1 = '_', c2= '.';
- for(int i=0; i<8; i++)
- {
- for(int j=0; j<8; j+=2)
- {
- BWBoard[i][j] = c1;
- BWBoard[i][j+1] = c2;
- }
- char temp = c1;
- c1 = c2;
- c2 = temp;
- }
- }
- void getInput()
- {
- char inp[1000];
- gets(inp);
- userInput(inp);
- }
- void userInput()
- {
- char inp[1000];
- int iCurrent, iDestination, jCurrent, jDestination;
- int f=0;
- while(!f)
- {
- gets(inp);
- if(strcmp("UNDO",inp)==0 || strcmp("undo",inp)==0 || strcmp("redo",inp)==0 || strcmp("REDO", inp)==0)
- {
- if(printErrors(iCurrent,iDestination,jCurrent,jDestination, inp)==0)
- f=1;
- }
- else
- {
- jCurrent = (int)inp[0] - (int)'A';
- iCurrent = (int)inp[1] - (int)'0';
- jDestination = (int)inp[2] - (int)'A';
- iDestination = (int)inp[3] - (int)'0';
- iDestination = 8 - iDestination;
- iCurrent = 8 - iCurrent;
- if(printErrors(iCurrent,iDestination,jCurrent,jDestination, inp)==0)
- f=1;
- }
- }
- if(strcmp("UNDO",inp)==0 || strcmp("undo",inp)==0)
- processUndo();
- else if(strcmp("redo",inp)==0 || strcmp("REDO", inp)==0)
- processRedo();
- else
- {
- capture(iCurrent, iDestination, jCurrent, jDestination);
- trackUndo(iCurrent,iDestination,jCurrent,jDestination);
- if(strlen(inp)==5 && promote(iCurrent,jCurrent,inp[4])==1)
- {
- move(iCurrent, iDestination, jCurrent, jDestination);
- piecesBoard[iDestination][jDestination]= inp[4];
- promotionTrack[promoteTurn] = turn;
- promotionPieces[promoteTurn] = inp[4];
- promoteTurn++;
- }
- else
- move(iCurrent, iDestination, jCurrent, jDestination);
- findKings();
- correspondPrototypeBoard();
- stalemate();
- checkmate();
- }
- }
- void move(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- piecesBoard[iDestination][jDestination] = piecesBoard[iCurrent][jCurrent];
- piecesBoard[iCurrent][jCurrent]='\0';
- }
- void printBoard()
- {
- board[0][0] = ' ', board[0][9] = ' ', board[9][0] = ' ', board[9][9] = ' ';
- for(int i=1; i<9; i++)
- {
- board[i][0] = 9-i;
- board[i][9]= 9-i;
- board[0][i] = 'A' +i-1;
- board[9][i] = 'A' +i-1;
- }
- for(int i=0; i<10; i++)
- {
- for(int j=0; j<10; j++)
- {
- if((board[i][j]>='A' && board[i][j] <= 'Z') || board[i][j]==' ')
- printf("%c\t", board[i][j]);
- else if((i>=1 && i<=8) && (j>=1 && j<=8))
- {
- if((piecesBoard[i-1][j-1]>='A' && piecesBoard[i-1][j-1]<='Z') || (piecesBoard[i-1][j-1]>='a' && piecesBoard[i-1][j-1]<='z'))
- printf("%c\t", piecesBoard[i-1][j-1]);
- else
- printf("%c\t", BWBoard[i-1][j-1]);
- }
- else
- printf("%d\t", (char)board[i][j]);
- }
- if(i==0||i==8)
- printf("\n\n\n");
- else
- printf("\n\n");
- }
- printf("\n\n\n");
- }
- int validate(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- if ((jCurrent<0) || (jCurrent>7) || (jDestination<0) || (jDestination>7) || (iCurrent<0) || (iCurrent>7) || (iDestination<0) || (iDestination>7))
- {
- return 1;
- }
- else if (((piecesBoard[iCurrent][jCurrent]>='a') && (piecesBoard[iCurrent][jCurrent]<='z')) && ((piecesBoard[iDestination][jDestination]>='a') && (piecesBoard[iDestination][jDestination]<='z')))
- {
- return 5;
- }
- else if (((piecesBoard[iCurrent][jCurrent]>='A') && (piecesBoard[iCurrent][jCurrent]<='Z')) && ((piecesBoard[iDestination][jDestination]>='A') && (piecesBoard[iDestination][jDestination]<='Z')))
- {
- return 5;
- }
- else if(piecesBoard[iCurrent][jCurrent]=='\0')
- {
- return 4;
- }
- else if(((piecesBoard[iCurrent][jCurrent]=='r')||(piecesBoard[iCurrent][jCurrent]=='R')) && (rook(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else if(((piecesBoard[iCurrent][jCurrent]=='p')||(piecesBoard[iCurrent][jCurrent]=='P')) && (pawn(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else if(((piecesBoard[iCurrent][jCurrent]=='b')||(piecesBoard[iCurrent][jCurrent]=='B')) && (bishop(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else if(((piecesBoard[iCurrent][jCurrent]=='q')||(piecesBoard[iCurrent][jCurrent]=='Q')) && (queen(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else if(((piecesBoard[iCurrent][jCurrent]=='k')||(piecesBoard[iCurrent][jCurrent]=='K')) && (king(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else if(((piecesBoard[iCurrent][jCurrent]=='n')||(piecesBoard[iCurrent][jCurrent]=='N')) && (knight(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else
- {
- return 0;
- }
- }
- int validateTurns(int iCurrent, int iDesination, int jCurrent, int jDestination)
- {
- if(turn%2!=0 && (piecesBoard[iCurrent][jCurrent]>='A' && piecesBoard[iCurrent][jCurrent]<='Z'))
- return 1;
- else if(turn%2==0 && (piecesBoard[iCurrent][jCurrent]>='a' && piecesBoard[iCurrent][jCurrent]<='z'))
- return 2;
- else
- return 0;
- }
- int validateUndoRedo(char inp[1000])
- {
- if((strcmp("undo",inp) == 0)||(strcmp("UNDO",inp) ==0) && turn==1)
- return 1;
- else if((strcmp("redo",inp)==0)||(strcmp("REDO",inp) ==0) && undo[movesCount]==0)
- return 2;
- else
- return 0;
- }
- int validatePromotion(char inp[1000])
- {
- int f2=0;
- if((predictPromotion()==1 && strlen(inp)==5))
- {
- f2=1;
- }
- if(strlen(inp)!=4 && !f2)
- {
- return 1;
- }
- }
- void capture(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- if(piecesBoard[iDestination][jDestination]>='a' && piecesBoard[iDestination][jDestination]<='z')
- {
- capturedWhite[countWhite] = piecesBoard[iDestination][jDestination];
- countWhite++;
- }
- else if(piecesBoard[iDestination][jDestination]>='A' && piecesBoard[iDestination][jDestination]<='Z')
- {
- capturedBlack[countBlack] = piecesBoard[iDestination][jDestination];
- countBlack++;
- }
- }
- int rook(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- int count,flag=0;
- if((jCurrent==jDestination) && (iCurrent!= iDestination))
- {
- if (iDestination>iCurrent)
- {
- for (count=1; (((iCurrent+count)<iDestination)&&flag==0); count++)
- {
- if (piecesBoard[iCurrent+count][jCurrent]=='\0')
- {
- flag=0 ;
- }
- else
- {
- flag=1;
- }
- }
- }
- else
- {
- for(count=1; (((iCurrent-count)>iDestination)&&flag==0); count++)
- {
- if (piecesBoard[iCurrent-count][jCurrent]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- }
- if (flag==0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- else if((jCurrent!=jDestination) && (iCurrent==iDestination))
- {
- if (jDestination>jCurrent)
- {
- for (count=1; (((jCurrent+count)<jDestination)&&flag==0); count++)
- {
- if (piecesBoard[iCurrent][jCurrent+count]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- }
- else
- {
- for (count=1; (jCurrent-count)>jDestination; count++)
- {
- if (piecesBoard[iCurrent][jCurrent-count]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- }
- if (flag==0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- else
- {
- return 1;
- }
- }
- int king(int iCurrent,int iDestination,int jCurrent,int jDestination)
- {
- int iDiff,jDiff;
- iDiff=iCurrent-iDestination;
- jDiff=jCurrent-jDestination;
- if (((iCurrent == iDestination) && (abs(jDiff)==1)) || ((jCurrent==jDestination) && (abs(iDiff)==1)) || (abs(iDiff)==1 && abs(jDiff)==1))
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- int bishop(int iCurrent,int iDestination,int jCurrent,int jDestination)
- {
- int iDiff,jDiff;
- int count=1,flag=0;
- iDiff=iDestination-iCurrent;
- jDiff=jDestination-jCurrent;
- int DeciCurrent,InciCurrent,DecjCurrent,IncjCurrent;
- if (abs(iDiff)==abs(jDiff))
- {
- if (iDestination>iCurrent)
- {
- count=1;
- do
- {
- DecjCurrent=jCurrent-count;
- IncjCurrent=jCurrent+count;
- InciCurrent=iCurrent+count;
- if (InciCurrent<iDestination)
- {
- if (jDestination<jCurrent)
- {
- if (piecesBoard[InciCurrent][DecjCurrent]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- else if (jDestination>jCurrent)
- {
- if (piecesBoard[InciCurrent][IncjCurrent]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- count++;
- }
- }
- while ((InciCurrent<iDestination) && (flag==0));
- if (flag==0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- else
- {
- count=1;
- do
- {
- DeciCurrent=iCurrent-count;
- DecjCurrent=jCurrent-count;
- IncjCurrent=jCurrent+count;
- if (DeciCurrent>iDestination)
- {
- if (jDestination<jCurrent)
- {
- if (piecesBoard[DeciCurrent][DecjCurrent]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- else if (jDestination>jCurrent)
- {
- if (piecesBoard[DeciCurrent][IncjCurrent]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- count++;
- }
- }
- while ((DeciCurrent>iDestination) && (flag==0));
- if (flag==0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- }
- else
- {
- return 1;
- }
- }
- int pawn(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- if(piecesBoard[iCurrent][jCurrent]=='p' && (jDestination==jCurrent) && (iDestination-iCurrent==-1))
- {
- if (piecesBoard[iDestination][jDestination]!='\0')
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- else if(piecesBoard[iCurrent][jCurrent]=='P' && (jDestination==jCurrent) && (iDestination-iCurrent==1))
- {
- if (piecesBoard[iDestination][jDestination]!='\0')
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- else if(piecesBoard[iCurrent][jCurrent]=='p' && iDestination-iCurrent==-1 && abs(jDestination-jCurrent)==1 && piecesBoard[iDestination][jDestination]>='A' && piecesBoard[iDestination][jDestination]<='Z')
- return 0;
- else if(piecesBoard[iCurrent][jCurrent]=='P' && iDestination-iCurrent==1 && abs(jDestination-jCurrent)==1 && piecesBoard[iDestination][jDestination]>='a' && piecesBoard[iDestination][jDestination]<='z')
- return 0;
- else if(piecesBoard[iCurrent][jCurrent]=='p' && iCurrent==6 && jCurrent==jDestination && (iDestination-iCurrent==-1 || iDestination-iCurrent==-2))
- return 0;
- else if(piecesBoard[iCurrent][jCurrent]=='P' && iCurrent==1 && jCurrent==jDestination && (iDestination-iCurrent==1 || iDestination-iCurrent==2))
- return 0;
- else
- return 1;
- }
- int queen(int iCurrent,int iDestination,int jCurrent,int jDestination)
- {
- int iDiff,jDiff;
- iDiff=iDestination-iCurrent;
- jDiff=jDestination-jCurrent;
- if(((iDestination == iCurrent && jDestination != jCurrent) || (iDestination != iCurrent && jDestination == jCurrent))&& rook(iCurrent,iDestination,jCurrent,jDestination)==0)
- return 0;
- else if (abs(iDiff)==abs(jDiff) && bishop(iCurrent, iDestination, jCurrent, jDestination)==0)
- return 0;
- else
- return 1;
- }
- int knight(int iCurrent,int iDestination,int jCurrent,int jDestination)
- {
- int iDiff,jDiff;
- iDiff=iDestination-iCurrent;
- jDiff=jDestination-jCurrent;
- if ((abs(iDiff)==2) && (abs(jDiff)==1))
- return 0;
- else if ((abs(jDiff)==2) && (abs(iDiff)==1))
- return 0;
- else
- return 1;
- }
- int check()
- {
- int f=0;
- for(int i=0; i<8; i++)
- {
- for(int j=0; j<8; j++)
- {
- if(turn%2==1)
- {
- findKings();
- if(validate(i, iBlackKing, j, jBlackKing)==0)
- {
- f=1;
- }
- }
- else if(turn%2==0)
- {
- findKings();
- if(validate(i, iWhiteKing, j, jWhiteKing)==0)
- {
- f=1;
- }
- }
- }
- }
- return f;
- }
- void checkmate()
- {
- int f=0;
- if(check()==1)
- {
- for(int iC=0; iC<8; iC++)
- {
- for(int jC=0; jC<8; jC++)
- {
- if(turn%2==1)
- {
- if(prototypeBoard[iC][jC]>='A' && prototypeBoard[iC][jC]<='Z')
- {
- for(int iD=0; iD<8; iD++)
- {
- for(int jD=0; jD<8; jD++)
- {
- if(validatePrototype(iC,iD,jC,jD)==0)
- {
- prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
- prototypeBoard[iC][jC] = '\0';
- if(checkPrototype()==0)
- {
- f=1;
- }
- correspondPrototypeBoard();
- }
- }
- }
- }
- }
- else if(turn%2==0)
- {
- if(prototypeBoard[iC][jC]>='a' && prototypeBoard[iC][jC]<='z')
- {
- for(int iD=0; iD<8; iD++)
- {
- for(int jD=0; jD<8; jD++)
- {
- if(validatePrototype(iC,iD,jC,jD)==0)
- {
- prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
- prototypeBoard[iC][jC] = '\0';
- if(checkPrototype()==0)
- {
- f=1;
- }
- correspondPrototypeBoard();
- }
- }
- }
- }
- }
- }
- }
- if(f==0)
- checkmateFlag=1;
- }
- }
- void stalemate()
- {
- int f=0;
- if(check()==0)
- {
- for(int iC=0; iC<8; iC++)
- {
- for(int jC=0; jC<8; jC++)
- {
- if(turn%2==1)
- {
- if(prototypeBoard[iC][jC]>='A' && prototypeBoard[iC][jC]<='Z')
- {
- for(int iD=0; iD<8; iD++)
- {
- for(int jD=0; jD<8; jD++)
- {
- if(validatePrototype(iC,iD,jC,jD)==0)
- {
- prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
- prototypeBoard[iC][jC] = '\0';
- if(checkPrototype()==0)
- {
- f=1;
- }
- correspondPrototypeBoard();
- }
- }
- }
- }
- }
- else if(turn%2==0)
- {
- if(prototypeBoard[iC][jC]>='a' && prototypeBoard[iC][jC]<='z')
- {
- for(int iD=0; iD<8; iD++)
- {
- for(int jD=0; jD<8; jD++)
- {
- if(validatePrototype(iC,iD,jC,jD)==0)
- {
- prototypeBoard[iD][jD]=prototypeBoard[iC][jC];
- prototypeBoard[iC][jC] = '\0';
- if(checkPrototype()==0)
- {
- f=1;
- }
- correspondPrototypeBoard();
- }
- }
- }
- }
- }
- }
- }
- if(f==0)
- checkmateFlag=2;
- }
- }
- int printErrors(int iCurrent, int iDestination, int jCurrent, int jDestination, char inp[1000])
- {
- int f=0;
- f = validateUndoRedo(inp);
- switch(f)
- {
- case 0:
- break;
- case 1:
- printf("You Can't Undo More!\n");
- return 1;
- case 2:
- printf("You Can't Redo More!\n");
- return 1;
- }
- if(strcmp("UNDO",inp)==0 || strcmp("undo",inp)==0 || strcmp("redo",inp)==0 || strcmp("REDO", inp)==0)
- return;
- f = validatePromotion(inp);
- switch(f)
- {
- case 0:
- break;
- case 1:
- printf("Invalid Input!\n");
- return 1;
- }
- f = validateTurns(iCurrent, iDestination, jCurrent, jDestination);
- switch(f)
- {
- case 0:
- break;
- case 1:
- printf("White Pieces Turn!\n");
- return 1;
- case 2:
- printf("Black Pieces Turn!\n");
- return 1;
- }
- f = validate(iCurrent, iDestination, jCurrent, jDestination);
- switch(f)
- {
- case 0:
- break;
- case 1:
- printf("Invalid Input!\n");
- return 1;
- case 4:
- printf("Empty Position!\n");
- return 1;
- case 5:
- printf("Wrong Move!\n");
- return 1;
- }
- f = validateCheck(iCurrent,iDestination,jCurrent,jDestination);
- switch(f)
- {
- case 0:
- break;
- case 1:
- if(turn%2==1)
- printf("WHITE KING WILL BE CHECKED!\n");
- else if(turn%2==0)
- printf("BLACK KING WILL BE CHECKED!\n");
- return 1;
- }
- f= (strlen(inp)==4 && (((piecesBoard[iCurrent][jCurrent]=='p' && iDestination==0)) || ((piecesBoard[iCurrent][jCurrent]=='P' && iDestination==7))));
- switch(f)
- {
- case 0:
- break;
- case 1:
- printf("You Must Enter A Valid Type For The Promotion!\n");
- return 1;
- }
- f = (strlen(inp)==5 && promote(iCurrent,jCurrent, inp[4])==0);
- switch(f)
- {
- case 0:
- return;
- case 1:
- printf("Invalid!\n");
- return 1;
- }
- return f;
- }
- int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- char tempPiece;
- int f=0, fC=0;
- if(validatePrototype(iCurrent,iDestination,jCurrent,jDestination)!=0)
- f=1;
- else
- {
- if(prototypeBoard[iCurrent][jCurrent]=='k')
- {
- iWhiteKing = iDestination;
- jWhiteKing = jDestination;
- fC=1;
- }
- else if(prototypeBoard[iCurrent][jCurrent]=='K')
- {
- iBlackKing = iDestination;
- jBlackKing = jDestination;
- fC=2;
- }
- prototypeBoard[iDestination][jDestination] = prototypeBoard[iCurrent][jCurrent];
- prototypeBoard[iCurrent][jCurrent] = '\0';
- turn++;
- if(checkPrototype()==1)
- {
- f=1;
- }
- if(fC==1)
- {
- iWhiteKing = iCurrent;
- jWhiteKing = jCurrent;
- }
- else if(fC==2)
- {
- iBlackKing = iCurrent;
- jBlackKing = jCurrent;
- }
- correspondPrototypeBoard();
- turn--;
- }
- return f;
- }
- void correspondPrototypeBoard()
- {
- for(int i=0; i<8; i++)
- {
- for(int j=0; j<8; j++)
- {
- prototypeBoard[i][j]=piecesBoard[i][j];
- }
- }
- }
- int checkPrototype()
- {
- int f=0;
- for(int i=0; i<8; i++)
- {
- for(int j=0; j<8; j++)
- {
- if(turn%2==1)
- {
- if(validatePrototype(i, iBlackKing, j, jBlackKing)==0)
- {
- f=1;
- }
- }
- else if(turn%2==0)
- {
- if(validatePrototype(i, iWhiteKing, j, jWhiteKing)==0)
- {
- f=1;
- }
- }
- }
- }
- return f;
- }
- int validatePrototype(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- if ((jCurrent<0) || (jCurrent>7) || (jDestination<0) || (jDestination>7) || (iCurrent<0) || (iCurrent>7) || (iDestination<0) || (iDestination>7))
- {
- return 1;
- }
- else if (((prototypeBoard[iCurrent][jCurrent]>='a') && (prototypeBoard[iCurrent][jCurrent]<='z')) && ((prototypeBoard[iDestination][jDestination]>='a') && (prototypeBoard[iDestination][jDestination]<='z')))
- {
- return 5;
- }
- else if (((prototypeBoard[iCurrent][jCurrent]>='A') && (prototypeBoard[iCurrent][jCurrent]<='Z')) && ((prototypeBoard[iDestination][jDestination]>='A') && (prototypeBoard[iDestination][jDestination]<='Z')))
- {
- return 5;
- }
- else if(prototypeBoard[iCurrent][jCurrent]=='\0')
- {
- return 4;
- }
- else if(((prototypeBoard[iCurrent][jCurrent]=='r')||(prototypeBoard[iCurrent][jCurrent]=='R')) && (prototypeRook(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else if(((prototypeBoard[iCurrent][jCurrent]=='p')||(prototypeBoard[iCurrent][jCurrent]=='P')) && (prototypePawn(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else if(((prototypeBoard[iCurrent][jCurrent]=='b')||(prototypeBoard[iCurrent][jCurrent]=='B')) && (prototypeBishop(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else if(((prototypeBoard[iCurrent][jCurrent]=='q')||(prototypeBoard[iCurrent][jCurrent]=='Q')) && (prototypeQueen(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else if(((prototypeBoard[iCurrent][jCurrent]=='k')||(prototypeBoard[iCurrent][jCurrent]=='K')) && (prototypeKing(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else if(((prototypeBoard[iCurrent][jCurrent]=='n')||(prototypeBoard[iCurrent][jCurrent]=='N')) && (knight(iCurrent, iDestination, jCurrent, jDestination)==1))
- {
- return 5;
- }
- else
- {
- return 0;
- }
- }
- int prototypeRook(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- int count,flag=0;
- if((jCurrent==jDestination) && (iCurrent!= iDestination))
- {
- if (iDestination>iCurrent)
- {
- for (count=1; (((iCurrent+count)<iDestination)&&flag==0); count++)
- {
- if (prototypeBoard[iCurrent+count][jCurrent]=='\0')
- {
- flag=0 ;
- }
- else
- {
- flag=1;
- }
- }
- }
- else
- {
- for(count=1; (((iCurrent-count)>iDestination)&&flag==0); count++)
- {
- if (prototypeBoard[iCurrent-count][jCurrent]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- }
- if (flag==0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- else if((jCurrent!=jDestination) && (iCurrent==iDestination))
- {
- if (jDestination>jCurrent)
- {
- for (count=1; (((jCurrent+count)<jDestination)&&flag==0); count++)
- {
- if (prototypeBoard[iCurrent][jCurrent+count]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- }
- else
- {
- for (count=1; (jCurrent-count)>jDestination; count++)
- {
- if (prototypeBoard[iCurrent][jCurrent-count]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- }
- if (flag==0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- else
- {
- return 1;
- }
- }
- int prototypeKing(int iCurrent,int iDestination,int jCurrent,int jDestination)
- {
- int iDiff,jDiff;
- iDiff=iCurrent-iDestination;
- jDiff=jCurrent-jDestination;
- if (((iCurrent == iDestination) && (abs(jDiff)==1)) || ((jCurrent==jDestination) && (abs(iDiff)==1)) || (abs(iDiff)==1 && abs(jDiff)==1))
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- int prototypeBishop(int iCurrent,int iDestination,int jCurrent,int jDestination)
- {
- int iDiff,jDiff;
- int count=1,flag=0;
- iDiff=iDestination-iCurrent;
- jDiff=jDestination-jCurrent;
- int DeciCurrent,InciCurrent,DecjCurrent,IncjCurrent;
- if (abs(iDiff)==abs(jDiff))
- {
- if (iDestination>iCurrent)
- {
- count=1;
- do
- {
- DecjCurrent=jCurrent-count;
- IncjCurrent=jCurrent+count;
- InciCurrent=iCurrent+count;
- if (InciCurrent<iDestination)
- {
- if (jDestination<jCurrent)
- {
- if (prototypeBoard[InciCurrent][DecjCurrent]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- else if (jDestination>jCurrent)
- {
- if (prototypeBoard[InciCurrent][IncjCurrent]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- count++;
- }
- }
- while ((InciCurrent<iDestination) && (flag==0));
- if (flag==0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- else
- {
- count=1;
- do
- {
- DeciCurrent=iCurrent-count;
- DecjCurrent=jCurrent-count;
- IncjCurrent=jCurrent+count;
- if (DeciCurrent>iDestination)
- {
- if (jDestination<jCurrent)
- {
- if (prototypeBoard[DeciCurrent][DecjCurrent]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- else if (jDestination>jCurrent)
- {
- if (prototypeBoard[DeciCurrent][IncjCurrent]=='\0')
- {
- flag=0;
- }
- else
- {
- flag=1;
- }
- }
- count++;
- }
- }
- while ((DeciCurrent>iDestination) && (flag==0));
- if (flag==0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- }
- else
- {
- return 1;
- }
- }
- int prototypePawn(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- if(prototypeBoard[iCurrent][jCurrent]=='p' && (jDestination==jCurrent) && (iDestination-iCurrent==-1))
- {
- if (prototypeBoard[iDestination][jDestination]!='\0')
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- else if(prototypeBoard[iCurrent][jCurrent]=='P' && (jDestination==jCurrent) && (iDestination-iCurrent==1))
- {
- if (prototypeBoard[iDestination][jDestination]!='\0')
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- else if(prototypeBoard[iCurrent][jCurrent]=='p' && iDestination-iCurrent==-1 && abs(jDestination-jCurrent)==1 && prototypeBoard[iDestination][jDestination]>='A' && prototypeBoard[iDestination][jDestination]<='Z')
- return 0;
- else if(prototypeBoard[iCurrent][jCurrent]=='P' && iDestination-iCurrent==1 && abs(jDestination-jCurrent)==1 && prototypeBoard[iDestination][jDestination]>='a' && prototypeBoard[iDestination][jDestination]<='z')
- return 0;
- else if(prototypeBoard[iCurrent][jCurrent]=='p' && iCurrent==6 && jCurrent==jDestination && (iDestination-iCurrent==-1 || iDestination-iCurrent==-2))
- return 0;
- else if(prototypeBoard[iCurrent][jCurrent]=='P' && iCurrent==1 && jCurrent==jDestination && (iDestination-iCurrent==1 || iDestination-iCurrent==2))
- return 0;
- else
- return 1;
- }
- int prototypeQueen(int iCurrent,int iDestination,int jCurrent,int jDestination)
- {
- int iDiff,jDiff;
- iDiff=iDestination-iCurrent;
- jDiff=jDestination-jCurrent;
- if(((iDestination == iCurrent && jDestination != jCurrent) || (iDestination != iCurrent && jDestination == jCurrent))&& prototypeRook(iCurrent,iDestination,jCurrent,jDestination)==0)
- return 0;
- else if (abs(iDiff)==abs(jDiff) && prototypeBishop(iCurrent, iDestination, jCurrent, jDestination)==0)
- return 0;
- else
- return 1;
- }
- void findKings()
- {
- for(int i=0; i<8; i++)
- {
- for(int j=0; j<8; j++)
- {
- if(piecesBoard[i][j]=='k')
- {
- iWhiteKing = i;
- jWhiteKing = j;
- }
- else if(piecesBoard[i][j]=='K')
- {
- iBlackKing = i;
- jBlackKing = j;
- }
- }
- }
- }
- void trackUndo(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- undo[movesCount] = iCurrent*1000 + iDestination*100 + jCurrent*10 + jDestination;
- movesCount++;
- if(piecesBoard[iDestination][jDestination] != '\0')
- {
- capturingTrack[captureTurn] = turn;
- captureTurn++;
- }
- }
- void processUndo()
- {
- int iC,iD,jC,jD;
- movesCount--;
- iC = undo[movesCount]/1000;
- iD = undo[movesCount]/100%10;
- jC = undo[movesCount]/10%10;
- jD = undo[movesCount]%10;
- int f=0;
- if((turn-1)==capturingTrack[captureTurn-1])
- {
- move(iD,iC,jD,jC);
- if(turn%2==1)
- {
- piecesBoard[iD][jD] = capturedWhite[countWhite-1];
- countWhite--;
- }
- else if(turn%2==0)
- {
- piecesBoard[iD][jD] = capturedBlack[countBlack-1];
- countBlack--;
- }
- captureTurn--;
- f=1;
- if((turn-1)==promotionTrack[promoteTurn-1] && promoteTurn!=0)
- {
- if(f==0)
- move(iD,iC,jD,jC);
- promoteTurn--;
- if(turn%2==1)
- piecesBoard[iC][jC] = 'P';
- else if(turn%2==0)
- piecesBoard[iC][jC] = 'p';
- }
- }
- else
- {
- move(iD, iC, jD, jC);
- }
- findKings();
- correspondPrototypeBoard();
- turn-=2;
- }
- void processRedo()
- {
- int iC, iD, jC, jD;
- iC = undo[movesCount]/1000;
- iD = undo[movesCount]/100%10;
- jC = undo[movesCount]/10%10;
- jD = undo[movesCount]%10;
- trackUndo(iC,iD,jC,jD);
- move(iC,iD,jC,jD);
- findKings();
- correspondPrototypeBoard();
- if((turn)==capturingTrack[captureTurn-1])
- {
- if(turn%2==0)
- {
- countWhite++;
- }
- else if(turn%2==1)
- {
- countBlack++;
- }
- if((turn)== promotionTrack[promoteTurn])
- {
- piecesBoard[iD][jD] = promotionPieces[promoteTurn];
- promoteTurn++;
- }
- }
- }
- int predictPromotion()
- {
- int f=0;
- for(int j=0; j<8; j++)
- {
- if(turn%2==1) {
- if(piecesBoard[1][j]=='p')
- {
- f=1;
- break;
- }
- }
- else if(turn%2==0) {
- if(piecesBoard[1][j]=='P')
- {
- f=1;
- break;
- }
- }
- }
- return f;
- }
- int promote(int i, int j, char type)
- {
- if(predictPromotion()==1)
- {
- if(piecesBoard[i][j]=='p' && (type=='q' || type=='b' || type=='n'))
- return 1;
- else if(piecesBoard[i][j]=='P' && (type=='Q' || type=='B' || type=='N'))
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement