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 board[10][10];
- int turn=0, capturedBlack[16], capturedWhite[16], countBlack=0, countWhite=0;
- 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);
- void printErrors(int, int, int, int, int);
- 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 checkerPieces[], checksCount;
- int check();
- int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination);
- int checkmate();
- int checkMateFlag=0;
- int main()
- {
- initializeBW();
- while(checkmate()==0)
- {
- turn++;
- printBoard();
- userInput();
- printf("\n");
- }
- printf("CHECKMATE!\n");
- printf("%d %d", iWhiteKing, jWhiteKing);
- }
- 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 userInput()
- {
- char inp[10];
- gets(inp);
- while(strlen(inp)!=4)
- {
- printf("Invalid Input!\n");
- gets(inp);
- }
- int iCurrent, iDestination, jCurrent, jDestination;
- 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(validate(iCurrent, iDestination, jCurrent, jDestination)!=0)
- {
- printErrors(validate(iCurrent, iDestination, jCurrent, jDestination), iCurrent, iDestination, jCurrent, jDestination);
- userInput();
- }
- else if(validateCheck(iCurrent,iDestination,jCurrent,jDestination==1)) {
- printf("KING WILL BE CHECKED!\n");
- userInput();
- }
- else if(validate(iCurrent, iDestination, jCurrent, jDestination)==0 && validateCheck(iCurrent,iDestination,jCurrent,jDestination)==0)
- {
- capture(iCurrent, iDestination, jCurrent, jDestination);
- move(iCurrent, iDestination, jCurrent, jDestination);
- }
- }
- 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(turn%2!=0 && (piecesBoard[iCurrent][jCurrent]>='A' && piecesBoard[iCurrent][jCurrent]<='Z'))
- {
- return 2;
- }
- else if(turn%2==0 && (piecesBoard[iCurrent][jCurrent]>='a' && piecesBoard[iCurrent][jCurrent]<='z'))
- {
- return 3;
- }
- 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 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 {
- return 0;
- }
- }
- 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++;
- }
- system("cls");
- 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");
- }
- 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))
- {
- if(piecesBoard[iCurrent][jCurrent]=='k')
- {
- iWhiteKing = iDestination;
- jWhiteKing = jDestination;
- }
- else if(piecesBoard[iCurrent][jCurrent]=='K')
- {
- iBlackKing = iDestination;
- jBlackKing = jDestination;
- }
- 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)
- {
- if(validate(i, iBlackKing, j, jBlackKing)==0)
- {
- f=1;
- }
- }
- else if(turn%2==0)
- {
- if(validate(i, iWhiteKing, j, jWhiteKing)==0)
- {
- f=1;
- }
- }
- }
- }
- return f;
- }
- int checkmate()
- {
- int f=0;
- if(check()==1) {
- if(turn%2==0) {
- if(validate(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing)==0)
- f = f + validateCheck(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing);
- else
- f = f+1;
- if(validate(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing)==0)
- f = f + validateCheck(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing);
- else
- f = f+1;
- if(validate(iWhiteKing, iWhiteKing, jWhiteKing, jWhiteKing+1)==0)
- f = f + validateCheck(iWhiteKing, iWhiteKing, jWhiteKing, jWhiteKing+1);
- else
- f = f+1;
- if(validate(iWhiteKing, iWhiteKing, jWhiteKing, jWhiteKing-1)==0)
- f = f + validateCheck(iWhiteKing, iWhiteKing, jWhiteKing, jWhiteKing-1);
- else
- f = f+1;
- if(validate(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing+1)==0)
- f = f + validateCheck(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing+1);
- else
- f = f+1;
- if(validate(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing-1)==0)
- f = f + validateCheck(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing-1);
- else
- f = f+1;
- if(validate(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing+1)==0)
- f = f + validateCheck(iWhiteKing, iWhiteKing-1, jWhiteKing, jWhiteKing+1);
- else
- f = f+1;
- if(validate(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing-1)==0)
- f = f + validateCheck(iWhiteKing, iWhiteKing+1, jWhiteKing, jWhiteKing-1);
- else
- f = f+1;
- }
- else if(turn%2==1) {
- if(validate(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing)==0)
- f = f + validateCheck(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing);
- else
- f = f+1;
- if(validate(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing)==0)
- f = f + validateCheck(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing);
- else
- f = f+1;
- if(validate(iBlackKing, iBlackKing, jBlackKing, jBlackKing+1)==0)
- f = f + validateCheck(iBlackKing, iBlackKing, jBlackKing, jBlackKing+1);
- else
- f = f+1;
- if(validate(iBlackKing, iBlackKing, jBlackKing, jBlackKing-1)==0)
- f = f + validateCheck(iBlackKing, iBlackKing, jBlackKing, jBlackKing-1);
- else
- f = f+1;
- if(validate(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing+1)==0)
- f = f + validateCheck(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing+1);
- else
- f = f+1;
- if(validate(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing-1)==0)
- f = f + validateCheck(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing-1);
- else
- f = f+1;
- if(validate(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing+1)==0)
- f = f + validateCheck(iBlackKing, iBlackKing-1, jBlackKing, jBlackKing+1);
- else
- f = f+1;
- if(validate(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing-1)==0)
- f = f + validateCheck(iBlackKing, iBlackKing+1, jBlackKing, jBlackKing-1);
- else
- f = f+1;
- }
- if(f==8)
- return 1;
- else
- return 0;
- }
- }
- void printErrors(int f, int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- f = validate(iCurrent, iDestination, jCurrent, jDestination);
- switch(f)
- {
- case 0:
- break;
- case 1:
- printf("Invalid Input!\n");
- break;
- case 2:
- printf("White Pieces Turn!\n");
- break;
- case 3:
- printf("Black Pieces Turn!\n");
- break;
- case 4:
- printf("Empty Position!\n");
- break;
- case 5:
- printf("Wrong Move!\n");
- break;
- }
- }
- int validateCheck(int iCurrent, int iDestination, int jCurrent, int jDestination)
- {
- int tempiBlackKing = iBlackKing;
- int tempjBlackKing = jBlackKing;
- int tempiWhiteKing = iWhiteKing;
- int tempjWhiteKing = jWhiteKing;
- char tempPiece;
- int f=0, fC=0;
- if(piecesBoard[iDestination][jDestination]!='\0') {
- tempPiece = piecesBoard[iDestination][jDestination];
- fC=1;
- }
- move(iCurrent, iDestination, jCurrent, jDestination);
- turn++;
- if(check()==1) {
- f=1;
- }
- move(iDestination, iCurrent, jDestination, jCurrent);
- if(fC=1) {
- piecesBoard[iDestination][jDestination] = tempPiece;
- }
- turn--;
- return f;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement