Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include<stdbool.h>
- #include<ctype.h>
- int max(int a,int b)
- {
- if(a>b)
- {
- return a;
- }
- else
- {
- return b;
- }
- }
- int min (int a,int b)
- {
- if(a<b)
- {
- return a;
- }
- else
- {
- return b;
- }
- }
- char board[8][8];
- char c[2];
- int player=0;
- char c2[2];
- char white ='-';
- char black ='.';
- char pawnw='p',pawnb='P',rookw='r',knightw='n',bishopw='b',queenw='q',kingw='k',rookb='R',knightb='N',bishopb='B',queenb='Q',kingb='K';
- void firstDisplay(){
- for(int i=0;i<8;i++){
- for(int j=0;j<8;j++){
- if(i==1){
- board[i][j]=pawnw;
- }
- else if (i==6){
- board[i][j]=pawnb;
- }
- else if (i!=7||i!=0||i!=1||i!=6){
- if((i==2||i==4)&&(j==0||j==2||j==4||j==6)){
- board[i][j]=white;
- }
- else if ((i==3||i==5)&&(j==1||j==3||j==5||j==7)){
- board[i][j]=white;
- }
- else {
- board[i][j]=black;
- }
- }
- }
- }
- board[0][0]=rookw;
- board[7][0]=rookb;
- board[0][1]=knightw;
- board[7][1]=knightb;
- board[0][2]=bishopw;
- board[7][2]=bishopb;
- board[0][3]=queenw;
- board[4][3]=queenb;
- board[0][4]=kingw;
- board[7][4]=kingb;
- board[0][5]=bishopw;
- board[7][5]=bishopb;
- board[0][6]=knightw;
- board[7][6]=knightb;
- board[0][7]=rookw;
- board[7][7]=rookb;
- board[6][0]=pawnb;
- board[1][0]=pawnw;
- //board[2][2]='A';
- //board[5][0]='a';
- }
- void printDisplay(){
- system("cls");
- for(int i=7;i>=0;i--){
- for (int j =0;j<8;j++){
- printf("%c",board[i][j]);
- }
- printf("\n");
- }
- }
- int fromc,fromr,tor,toc;
- void read_move(char c[],char c2[])
- {
- gets(c);
- fromc=c[0]-97;
- fromr=c[1]-'0'-1;
- printf("%d %d\n",fromr,fromc);
- //printf("%c",board[fromr][fromc]);
- printf("To ");
- gets(c2);
- toc=c2[0]-97;
- tor=c2[1]-'0'-1;
- printf("%d %d\n",tor,toc);
- // make_move(fromr,fromc,tor,toc);
- //printf("%c\n",board[tor][toc]);
- }
- int validinput(int fromr,int fromc,int tor,int toc)
- {
- if(fromr>7||fromc>7||fromr<0||fromc<0)
- {
- return 0;
- }
- else if(tor>7||toc>7||tor<0||toc<0)
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- int validpawn(int fromr,int fromc,int tor,int toc)
- {
- if(board[fromr][fromc]=='p')
- {
- if((fromr==1)&&(fromc==0||fromc==1||fromc==2||fromc==3||fromc==4||fromc==5||fromc==6||fromc==7))
- {
- if(tor-fromr!=1&&tor-fromr!=2)
- {
- printf("df");
- return 0;
- }
- }
- else if(tor-fromr!=1)
- {
- printf("df");
- return 0;
- }
- if(toc-fromc!=0&&tor-fromr==1)//coloumns
- {
- if(toc-fromc==1||toc-fromc==-1)
- {
- printf("d");
- if(board[tor][toc]!='.'&&board[tor][toc]!='-')
- {
- if(isupper(board[tor][toc]))
- {
- printf("ad");
- return 1;
- }
- }
- else
- {
- printf("sdff");
- return 0;
- }
- }
- else
- {
- return 0;
- }
- }
- if(board[tor][toc]!='-'&&board[tor][toc]!='.')
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- else if(board[fromr][fromc]=='P')
- {
- if((fromr==6)&&(fromc==0||fromc==1||fromc==2||fromc==3||fromc==4||fromc==5||fromc==6||fromc==7))
- {
- if(tor-fromr!=-1&&tor-fromr!=-2)
- {
- return 0;
- }
- }
- else if(tor-fromr!=-1)
- {
- return 0;
- }
- if(toc-fromc!=0&&tor-fromr==-1)
- {
- if(toc-fromc==1||toc-fromc==-1)
- {
- if(board[tor][toc]!='.'&&board[tor][toc]!='-')//it just need to add all of the same chess colours as it can't move if one of them is there
- {
- if(islower(board[tor][toc]))
- return 1;
- }
- else
- {
- return 0;
- }
- }
- else
- {
- return 0;
- }
- }
- if(board[tor][toc]!='-'&&board[tor][toc]!='.')
- {
- return 0;
- }
- else
- {
- return 1;
- }
- }
- }
- int validrook(int fromr,int fromc,int tor,int toc){
- if(board[fromr][fromc]=='r'||board[fromr][fromc]=='R')
- {
- if(board[fromr][fromc]=='r'&&islower(board[tor][toc]))
- {
- return 0;
- }
- else if(board[fromr][fromc]=='R'&&isupper(board[tor][toc]))
- {
- return 0;
- }
- if(tor-fromr==0&&toc-fromc!=0)
- {
- for(int i=min(toc,fromc)+1;i<max(toc,fromc);i++)
- {
- if(board[fromr][i]!='-'&&board[fromr][i]!='.')
- {
- printf("empty");
- return 0;
- }
- }
- return 1;
- }
- else if(tor-fromr!=0&&toc-fromc==0)
- {
- printf("a7a");
- for(int i=min(tor,fromr)+1;i<max(tor,fromr);i++)
- {
- if(board[i][fromc]!='-'&&board[i][fromc]!='.')
- {
- printf("empty");
- return 0;
- }
- }
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }
- int validknight(int fromr,int fromc,int tor,int toc){
- if(board[fromr][fromc]=='n'||board[fromr][fromc]=='N'){
- if(abs(toc-fromc)==2&&abs(tor-fromr)==1||abs(toc-fromc)==1&&abs(tor-fromr)==2){
- if (board[tor][toc]=='-'||board[tor][toc]=='.'){
- return 1 ;
- }
- else if (((board[fromr][fromc]=='n')&&isupper((int)board[tor][toc]))==1||((board[fromr][fromc]=='N')&&islower(board[tor][toc]))){
- return 1;
- }
- }
- }
- return 0 ;
- }
- int validking(int fromr,int fromc,int tor,int toc){
- if(board[fromr][fromc]=='k'||board[fromr][fromc]=='K'){
- if(abs(toc-fromc)==1&&tor-fromr==0||abs(tor-fromr)==1&&toc-fromc==0||abs(tor-fromr)==1&&abs(toc-fromc)==1){
- if (board[tor][toc]=='-'||board[tor][toc]=='.'){
- return 1 ;
- }
- else if (((board[fromr][fromc]=='k')&&isupper((int)board[tor][toc]))==1||((board[fromr][fromc]=='K')&&islower(board[tor][toc]))){
- return 1;
- }
- }
- }
- return 0 ;
- }
- int validbishop(int fromr,int fromc,int tor,int toc)
- {
- if(board[fromr][fromc]=='b'||board[fromr][fromc]=='B')
- {
- if(abs(tor-fromr)==abs(toc-fromc))
- {
- if(board[fromr][fromc]=='b'&&islower(board[tor][toc]))
- {
- //printf("a");
- return 0;
- }
- else if(board[fromr][fromc]=='B'&&isupper(board[tor][toc]))
- {
- return 0;
- }
- int j=min(toc,fromc)+1;
- for(int i=min(fromr,tor)+1;i<max(fromr,tor);i++)
- {
- if(board[i][j]!='-'&&board[i][j]!='.')
- {
- return 0;
- }
- j++;
- }
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }
- int validqueen (int fromr,int fromc,int tor,int toc){
- if(board[fromr][fromc]=='q'||board[fromr][fromc]=='Q'){
- if(abs(tor-fromr)==abs(toc-fromc))
- {
- if(board[fromr][fromc]=='q'&&islower(board[tor][toc]))
- {
- //printf("a");
- return 0;
- }
- else if(board[fromr][fromc]=='Q'&&isupper(board[tor][toc]))
- {
- return 0;
- }
- int j=min(toc,fromc)+1;
- for(int i=min(fromr,tor)+1;i<max(fromr,tor);i++)
- {
- if(board[i][j]!='-'&&board[i][j]!='.')
- {
- return 0;
- }
- j++;
- }
- return 1;
- }
- else if(board[fromr][fromc]=='q'&&islower(board[tor][toc]))
- {
- return 0;
- }
- else if(board[fromr][fromc]=='Q'&&isupper(board[tor][toc]))
- {
- return 0;
- }
- if(tor-fromr==0&&toc-fromc!=0)
- {
- for(int i=min(toc,fromc)+1;i<max(toc,fromc);i++)
- {
- if(board[fromr][i]!='-'&&board[fromr][i]!='.')
- {
- printf("empty");
- return 0;
- }
- }
- return 1;
- }
- else if(tor-fromr!=0&&toc-fromc==0)
- {
- printf("a7a");
- for(int i=min(tor,fromr)+1;i<max(tor,fromr);i++)
- {
- if(board[i][fromc]!='-'&&board[i][fromc]!='.')
- {
- printf("empty");
- return 0;
- }
- }
- return 1;
- }
- else
- {
- return 0;
- }
- }
- }
- void make_move (int fromr,int fromc,int tor,int toc){
- board[tor][toc]=board[fromr][fromc];
- board[fromr][fromc]=white;
- printDisplay();
- /*
- if(validinput(fromr,fromc,tor,toc)==1&&validpawn(fromr,fromc,tor,toc)==1){
- if(board[fromr][fromc]==pawnw)
- {
- board[tor][toc]=pawnw;
- board[fromr][fromc]=white;
- }
- else if(board[fromr][fromc]==pawnb)
- {
- board[tor][toc]=pawnb;
- board[fromr][fromc]=white;
- }
- printDisplay() ;
- }
- else if(validinput(fromr,fromc,tor,toc)==1&&validrook(fromr,fromc,tor,toc)==1){
- if(board[fromr][fromc]=='r'){
- board[tor][toc]=rookw;
- board[fromr][fromc]=white;
- printDisplay();
- }
- else{board[tor][toc]=rookb;
- board[fromr][fromc]=black;
- printDisplay();}
- }
- else if(validinput(fromr,fromc,tor,toc)==1&&validknight(fromr,fromc,tor,toc)==1){
- if(board[fromr][fromc]=='n'){
- board[tor][toc]=knightw;
- board[fromr][fromc]=white;
- printDisplay();
- }
- else{board[tor][toc]=knightb;
- board[fromr][fromc]=black;
- printDisplay();}
- }
- else if(validinput(fromr,fromc,tor,toc)==1&&validking(fromr,fromc,tor,toc)==1){
- if(board[fromr][fromc]=='k'){
- board[tor][toc]=kingw;
- board[fromr][fromc]=white;
- printDisplay();
- }
- else{board[tor][toc]=kingb;
- board[fromr][fromc]=black;
- printDisplay();}
- }
- else if(validinput(fromr,fromc,tor,toc)==1&&validbishop(fromr,fromc,tor,toc)==1){
- if(board[fromr][fromc]=='b'){
- board[tor][toc]=bishopw;
- board[fromr][fromc]=white;
- printDisplay();
- }
- else{board[tor][toc]=bishopb;
- board[fromr][fromc]=black;
- printDisplay();}
- }
- else if(validinput(fromr,fromc,tor,toc)==1&&validqueen(fromr,fromc,tor,toc)==1){
- if(board[fromr][fromc]=='q'){
- board[tor][toc]=queenw;
- board[fromr][fromc]=white;
- printDisplay();
- }
- else{board[tor][toc]=queenb;
- board[fromr][fromc]=black;
- printDisplay();}
- }
- */
- }
- int validmove(int fromr,int fromc,int tor,int toc)
- {
- if(islower(board[fromr][fromc])&&player!=1)
- {
- return 0;
- }
- else if(isupper(board[fromr][fromc])&&player!=2)
- {
- return 0;
- }
- if(validinput(fromr,fromc,tor,toc)==1&&validpawn(fromr,fromc,tor,toc)==1)
- {
- return 1;
- }
- else if(validinput(fromr,fromc,tor,toc)==1&&validrook(fromr,fromc,tor,toc)==1)
- {
- return 1;
- }
- else if(validinput(fromr,fromc,tor,toc)==1&&validknight(fromr,fromc,tor,toc)==1)
- {
- return 1;
- }
- else if(validinput(fromr,fromc,tor,toc)==1&&validking(fromr,fromc,tor,toc)==1)
- {
- return 1;
- }
- else if(validinput(fromr,fromc,tor,toc)==1&&validbishop(fromr,fromc,tor,toc)==1)
- {
- return 1;
- }
- else if(validinput(fromr,fromc,tor,toc)==1&&validqueen(fromr,fromc,tor,toc)==1)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
- void playgame()
- {
- while(true)
- {
- if(player==1)
- {
- player=2;
- }
- else
- {
- player=1;
- }
- printf("Player %d's turn: ",player);
- read_move(c,c2);
- while(validmove(fromr,fromc,tor,toc)!=1)
- {
- printf("invalid");
- read_move(c,c2);
- }
- make_move(fromr,fromc,tor,toc);
- }
- }
- int main()
- {
- firstDisplay();
- printDisplay();
- playgame();
- // read_move(c,c2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement