Advertisement
Guest User

cefe

a guest
Dec 16th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 24.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include<windows.h>
  3. #include <stdlib.h>
  4. #include<stdbool.h>
  5. #include<ctype.h>
  6. int king_row,king_col;
  7. char white ='-';
  8. char black ='.';
  9. int flag =1 ;
  10. int lostb =0;
  11. int lostw =0 ;
  12. char lost_white[16];
  13. char lost_black[16];
  14. char list[8][8][100000]={0};
  15. int in=-1;
  16. int d=2;
  17. int kingswhiterow=0;
  18. int kingswhitecoloumn=4;
  19. int kingsblackrow=7;
  20. int kingsblackcoloumn=4;
  21. char white_black(int fromr,int fromc){
  22. if(fromr%2==0&&fromc%2==0){
  23. return black ;
  24. }
  25. else if (fromr%2!=0&&fromc%2!=0){
  26. return black ;
  27. }
  28. else if (fromc%2==0&&fromr%2!=0){
  29. return white ;
  30. }
  31. else {return white ;}
  32. }
  33. int max1(int a,int b)
  34. {
  35. if(a>b)
  36. {
  37. return a;
  38. }
  39. else
  40. {
  41. return b;
  42. }
  43. }
  44. int min1 (int a,int b)
  45. {
  46. if(a<b)
  47. {
  48. return a;
  49. }
  50. else
  51. {
  52. return b;
  53. }
  54. }
  55. char board[8][8];
  56. char c[5];
  57. int player=0;
  58. //char c2[2];
  59.  
  60. 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';
  61. void firstDisplay(){
  62. for(int i=0;i<8;i++){
  63. for(int j=0;j<8;j++){
  64. if(i==1){
  65. board[i][j]=pawnw;
  66. }
  67. else if (i==6){
  68. board[i][j]=pawnb;
  69. }
  70. else if (i!=7||i!=0||i!=1||i!=6){
  71. if((i==2||i==4)&&(j==0||j==2||j==4||j==6)){
  72. board[i][j]=black;
  73.  
  74. }
  75. else if ((i==3||i==5)&&(j==1||j==3||j==5||j==7)){
  76. board[i][j]=black;
  77. }
  78. else {
  79. board[i][j]=white;
  80. }
  81.  
  82. }
  83.  
  84. }
  85. }
  86.  
  87. board[0][0]=rookw;
  88. board[7][0]=rookb;
  89. board[0][1]=knightw;
  90. board[7][1]=knightb;
  91. board[0][2]=bishopw;
  92. board[7][2]=bishopb;
  93. board[0][3]=queenw;
  94. board[7][3]=queenb;
  95. board[0][4]=kingw;
  96. board[7][4]=kingb;
  97. board[0][5]=bishopw;
  98. board[7][5]=bishopb;
  99. board[0][6]=knightw;
  100. board[7][6]=knightb;
  101. board[0][7]=rookw;
  102. board[7][7]=rookb;
  103. board[6][0]=pawnb;
  104. board[1][0]=pawnw;
  105. //board[3][4]='k';
  106. //board[2][2]='A';
  107. //board[5][0]='a';
  108. //board[6][3]='-';
  109. //board[7][5]='k';
  110. //board[0][4]='-';
  111.  
  112. }
  113. void printDisplay(){
  114. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);
  115. system("cls");
  116. int v=8;
  117. in++;
  118. printf("\n");
  119. printf("lost in black");
  120. printf(" \t ");
  121. for(int i=0;i<8;i++)
  122. {
  123. printf(" %c",'A'+i);
  124. }
  125. printf("\n");
  126. for(int i =0;i<lostb;i++){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3);printf("%c",lost_black[i]);}
  127. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);
  128. printf("\n");
  129. for(int i=7;i>=0;i--){
  130. if(v!=9)
  131. {
  132. printf("\t\t %d ",v);
  133. v--;
  134. }
  135. else
  136. {
  137. printf(" ");
  138. }
  139. for (int j =0;j<8;j++){
  140. list[i][j][in]=board[i][j];
  141. if(check1()&&i==king_row&&j==king_col){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),4);printf(" %c",board[i][j]); SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);}
  142. else{
  143. if(board[i][j]==black){board[i][j]=219;SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3);
  144. printf(" %c",board[i][j]);
  145. board[i][j]=black;
  146. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);}
  147. else if(board[i][j]==white){board[i][j]=219;
  148. printf(" %c",board[i][j]);
  149. board[i][j]=white;}
  150. else{
  151. if(isupper(board[i][j])){SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),3);}
  152. printf(" %c",board[i][j]);
  153. SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),15);
  154. }}}
  155. if(i!=-1)
  156. {
  157. printf(" %d",i+1);
  158. }
  159. printf("\n\n");
  160. }
  161. printf("lost in white");
  162. printf("\t ");
  163. for(int i=0;i<8;i++)
  164. {
  165. printf(" %c",'A'+i);
  166. }
  167. printf("\n");
  168. for(int i =0;i<lostw;i++){printf("%c",lost_white[i]);}
  169. printf("\n");
  170. printf("\n");
  171. printf("For Undo press U and for Redo press R");
  172. printf("\n");
  173.  
  174. }
  175. int fromc,fromr,tor,toc;
  176. void read_move(char c[])
  177. {
  178. scanf("%d",&c[4]);
  179. gets(c);
  180. if(c[0]=='U'){}
  181. else if(c[0]=='R'){}
  182. else{
  183. fromc=c[0]-65;
  184. fromr=c[1]-'0'-1;
  185. //printf("%c",board[fromr][fromc]);
  186. //printf("To ");
  187. // gets(c2);
  188. toc=c[2]-65;
  189. tor=c[3]-'0'-1;
  190. }}
  191. int validinput(int fromr,int fromc,int tor,int toc)
  192. {if(c[0]=='U'){
  193. return 1;}
  194. else if(c[0]=='R'){return 1 ;}
  195. if(fromr>7||fromc>7||fromr<0||fromc<0)
  196. {
  197. return 0;
  198. }
  199. else if(tor>7||toc>7||tor<0||toc<0)
  200. {
  201. return 0;
  202. }
  203. else if(c=='\n')
  204. {
  205. return 0;
  206. }
  207. else
  208. {
  209. return 1;
  210. }
  211. }
  212. int validpawn(int fromr,int fromc,int tor,int toc)
  213. {
  214.  
  215. if(board[fromr][fromc]=='p')
  216. {
  217. if((fromr==1)&&(fromc==0||fromc==1||fromc==2||fromc==3||fromc==4||fromc==5||fromc==6||fromc==7))
  218. {
  219. if(tor-fromr!=1&&tor-fromr!=2)
  220. {
  221. //printf("df");
  222. return 0;
  223. }
  224. else if(tor-fromr==2)
  225. {
  226. if(board[tor-1][fromc]!=white&&board[tor-1][fromc]!=black)
  227. {
  228. return 0;
  229. }
  230. }
  231. }
  232. else if(tor-fromr!=1)
  233. {
  234. //printf("df");
  235. return 0;
  236. }
  237. if(toc-fromc!=0&&tor-fromr==1)//coloumns
  238. {
  239. if(toc-fromc==1||toc-fromc==-1)
  240. {
  241. //printf("d");
  242. if(board[tor][toc]!=white&&board[tor][toc]!=black)
  243. {
  244. if(isupper(board[tor][toc]))
  245. {
  246. // printf("ad");
  247. return 1;
  248. }
  249. }
  250. else
  251. {
  252. //printf("sdff");
  253. return 0;
  254. }
  255. }
  256. else
  257. {
  258. return 0;
  259.  
  260. }
  261. }
  262. if(board[tor][toc]!=white&&board[tor][toc]!=black)
  263. {
  264. return 0;
  265. }
  266.  
  267. else
  268. {
  269. return 1;
  270. }
  271. }
  272. else if(board[fromr][fromc]=='P')
  273. {
  274. if((fromr==6)&&(fromc==0||fromc==1||fromc==2||fromc==3||fromc==4||fromc==5||fromc==6||fromc==7))
  275. {
  276. if(tor-fromr!=-1&&tor-fromr!=-2)
  277. {
  278. return 0;
  279. }
  280. else if(tor-fromr==-2)
  281. {
  282. if(board[tor+1][fromc]!=white&&board[tor+1][fromc]!=black)
  283. {
  284. return 0;
  285. }
  286. }
  287. }
  288. else if(tor-fromr!=-1)
  289. {
  290. return 0;
  291. }
  292. if(toc-fromc!=0&&tor-fromr==-1)
  293. {
  294. if(toc-fromc==1||toc-fromc==-1)
  295. {
  296. if(board[tor][toc]!=black&&board[tor][toc]!=white)//it just need to add all of the same chess colours as it can't move if one of them is there
  297. {
  298. if(islower(board[tor][toc]))
  299. return 1;
  300. }
  301. else
  302. {
  303. return 0;
  304. }
  305. }
  306. else
  307. {
  308. return 0;
  309.  
  310. }
  311. }
  312. if(board[tor][toc]!=white&&board[tor][toc]!=black)
  313. {
  314. return 0;
  315. }
  316.  
  317. else
  318. {
  319. return 1;
  320. }
  321. }
  322. }
  323. int validrook(int fromr,int fromc,int tor,int toc){
  324. if(board[fromr][fromc]=='r'||board[fromr][fromc]=='R')
  325. {
  326. if(board[fromr][fromc]=='r'&&islower(board[tor][toc]))
  327. {
  328. return 0;
  329. }
  330. else if(board[fromr][fromc]=='R'&&isupper(board[tor][toc]))
  331. {
  332. return 0;
  333. }
  334. if(tor-fromr==0&&toc-fromc!=0)
  335. {
  336. for(int i=min1(toc,fromc)+1;i<max1(toc,fromc);i++)
  337. {
  338. if(board[fromr][i]!=white&&board[fromr][i]!=black)
  339. {
  340. // printf("empty");
  341. return 0;
  342. }
  343. }
  344. return 1;
  345. }
  346. else if(tor-fromr!=0&&toc-fromc==0)
  347. {
  348. for(int i=min1(tor,fromr)+1;i<max1(tor,fromr);i++)
  349. {
  350. if(board[i][fromc]!=white&&board[i][fromc]!=black)
  351. {
  352. // printf("empty");
  353. return 0;
  354. }
  355. }
  356. return 1;
  357. }
  358. else
  359. {
  360. return 0;
  361. }
  362. }
  363. }
  364. int validknight(int fromr,int fromc,int tor,int toc){
  365. if(board[fromr][fromc]=='n'||board[fromr][fromc]=='N'){
  366. if(abs(toc-fromc)==2&&abs(tor-fromr)==1||abs(toc-fromc)==1&&abs(tor-fromr)==2){
  367. if (board[tor][toc]==white||board[tor][toc]==black){
  368. return 1 ;
  369. }
  370. else if (((board[fromr][fromc]=='n')&&isupper((int)board[tor][toc]))==1||((board[fromr][fromc]=='N')&&islower(board[tor][toc]))){
  371. return 1;
  372. }
  373. }
  374. }
  375. return 0 ;
  376. }
  377. int validking(int fromr,int fromc,int tor,int toc){
  378. if(board[fromr][fromc]=='k'||board[fromr][fromc]=='K'){
  379. if(abs(toc-fromc)==1&&tor-fromr==0||abs(tor-fromr)==1&&toc-fromc==0||abs(tor-fromr)==1&&abs(toc-fromc)==1){
  380. if (board[tor][toc]==white||board[tor][toc]==black){
  381. return 1 ;
  382. }
  383. else if (((board[fromr][fromc]=='k')&&isupper((int)board[tor][toc]))==1||((board[fromr][fromc]=='K')&&islower(board[tor][toc]))){
  384. return 1;
  385. }
  386.  
  387. }
  388.  
  389. }
  390. return 0 ;
  391. }
  392. int validbishop(int fromr,int fromc,int tor,int toc)
  393. {
  394. if(board[fromr][fromc]=='b'||board[fromr][fromc]=='B')
  395. {
  396. if(abs(tor-fromr)==abs(toc-fromc))
  397. {
  398. if(board[fromr][fromc]=='b'&&islower(board[tor][toc]))
  399. {
  400. // printf("a");
  401. return 0;
  402. }
  403. else if(board[fromr][fromc]=='B'&&isupper(board[tor][toc]))
  404. {
  405. return 0;
  406. }
  407. int j=min1(fromc,toc)+1;
  408. int detect;
  409. if((fromr==min1(fromr,tor)&&fromc==min1(fromc,toc))||(tor==min1(fromr,tor)&&toc==min1(fromc,toc)))
  410. {
  411. detect=1;
  412. }
  413. else
  414. {
  415. j=max1(toc,fromc)-1;
  416. detect=-1;
  417. }
  418. for(int i=min1(fromr,tor)+1;i<max1(fromr,tor);i++)
  419. {
  420. if(board[i][j]!=white&&board[i][j]!=black)
  421. {
  422. return 0;
  423. }
  424. j+=detect;
  425. }
  426. return 1;
  427. }
  428. else
  429. {
  430. return 0;
  431. }
  432. }
  433. }
  434. int validqueen (int fromr,int fromc,int tor,int toc){
  435. if(board[fromr][fromc]=='q'||board[fromr][fromc]=='Q'){
  436. if(abs(tor-fromr)==abs(toc-fromc))
  437. {
  438. if(board[fromr][fromc]=='q'&&islower(board[tor][toc]))
  439. {
  440. //printf("a");
  441. return 0;
  442. }
  443. else if(board[fromr][fromc]=='Q'&&isupper(board[tor][toc]))
  444. {
  445. return 0;
  446. }
  447. int j=min(fromc,toc)+1;
  448. int detect;
  449. if((fromr==min1(fromr,tor)&&fromc==min1(fromc,toc))||(tor==min1(fromr,tor)&&toc==min1(fromc,toc)))
  450. {
  451. detect=1;
  452. }
  453. else
  454. {
  455. j=max1(toc,fromc)-1;
  456. detect=-1;
  457. }
  458. for(int i=min1(fromr,tor)+1;i<max1(fromr,tor);i++)
  459. {
  460. if(board[i][j]!=white&&board[i][j]!=black)
  461. {
  462. return 0;
  463. }
  464. j+=detect;
  465. }
  466. return 1;
  467. }
  468. else if(board[fromr][fromc]=='q'&&islower(board[tor][toc]))
  469. {
  470. return 0;
  471. }
  472. else if(board[fromr][fromc]=='Q'&&isupper(board[tor][toc]))
  473. {
  474. return 0;
  475. }
  476. if(tor-fromr==0&&toc-fromc!=0)
  477. {
  478. for(int i=min1(toc,fromc)+1;i<max1(toc,fromc);i++)
  479. {
  480. if(board[fromr][i]!=white&&board[fromr][i]!=black)
  481. {
  482. // printf("empty");
  483. return 0;
  484. }
  485. }
  486. return 1;
  487. }
  488. else if(tor-fromr!=0&&toc-fromc==0)
  489. {
  490. for(int i=min1(tor,fromr)+1;i<max1(tor,fromr);i++)
  491. {
  492. if(board[i][fromc]!=white&&board[i][fromc]!=black)
  493. {
  494. // printf("empty");
  495. return 0;
  496. }
  497. }
  498. return 1;
  499. }
  500. else
  501. {
  502. return 0;
  503. }
  504.  
  505. }
  506. }
  507. void make_move (int fromr,int fromc,int tor,int toc){
  508. if(c[0]=='U'){if(player==1){player=2;}
  509. else {player=1;}
  510. undo();}
  511. else if(c[0]=='R'){if(player==1){player=2;}
  512. else {player=1;}redomove();}
  513. else{
  514. if(board[tor][toc]!=white&&board[tor][toc]!=black){
  515. if(isupper((int)board[tor][toc])==1){
  516. lost_black[lostb]=board[tor][toc];
  517. lostb++;
  518. }
  519. else{
  520. lost_white[lostw]=board[tor][toc];
  521. lostw++;
  522. }
  523. board[tor][toc]=board[fromr][fromc];
  524. board[fromr][fromc]=white_black(fromr,fromc);
  525. permotion();
  526. printDisplay() ;
  527. }
  528. else {
  529. board[tor][toc]=board[fromr][fromc];
  530. board[fromr][fromc]=white_black(fromr,fromc);
  531. permotion();
  532. printDisplay();
  533. }}}
  534. int validmove(int fromr,int fromc,int tor,int toc)
  535. {
  536. if(islower(board[fromr][fromc])&&player!=1)
  537. {
  538. return 0;
  539. }
  540. else if(isupper(board[fromr][fromc])&&player!=2)
  541. {
  542. return 0;
  543. }
  544. if(validinput(fromr,fromc,tor,toc)==1&&validpawn(fromr,fromc,tor,toc)==1)
  545. {
  546. return checknow(fromr,fromc,tor,toc);
  547. }
  548. else if(validinput(fromr,fromc,tor,toc)==1&&validrook(fromr,fromc,tor,toc)==1)
  549. {
  550. return checknow(fromr,fromc,tor,toc);
  551. }
  552. else if(validinput(fromr,fromc,tor,toc)==1&&validknight(fromr,fromc,tor,toc)==1)
  553. {
  554. return checknow(fromr,fromc,tor,toc);
  555. }
  556. else if(validinput(fromr,fromc,tor,toc)==1&&validking(fromr,fromc,tor,toc)==1)
  557. {
  558. return checknow(fromr,fromc,tor,toc);
  559. }
  560. else if(validinput(fromr,fromc,tor,toc)==1&&validbishop(fromr,fromc,tor,toc)==1)
  561. {
  562. return checknow(fromr,fromc,tor,toc);
  563. }
  564. else if(validinput(fromr,fromc,tor,toc)==1&&validqueen(fromr,fromc,tor,toc)==1)
  565. {
  566. return checknow(fromr,fromc,tor,toc);
  567. }
  568.  
  569. else
  570. {
  571. return 0;
  572. }
  573. }
  574. int checknow(fromr,fromc,tor,toc)
  575. {
  576. char temp=board[fromr][fromc];
  577. char temp2=board[tor][toc];
  578. board[tor][toc]=board[fromr][fromc];
  579. board[fromr][fromc]=white;
  580. if(!check())
  581. {
  582. board[fromr][fromc]=temp;
  583. board[tor][toc]=temp2;
  584. return 1;
  585. }
  586. board[fromr][fromc]=temp;
  587. board[tor][toc]=temp2;
  588. return 0;
  589. }
  590. int check1()
  591. {player=player%2+1;
  592. if(player==1)
  593. {
  594. for(int i=0;i<8;i++)
  595. {
  596. for(int j=0;j<8;j++)
  597. {
  598. if(board[i][j]=='k')
  599. {
  600. king_row=i;
  601. king_col=j;
  602. break;
  603. }
  604. }
  605. }
  606. }
  607. else if(player==2)
  608. {
  609. for(int i=0;i<8;i++)
  610. {
  611. for(int j=0;j<8;j++)
  612. {
  613. if(board[i][j]=='K')
  614. {
  615. king_row=i;
  616. king_col=j;
  617. break;
  618. }
  619. }
  620. }
  621. }
  622. int temp=player;
  623. player=(player%2)+1;
  624. for(int i=0;i<8;i++)
  625. {
  626. for(int j=0;j<8;j++)
  627. {
  628. if(validmove(i,j,king_row,king_col))
  629. {
  630. //printf("%d %d %d %d\n",i+1,j+1,x+1,y+1);
  631. player=temp;
  632. player=player%2+1;
  633. return 1;
  634. }
  635. }
  636. }
  637. player=temp;
  638. player=player%2+1;
  639. return 0;
  640. }
  641. int check()
  642. {int x,y;
  643. if(player==1)
  644. {
  645. for(int i=0;i<8;i++)
  646. {
  647. for(int j=0;j<8;j++)
  648. {
  649. if(board[i][j]=='k')
  650. {
  651. x=i;
  652. y=j;
  653. break;
  654. }
  655. }
  656. }
  657. }
  658. else if(player==2)
  659. {
  660. for(int i=0;i<8;i++)
  661. {
  662. for(int j=0;j<8;j++)
  663. {
  664. if(board[i][j]=='K')
  665. {
  666. x=i;
  667. y=j;
  668. break;
  669. }
  670. }
  671. }
  672. }
  673. int temp=player;
  674. player=(player%2)+1;
  675. for(int i=0;i<8;i++)
  676. {
  677. for(int j=0;j<8;j++)
  678. {
  679. if(validmove(i,j,x,y))
  680. {
  681. //printf("%d %d %d %d\n",i+1,j+1,x+1,y+1);
  682. player=temp;
  683. return 1;
  684. }
  685. }
  686. }
  687. player=temp;
  688. return 0;
  689. }
  690. int checkmat()
  691. {
  692. if(check())
  693. {
  694. // printf("asd");
  695. for(int i=0;i<8;i++)
  696. {
  697. for(int j=0;j<8;j++)
  698. {
  699. for(int k=0;k<8;k++)
  700. {
  701. for(int l=0;l<8;l++)
  702. {
  703. if(validmove(i,j,k,l))
  704. {
  705. // printf(" fff%d %d %d %d\n",i+1,j+1,k+1,l+1);
  706. return 0;
  707. }
  708. }
  709. }
  710. }
  711. }
  712. return 1;
  713. }
  714. else
  715. {
  716. return 0;
  717. }
  718. }
  719. int stalemat()
  720. {
  721. if(!check())
  722. {
  723. for(int i=0;i<8;i++)
  724. {
  725. for(int j=0;j<8;j++)
  726. {
  727. if(board[i][j]!=white&&board[i][j]!=black)
  728. {
  729. for(int x=0;x<8;x++)
  730. {
  731. for(int y=0;y<8;y++)
  732. {
  733. if(validmove(i,j,x,y))
  734. {
  735. return 0;
  736. }
  737. }
  738. }
  739. }
  740. }
  741. }
  742. return 1;
  743. }
  744. else
  745. {
  746. return 0;
  747. }
  748. }
  749. int k=0;
  750. void playgame()
  751. {
  752. printf("CHOOSE 1 FOR SAVED GAME\nCHOOSE 2 FOR NEW GAME\n");
  753. scanf("%d",&d);
  754. firstDisplay();
  755. if(d!=1)
  756. {
  757. printDisplay();
  758. }
  759. else
  760. {
  761. load();
  762. }
  763. while(true)
  764. {
  765. if(player==1)
  766. {
  767. player=2;
  768. }
  769. else
  770. {
  771. player=1;
  772. }
  773. //player=2;
  774. if(checkmat())
  775. {
  776. system("cls");
  777. printf("\n");
  778. printf("%d",(player%2)+1);
  779. printf("WINS!!");
  780. break;
  781. }
  782. else if(check())
  783. {
  784. printf("Player %d checks Player %d\a\a\a\a\a\a\a\a\n",(player%2)+1,player);
  785. }
  786. else if(stalemat())
  787. {
  788. system("cls");
  789. printf("\n");
  790. printf("DRAW");
  791. break;
  792. }
  793. //player;
  794. printf("Player %d's turn: ",player);
  795. read_move(c);
  796. if(c[0]!='U'&&c[0]!='R')
  797. {
  798. while(validmove(fromr,fromc,tor,toc)!=1)
  799. {COORD coord ;
  800. coord.X=17;
  801. coord.Y=23;
  802. printf("Invalid MOVE ENTER MOVE AGAIN : ");
  803. printf("\r");
  804. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),coord);
  805. read_move(c);
  806. if(c[0]=='U'||c[0]=='R')
  807. {
  808. break;
  809. }
  810. }
  811. }
  812. make_move(fromr,fromc,tor,toc);
  813. save();
  814. }
  815. }
  816.  
  817. void save()
  818. {
  819. FILE * fpointer;
  820. fpointer=fopen("save.text","w");
  821. for(int i=7;i>=0;i--)
  822. {
  823. for(int j=0;j<8;j++)
  824. {
  825. fprintf(fpointer,"%c",board[i][j]);
  826. }
  827. }
  828. fprintf(fpointer,"%d",player);
  829. if(lostw!=0)
  830. {
  831. for(int i=0;i<=lostw;i++)
  832. {
  833. fprintf(fpointer,"%c",lost_white[i]);
  834. }
  835. }
  836. if(lostb!=0)
  837. {
  838. for(int i=0;i<=lostb;i++)
  839. {
  840. fprintf(fpointer,"%c",lost_black[i]);
  841. }
  842. }
  843. fclose(fpointer);
  844. }
  845. char s[150];
  846. void load()
  847. {
  848. FILE * fpointer;
  849. fpointer=fopen("save.text","r");
  850. if(fpointer=fopen("save.text","r")){
  851. int counter=0;
  852. while(!feof(fpointer))
  853. {
  854. fgets(s,200,fpointer);
  855. for(int i=7;i>=0;i--)
  856. {
  857. for(int j=0;j<8;j++)
  858. {
  859. board[i][j]=s[counter];
  860. counter++;
  861. }
  862. }
  863. player=s[counter]-'0';
  864. counter++;
  865. int i=0;
  866. while(islower(s[counter]))
  867. {
  868. lost_white[i]=s[counter];
  869. lostw++;
  870. i++;
  871. counter++;
  872. }
  873. i=0;
  874. if(s[counter]=' ')
  875. {
  876. counter++;
  877. }
  878. while(isupper(s[counter]))
  879. {
  880. lost_black[i]=s[counter];
  881. i++;
  882. lostb++;
  883. counter++;
  884. }
  885. break;
  886. }
  887. printDisplay();
  888. }
  889. else{printDisplay();}}
  890. int cunt=0;
  891. int cunt2=0;
  892. int cunt3=0;
  893. void undo()
  894. {
  895. //player=(player%2)+1;
  896. if(in<=0)
  897. {
  898. return ;
  899. }
  900. cunt=in;
  901. player=(player%2)+1;
  902. int countblack_after=0;
  903. int countwhite_after=0;
  904. int countblack_before=0;
  905. int countwhite_before=0;
  906. for(int i=7;i>=0;i--)
  907. {
  908. for(int j=0;j<8;j++)
  909. {
  910. if(board[i][j]!=white&&board[i][j]!=black)
  911. {
  912. if(islower(board[i][j]))
  913. {
  914. countwhite_before++;
  915. }
  916. else if(isupper(board[i][j]))
  917. {
  918. countblack_before++;
  919. }
  920. }
  921. board[i][j]=list[i][j][in-1];
  922. if(board[i][j]!=white&&board[i][j]!=black)
  923. {
  924. if(islower(board[i][j]))
  925. {
  926. countwhite_after++;
  927. }
  928. else if(isupper(board[i][j]))
  929. {
  930. countblack_after++;
  931. }
  932. }
  933. }
  934. }
  935. if(countwhite_after>countwhite_before)
  936. {
  937. lostw--;
  938. }
  939. if(countblack_after>countblack_before)
  940. {
  941. // printf("%d %d\n",countblack_before,countblack_after);
  942. lostb--;
  943. }
  944. in=in-2;
  945. printDisplay();
  946. cunt3=in;
  947. return ;
  948. }
  949. void redomove()
  950. {
  951. if(list[0][0][in+1]==0||cunt3!=in)
  952. {
  953. if(cunt3!=in)
  954. {
  955. list[0][0][in+1]=0;
  956. }
  957. return;
  958. }
  959. player=(player%2)+1;
  960. int countblack_after=0;
  961. int countwhite_after=0;
  962. int countblack_before=0;
  963. int countwhite_before=0;
  964. for(int i=7;i>=0;i--)
  965. {
  966. for(int j=0;j<8;j++)
  967. {
  968. if(board[i][j]!=white&&board[i][j]!=black)
  969. {
  970. if(islower(board[i][j]))
  971. {
  972. countwhite_before++;
  973. }
  974. else if(isupper(board[i][j]))
  975. {
  976. countblack_before++;
  977. }
  978. }
  979. board[i][j]=list[i][j][in+1];
  980. if(board[i][j]!=white&&board[i][j]!=black)
  981. {
  982. if(islower(board[i][j]))
  983. {
  984. countwhite_after++;
  985. }
  986. else if(isupper(board[i][j]))
  987. {
  988. countblack_after++;
  989. }
  990. }
  991. }
  992. }
  993. if(countwhite_after<countwhite_before)
  994. {
  995. lostw++;
  996. }
  997. else if(countblack_after<countblack_before)
  998. {
  999. lostb++;
  1000. }
  1001. in-1;
  1002. printDisplay();
  1003. cunt3++;
  1004. }
  1005. char permote='p';
  1006. int permotion()
  1007. {
  1008. if(board[tor][toc]=='p'&&tor==7)
  1009. {
  1010. printf("WHAT DO YOU WANT TO PERMOTE THE PAWN TO?");
  1011. scanf("%c",&permote);
  1012. while(!islower(permote)||permote=='k')
  1013. {
  1014. printf("INVALID PERMOTION\N");
  1015. scanf("%c",&permote);
  1016. }
  1017. board[tor][toc]=permote;
  1018. //p=1;
  1019. return 1;
  1020. }
  1021. else if(board[tor][toc]=='P'&&tor==0)
  1022. {
  1023. printf("WHAT DO YOU WANT TO PERMOTE THE PAWN TO?");
  1024. scanf("%c",&permote);
  1025. while(!isupper(permote)||permote=='K')
  1026. {
  1027. printf("INVALID PERMOTION\N");
  1028. scanf("%c",&permote);
  1029. }
  1030. board[tor][toc]=permote;
  1031. // p=1;
  1032. return 1;
  1033. }
  1034. return 0;
  1035. }
  1036. int main()
  1037. {
  1038. playgame();
  1039. // read_move(c,c2);
  1040. return 0;
  1041. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement