Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <windows.h>
- #include <math.h>
- /*Used Variables c,k,n,char temp*/
- int k=0,n=0,g=2,xk=8,xK=1;
- char board[8][8],yk='E',yK='E',p;
- int c=1;
- int x1,x2;
- char eaten1[16]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
- char eaten2[16]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
- char index1[8]={'A','B','C','D','E','F','G','H'};
- int index2[8]={1,2,3,4,5,6,7,8};
- int valid(int x1,char y1,int x2,char y2);
- int removecheckwhite (int x1,char y1,int x2,char y2);
- int removecheckblack(int x1,char y1,int x2,char y2);
- int checkTurns(int x1,char y1);
- int check1 (int xk,char yk,int xK,char yK);
- int checkwhite (int xk,char yk,int xK,char yK);
- int checkblack (int xk,char yk,int xK,char yK);
- int stalemateWhite(int);
- int stalemateBlack(int);
- void gotoxy(int x, int y)
- {
- COORD coord;
- coord.X = x;
- coord.Y = y;
- SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
- }
- void delay(unsigned int mseconds)
- {
- clock_t goal = mseconds + clock();
- while (goal > clock());
- }
- int main()
- {
- char y1,y2,mode,temp;
- /*Initializing the initial board*/
- for(int i=0;i<2;i++)
- {
- if(i==1)
- {
- for(int j=0;j<8;j++)
- {
- board[i][j]='P';
- }
- }
- else
- {
- for(int j=0;j<8;j++)
- {
- switch (j)
- {
- case 0: board[i][j]='R'; break;
- case 1: board[i][j]='N'; break;
- case 2: board[i][j]='B'; break;
- case 3: board[i][j]='Q'; break;
- case 4: board[i][j]='K'; break;
- case 5: board[i][j]='B'; break;
- case 6: board[i][j]='N'; break;
- case 7: board[i][j]='R'; break;
- }
- }
- }
- }
- for(int i=6;i<8;i++)
- {
- if(i==6)
- {
- for(int j=0;j<8;j++)
- {
- board[i][j]='p';
- }
- }
- else
- {
- for(int j=0;j<8;j++)
- {
- switch (j)
- {
- case 0: board[i][j]='r'; break;
- case 1: board[i][j]='n'; break;
- case 2: board[i][j]='b'; break;
- case 3: board[i][j]='q'; break;
- case 4: board[i][j]='k'; break;
- case 5: board[i][j]='b'; break;
- case 6: board[i][j]='n'; break;
- case 7: board[i][j]='r'; break;
- }
- }
- }
- }
- for(int i=2;i<6;i++)
- {
- for(int j=0;j<8;j++)
- {
- if ((i+j)%2==1)
- board[i][j]=' ';
- else
- board[i][j]=219;
- }
- }
- /*End of Assiging Values*/
- /*Printing the board at the begining*/
- printf("\n\n");
- printf(" Eaten Pieces Of Player (1) ");
- for(int i=0;i<16;i++)
- {
- printf("%c|",eaten1[i]);
- }
- printf("\n");
- printf(" Eaten Pieces Of Player (2) ");
- for(int i=0;i<16;i++)
- {
- printf("%c|",eaten2[i]);
- }
- printf("\n\n\n\n");
- printf(" Player {2} Black\n\n");
- printf(" ");
- for(int i=0;i<8;i++)
- {
- printf(" .%c.",index1[i]);
- }
- printf("\n");
- for(int row=0;row<8;row++)
- {
- /* Draw the top line */
- printf(" ");
- for(int column=0;column<8;column++)
- printf(" ---");
- putchar('\n');
- /* Draw the squares */
- printf(" ");
- printf("%d",c++);
- for(int column=0;column<8;column++)
- {
- printf("| %c ",board[row][column]);
- }
- printf("|\n");
- }
- printf(" ");
- for(int column=0;column<8;column++)
- printf(" ---");
- printf("\n\n");
- printf(" Player {1} White ");
- printf("\n\n");
- /* End of Printing the board before playing*//*Printing the board before playing*//*Printing the board before playing*/
- printf("Enter the desired piece\n");
- scanf("%d %c",&x1,&y1);
- printf("Enter the desired destination\n");
- scanf("%d %c",&x2,&y2);
- while((valid(x1,y1,x2,y2)==0 || checkTurns(x1,y1)==0)){
- printf("NOT VALID MOVE\n");
- printf("Enter the desired piece\n");
- scanf("%d %c",&x1,&y1);
- printf("Enter the desired destination\n");
- scanf("%d %c",&x2,&y2);}
- g++;
- x1=x1-1;
- x2=x2-1;
- y1=y1-65;
- y2=y2-65;
- temp=board[x2][y2];
- board[x2][y2]=board[x1][y1];
- if((x1+y1)%2==1)
- {
- board[x1][y1]=' ';
- }
- else
- {
- board[x1][y1]=219;
- }
- /*Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooor*/
- for(;;)
- {
- system("cls");
- if(check1(xk,yk,xK,yK)!=0){printf("CHECK!");}
- int c=1;
- /*Printing The Board At Every Move*/
- printf("\n\n");
- printf(" Eaten Pieces Of Player (1) ");
- for(int i=0;i<16;i++)
- {
- printf("%c|",eaten1[i]);
- }
- printf("\n");
- printf(" Eaten Pieces Of Player (2) ");
- for(int i=0;i<16;i++)
- {
- printf("%c|",eaten2[i]);
- }
- printf("\n\n\n\n");
- printf(" Player {2} Black\n\n");
- printf(" ");
- for(int i=0;i<8;i++)
- {
- printf(" .%c.",index1[i]);
- }
- printf("\n");
- for(int row=0;row<8;row++)
- {
- /* Draw the top line */
- printf(" ");
- for(int column=0;column<8;column++)
- printf(" ---");
- putchar('\n');
- /* Draw the squares */
- printf(" ");
- printf("%d",c++);
- for(int column=0;column<8;column++)
- {
- printf("| %c ",board[row][column]);
- }
- printf("|\n");
- }
- printf(" ");
- for(int column=0;column<8;column++)
- printf(" ---");
- printf("\n\n");
- printf(" Player {1} White ");
- printf("\n\n");
- /*End of Printing The Board At Every Move*/
- if(checkblack(xk,yk,xK,yK)==1 && check1(xk,yk,xK,yK)==1)
- {
- printf("Player 1 wins\n");
- break;
- }
- if(checkwhite(xk,yk,xK,yK)==1 && check1(xk,yk,xK,yK)==2)
- {
- printf("Player 2 wins\n");
- break;
- }
- if(stalemateWhite(1)==1 || stalemateBlack(1)==1)
- {
- printf("IT'S A STALEMATE");
- break;
- }
- printf("To Move Press (M) , To Undo Press (U), To Redo Press (R)\n");
- mode=getch();
- if(mode=='U')
- {
- board[x1][y1]=board[x2][y2];
- board[x2][y2]=temp;
- }
- else if(mode=='R')
- {
- }
- else
- {
- printf("Enter the desired piece\n");
- scanf("%d %c",&x1,&y1);
- printf("Enter the desired destination\n");
- scanf("%d %c",&x2,&y2);
- while(valid(x1,y1,x2,y2)==0){
- printf("NOT VALID MOVE\n");
- printf("Enter the desired piece\n");
- scanf("%d %c",&x1,&y1);
- printf("Enter the desired destination\n");
- scanf("%d %c",&x2,&y2);}
- if(board[x1-1][y1-65]=='k'){xk=x2;
- yk=y2;}
- if(board[x1-1][y1-65]=='K'){xK=x2;
- yK=y2;}
- //check(xk,yk,xK,yK);
- x1=x1-1;
- x2=x2-1;
- y1=y1-65;
- y2=y2-65;
- /*Printing Eaten if Exist */
- if((board[x2][y2]>=97 && board[x2][y2]<=122))
- {
- eaten1[k]=board[x2][y2];
- k++;
- }
- if((board[x2][y2]>=65 && board[x2][y2]<=90))
- {
- eaten2[n]=board[x2][y2];
- n++;
- }
- /*Printing Eaten if Exist */
- temp=board[x2][y2];
- board[x2][y2]=board[x1][y1];
- if((x1+y1)%2==1)
- {
- board[x1][y1]=' ';
- }
- else
- {
- board[x1][y1]=219;
- }
- /*black*/
- while(check1(xk,yk,xK,yK)==1 && (board[x2][y2]>=65 && board[x2][y2]<=90))
- {
- board[x1][y1]=board[x2][y2];
- board[x2][y2]=temp;
- if(board[x1][y1]=='k'){xk=x1+1;
- yk=y1+65;}
- if(board[x1][y1]=='K'){xK=x1+1;
- yK=y1+65;}
- printf("CHECK!\n");
- printf("Enter the desired piece\n");
- scanf("%d %c",&x1,&y1);
- printf("Enter the desired destination\n");
- scanf("%d %c",&x2,&y2);
- if(board[x1-1][y1-65]=='k'){xk=x2;
- yk=y2;}
- if(board[x1-1][y1-65]=='K'){xK=x2;
- yK=y2;}
- x1=x1-1;
- x2=x2-1;
- y1=y1-65;
- y2=y2-65;
- temp=board[x2][y2];
- board[x2][y2]=board[x1][y1];}
- }
- while(check1(xk,yk,xK,yK)==2 && (board[x2][y2]>=97 && board[x2][y2]<=122))
- {
- board[x1][y1]=board[x2][y2];
- board[x2][y2]=temp;
- if(board[x1][y1]=='k'){xk=x1+1;
- yk=y1+65;}
- if(board[x1][y1]=='K'){xK=x1+1;
- yK=y1+65;}
- printf("CHECK!\n");
- printf("Enter the desired piece\n");
- scanf("%d %c",&x1,&y1);
- printf("Enter the desired destination\n");
- scanf("%d %c",&x2,&y2);
- if(board[x1-1][y1-65]=='k'){xk=x2;
- yk=y2;}
- if(board[x1-1][y1-65]=='K'){xK=x2;
- yK=y2;}
- x1=x1-1;
- x2=x2-1;
- y1=y1-65;
- y2=y2-65;
- temp=board[x2][y2];
- board[x2][y2]=board[x1][y1];}
- if((x1+y1)%2==1)
- {
- board[x1][y1]=' ';
- }
- else
- {
- board[x1][y1]=219;
- }
- }
- return 0;
- }
- int valid(int x1,char y1,int x2,char y2)
- {
- int condition=1,i,t1=0,t2=0; char tempo;
- y1=y1-65;
- y2=y2-65;
- x1=x1-1;
- x2=x2-1;
- char w[6]={'p','r','n','b','k','q'};
- char b[6]={'P','R','N','B','K','Q'};
- char c=219,tempq;
- if(board[x1][y1]=='b'||board[x1][y1]=='B'){
- if(abs(x2-x1)!=abs(y2-y1)){condition=0;}}
- else if(board[x1][y1]=='n'||board[x1][y1]=='N')
- {
- if(!((abs(x2-x1)==1&&abs(y2-y1)==2)||(abs(y2-y1)==1&&abs(x2-x1)==2))){condition=0;}
- }
- else if(board[x1][y1]=='r'||board[x1][y1]=='R')
- {
- if(x1!=x2&&y1!=y2){condition=0;}
- }
- else if(board[x1][y1]=='K'||board[x1][y1]=='k')
- {
- if(abs(x2-x1)>=2||abs(y2-y1)>=2){condition=0;}
- }
- else if(board[x1][y1]=='p'){
- if(!(x1-x2==1&&y1==y2)){condition=0;}
- }
- else if(board[x1][y1]=='P'){
- if(!(x2-x1==1&&y1==y2)){condition=0;}
- }
- else if(board[x1][y1]=='q'||board[x1][y1]=='Q')
- {
- if((abs(x2-x1)!=abs(y2-y1))&&(x1!=x2&&y1!=y2)){condition=0;}
- }
- for(i=0;i<6;i++){
- if(board[x1][y1]==w[i]){t1=1;}}
- for(i=0;i<6;i++){
- if(board[x2][y2]==w[i]){t2=1;}}
- if(t1==1&&t2==1){condition=0;}
- t1=0;
- t2=0;
- for(i=0;i<6;i++){
- if(board[x1][y1]==b[i]){t1=1;}}
- for(i=0;i<6;i++){
- if(board[x2][y2]==b[i]){t2=1;}}
- if(t1==1&&t2==1){condition=0;}
- if(board[x1][y1]==' '||board[x1][y1]==c){condition=0;}
- t1=0;
- t2=0;
- if(board[x1][y1]=='p'&&(x1-x2)==1&&abs(y2-y1)==1)
- {
- for(i=0;i<6;i++){
- if(board[x2][y2]==b[i]){condition=1;}}}
- if(board[x1][y1]=='P'&&(x2-x1)==1&&abs(y2-y1)==1)
- {
- for(i=0;i<6;i++){
- if(board[x2][y2]==w[i]){condition=1;}}}
- i=1,t1=0,t2=0;
- if((board[x1][y1]=='b'||board[x1][y1]=='B')&&condition==1)
- {
- if(x2>x1&&y2>y1){
- while(x1+i!=x2){
- if(board[x1+i][y1+i]!=' '&&board[x1+i][y1+i]!=c){t1=1;}
- i++;}
- if(t1==1){condition=0;}}
- else if(x2>x1&&y2<y1){
- t1=0,i=1;
- while(x1+i!=x2){
- if(board[x1+i][y1-i]!=' '&&board[x1+i][y1-i]!=c){t1=1;}
- i++;}
- if(t1==1){condition=0;}}
- else if(x2<x1&&y2<y1){
- t1=0,i=1;
- while(x1-i!=x2){
- if(board[x1-i][y1-i]!=' '&&board[x1-i][y1-i]!=c){t1=1;}
- i++;}
- if(t1==1){condition=0;}}
- else if(x2<x1&&y2>y1){
- t1=0,i=1;
- while(x1-i!=x2){
- if(board[x1-i][y1+i]!=' '&&board[x1-i][y1+i]!=c){t1=1;}
- i++;}
- if(t1==1){condition=0;}}
- t1=0,i=1;}
- if((board[x1][y1]=='r'||board[x1][y1]=='R')&&condition==1){
- if(x2>x1){
- i=1,t1=0;
- while(x1+i!=x2){
- if(board[x1+i][y1]!=' '&&board[x1+i][y1]!=c){t1=1;}
- i++;}
- if(t1==1){condition=0;}}
- if(x2<x1){
- i=1,t1=0;
- while(x1-i!=x2){
- if(board[x1-i][y1]!=' '&&board[x1-i][y1]!=c){t1=1;}
- i++;}
- if(t1==1){condition=0;}}
- if(y2>y1){
- i=1,t1=0;
- while(y1+i!=y2){
- if(board[x1][y1+i]!=' '&&board[x1][y1+i]!=c){t1=1;}
- i++;}
- if(t1==1){condition=0;}}
- if(y2<y1){
- i=1,t1=0;
- while(y1-i!=y2){
- if(board[x1][y1-i]!=' '&&board[x1][y1-i]!=c){t1=1;}
- i++;}
- if(t1==1){condition=0;}}}
- if(board[x1][y1]=='q'&&condition==1){
- if(abs(x2-x1)==abs(y2-y1)){
- tempq=board[x1][y1];
- board[x1][y1]='b';
- if(valid(x1+1,y1+65,x2+1,y2+65)==0){condition=0;}
- board[x1][y1]=tempq;}
- else {
- tempq=board[x1][y1];
- board[x1][y1]='r';
- if(valid(x1+1,y1+65,x2+1,y2+65)==0){condition=0;}
- board[x1][y1]=tempq;}}
- if(board[x1][y1]=='Q'&&condition==1){
- if(abs(x2-x1)==abs(y2-y1)){
- tempq=board[x1][y1];
- board[x1][y1]='B';
- if(valid(x1+1,y1+65,x2+1,y2+65)==0){condition=0;}
- board[x1][y1]=tempq;}
- else {
- tempq=board[x1][y1];
- board[x1][y1]='R';
- if(valid(x1+1,y1+65,x2+1,y2+65)==0){condition=0;}
- board[x1][y1]=tempq;}}
- if(x1>=8||x1<0||y1>=8||y1<0||x2>=8||x2<0||y2>=8||y2<0){condition=0;}
- if(board[x1][y1]=='P'&&condition==0){
- if((x2-x1)==2&&x1==1&&y1==y2&&(board[x2][y2]==' '||board[x2][y2]==c)){condition=1;}}
- if(board[x1][y1]=='p'&&condition==0){
- if((x1-x2)==2&&x1==6&&y1==y2&&(board[x2][y2]==' '||board[x2][y2]==c)){condition=1;}}
- /* if(board[x1][y1]=='P'&&condition==1&&y1==y2)
- {
- if(((x2-x1)==1)&&(board[x2][y2]!=' '&&board[x2][y2]!=c)){condition=0;}
- }
- if(board[x1][y1]=='p'&&condition==1&&y1==y2)
- {
- if(((x1-x2)==1)&&(board[x2][y2]!=' '&&board[x2][y2]!=c)){condition=0;}
- }*/
- if((board[x1][y1]=='k')&& (check1(x2+1,y2+65,xK,yK)==2)){condition=0;}
- if((board[x1][y1]=='K')&&(check1(xk,yk,x2+1,y2+65)==1)){condition=0;}
- return condition;
- }
- int check1(int xk,char yk,int xK,char yK)
- { int i,j,k,check=0,t1,t2;
- char temp;
- char w[6]={'q','r','n','b','p','k'};
- char b[6]={'Q','R','N','B','P','K'};
- for(t1=0;t1<5;t1++)
- {
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- if(w[t1]==board[i][j])
- {
- if(valid(i+1,j+65,xK,yK)==1)
- {
- check=1;
- break;
- }
- }
- if(b[t1]==board[i][j])
- {
- if(valid(i+1,j+65,xk,yk)==1)
- {
- check=2;
- break;
- }
- }
- }
- }
- }
- return check;
- }
- int checkTurns(int x1,char y1)
- {
- int turn=1;
- /*Validity of turns*/
- y1=y1-65;
- x1=x1-1;
- if((board[x1][y1]>=65 && board[x1][y1]<=90) && (g%2)==0){turn=0;}
- if((board[x1][y1]>=97 && board[x1][y1]<=122)&&(g%2)==1){turn=0;}
- return turn;
- }
- int checkblack (int xk,char yk,int xK,char yK)
- {
- int i,j,t2,mate=1,t1;
- char tempz;
- if(check1(xk,yk,xK,yK)==1)
- {
- for(i=0;i<8;i++) //black//
- {
- for(j=0;j<8;j++)
- {
- if(i+1==xK&&j+65==yK){continue;}
- if(board[i][j]>=65 && board[i][j]<=90)
- {
- for(t1=0;t1<8;t1++)
- {
- for(t2=0;t2<8;t2++)
- {
- if(valid(i+1,j+65,t1+1,t2+65)==1)
- {
- tempz=board[t1][t2];
- board[t1][t2]=board[i][j];
- if(check1(xk,yk,xK,yK)==0)
- {
- mate=0; printf("%d %d \n",i,j);
- printf("%d %d\n",t1,t2);
- }
- board[i][j]=board[t1][t2];
- board[t1][t2]=tempz;
- }
- }
- }
- }
- }
- }
- for(t1=-1;t1<2;t1++)
- {
- for(t2=-1;t2<2;t2++)
- {
- if(valid(xK,yK,xK+t1,yK+t2)==1)
- {
- tempz=board[xK+t1-1][yK+t2-65];
- board[xK+t1-1][yK+t2-65]=board[xK-1][yK-65];
- board[xK-1][yK-65]=219;
- if(check1(xk,yk,xK+t1,yK+t2)==0)
- {
- mate=0; printf("%d %d\n",t1,t2);
- }
- board[xK-1][yK-65]=board[xK+t1-1][yK+t2-65];
- board[xK+t1-1][yK+t2-65]=tempz;
- }
- }
- }
- }
- return mate;
- }
- int checkwhite(int xk,char yk,int xK,char yK)
- {
- int i,j,t2,mate=1,t1;
- char tempz;
- if (check1(xk,yk,xK,yK)==2)
- {
- for(i=0;i<8;i++) //white
- {
- for(j=0;j<8;j++)
- {
- if(i+1==xk&&j+65==yk){continue;}
- if(board[i][j]>=97 && board[i][j]<=122)
- {
- for(t1=0;t1<8;t1++)
- {
- for(t2=0;t2<8;t2++)
- {
- if(valid(i+1,j+65,t1+1,t2+65)==1)
- {
- tempz=board[t1][t2];
- board[t1][t2]=board[i][j];
- if(check1(xk,yk,xK,yK)==0)
- {
- mate=0; printf("%d %d \n",i,j);
- printf("%d %d\n",t1,t2);
- }
- board[i][j]=board[t1][t2];
- board[t1][t2]=tempz;
- }
- }
- }
- }
- }
- }
- for(t1=-1;t1<2;t1++)
- {
- for(t2=-1;t2<2;t2++)
- {
- if(valid(xk,yk,xk+t1,yk+t2)==1)
- {
- tempz=board[xk+t1-1][yk+t2-65];
- board[xk+t1-1][yk+t2-65]=board[xk-1][yk-65];
- board[xk-1][yk-65]=219;
- if(check1(xk+t1,yk+t2,xK,yK)==0)
- {
- mate=0;
- printf("%d %d\n",t1,t2);
- printf("%c",board[xk+t1-1][yk+t2-65]);
- }
- board[xk-1][yk-65]=board[xk+t1-1][yk+t2-65];
- board[xk+t1-1][yk+t2-65]=tempz;
- }
- }
- }
- }
- return mate;
- }
- int removecheckwhite (int x1,char y1,int x2,char y2)
- {
- int condition=1; char tempo;
- y1=y1-65;
- y2=y2-65;
- x1=x1-1;
- x2=x2-1;
- if(board[x1][y1]>=97 && board[x1][y1]<=122)
- {
- tempo=board[x2][y2];
- board[x2][y2]=board[x1][y1];
- if(check1(xk,yk,xK,yK)==2)
- {
- condition=0;
- }
- board[x1][y1]=board[x2][y2];
- board[x2][y2]=tempo;
- }
- return condition;
- }
- int removecheckblack(int x1,char y1,int x2,char y2)
- {
- int condition=1; char tempo;
- y1=y1-65;
- y2=y2-65;
- x1=x1-1;
- x2=x2-1;
- if(board[x1][y1]>=65 && board[x1][y1]<=90)
- {
- tempo=board[x2][y2];
- board[x2][y2]=board[x1][y1];
- if(check1(xk,yk,xK,yK)==1)
- {
- condition=0;
- }
- board[x1][y1]=board[x2][y2];
- board[x2][y2]=tempo;
- }
- return condition;
- }
- int stalemateWhite(int a)
- {
- int i,j,stale=1,t1,t2;
- char w[6]={'q','r','n','b','p','k'};
- char b[6]={'Q','R','N','B','P','K'};
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- if(board[i][j]>=97 && board[i][j]<=122) //WHITE//
- {
- for(t1=0;t1<8;t1++)
- {
- for(t2=0;t2<8;t2++)
- {
- if(valid(i+1,j+65,t1+1,t2+65)==1 && removecheckwhite(i+1,j+65,t1+1,t2+65)==1)
- {
- stale=0;
- printf("%d %c \n",i+1,j+65);
- }
- }
- }
- }
- }
- }
- return stale;
- }
- int stalemateBlack(int a)
- {
- int i,j,stale=1,t1,t2;
- for(i=0;i<8;i++)
- {
- for(j=0;j<8;j++)
- {
- if(board[i][j]>=65 && board[i][j]<=90)
- {
- for(t1=0;t1<8;t1++)
- {
- for(t2=0;t2<8;t2++)
- {
- if(valid(i+1,j+65,t1+1,t2+65)==1 && removecheckblack(i+1,j+65,t1+1,t2+65)==1)
- {
- stale=0;
- printf("%d %c \n",i+1,j+65);
- }
- }
- }
- }
- }
- }
- return stale;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement