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';
- 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 check (int xk,char yk,int xK,char yK);
- 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){
- 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(check(xk,yk,xK,yK)==1||check(xk,yk,xK,yK)==2){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*/
- 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=x1;
- yk=y1;}
- else if(board[x1-1][y1-65]=='K'){xK=x1;
- yK=y1;}
- //check(xk,yk,xK,yK);
- g++;
- 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;
- }
- }
- }
- return 0;
- }
- int valid(int x1,char y1,int x2,char y2)
- {
- int condition=1,i,t1=0,t2=0;
- 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'||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){condition=1;}}
- if(board[x1][y1]=='p'&&condition==0){
- if((x1-x2)==2&&x1==6){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')&&check(x2+1,y2+65,xK,yK)==2){condition=0;}
- if((board[x1][y1]=='K')&&check(xk,yk,x2+1,y2+65)==1){condition=0;}
- for(i=0;i<6;i++){
- if((board[x1][y1]==w[i])&&check(xk,yk,xK,yK)==2){condition=0;}}
- for(i=0;i<6;i++){
- if((board[x1][y1]==b[i])&&check(xk,yk,xK,yK)==1){condition=0;}}
- /*Validity of turns*/
- // if((board[x1][y1]>=65 && board[x1][y1]<=90) && (g%2)==0){condition=0;}
- // if((board[x1][y1]>=97 && board[x1][y1]<=122)&&(g%2)==1){condition=0;}
- return condition;
- }
- int check(int xk,char yk,int xK,char yK)
- { int i=0,j=0,k,check=0,checkmate=1,t1=0,t2=0;
- char temp;
- char w[6]={'q','r','n','b','k','p'};
- char b[6]={'Q','R','N','B','K','P'};
- for(k=0;k<5;k++){
- for(i=0;i<8;i++){
- for(j=0;j<8;j++){
- if(w[k]==board[i][j]){
- if(valid(i+1,j+65,xK,yK)==1){check=1;
- break;}}
- if(b[k]==board[i][j]){
- if(valid(i+1,j+65,xk,yk)==1){check=2;
- break;}}}}}
- return check;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement