Untitled


SUBMITTED BY: Guest

DATE: July 2, 2014, 12:56 a.m.

FORMAT: Text only

SIZE: 18.5 kB

HITS: 999

  1. #include<iostream>
  2. #include<string>
  3. #include<cstdlib>
  4. #include"chess.h"
  5. bool touchMove(Board*,Piece [],int,int,bool);
  6. bool checkMove(int);
  7. bool emptySpot(Piece [],int , int);
  8. bool pieceMove(Piece [],string ,string ,int ,int );
  9. int switchNum(int,char);
  10. void resetBoard(Board *,Piece []);
  11. using namespace std;
  12. int main()
  13. { ///// 2 6 10 14 18 22 26 30
  14. string arrPcs[26]={"--1---2---3---4---5---6---7---8---",
  15. "- #### #### #### ####-",
  16. "1 #### #### #### ####1",//2
  17. "- #### #### #### ####-",
  18. "-#### #### #### #### -",
  19. "2#### #### #### #### 2",//5
  20. "-#### #### #### #### -",
  21. "- #### #### #### ####-",
  22. "3 #### #### #### ####3",//8
  23. "- #### #### #### ####-",
  24. "-#### #### #### #### -",
  25. "4#### #### #### #### 4",//11
  26. "-#### #### #### #### -",
  27. "- #### #### #### ####-",
  28. "5 #### #### #### ####5",//14
  29. "- #### #### #### ####-",
  30. "-#### #### #### #### -",
  31. "6#### #### #### #### 6",//17
  32. "-#### #### #### #### -",
  33. "- #### #### #### ####-",
  34. "7 #### #### #### ####7",//20
  35. "- #### #### #### ####-",
  36. "-#### #### #### #### -",
  37. "8#### #### #### #### 8",//23
  38. "-#### #### #### #### -",
  39. "--1---2---3---4---5---6---7---8---"
  40. };
  41. Board gameBoard;
  42. gameBoard.setItsBoard(arrPcs );
  43. Piece pcs[64];
  44. resetBoard(&gameBoard,pcs);
  45. gameBoard.DisplayBoard();/////display the game board on screen
  46. cout<<"0 to end the game anytime"<<endl;
  47. cout<<"entry:2 digits each from 1-8.first: vertical count"<<endl;
  48. bool exit = false;
  49. bool white= true; //////the turn is for white
  50. cout<<"White Turn"<<endl;
  51. while(exit==false)
  52. {
  53. int touch,move;
  54. cout<<"Choose the piece you want to move!"<<endl;
  55. cin>>touch;
  56. if(touch!=0)
  57. {
  58. cout<<"CHoose the target location "<<endl;
  59. cin>>move;
  60. if((checkMove(move))&&(checkMove(touch))&&(touchMove(&gameBoard,pcs,touch,move,white)))
  61. {
  62. gameBoard.DisplayBoard();/////display the game board on screen
  63. if(white)
  64. {
  65. white=false;/////switch player
  66. cout<<"Black Turn"<<endl;
  67. }
  68. else
  69. {
  70. white=true;
  71. cout<<"White Turn"<<endl;
  72. }
  73. }
  74. else cout<<"not accepted"<<endl;
  75. }
  76. else
  77. exit=true;
  78. }
  79. return 0;
  80. }
  81. bool touchMove(Board *aboard,Piece pieces[],int addtouch,int addmove,bool white) ////
  82. {
  83. string nameT,nameM;
  84. bool ok_touch = false;
  85. int THorz = addtouch%10;
  86. int TVert = addtouch/10;
  87. int MHorz = addmove%10;
  88. int MVert = addmove/10;
  89. THorz=switchNum(THorz,'h');
  90. MHorz=switchNum(MHorz,'h');
  91. TVert=switchNum(TVert,'v');
  92. MVert=switchNum(MVert,'v');
  93. for(int i=0;i<=64;i++)//searching for the touched piece
  94. if((pieces[i].getItsHorzAddress()==TVert)&&(pieces[i].getItsVertAddress()==THorz)&&(pieces[i].getItsName()!=" "))
  95. {
  96. nameT=pieces[i].getItsName();
  97. if((white)&&(nameT[0]=='W')) ////white can move white only
  98. {
  99. for(int k=0;k<=64;k++)////searching for the target
  100. if((pieces[k].getItsHorzAddress()==MVert)&&(pieces[k].getItsVertAddress()==MHorz))
  101. {
  102. nameM=pieces[k].getItsName();
  103. if((white)&&(nameM[0]!='W'))
  104. {
  105. if(pieceMove(pieces,nameT,nameM,addtouch,addmove))////////////fuction for the rules of each piece movement
  106. {
  107. pieces[k].setItsName(pieces[i].getItsName());
  108. aboard->setPcsLoc(pieces[k].getItsName(),MHorz,MVert);
  109. pieces[i].setItsName(" ");
  110. aboard->setPcsLoc(pieces[i].getItsName(),THorz,TVert);
  111. ok_touch=true;
  112. }///////////////////////////////////////////////////
  113. }
  114. }
  115. }
  116. else if
  117. ((!white)&&(nameT[0]=='B')) ////black can move black only
  118. {
  119. for(int k=0;k<=64;k++) ///////searching for the touched piece
  120. if((pieces[k].getItsHorzAddress()==MVert)&&(pieces[k].getItsVertAddress()==MHorz))
  121. {
  122. nameM=pieces[k].getItsName();
  123. if((!white)&&(nameM[0]!='B'))
  124. {
  125. ///////////////////////////////////////////////////
  126. if(pieceMove(pieces,nameT,nameM,addtouch,addmove))////////////fuction for the rules of each piece movement
  127. {
  128. pieces[k].setItsName(pieces[i].getItsName());
  129. aboard->setPcsLoc(pieces[k].getItsName(),MHorz,MVert);
  130. pieces[i].setItsName(" ");
  131. aboard->setPcsLoc(pieces[i].getItsName(),THorz,TVert);
  132. ok_touch=true;
  133. }
  134. //////////////////////////////////////////////////////////
  135. }
  136. }
  137. }
  138. else ok_touch=false;
  139. }
  140. return ok_touch;
  141. }
  142. int switchNum(int num,char xChar)
  143. {
  144. if(xChar=='h')
  145. switch(num)
  146. {
  147. case 1 : num =2;
  148. break;
  149. case 2 : num =6;
  150. break;
  151. case 3 : num =10;
  152. break;
  153. case 4 : num =14;
  154. break;
  155. case 5 : num =18;
  156. break;
  157. case 6 : num =22;
  158. break;
  159. case 7 : num =26;
  160. break;
  161. case 8 : num =30;
  162. break;
  163. default :
  164. break;
  165. }
  166. else
  167. switch(num)
  168. {
  169. case 1 : num =2;
  170. break;
  171. case 2 : num =5;
  172. break;
  173. case 3 : num =8;
  174. break;
  175. case 4 : num =11;
  176. break;
  177. case 5 : num =14;
  178. break;
  179. case 6 : num =17;
  180. break;
  181. case 7 : num =20;
  182. break;
  183. case 8 : num =23;
  184. break;
  185. default :
  186. break;
  187. }
  188. return num;
  189. }
  190. void resetBoard(Board *gameBoard,Piece pcs[])
  191. {
  192. for(int i=0,j=2;i<8;i++)
  193. {
  194. pcs[i].setItsName("WP");
  195. pcs[i].setItsVertAddress(j);
  196. pcs[i].setItsHorzAddress(5);
  197. gameBoard->setPcsLoc(pcs[i].getItsName(),j,5);
  198. j+=4;
  199. }
  200. for(int i=8,j=2;i<16;i++)
  201. {
  202. pcs[i].setItsName("BP");
  203. pcs[i].setItsVertAddress(j);
  204. pcs[i].setItsHorzAddress(20);
  205. gameBoard->setPcsLoc(pcs[i].getItsName(),j,20);
  206. j+=4;
  207. }
  208. /////set rook locations
  209. for(int i=16,j=2;i<18;i++)
  210. {
  211. //////set location for white rook
  212. pcs[i].setItsName("WR");
  213. pcs[i].setItsVertAddress(j);
  214. pcs[i].setItsHorzAddress(2);
  215. gameBoard->setPcsLoc(pcs[i].getItsName(),j,2);
  216. ////set location for black rook
  217. pcs[i+2].setItsName("BR");
  218. pcs[i+2].setItsVertAddress(j);
  219. pcs[i+2].setItsHorzAddress(23);
  220. gameBoard->setPcsLoc(pcs[i+2].getItsName(),j,23);
  221. j=30;
  222. }
  223. /////set Knight locations
  224. for(int i=20,j=6;i<22;i++)
  225. {
  226. //////set location for white knight
  227. pcs[i].setItsName("WN");
  228. pcs[i].setItsVertAddress(j);
  229. pcs[i].setItsHorzAddress(2);
  230. gameBoard->setPcsLoc(pcs[i].getItsName(),j,2);
  231. ////set location for black knight
  232. pcs[i+2].setItsName("BN");
  233. pcs[i+2].setItsVertAddress(j);
  234. pcs[i+2].setItsHorzAddress(23);
  235. gameBoard->setPcsLoc(pcs[i+2].getItsName(),j,23);
  236. j=26;
  237. }
  238. /////set Bishops locations
  239. for(int i=24,j=10;i<26;i++)
  240. {
  241. //////set location for white bishop
  242. pcs[i].setItsName("WB");
  243. pcs[i].setItsVertAddress(j);
  244. pcs[i].setItsHorzAddress(2);
  245. gameBoard->setPcsLoc(pcs[i].getItsName(),j,2);
  246. ////set location for black bishop
  247. pcs[i+2].setItsName("BB");
  248. pcs[i+2].setItsVertAddress(j);
  249. pcs[i+2].setItsHorzAddress(23);
  250. gameBoard->setPcsLoc(pcs[i+2].getItsName(),j,23);
  251. j=22;
  252. }
  253. /////set Queens locations
  254. //////set location for white queen
  255. pcs[28].setItsName("WQ");
  256. pcs[28].setItsVertAddress(14);
  257. pcs[28].setItsHorzAddress(2);
  258. gameBoard->setPcsLoc(pcs[28].getItsName(),14,2);
  259. ////set location for black queen
  260. pcs[29].setItsName("BQ");
  261. pcs[29].setItsVertAddress(14);
  262. pcs[29].setItsHorzAddress(23);
  263. gameBoard->setPcsLoc(pcs[29].getItsName(),14,23);
  264. /////set kings locations
  265. //////set location for white king
  266. pcs[30].setItsName("WK");
  267. pcs[30].setItsVertAddress(18);
  268. pcs[30].setItsHorzAddress(2);
  269. gameBoard->setPcsLoc(pcs[30].getItsName(),18,2);
  270. ////set location for black king
  271. pcs[31].setItsName("BK");
  272. pcs[31].setItsVertAddress(18);
  273. pcs[31].setItsHorzAddress(23);
  274. gameBoard->setPcsLoc(pcs[31].getItsName(),18,23);
  275. /////////////set the other spots
  276. int k=8;
  277. for(int i=32,j=2;i<64;i++)
  278. {
  279. //////set location for white knight
  280. pcs[i].setItsName(" ");
  281. pcs[i].setItsVertAddress(j);
  282. pcs[i].setItsHorzAddress(k);
  283. gameBoard->setPcsLoc(pcs[i].getItsName(),j,k);
  284. if(j<30)
  285. j+=4;
  286. else
  287. {
  288. j=2;
  289. k+=3;
  290. }
  291. }
  292. }
  293. bool checkMove(int move)
  294. {
  295. int first = move/10;
  296. int second = move%10;
  297. if((first<1)||(first>8)||(second<1)||(second>8))
  298. return false;
  299. return true;
  300. }
  301. bool pieceMove(Piece pieces[],string nameT,string nameM,int touch ,int move )
  302. {
  303. int moving=touch;
  304. int THorz = touch%10;
  305. int TVert = touch/10;
  306. int MHorz = move%10;
  307. int MVert = move/10;
  308. int movingH=THorz;
  309. int movingV=TVert;
  310. int diff=abs(touch-move);
  311. int signedDiff=touch-move;
  312. if(nameT[1]=='P') ////if its pawn
  313. {
  314. if(nameT[0]=='W')//////white pawn
  315. {
  316. if(nameM[0]==' ')//////////////piece not attacking
  317. {
  318. if(move==touch+10)////one step forward ?
  319. return true;//yes? go
  320. return false;//no ? not accepted
  321. }
  322. else if(nameM[0]!=' ')////attacking abviously black piece
  323. if((move==touch+11)||(move==touch+9)) //moving diagonal down?
  324. return true; /////diagonal
  325. return false;/////not diagonal
  326. }
  327. if(nameT[0]=='B')//////black pawn
  328. {
  329. if(nameM[0]==' ')//////////////piece not attacking
  330. {
  331. if(move==touch-10)////one step up
  332. return true;/////up ok
  333. return false;/////not up not accepted
  334. }
  335. if(nameM[0]!='.')////attacking abviously white piece
  336. if((move==touch-11)||(move==touch-9)) ////diagonal up
  337. return true;/////ok move
  338. return false;////not accepted
  339. }
  340. return false;////any unexpected mistake will return a non accepted move
  341. }///////////////////////////////////////////////////////////////////end of pawn statment
  342. ////////////knight
  343. if(nameT[1]=='N')
  344. {
  345. int Hdiff=abs(THorz-MHorz);
  346. int Vdiff=abs(TVert-MVert);
  347. cout<<"Hdiff and Vdiff are "<<Hdiff<<" "<<Vdiff<<endl;
  348. if(((Hdiff==1)&&(Vdiff==2))||((Hdiff==2)&&(Vdiff==1)))
  349. return true;
  350. return false;
  351. }
  352. ////////////////////////////////////////////////////////////////////end night statement
  353. if(nameT[1]=='K')
  354. {
  355. if((diff==1)||(diff==11)||(diff==9)||(diff==10))////one step any direction
  356. return true; /////good move
  357. return false;////not accepted
  358. //////king
  359. }
  360. else if(nameT[1]=='R')
  361. {
  362. if((movingH==MHorz)||(movingV==MVert)) ////if its vertical or horizontal moving rook or queen
  363. {
  364. ////while loop to the check that there are no objects to block the moving objects's way
  365. while(moving!=move) ////the while loop ends if we reach the target
  366. {
  367. if(movingH>MHorz)//theobjects is moving to the left
  368. movingH--;
  369. if(movingH<MHorz)////right direction
  370. movingH++;
  371. if(movingV>MVert)//theobjects is moving Down
  372. movingV--;
  373. if(movingV<MVert)////Up direction
  374. movingV++;
  375. ////after we move and before we accept the movement we need to check if
  376. ////there is no object is blocking the way of our moving object
  377. moving=movingV*10+movingH;
  378. if(moving!=move)
  379. if(!emptySpot(pieces,switchNum(movingH,'h'),switchNum(movingV,'v')))
  380. return false; ////if an object is found the movement cant be done
  381. }
  382. ////if the while loop ends it means the path is clear and its ok to move
  383. return true;
  384. }
  385. else return false;
  386. }
  387. /////////different selection
  388. if(nameT[1]=='B')
  389. {
  390. if((diff%9==0)||(diff%11==0)) ////if its diagonal moving bishop or queen
  391. {
  392. ////while loop to the check that there are no objects to block the moving objects's way
  393. while(moving!=move) ////the while loop ends if we reach the target
  394. {
  395. if((MHorz>THorz)&&(MVert>TVert))//theobjects is moving to the left
  396. {
  397. movingH++;
  398. movingV++;
  399. }
  400. if((MHorz<THorz)&&(MVert<TVert))////right direction
  401. {
  402. movingH--;
  403. movingV--;
  404. }
  405. if((MHorz>THorz)&&(MVert<TVert))////right direction
  406. {
  407. movingH++;
  408. movingV--;
  409. }
  410. if((MHorz<THorz)&&(MVert>TVert))////right direction
  411. {
  412. movingH--;
  413. movingV++;
  414. }
  415. ////after we move and before we accept the movement we need to check if
  416. ////there is no object is blocking the way of our moving object
  417. moving=movingV*10+movingH;
  418. if(moving!=move)
  419. if(!emptySpot(pieces,switchNum(movingH,'h'),switchNum(movingV,'v')))
  420. return false; ////if an object is found the movement cant be done
  421. }
  422. ////if the while loop ends it means the path is clear and its ok to move
  423. return true;
  424. }
  425. else return false;
  426. }
  427. if(nameT[1]=='Q')
  428. {
  429. if((movingH==MHorz)||(movingV==MVert)) ////if its vertical or horizontal moving rook or queen
  430. {
  431. ////while loop to the check that there are no objects to block the moving objects's way
  432. while(moving!=move) ////the while loop ends if we reach the target
  433. {
  434. if(movingH>MHorz)//theobjects is moving to the left
  435. movingH--;
  436. if(movingH<MHorz)////right direction
  437. movingH++;
  438. if(movingV>MVert)//theobjects is moving Down
  439. movingV--;
  440. if(movingV<MVert)////Up direction
  441. movingV++;
  442. ////after we move and before we accept the movement we need to check if
  443. ////there is no object is blocking the way of our moving object
  444. moving=movingV*10+movingH;
  445. if(moving!=move)
  446. if(!emptySpot(pieces,switchNum(movingH,'h'),switchNum(movingV,'v')))
  447. return false; ////if an object is found the movement cant be done
  448. }
  449. ////if the while loop ends it means the path is clear and its ok to move
  450. return true;
  451. }
  452. if((diff%9==0)||(diff%11==0)) ////if its diagonal moving bishop or queen
  453. {
  454. ////while loop to the check that there are no objects to block the moving objects's way
  455. while(moving!=move) ////the while loop ends if we reach the target
  456. {
  457. if((MHorz>THorz)&&(MVert>TVert))//theobjects is moving to the left
  458. {
  459. movingH++;
  460. movingV++;
  461. }
  462. if((MHorz<THorz)&&(MVert<TVert))////right direction
  463. {
  464. movingH--;
  465. movingV--;
  466. }
  467. if((MHorz>THorz)&&(MVert<TVert))////right direction
  468. {
  469. movingH++;
  470. movingV--;
  471. }
  472. if((MHorz<THorz)&&(MVert>TVert))////right direction
  473. {
  474. movingH--;
  475. movingV++;
  476. }
  477. ////after we move and before we accept the movement we need to check if
  478. ////there is no object is blocking the way of our moving object
  479. moving=movingV*10+movingH;
  480. if(moving!=move)
  481. if(!emptySpot(pieces,switchNum(movingH,'h'),switchNum(movingV,'v')))
  482. return false; ////if an object is found the movement cant be done
  483. }
  484. ////if the while loop ends it means the path is clear and its ok to move
  485. return true;
  486. }
  487. }
  488. return false;
  489. }
  490. bool emptySpot(Piece pieces[],int h, int v)
  491. {
  492. for(int i=0;i<=64;i++)
  493. if((pieces[i].getItsHorzAddress()==v)&&(pieces[i].getItsVertAddress()==h)&&(pieces[i].getItsName()==" "))
  494. return true;
  495. return false;
  496. }

comments powered by Disqus