#include #include #include #include"chess.h" bool touchMove(Board*,Piece [],int,int,bool); bool checkMove(int); bool emptySpot(Piece [],int , int); bool pieceMove(Piece [],string ,string ,int ,int ); int switchNum(int,char); void resetBoard(Board *,Piece []); using namespace std; int main() { ///// 2 6 10 14 18 22 26 30 string arrPcs[26]={"--1---2---3---4---5---6---7---8---", "- #### #### #### ####-", "1 #### #### #### ####1",//2 "- #### #### #### ####-", "-#### #### #### #### -", "2#### #### #### #### 2",//5 "-#### #### #### #### -", "- #### #### #### ####-", "3 #### #### #### ####3",//8 "- #### #### #### ####-", "-#### #### #### #### -", "4#### #### #### #### 4",//11 "-#### #### #### #### -", "- #### #### #### ####-", "5 #### #### #### ####5",//14 "- #### #### #### ####-", "-#### #### #### #### -", "6#### #### #### #### 6",//17 "-#### #### #### #### -", "- #### #### #### ####-", "7 #### #### #### ####7",//20 "- #### #### #### ####-", "-#### #### #### #### -", "8#### #### #### #### 8",//23 "-#### #### #### #### -", "--1---2---3---4---5---6---7---8---" }; Board gameBoard; gameBoard.setItsBoard(arrPcs ); Piece pcs[64]; resetBoard(&gameBoard,pcs); gameBoard.DisplayBoard();/////display the game board on screen cout<<"0 to end the game anytime"<>touch; if(touch!=0) { cout<<"CHoose the target location "<>move; if((checkMove(move))&&(checkMove(touch))&&(touchMove(&gameBoard,pcs,touch,move,white))) { gameBoard.DisplayBoard();/////display the game board on screen if(white) { white=false;/////switch player cout<<"Black Turn"<setPcsLoc(pieces[k].getItsName(),MHorz,MVert); pieces[i].setItsName(" "); aboard->setPcsLoc(pieces[i].getItsName(),THorz,TVert); ok_touch=true; }/////////////////////////////////////////////////// } } } else if ((!white)&&(nameT[0]=='B')) ////black can move black only { for(int k=0;k<=64;k++) ///////searching for the touched piece if((pieces[k].getItsHorzAddress()==MVert)&&(pieces[k].getItsVertAddress()==MHorz)) { nameM=pieces[k].getItsName(); if((!white)&&(nameM[0]!='B')) { /////////////////////////////////////////////////// if(pieceMove(pieces,nameT,nameM,addtouch,addmove))////////////fuction for the rules of each piece movement { pieces[k].setItsName(pieces[i].getItsName()); aboard->setPcsLoc(pieces[k].getItsName(),MHorz,MVert); pieces[i].setItsName(" "); aboard->setPcsLoc(pieces[i].getItsName(),THorz,TVert); ok_touch=true; } ////////////////////////////////////////////////////////// } } } else ok_touch=false; } return ok_touch; } int switchNum(int num,char xChar) { if(xChar=='h') switch(num) { case 1 : num =2; break; case 2 : num =6; break; case 3 : num =10; break; case 4 : num =14; break; case 5 : num =18; break; case 6 : num =22; break; case 7 : num =26; break; case 8 : num =30; break; default : break; } else switch(num) { case 1 : num =2; break; case 2 : num =5; break; case 3 : num =8; break; case 4 : num =11; break; case 5 : num =14; break; case 6 : num =17; break; case 7 : num =20; break; case 8 : num =23; break; default : break; } return num; } void resetBoard(Board *gameBoard,Piece pcs[]) { for(int i=0,j=2;i<8;i++) { pcs[i].setItsName("WP"); pcs[i].setItsVertAddress(j); pcs[i].setItsHorzAddress(5); gameBoard->setPcsLoc(pcs[i].getItsName(),j,5); j+=4; } for(int i=8,j=2;i<16;i++) { pcs[i].setItsName("BP"); pcs[i].setItsVertAddress(j); pcs[i].setItsHorzAddress(20); gameBoard->setPcsLoc(pcs[i].getItsName(),j,20); j+=4; } /////set rook locations for(int i=16,j=2;i<18;i++) { //////set location for white rook pcs[i].setItsName("WR"); pcs[i].setItsVertAddress(j); pcs[i].setItsHorzAddress(2); gameBoard->setPcsLoc(pcs[i].getItsName(),j,2); ////set location for black rook pcs[i+2].setItsName("BR"); pcs[i+2].setItsVertAddress(j); pcs[i+2].setItsHorzAddress(23); gameBoard->setPcsLoc(pcs[i+2].getItsName(),j,23); j=30; } /////set Knight locations for(int i=20,j=6;i<22;i++) { //////set location for white knight pcs[i].setItsName("WN"); pcs[i].setItsVertAddress(j); pcs[i].setItsHorzAddress(2); gameBoard->setPcsLoc(pcs[i].getItsName(),j,2); ////set location for black knight pcs[i+2].setItsName("BN"); pcs[i+2].setItsVertAddress(j); pcs[i+2].setItsHorzAddress(23); gameBoard->setPcsLoc(pcs[i+2].getItsName(),j,23); j=26; } /////set Bishops locations for(int i=24,j=10;i<26;i++) { //////set location for white bishop pcs[i].setItsName("WB"); pcs[i].setItsVertAddress(j); pcs[i].setItsHorzAddress(2); gameBoard->setPcsLoc(pcs[i].getItsName(),j,2); ////set location for black bishop pcs[i+2].setItsName("BB"); pcs[i+2].setItsVertAddress(j); pcs[i+2].setItsHorzAddress(23); gameBoard->setPcsLoc(pcs[i+2].getItsName(),j,23); j=22; } /////set Queens locations //////set location for white queen pcs[28].setItsName("WQ"); pcs[28].setItsVertAddress(14); pcs[28].setItsHorzAddress(2); gameBoard->setPcsLoc(pcs[28].getItsName(),14,2); ////set location for black queen pcs[29].setItsName("BQ"); pcs[29].setItsVertAddress(14); pcs[29].setItsHorzAddress(23); gameBoard->setPcsLoc(pcs[29].getItsName(),14,23); /////set kings locations //////set location for white king pcs[30].setItsName("WK"); pcs[30].setItsVertAddress(18); pcs[30].setItsHorzAddress(2); gameBoard->setPcsLoc(pcs[30].getItsName(),18,2); ////set location for black king pcs[31].setItsName("BK"); pcs[31].setItsVertAddress(18); pcs[31].setItsHorzAddress(23); gameBoard->setPcsLoc(pcs[31].getItsName(),18,23); /////////////set the other spots int k=8; for(int i=32,j=2;i<64;i++) { //////set location for white knight pcs[i].setItsName(" "); pcs[i].setItsVertAddress(j); pcs[i].setItsHorzAddress(k); gameBoard->setPcsLoc(pcs[i].getItsName(),j,k); if(j<30) j+=4; else { j=2; k+=3; } } } bool checkMove(int move) { int first = move/10; int second = move%10; if((first<1)||(first>8)||(second<1)||(second>8)) return false; return true; } bool pieceMove(Piece pieces[],string nameT,string nameM,int touch ,int move ) { int moving=touch; int THorz = touch%10; int TVert = touch/10; int MHorz = move%10; int MVert = move/10; int movingH=THorz; int movingV=TVert; int diff=abs(touch-move); int signedDiff=touch-move; if(nameT[1]=='P') ////if its pawn { if(nameT[0]=='W')//////white pawn { if(nameM[0]==' ')//////////////piece not attacking { if(move==touch+10)////one step forward ? return true;//yes? go return false;//no ? not accepted } else if(nameM[0]!=' ')////attacking abviously black piece if((move==touch+11)||(move==touch+9)) //moving diagonal down? return true; /////diagonal return false;/////not diagonal } if(nameT[0]=='B')//////black pawn { if(nameM[0]==' ')//////////////piece not attacking { if(move==touch-10)////one step up return true;/////up ok return false;/////not up not accepted } if(nameM[0]!='.')////attacking abviously white piece if((move==touch-11)||(move==touch-9)) ////diagonal up return true;/////ok move return false;////not accepted } return false;////any unexpected mistake will return a non accepted move }///////////////////////////////////////////////////////////////////end of pawn statment ////////////knight if(nameT[1]=='N') { int Hdiff=abs(THorz-MHorz); int Vdiff=abs(TVert-MVert); cout<<"Hdiff and Vdiff are "<MHorz)//theobjects is moving to the left movingH--; if(movingHMVert)//theobjects is moving Down movingV--; if(movingVTHorz)&&(MVert>TVert))//theobjects is moving to the left { movingH++; movingV++; } if((MHorzTHorz)&&(MVertTVert))////right direction { movingH--; movingV++; } ////after we move and before we accept the movement we need to check if ////there is no object is blocking the way of our moving object moving=movingV*10+movingH; if(moving!=move) if(!emptySpot(pieces,switchNum(movingH,'h'),switchNum(movingV,'v'))) return false; ////if an object is found the movement cant be done } ////if the while loop ends it means the path is clear and its ok to move return true; } else return false; } if(nameT[1]=='Q') { if((movingH==MHorz)||(movingV==MVert)) ////if its vertical or horizontal moving rook or queen { ////while loop to the check that there are no objects to block the moving objects's way while(moving!=move) ////the while loop ends if we reach the target { if(movingH>MHorz)//theobjects is moving to the left movingH--; if(movingHMVert)//theobjects is moving Down movingV--; if(movingVTHorz)&&(MVert>TVert))//theobjects is moving to the left { movingH++; movingV++; } if((MHorzTHorz)&&(MVertTVert))////right direction { movingH--; movingV++; } ////after we move and before we accept the movement we need to check if ////there is no object is blocking the way of our moving object moving=movingV*10+movingH; if(moving!=move) if(!emptySpot(pieces,switchNum(movingH,'h'),switchNum(movingV,'v'))) return false; ////if an object is found the movement cant be done } ////if the while loop ends it means the path is clear and its ok to move return true; } } return false; } bool emptySpot(Piece pieces[],int h, int v) { for(int i=0;i<=64;i++) if((pieces[i].getItsHorzAddress()==v)&&(pieces[i].getItsVertAddress()==h)&&(pieces[i].getItsName()==" ")) return true; return false; }