Advertisement
Seif45

final b2a aboos eidak

Dec 18th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 63.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5. char pieces[12][12];
  6. char gameBoard [12][12];
  7. char deadPieces[37];
  8. int undo[500][5];
  9. int redo[500][4];
  10. int pUndo;
  11. int pRedo;
  12. char play[5];
  13. int index[4];
  14. int died=1; //deadPieces index
  15. int diedLast;
  16. int i,j,k,l;
  17. int valid;
  18. int turn;
  19. int king1x, king1y, king2x, king2y;
  20. int checkWhite, checkBlack;
  21. int checkMateBlack,checkMateWhite;
  22. FILE *fPointer;
  23.  
  24. //scanning all of the user inputs
  25. void readLine(char s[],int n)
  26. {
  27. int ch, i=0;
  28. while((ch=getchar()) != '\n'){
  29. if(i<n)
  30. {
  31. s[i++]=ch;
  32. }
  33. }
  34. s[i]='\0';
  35. }
  36.  
  37. //function prototypes
  38. void gameBoardBorders();
  39. void checkerBoard();
  40. void startingChessBoard();
  41. void printGame ();
  42. void scanAndConvert();
  43. void checkValidMove();
  44. void swapElements ();
  45. void reverseSwapElement();
  46. void rook();
  47. void knight();
  48. void king();
  49. void king();
  50. void bishop();
  51. void queen();
  52. void pawn();
  53. void moveSet();
  54. void fullKingCheckWhite(int x,int y);
  55. void fullKingCheckBlack(int x,int y);
  56. void kingWhiteCheckMate();
  57. void kingBlackCheckMate();
  58. void checkMateBlackPath(int x,int y);
  59. void checkMateWhitePath(int x,int y);
  60. void kingWhiteStaleMate();
  61. void kingBlackStaleMate();
  62. void fullPieceCheckBlack(int x,int y);
  63. void fullPieceCheckWhite(int x,int y);
  64. void saveRedo();
  65. void saveUndo();
  66. void gameOver();
  67. void saveGame();
  68. void loadGame();
  69.  
  70. int main(){
  71. //setting up the start board
  72. newGame:startingChessBoard();
  73. gameBoardBorders();
  74. checkerBoard();
  75. printGame();
  76. //pinpointing the positions of the king
  77. king1x=9, king1y=6, king2x=2, king2y=6;
  78. //setting up the parameters
  79. turn=0;
  80. diedLast=0;
  81. checkWhite=0;
  82. checkBlack=0;
  83. pUndo=0;
  84. pRedo=0;
  85. int r, u;
  86. while (1){
  87. valid = 0;
  88. checkMateBlack=-1;checkMateWhite=-1;
  89. //white turn
  90. if (turn%2==0){
  91. while ((valid==0)||(checkWhite==1)){
  92. pUndo=0;
  93. printf("\nWhite's turn:\t (for undo press \"U\" / for redo press \"R\" / for save press \"S\" / for load press \"L\")\n");
  94. scanAndConvert();
  95. //to save the game
  96. while ((play[0]=='S')&&(play[1]=='\0')){
  97. saveGame();
  98. printf("\nWhite's turn:\t (for undo press \"U\" / for redo press \"R\" / for save press \"S\" / for load press \"L\")\n");
  99. scanAndConvert();
  100. }
  101. //to load the game
  102. if ((play[0]=='L')&&(play[1]=='\0')){
  103. printf("Enter the file name to be loaded.\n");
  104. char saveName[100];
  105. readLine(saveName,100);
  106. fPointer = fopen(saveName,"r");
  107. if (fPointer){ //checking the file name
  108. loadGame();
  109. break;
  110. }
  111. else {
  112. printf("File doesn't exist.\n");
  113. }
  114. }
  115. //if player wants to undo
  116. if((play[0]=='U'&&turn>0)&&(play[1]=='\0')){
  117. //making the pieces index as the last turn
  118. for(u=0;u<4;u++){
  119. index[u]=undo[turn-1][u];
  120. }
  121. //checking if anything dead
  122. diedLast=undo[turn-1][4];
  123. //saving redo
  124. saveRedo();
  125. reverseSwapElement();
  126. turn--;
  127. pUndo=1;
  128. break;
  129. }
  130. //if player wants to redo
  131. else if((play[0]=='R'&&pRedo-1>=0)&&(play[1]=='\0')){
  132. for(r=0;r<4;r++){
  133. //making the pieces index as the last turn
  134. index[r]=redo[pRedo-1][r];
  135. }
  136. //saving the undo
  137. saveUndo();
  138. swapElements();
  139. undo[turn][4]=diedLast;
  140. pRedo--;
  141. break;
  142. }
  143. else{
  144. //if the input is valid
  145. if(valid==1){
  146. checkValidMove(); //checking if the move of valid
  147. }
  148. if(valid==1){
  149. moveSet(); //seeing the move set of the selected piece
  150. }
  151. if (valid==1){
  152. saveUndo();
  153. swapElements();//moving
  154. undo[turn][4]=diedLast;
  155. }
  156. if(valid==1){
  157. fullKingCheckWhite(king1x,king1y); //checking the king is in check or not
  158. }
  159. if ((valid==1)&&(checkWhite==1)){
  160. printf("Invalid move.\n");
  161. reverseSwapElement();//reversing the swap if the king becomes in check
  162. }
  163. }
  164. }
  165. //if game is loaded start from beginning
  166. if (play[0]=='L'){
  167. continue;
  168. }
  169. printf("\n");
  170. printGame();
  171. kingWhiteStaleMate();//checking own king stalemate
  172. if (checkMateBlack==0){
  173. printf("Stalemate.\n");
  174. gameOver();
  175. if (play[0]=='N'){
  176. goto newGame;
  177. }
  178. else if (play[0]=='L'){
  179. printf("Enter the file name to be loaded.\n");
  180. char saveName[100];
  181. readLine(saveName,100);
  182. fPointer = fopen(saveName,"r");
  183. while (!fPointer){
  184. printf("File doesn't exist.\n");
  185. printf("Enter the file name to be loaded.\n");
  186. readLine(saveName,100);
  187. fPointer = fopen(saveName,"r");
  188. }
  189. loadGame();
  190. continue;
  191. }
  192. else if (play[0]=='E'){
  193. exit(EXIT_SUCCESS);
  194. }
  195. }
  196. fullKingCheckBlack(king2x,king2y);//checking the opposite player king
  197. if (checkMateBlack==1){
  198. kingBlackCheckMate();//checking checkmate on the opposite player
  199. if (checkMateWhite==0){
  200. printf("Check mate.\n");
  201. gameOver();
  202. if (play[0]=='N'){
  203. goto newGame;
  204. }
  205. else if (play[0]=='L'){
  206. printf("Enter the file name to be loaded.\n");
  207. char saveName[100];
  208. readLine(saveName,100);
  209. fPointer = fopen(saveName,"r");
  210. while (!fPointer){
  211. printf("File doesn't exist.\n");
  212. printf("Enter the file name to be loaded.\n");
  213. readLine(saveName,100);
  214. fPointer = fopen(saveName,"r");
  215. }
  216. loadGame();
  217. continue;
  218. }
  219. else if (play[0]=='E'){
  220. exit(EXIT_SUCCESS);
  221. }
  222. }
  223. }
  224. fullKingCheckBlack(king2x,king2y);//checking again to be sure of the values
  225. if (checkBlack==0){
  226. kingBlackStaleMate();//checking stalemate of the other player
  227. if (checkMateWhite==0){
  228. printf("Stalemate.\n");
  229. gameOver();
  230. if (play[0]=='N'){
  231. goto newGame;
  232. }
  233. else if (play[0]=='L'){
  234. printf("Enter the file name to be loaded.\n");
  235. char saveName[100];
  236. readLine(saveName,100);
  237. fPointer = fopen(saveName,"r");
  238. while (!fPointer){
  239. printf("File doesn't exist.\n");
  240. printf("Enter the file name to be loaded.\n");
  241. readLine(saveName,100);
  242. fPointer = fopen(saveName,"r");
  243. }
  244. loadGame();
  245. continue;
  246. }
  247. else if (play[0]=='E'){
  248. exit(EXIT_SUCCESS);
  249. }
  250. }
  251. }
  252. fullKingCheckBlack(king2x,king2y);//checking again to be sure of the values
  253. if (checkBlack==1){
  254. printf("Check black.\n");
  255. }
  256. if(pUndo==0){//changing turn
  257. turn++;
  258. }
  259. }
  260. else {//Black turn same steps as the white
  261. while ((valid==0)||(checkBlack==1)){
  262. pUndo=0;
  263. printf("\nBlack's turn:\t (for undo press \"U\" / for redo press \"R\" / for save press \"S\" / for load press \"L\")\n");
  264. scanAndConvert();
  265. while ((play[0]=='S')&&(play[1]=='\0')){
  266. saveGame();
  267. printf("\nWhite's turn:\t (for undo press \"U\" / for redo press \"R\" / for save press \"S\" / for load press \"L\")\n");
  268. scanAndConvert();
  269. }
  270. if ((play[0]=='L')&&(play[1]=='\0')){
  271. printf("Enter the file name to be loaded.\n");
  272. char saveName[100];
  273. readLine(saveName,100);
  274. fPointer = fopen(saveName,"r");
  275. if (fPointer){ //checking the file name
  276. loadGame();
  277. break;
  278. }
  279. else {
  280. printf("File doesn't exist.\n");
  281. }
  282. }
  283. if((play[0]=='U'&&turn>0)&&(play[1]=='\0')){
  284. for(u=0;u<4;u++){
  285. index[u]=undo[turn-1][u];
  286. }
  287. diedLast=undo[turn-1][4];
  288. saveRedo();
  289. reverseSwapElement();
  290. turn--;
  291. pUndo=1;
  292. break;
  293. }
  294. else if((play[0]=='R'&&pRedo-1>=0)&&(play[1]=='\0')){
  295. for(r=0;r<4;r++){
  296. index[r]=redo[pRedo-1][r];
  297. }
  298. saveUndo();
  299. swapElements();
  300. undo[turn][4]=diedLast;
  301. pRedo--;
  302. break;
  303. }
  304. else{
  305. if(valid==1){
  306. checkValidMove();
  307. }
  308. if(valid==1){
  309. moveSet();
  310. }
  311. if(valid==1){
  312. saveUndo();
  313. swapElements();
  314. undo[turn][4]=diedLast;
  315. }
  316. if(valid==1){
  317. fullKingCheckBlack(king2x,king2y);
  318. }
  319. if(checkBlack==1 && valid==1){
  320. printf("Invalid move.\n");
  321. reverseSwapElement();
  322. }
  323. }
  324. }
  325. if (play[0]=='L'){
  326. continue;
  327. }
  328. printf("\n");
  329. printGame();
  330. kingBlackStaleMate();
  331. if (checkMateWhite==0){
  332. printf("Stalemate.\n");
  333. gameOver();
  334. if (play[0]=='N'){
  335. goto newGame;
  336. }
  337. else if (play[0]=='L'){
  338. printf("Enter the file name to be loaded.\n");
  339. char saveName[100];
  340. readLine(saveName,100);
  341. fPointer = fopen(saveName,"r");
  342. while (!fPointer){
  343. printf("File doesn't exist.\n");
  344. printf("Enter the file name to be loaded.\n");
  345. readLine(saveName,100);
  346. fPointer = fopen(saveName,"r");
  347. }
  348. loadGame();
  349. continue;
  350. }
  351. else if (play[0]=='E'){
  352. exit(EXIT_SUCCESS);
  353. }
  354. }
  355. fullKingCheckWhite(king1x,king1y);
  356. if (checkMateWhite==1){
  357. kingWhiteCheckMate();
  358. if (checkMateBlack==0){
  359. printf("Check mate.\n");
  360. gameOver();
  361. if (play[0]=='N'){
  362. goto newGame;
  363. }
  364. else if (play[0]=='L'){
  365. printf("Enter the file name to be loaded.\n");
  366. char saveName[100];
  367. readLine(saveName,100);
  368. fPointer = fopen(saveName,"r");
  369. while (!fPointer){
  370. printf("File doesn't exist.\n");
  371. printf("Enter the file name to be loaded.\n");
  372. readLine(saveName,100);
  373. fPointer = fopen(saveName,"r");
  374. }
  375. loadGame();
  376. continue;
  377. }
  378. else if (play[0]=='E'){
  379. exit(EXIT_SUCCESS);
  380. }
  381. }
  382. }
  383. fullKingCheckWhite(king1x,king1y);
  384. if (checkWhite==0){
  385. kingWhiteStaleMate();
  386. if (checkMateBlack==0){
  387. printf("Stalemate.\n");
  388. gameOver();
  389. if (play[0]=='N'){
  390. goto newGame;
  391. }
  392. else if (play[0]=='L'){
  393. printf("Enter the file name to be loaded.\n");
  394. char saveName[100];
  395. readLine(saveName,100);
  396. fPointer = fopen(saveName,"r");
  397. while (!fPointer){
  398. printf("File doesn't exist.\n");
  399. printf("Enter the file name to be loaded.\n");
  400. readLine(saveName,100);
  401. fPointer = fopen(saveName,"r");
  402. }
  403. loadGame();
  404. continue;
  405. }
  406. else if (play[0]=='E'){
  407. exit(EXIT_SUCCESS);
  408. }
  409. }
  410. }
  411. fullKingCheckWhite(king1x,king1y);
  412. if (checkWhite==1){
  413. printf("Check white.\n");
  414. }
  415. if(pUndo==0){
  416. turn++;
  417. }
  418. }
  419. }
  420. return 0;
  421. }
  422.  
  423. void gameBoardBorders(){
  424. for (i=0;i<12;i++){
  425. for (j=0;j<12;j++){
  426. gameBoard[i][j]='\0';
  427. }
  428. }
  429. for (i=0;i<12;i+=11){
  430. for (j=2;j<10;j++){
  431. gameBoard[i][j]='A'+(j-2);
  432. }
  433. }
  434. for (j=2;j<10;j++){
  435. gameBoard[1][j]='-';
  436. gameBoard[10][j]='-';
  437. }
  438. for (i=2;i<10;i++){
  439. for (j=0;j<12;j+=11){
  440. gameBoard[i][j]='8'-(i-2);
  441. }
  442. }
  443. for (i=2;i<10;i++){
  444. gameBoard[i][1]='|';
  445. gameBoard[i][10]='|';
  446. }
  447.  
  448. }
  449.  
  450. void checkerBoard(){
  451. for (i=2;i<10;i++){
  452. for (j=2;j<10;j+=2){
  453. if (i%2==0){
  454. gameBoard[i][j]='.';
  455. }
  456. else {
  457. gameBoard[i][j]='_';
  458. }
  459. }
  460. }
  461. for (i=2;i<10;i++){
  462. for (j=3;j<10;j+=2){
  463. if (i%2==0){
  464. gameBoard[i][j]='_';
  465. }
  466. else {
  467. gameBoard[i][j]='.';
  468. }
  469. }
  470. }
  471. }
  472.  
  473. void startingChessBoard(){
  474. for (i=0;i<12;i++){
  475. for(j=0;j<12;j++){
  476. pieces[i][j]='\0';
  477. }
  478. }
  479. pieces[2][2]= 'R';
  480. pieces[2][3]= 'N';
  481. pieces[2][4]= 'B';
  482. pieces[2][5]= 'Q';
  483. pieces[2][6]= 'K';
  484. pieces[2][7]= 'B';
  485. pieces[2][8]= 'N';
  486. pieces[2][9]= 'R';
  487.  
  488. pieces[9][2]= 'r';
  489. pieces[9][3]= 'n';
  490. pieces[9][4]= 'b';
  491. pieces[9][5]= 'q';
  492. pieces[9][6]= 'k';
  493. pieces[9][7]= 'b';
  494. pieces[9][8]= 'n';
  495. pieces[9][9]= 'r';
  496.  
  497. for(j=2;j<10;j++){
  498. pieces[3][j]= 'P';
  499. }
  500.  
  501. for(j=2;j<10;j++){
  502. pieces[8][j]= 'p';
  503. }
  504. for(i=0;i<36;i++){
  505. deadPieces[i]='\0';
  506. }
  507. }
  508.  
  509. void printGame(){
  510. system("cls");
  511. printf("\n\t\t\t\t\t\tDead\n\n");
  512. for (i=0;i<12;i++){
  513. for (j=0;j<12;j++){
  514. printf("%c",gameBoard[i][j]);
  515. printf("%c ", pieces[i][j]);
  516. }
  517. printf("\t\t%c %c %c", deadPieces[i+1], deadPieces[i+13], deadPieces[i+25]);
  518. printf("\n\n");
  519. }
  520. printf("\n");
  521. }
  522.  
  523. void scanAndConvert(){
  524. readLine(play,5);
  525. if((play[0]=='U')&&(play[1]=='\0')){
  526. return;
  527. }
  528. else if((play[0]=='R')&&(play[1]=='\0')){
  529. return;
  530. }
  531. else if ((play[0]=='S')&&(play[1]=='\0')){
  532. return;
  533. }
  534. else if ((play[0]=='L')&&(play[1]=='\0')){
  535. return;
  536. }
  537. else{
  538. if (play[4]!='\0'){
  539. printf("Invalid move.\n");
  540. valid=0;
  541. return;
  542. }//checking the range of the input
  543. if ((play[0]<'A')||(play[0]>'H')||(play[1]<'1')||(play[1]>'8')||(play[2]<'A')||(play[2]>'H')||(play[3]<'1')||(play[3]>'8')){
  544. printf("Invalid move.\n");
  545. valid=0;
  546. return;
  547. }
  548. } //converting to have indexes
  549. index[0]=58-play[1];
  550. index[1]=play[0]-63;
  551. index[2]=58-play[3];
  552. index[3]=play[2]-63;
  553. valid=1;
  554. }
  555.  
  556. void checkValidMove(){
  557. if (turn%2==0){
  558. if ((pieces[index[0]][index[1]]<'a')||(pieces[index[0]][index[1]]>'z')){
  559. printf("Invalid move.\n");
  560. valid=0;
  561. return;
  562. }
  563. if ((pieces[index[2]][index[3]]>='a')&&(pieces[index[2]][index[3]]<='z')){
  564. printf("Invalid move.\n");
  565. valid=0;
  566. return;
  567. }
  568. }
  569. else if (turn%2==1){
  570. if ((pieces[index[0]][index[1]]<'A')||(pieces[index[0]][index[1]]>'Z')){
  571. printf("Invalid move.\n");
  572. valid=0;
  573. return;
  574. }
  575. if((pieces[index[2]][index[3]]>='A')&&(pieces[index[2]][index[3]]<='Z')){
  576. printf("Invalid move.\n");
  577. valid=0;
  578. return;
  579. }
  580. }
  581. valid=1;
  582. }
  583.  
  584. void swapElements (){
  585. if ((pieces[index[2]][index[3]])!='\0'){//checking if the place has an opposite piece
  586. deadPieces[died]= pieces[index[2]][index[3]];
  587. died++;
  588. pieces[index[2]][index[3]]='\0';
  589. diedLast=1;
  590. }else{
  591. diedLast=0;
  592. }
  593. char temp=pieces[index[0]][index[1]];
  594. pieces[index[0]][index[1]]=pieces[index[2]][index[3]];
  595. pieces[index[2]][index[3]]=temp;
  596. }
  597.  
  598. void saveUndo(){ //saving the index of the turn in the undo list
  599. int u;
  600. for(u=0;u<4;u++){
  601. undo[turn][u]=index[u];
  602. }
  603. }
  604.  
  605. void saveRedo(){//if undo is done saves the move in the redo
  606. int r;
  607. for(r=0;r<4;r++){
  608. redo[pRedo][r]=undo[turn-1][r];
  609. }
  610. pRedo++;
  611. }
  612.  
  613. void reverseSwapElement(){
  614. if(diedLast==1){//if last move a piece died then we bring it back
  615. if(pieces[index[0]][index[1]]=='\0' || deadPieces[died-1]!='\0'){
  616. pieces[index[0]][index[1]]=deadPieces[died-1];
  617. died--;
  618. deadPieces[died]='\0';
  619. }
  620. }
  621. char temp=pieces[index[2]][index[3]];
  622. pieces[index[2]][index[3]]=pieces[index[0]][index[1]];
  623. pieces[index[0]][index[1]]=temp;
  624. }
  625.  
  626. void rook(){//movement of the rook
  627. if((index[3]-index[1])==0 && (index[2]-index[0]>0)){
  628. for (i=1;i<index[2]-index[0];i++){
  629. if (pieces[index[0]+i][index[1]]!='\0'){
  630. printf("Invalid move.");
  631. valid=0;
  632. return;
  633. }
  634. }
  635. if(index[0]+i==index[2]){
  636. valid=1;
  637. }
  638. }
  639. else if ((index[3]-index[1])==0 && (index[0]-index[2]>0)){
  640. for (i=1;i<index[0]-index[2];i++){
  641. if (pieces[index[0]-i][index[1]]!='\0'){
  642. printf("Invalid move.");
  643. valid=0;
  644. return;
  645. }
  646. }
  647. if (index[0]-i==index[2]){
  648. valid=1;
  649. }
  650. }
  651. else if ((index[3]-index[1])>0 && (index[2]-index[0]==0)){
  652. for (i=1;i<index[3]-index[1];i++){
  653. if (pieces[index[0]][index[1]+i]!='\0'){
  654. printf("Invalid move.");
  655. valid=0;
  656. return;
  657. }
  658. }
  659. if(index[1]+i==index[3]){
  660. valid=1;
  661. }
  662. }
  663. else if ((index[1]-index[3])>0 && (index[2]-index[0]==0)){
  664. for (i=1;i<index[1]-index[3];i++){
  665. if (pieces[index[0]][index[1]-i]!='\0'){
  666. printf("Invalid move.");
  667. valid=0;
  668. return;
  669. }
  670. }
  671. if(index[1]-i==index[3]){
  672. valid=1;
  673. }
  674. }
  675. else{
  676. printf("Invalid move.");
  677. valid=0;
  678. return;
  679. }
  680. }
  681.  
  682. void knight(){//movement of the knight
  683. if ((((abs(index[3]-index[1]))==1)&&((abs(index[2]-index[0]))==2)) || (((abs(index[3]-index[1]))==2)&&((abs(index[2]-index[0]))==1)))
  684. {
  685. valid=1;
  686. }else{
  687. printf("Invalid move.");
  688. valid=0;
  689. return;
  690. }
  691. }
  692.  
  693. void king(){//movement of the king
  694. if((((abs(index[3]-index[1]))==1) && ((abs(index[2]-index[0]))==1)) || (((abs(index[3]-index[1]))==1) && ((abs(index[2]-index[0]))==0)) || (((abs(index[3]-index[1]))==0) && ((abs(index[2]-index[0]))==1)))
  695. {
  696. if (turn%2==0){
  697. king1x=index[2];
  698. king1y=index[3];
  699. }
  700. else{
  701. king2x=index[2];
  702. king2y=index[3];
  703. }
  704. valid=1;
  705. }else{
  706. printf("Invalid move.");
  707. valid=0;
  708. return;
  709. }
  710. }
  711.  
  712. void bishop(){//movement of the bishop
  713. if (((index[0]-index[2])==(index[1]-index[3]))&&((index[0]-index[2])>0)){
  714. for (i=1;i<index[0]-index[2];i++){
  715. if ((pieces[index[0]-i][index[1]-i])!='\0'){
  716. printf("Invalid move.");
  717. valid=0;
  718. return;
  719. }
  720. }
  721. if ((index[0]-i==index[2])&&(index[1]-i==index[3])){
  722. valid=1;
  723. }
  724. }
  725. else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[2]-index[0])>0)){
  726. for (i=1;i<index[2]-index[0];i++){
  727. if ((pieces[index[0]+i][index[1]+i])!='\0'){
  728. printf("Invalid move.");
  729. valid=0;
  730. return;
  731. }
  732. }
  733. if ((index[0]+i==index[2])&&(index[1]+i==index[3])){
  734. valid=1;
  735. }
  736. }
  737. else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[0]-index[2])>0)){
  738. for (i=1;i<index[0]-index[2];i++){
  739. if ((pieces[index[0]-i][index[1]+i])!='\0'){
  740. printf("Invalid move.");
  741. valid=0;
  742. return;
  743. }
  744. }
  745. if ((index[0]-i==index[2])&&(index[1]+i==index[3])){
  746. valid=1;
  747. }
  748. }
  749. else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[2]-index[0])>0)){
  750. for (i=1;i<index[0]-index[2];i++){
  751. if ((pieces[index[0]+i][index[1]-i])!='\0'){
  752. printf("Invalid move.");
  753. valid=0;
  754. return;
  755. }
  756. }
  757. if ((index[0]+i==index[2])&&(index[1]-i==index[3])){
  758. valid=1;
  759. }
  760. }
  761. else{
  762. printf("Invalid move.");
  763. valid=0;
  764. return;
  765. }
  766. }
  767.  
  768. void queen(){//movement of the queen
  769. if((index[3]-index[1])==0 && (index[2]-index[0]>0)){
  770. for (i=1;i<index[2]-index[0];i++){
  771. if (pieces[index[0]+i][index[1]]!='\0'){
  772. printf("Invalid move.");
  773. valid=0;
  774. return;
  775. }
  776. }
  777. if(index[0]+i==index[2]){
  778. valid=1;
  779. }
  780. }
  781. else if ((index[3]-index[1])==0 && (index[0]-index[2]>0)){
  782. for (i=1;i<index[0]-index[2];i++){
  783. if (pieces[index[0]-i][index[1]]!='\0'){
  784. printf("Invalid move.");
  785. valid=0;
  786. return;
  787. }
  788. }
  789. if (index[0]-i==index[2]){
  790. valid=1;
  791. }
  792. }
  793. else if ((index[3]-index[1])>0 && (index[2]-index[0]==0)){
  794. for (i=1;i<index[3]-index[1];i++){
  795. if (pieces[index[0]][index[1]+i]!='\0'){
  796. printf("Invalid move.");
  797. valid=0;
  798. return;
  799. }
  800. }
  801. if(index[1]+i==index[3]){
  802. valid=1;
  803. }
  804. }
  805. else if ((index[1]-index[3])>0 && (index[2]-index[0]==0)){
  806. for (i=1;i<index[1]-index[3];i++){
  807. if (pieces[index[0]][index[1]-i]!='\0'){
  808. printf("Invalid move.");
  809. valid=0;
  810. return;
  811. }
  812. }
  813. if(index[1]-i==index[3]){
  814. valid=1;
  815. }
  816. }
  817. else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[0]-index[2])>0)){
  818. for (i=1;i<index[0]-index[2];i++){
  819. if ((pieces[index[0]-i][index[1]-i])!='\0'){
  820. printf("Invalid move.");
  821. valid=0;
  822. return;
  823. }
  824. }
  825. if ((index[0]-i==index[2])&&(index[1]-i==index[3])){
  826. valid=1;
  827. }
  828. }
  829. else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[2]-index[0])>0)){
  830. for (i=1;i<index[2]-index[0];i++){
  831. if ((pieces[index[0]+i][index[1]+i])!='\0'){
  832. printf("Invalid move.");
  833. valid=0;
  834. return;
  835. }
  836. }
  837. if ((index[0]+i==index[2])&&(index[1]+i==index[3])){
  838. valid=1;
  839. }
  840. }
  841. else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[0]-index[2])>0)){
  842. for (i=1;i<index[0]-index[2];i++){
  843. if ((pieces[index[0]-i][index[1]+i])!='\0'){
  844. printf("Invalid move.");
  845. valid=0;
  846. return;
  847. }
  848. }
  849. if ((index[0]-i==index[2])&&(index[1]+i==index[3])){
  850. valid=1;
  851. }
  852. }
  853. else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[2]-index[0])>0)){
  854. for (i=1;i<index[0]-index[2];i++){
  855. if ((pieces[index[0]+i][index[1]-i])!='\0'){
  856. printf("Invalid move.");
  857. valid=0;
  858. return;
  859. }
  860. }
  861. if ((index[0]+i==index[2])&&(index[1]-i==index[3])){
  862. valid=1;
  863. }
  864. }
  865. else{
  866. printf("Invalid move.");
  867. valid=0;
  868. return;
  869. }
  870. }
  871.  
  872. void pawn(){//movement of the pawn
  873. char promote[2];
  874. if (pieces[index[0]][index[1]]=='p'){
  875. if(index[0]==8){
  876. if(((index[2]-index[0]==-1)||(index[2]-index[0]==-2)) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
  877. valid = 1;
  878. }
  879. else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
  880. valid = 1;
  881. }
  882. else if ((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
  883. valid=1;
  884. }
  885. else{
  886. printf("Invalid move.");
  887. valid=0;
  888. return;
  889. }
  890. }else{
  891. if((index[2]-index[0]==-1) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
  892. valid = 1;
  893. }
  894. else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
  895. valid = 1;
  896. }
  897. else{
  898. printf("Invalid move.");
  899. valid=0;
  900. return;
  901. }
  902. }
  903. if (index[2]==2){
  904. printf("Promote to: ");
  905. readLine(promote,2);
  906. while ((promote[1]!='\0')||((promote[0] != 'R')&&(promote[0] != 'N')&&(promote[0] != 'B')&&(promote[0] != 'Q')&&(promote[0] != 'P'))){
  907. printf("Wrong promotion\n");
  908. printf("Promote to: ");
  909. readLine(promote,2);
  910. }
  911. if(promote[0] == 'R'){
  912. pieces[index[0]][index[1]]='r';
  913. valid=1;
  914. }
  915. else if(promote[0] == 'N'){
  916. pieces[index[0]][index[1]]='n';
  917. valid=1;
  918. }
  919. else if(promote[0] == 'B'){
  920. pieces[index[0]][index[1]]='b';
  921. valid=1;
  922. }
  923. else if(promote[0] == 'Q'){
  924. pieces[index[0]][index[1]]='q';
  925. valid=1;
  926. }else if(promote[0] == 'P'){
  927. pieces[index[0]][index[1]]='p';
  928. valid=1;
  929. }
  930. }
  931. }
  932. else{
  933. if(index[0]==3){
  934. if(((index[2]-index[0]==1)||(index[2]-index[0]==2)) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
  935. valid = 1;
  936. }
  937. else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
  938. valid = 1;
  939. }
  940. else if ((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==1)&& (abs(index[3]-index[1])==1))){
  941. valid=1;
  942. }
  943. else{
  944. printf("Invalid move.");
  945. valid=0;
  946. return;
  947. }
  948. }else{
  949. if((index[2]-index[0]==1) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
  950. valid = 1;
  951. }
  952. else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==1)&& (abs(index[3]-index[1])==1))){
  953. valid = 1;
  954. }
  955. else{
  956. printf("Invalid move.");
  957. valid=0;
  958. return;
  959. }
  960. }
  961. if (index[2]==9){
  962. printf("Promote to: ");
  963. readLine(promote,2);
  964. while((promote[0] != 'R' && promote[0] != 'N' && promote[0] != 'B' && promote[0] != 'Q' && promote[0] != 'P')||(promote[1]!='\0')){
  965. printf("Wrong promotion\n");
  966. printf("Promote to: ");
  967. readLine(promote,2);
  968. }
  969. if(promote[0] == 'R'){
  970. pieces[index[0]][index[1]]='R';
  971. valid=1;
  972. }
  973. else if(promote[0] == 'N'){
  974. pieces[index[0]][index[1]]='N';
  975. valid=1;
  976. }
  977. else if(promote[0] == 'B'){
  978. pieces[index[0]][index[1]]='B';
  979. valid=1;
  980. }
  981. else if(promote[0] == 'Q'){
  982. pieces[index[0]][index[1]]='Q';
  983. valid=1;
  984. }else if(promote[0] == 'P'){
  985. pieces[index[0]][index[1]]='P';
  986. valid=1;
  987. }
  988. }
  989. }
  990. }
  991.  
  992. void moveSet(){//checking which movement the code should do
  993. if ((pieces[index[0]][index[1]]=='p')||(pieces[index[0]][index[1]]=='P')){
  994. pawn();
  995. }
  996. else if ((pieces[index[0]][index[1]]=='r')||(pieces[index[0]][index[1]]=='R')){
  997. rook();
  998. }
  999. else if ((pieces[index[0]][index[1]]=='n')||(pieces[index[0]][index[1]]=='N')){
  1000. knight();
  1001. }
  1002. else if ((pieces[index[0]][index[1]]=='b')||(pieces[index[0]][index[1]]=='B')){
  1003. bishop();
  1004. }
  1005. else if ((pieces[index[0]][index[1]]=='q')||(pieces[index[0]][index[1]]=='Q')){
  1006. queen();
  1007. }
  1008. else if ((pieces[index[0]][index[1]]=='k')||(pieces[index[0]][index[1]]=='K')){
  1009. king();
  1010. }
  1011. }
  1012.  
  1013. void fullKingCheckWhite(int x,int y){ //check if own king is in check
  1014. //checking if opposite knight can kill the king
  1015. if ((pieces[x-1][y+2]=='N')||(pieces[x+1][y+2]=='N')||(pieces[x-1][y-2]=='N')||(pieces[x+1][y-2]=='N')||(pieces[x+2][y-1]=='N')||(pieces[x+2][y+1]=='N')||(pieces[x-2][y-1]=='N')||(pieces[x-2][y+1]=='N')){
  1016. checkWhite=1;
  1017. checkMateWhite=1;
  1018. return;
  1019. }
  1020. //checking if opposite pawn can kill the king
  1021. else if ((pieces[x-1][y-1]=='P')||(pieces[x-1][y+1]=='P')){
  1022. checkWhite=1;
  1023. checkMateWhite=1;
  1024. return;
  1025. }
  1026. else if ((pieces[x-1][y]=='P')&&(pieces[x][y]=='\0')){
  1027. checkWhite=1;
  1028. checkMateWhite=1;
  1029. return;
  1030. }
  1031. //checking if opposite king can kill the king
  1032. else if ((pieces[x+1][y]=='K')||(pieces[x-1][y]=='K')||(pieces[x][y+1]=='K')||(pieces[x][y-1]=='K')||(pieces[x+1][y+1]=='K')||(pieces[x-1][y-1]=='K')||(pieces[x+1][y-1]=='K')||(pieces[x-1][y+1]=='K')){
  1033. checkWhite=1;
  1034. checkMateWhite=1;
  1035. return;
  1036. }
  1037. else{
  1038. //checking if opposite rook or queen can kill the king
  1039. for(i=1; i<=7;i++){
  1040. if(x+i>9){
  1041. break;
  1042. }
  1043. else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='R' && pieces[x+i][y]!='Q')){
  1044. checkWhite=0;
  1045. checkMateWhite=0;
  1046. break;
  1047. }
  1048. else if(pieces[x+i][y]=='R'||pieces[x+i][y]=='Q'){
  1049. checkWhite=1;
  1050. checkMateWhite=1;
  1051. return;
  1052. }
  1053. }
  1054. for(i=1; i<=7;i++){
  1055. if(x-i<2){
  1056. break;
  1057. }
  1058. else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='R' && pieces[x-i][y]!='Q')){
  1059. checkWhite=0;
  1060. checkMateWhite=0;
  1061. break;
  1062. }
  1063. else if(pieces[x-i][y]=='R'||pieces[x-i][y]=='Q'){
  1064. checkWhite=1;
  1065. checkMateWhite=1;
  1066. return;
  1067. }
  1068. }
  1069. for(i=1; i<=7;i++){
  1070. if(y+i>9){
  1071. break;
  1072. }
  1073. else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='R' && pieces[x][y+i]!='Q')){
  1074. checkWhite=0;
  1075. checkMateWhite=0;
  1076. break;
  1077. }
  1078. else if(pieces[x][y+i]=='R'||pieces[x][y+i]=='Q'){
  1079. checkWhite=1;
  1080. checkMateWhite=1;
  1081. return;
  1082. }
  1083. }
  1084. for(i=1; i<=7;i++){
  1085. if(y-i<2){
  1086. break;
  1087. }
  1088. else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='R' && pieces[x][y-i]!='Q')){
  1089. checkWhite=0;
  1090. checkMateWhite=0;
  1091. break;
  1092. }
  1093. else if(pieces[x][y-i]=='R'||pieces[x][y-i]=='Q'){
  1094. checkWhite=1;
  1095. checkMateWhite=1;
  1096. return;
  1097. }
  1098. }
  1099. //checking if opposite bishop or queen can kill the king
  1100. for(i=1; i<=7;i++){
  1101. if(x+i>9 && y+i>9){
  1102. break;
  1103. }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='B' && pieces[x+i][y+i]!='Q')){
  1104. checkWhite=0;
  1105. checkMateWhite=0;
  1106. break;
  1107. }
  1108. else if(pieces[x+i][y+i]=='B'||pieces[x+i][y+i]=='Q'){
  1109. checkWhite=1;
  1110. checkMateWhite=1;
  1111. return;
  1112. }
  1113. }
  1114. for(i=1; i<=7;i++){
  1115. if(x-i<2 && y+i>9){
  1116. break;
  1117. }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='B' && pieces[x-i][y+i]!='Q')){
  1118. checkWhite=0;
  1119. checkMateWhite=0;
  1120. break;
  1121. }
  1122. else if(pieces[x-i][y+i]=='B'||pieces[x-i][y+i]=='Q'){
  1123. checkWhite=1;
  1124. checkMateWhite=1;
  1125. return;
  1126. }
  1127. }
  1128. for(i=1; i<=7;i++){
  1129. if(x-i<2 && y-i<2){
  1130. break;
  1131. }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='B' && pieces[x-i][y-i]!='Q')){
  1132. checkWhite=0;
  1133. checkMateWhite=0;
  1134. break;
  1135. }
  1136. else if(pieces[x-i][y-i]=='B'||pieces[x-i][y-i]=='Q'){
  1137. checkWhite=1;
  1138. checkMateWhite=1;
  1139. return;
  1140. }
  1141. }
  1142. for(i=1; i<=7;i++){
  1143. if(x+i>9 && y-i<2){
  1144. break;
  1145. }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='B' && pieces[x+i][y-i]!='Q')){
  1146. checkWhite=0;
  1147. checkMateWhite=0;
  1148. break;
  1149. }
  1150. else if(pieces[x+i][y-i]=='B'||pieces[x+i][y-i]=='Q'){
  1151. checkWhite=1;
  1152. checkMateWhite=1;
  1153. return;
  1154. }
  1155. }
  1156. }
  1157. }
  1158.  
  1159. void fullKingCheckBlack(int x,int y){
  1160. if ((pieces[x-1][y+2]=='n')||(pieces[x+1][y+2]=='n')||(pieces[x-1][y-2]=='n')||(pieces[x+1][y-2]=='n')||(pieces[x+2][y-1]=='n')||(pieces[x+2][y+1]=='n')||(pieces[x-2][y-1]=='n')||(pieces[x-2][y+1]=='n')){
  1161. checkBlack=1;
  1162. checkMateBlack=1;
  1163. return;
  1164. }
  1165. else if ((pieces[x+1][y-1]=='p')||(pieces[x+1][y+1]=='p')){
  1166. checkBlack=1;
  1167. checkMateBlack=1;
  1168. return;
  1169. }
  1170. else if ((pieces[x+1][y]=='k')||(pieces[x-1][y]=='k')||(pieces[x][y+1]=='k')||(pieces[x][y-1]=='k')||(pieces[x+1][y+1]=='k')||(pieces[x-1][y-1]=='k')||(pieces[x+1][y-1]=='k')||(pieces[x-1][y+1]=='k')){
  1171. checkBlack=1;
  1172. checkMateBlack=1;
  1173. return;
  1174. }
  1175. else{
  1176. for(i=1; i<=7;i++){
  1177. if(x+i>9){
  1178. break;
  1179. }
  1180. else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='r' && pieces[x+i][y]!='q')){
  1181. checkBlack=0;
  1182. checkMateBlack=0;
  1183. break;
  1184. }
  1185. else if(pieces[x+i][y]=='r'||pieces[x+i][y]=='q'){
  1186. checkBlack=1;
  1187. checkMateBlack=1;
  1188. return;
  1189. }
  1190. }
  1191. for(i=1; i<=7;i++){
  1192. if(x-i<2){
  1193. break;
  1194. }
  1195. else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='r' && pieces[x-i][y]!='q')){
  1196. checkBlack=0;
  1197. checkMateBlack=0;
  1198. break;
  1199. }
  1200. else if(pieces[x-i][y]=='r'||pieces[x-i][y]=='q'){
  1201. checkBlack=1;
  1202. checkMateBlack=1;
  1203. return;
  1204. }
  1205. }
  1206. for(i=1; i<=7;i++){
  1207. if(y+i>9){
  1208. break;
  1209. }
  1210. else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='r' && pieces[x][y+i]!='q')){
  1211. checkBlack=0;
  1212. checkMateBlack=0;
  1213. break;
  1214. }
  1215. else if(pieces[x][y+i]=='r'||pieces[x][y+i]=='q'){
  1216. checkBlack=1;
  1217. checkMateBlack=1;
  1218. return;
  1219. }
  1220. }
  1221. for(i=1; i<=7;i++){
  1222. if(y-i<2){
  1223. break;
  1224. }
  1225. else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='r' && pieces[x][y-i]!='q')){
  1226. checkBlack=0;
  1227. checkMateBlack=0;
  1228. break;
  1229. }
  1230. else if(pieces[x][y-i]=='r'||pieces[x][y-i]=='q'){
  1231. checkBlack=1;
  1232. checkMateBlack=1;
  1233. return;
  1234. }
  1235. }
  1236. for(i=1; i<=7;i++){
  1237. if(x+i>9 && y+i>9){
  1238. break;
  1239. }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='b' && pieces[x+i][y+i]!='q')){
  1240. checkBlack=0;
  1241. checkMateBlack=0;
  1242. break;
  1243. }
  1244. else if(pieces[x+i][y+i]=='b'||pieces[x+i][y+i]=='q'){
  1245. checkBlack=1;
  1246. checkMateBlack=1;
  1247. return;
  1248. }
  1249. }
  1250. for(i=1; i<=7;i++){
  1251. if(x-i<2 && y+i>9){
  1252. break;
  1253. }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='b' && pieces[x-i][y+i]!='q')){
  1254. checkBlack=0;
  1255. checkMateBlack=0;
  1256. break;
  1257. }
  1258. else if(pieces[x-i][y+i]=='b'||pieces[x-i][y+i]=='q'){
  1259. checkBlack=1;
  1260. checkMateBlack=1;
  1261. return;
  1262. }
  1263. }
  1264. for(i=1; i<=7;i++){
  1265. if(x-i<2 && y-i<2){
  1266. break;
  1267. }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='b' && pieces[x-i][y-i]!='q')){
  1268. checkBlack=0;
  1269. checkMateBlack=0;
  1270. break;
  1271. }
  1272. else if(pieces[x-i][y-i]=='b'||pieces[x-i][y-i]=='q'){
  1273. checkBlack=1;
  1274. checkMateBlack=1;
  1275. return;
  1276. }
  1277. }
  1278. for(i=1; i<=7;i++){
  1279. if(x+i>9 && y-i<2){
  1280. break;
  1281. }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='b' && pieces[x+i][y-i]!='q')){
  1282. checkBlack=0;
  1283. checkMateBlack=0;
  1284. break;
  1285. }
  1286. else if(pieces[x+i][y-i]=='b'||pieces[x+i][y-i]=='q'){
  1287. checkBlack=1;
  1288. checkMateBlack=1;
  1289. return;
  1290. }
  1291. }
  1292. }
  1293. }
  1294.  
  1295. void kingWhiteCheckMate(){ //check if own king is in checkmate
  1296. checkMateWhite=1;
  1297. //check if all movements available for the king are in check
  1298. if ((pieces[king1x+1][king1y]<'a')&&(king1x+1>1)&&(king1x+1<10)){
  1299. fullKingCheckWhite(king1x+1,king1y);
  1300. }
  1301. if ((checkMateWhite==1)&&(pieces[king1x][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)){
  1302. fullKingCheckWhite(king1x,king1y+1);
  1303. }
  1304. if ((checkMateWhite==1)&&(pieces[king1x+1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x+1>1)&&(king1x+1<10)){
  1305. fullKingCheckWhite(king1x+1,king1y+1);
  1306. }
  1307. if ((checkMateWhite==1)&&(pieces[king1x-1][king1y]<'a')&&(king1x-1>1)&&(king1x-1<10)){
  1308. fullKingCheckWhite(king1x-1,king1y);
  1309. }
  1310. if ((checkMateWhite==1)&&(pieces[king1x][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)){
  1311. fullKingCheckWhite(king1x,king1y-1);
  1312. }
  1313. if ((checkMateWhite==1)&&(pieces[king1x-1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x-1>1)&&(king1x-1<10)){
  1314. fullKingCheckWhite(king1x-1,king1y-1);
  1315. }
  1316. if ((checkMateWhite==1)&&(pieces[king1x+1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x+1>1)&&(king1x+1<10)){
  1317. fullKingCheckWhite(king1x+1,king1y-1);
  1318. }
  1319. if ((checkMateWhite==1)&&(pieces[king1x-1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x-1>1)&&(king1x-1<10)){
  1320. fullKingCheckWhite(king1x-1,king1y+1);
  1321. }
  1322. if (checkMateWhite==1){
  1323. fullPieceCheckBlack(index[2],index[3]);//check if any owned piece can kill the opposite piece so own king becomes not in check
  1324. if (checkMateBlack==0){
  1325. checkMateBlackPath(king1x,king1y);//check if any owned piece can be in the path of opposite checking piece so own king becomes not in check
  1326. }
  1327. }
  1328. }
  1329.  
  1330. void kingBlackCheckMate(){
  1331. checkMateWhite=1;
  1332. if (((pieces[king2x+1][king2y]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2x+1>1)&&(king2x+1<10)){
  1333. fullKingCheckBlack(king2x+1,king2y);
  1334. }
  1335. if ((checkMateBlack==1)&&((pieces[king2x][king2y+1]>='a')||(pieces[king2x][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)){
  1336. fullKingCheckBlack(king2x,king2y+1);
  1337. }
  1338. if ((checkMateBlack==1)&&((pieces[king2x+1][king2y+1]>='a')||(pieces[king2x+1][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x+1>1)&&(king2x+1<10)){
  1339. fullKingCheckBlack(king2x+1,king2y+1);
  1340. }
  1341. if ((checkMateBlack==1)&&((pieces[king2x-1][king2y]>='a')||(pieces[king2x-1][king2y]=='\0'))&&(king2x-1>1)&&(king2x-1<10)){
  1342. fullKingCheckBlack(king2x-1,king2y);
  1343. }
  1344. if ((checkMateBlack==1)&&((pieces[king2x][king2y-1]>='a')||(pieces[king2x][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)){
  1345. fullKingCheckBlack(king2x,king2y-1);
  1346. }
  1347. if ((checkMateBlack==1)&&((pieces[king2x-1][king2y-1]>='a')||(pieces[king2x-1][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x-1>1)&&(king2x-1<10)){
  1348. fullKingCheckBlack(king2x-1,king2y-1);
  1349. }
  1350. if ((checkMateBlack==1)&&((pieces[king2x+1][king2y-1]>='a')||(pieces[king2x+1][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x+1>1)&&(king2x+1<10)){
  1351. fullKingCheckBlack(king2x+1,king2y-1);
  1352. }
  1353. if ((checkMateBlack==1)&&((pieces[king2x-1][king2y+1]>='a')||(pieces[king2x-1][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x-1>1)&&(king2x-1<10)){
  1354. fullKingCheckBlack(king2x-1,king2y+1);
  1355. }
  1356. if (checkMateBlack==1){
  1357. fullPieceCheckWhite(index[2],index[3]);
  1358. if (checkMateWhite==0){
  1359. checkMateWhitePath(king2x,king2y);
  1360. }
  1361. }
  1362. }
  1363.  
  1364. void checkMateWhitePath(int x,int y){ //check if any possible path for his own piece has opposite piece threatens his own king
  1365. //vertical path
  1366. if((index[3]-y)==0 && (index[2]-x>0)){
  1367. for (k=1;k<index[2]-x;k++){
  1368. fullKingCheckBlack(x+k,y);
  1369. if (checkMateBlack==1){
  1370. break;
  1371. }
  1372. }
  1373. }
  1374. else if ((index[3]-y)==0 && (x-index[2]>0)){
  1375. for (k=1;k<x-index[2];k++){
  1376. fullKingCheckBlack(x-k,y);
  1377. if (checkMateBlack==1){
  1378. break;
  1379. }
  1380. }
  1381. }
  1382. //horizontal path
  1383. else if ((index[3]-y)>0 && (index[2]-x==0)){
  1384. for (k=1;k<index[3]-y;k++){
  1385. fullKingCheckBlack(x,y+k);
  1386. if (checkMateBlack==1){
  1387. break;
  1388. }
  1389. }
  1390. }
  1391. else if ((y-index[3])>0 && (index[2]-x==0)){
  1392. for (k=1;k<y-index[3];k++){
  1393. fullKingCheckBlack(x,y-k);
  1394. if (checkMateBlack==1){
  1395. break;
  1396. }
  1397. }
  1398. }
  1399. //diagonal path
  1400. else if (((x-index[2])==(y-index[3]))&&((x-index[2])>0)){
  1401. for (k=1;k<x-index[2];k++){
  1402. fullKingCheckBlack(x-k,y-k);
  1403. if (checkMateBlack==1){
  1404. break;
  1405. }
  1406. }
  1407. }
  1408. else if (((x-index[2])==(y-index[3]))&&((index[2]-x)>0)){
  1409. for (k=1;k<index[2]-x;k++){
  1410. fullKingCheckBlack(x+k,y+k);
  1411. if (checkMateBlack==1){
  1412. break;
  1413. }
  1414. }
  1415. }
  1416. else if (((x-index[2])==(index[3]-y))&&((x-index[2])>0)){
  1417. for (k=1;k<x-index[2];k++){
  1418. fullKingCheckBlack(x-k,y+k);
  1419. if (checkMateBlack==1){
  1420. break;
  1421. }
  1422. }
  1423. }
  1424. else if (((x-index[2])==(index[3]-y))&&((index[2]-x)>0)){
  1425. for (k=1;k<index[0]-index[2];k++){
  1426. fullKingCheckBlack(x+k,y-k);
  1427. if (checkMateBlack==1){
  1428. break;
  1429. }
  1430. }
  1431. }
  1432. }
  1433.  
  1434. void checkMateBlackPath(int x,int y){
  1435. if((index[3]-y)==0 && (index[2]-x>0)){
  1436. for (k=1;k<index[2]-x;k++){
  1437. fullKingCheckBlack(x+k,y);
  1438. if (checkMateBlack==1){
  1439. break;
  1440. }
  1441. }
  1442. }
  1443. else if ((index[3]-y)==0 && (x-index[2]>0)){
  1444. for (k=1;k<x-index[2];k++){
  1445. fullKingCheckBlack(x-k,y);
  1446. if (checkMateBlack==1){
  1447. break;
  1448. }
  1449. }
  1450. }
  1451. else if ((index[3]-y)>0 && (index[2]-x==0)){
  1452. for (k=1;k<index[3]-y;k++){
  1453. fullKingCheckBlack(x,y+k);
  1454. if (checkMateBlack==1){
  1455. break;
  1456. }
  1457. }
  1458. }
  1459. else if ((y-index[3])>0 && (index[2]-x==0)){
  1460. for (k=1;k<y-index[3];k++){
  1461. fullKingCheckBlack(x,y-k);
  1462. if (checkMateBlack==1){
  1463. break;
  1464. }
  1465. }
  1466. }
  1467. else if (((x-index[2])==(y-index[3]))&&((x-index[2])>0)){
  1468. for (k=1;k<x-index[2];k++){
  1469. fullKingCheckBlack(x-k,y-k);
  1470. if (checkMateBlack==1){
  1471. break;
  1472. }
  1473. }
  1474. }
  1475. else if (((x-index[2])==(y-index[3]))&&((index[2]-x)>0)){
  1476. for (k=1;k<index[2]-x;k++){
  1477. fullKingCheckBlack(x+k,y+k);
  1478. if (checkMateBlack==1){
  1479. break;
  1480. }
  1481. }
  1482. }
  1483. else if (((x-index[2])==(index[3]-y))&&((x-index[2])>0)){
  1484. for (k=1;k<x-index[2];k++){
  1485. fullKingCheckBlack(x-k,y+k);
  1486. if (checkMateBlack==1){
  1487. break;
  1488. }
  1489. }
  1490. }
  1491. else if (((x-index[2])==(index[3]-y))&&((index[2]-x)>0)){
  1492. for (k=1;k<index[0]-index[2];k++){
  1493. fullKingCheckBlack(x+k,y-k);
  1494. if (checkMateBlack==1){
  1495. break;
  1496. }
  1497. }
  1498. }
  1499. }
  1500.  
  1501. void kingWhiteStaleMate(){ //check if his own king is in stalemate
  1502. checkWhite=1;
  1503. checkMateBlack=1;
  1504. //check if all possible movements for the king will put him in check
  1505. if ((pieces[king1x+1][king1y]<'a')&&(king1x+1>1)&&(king1x+1<10)){
  1506. fullKingCheckWhite(king1x+1,king1y);
  1507. }
  1508. if ((checkWhite==1)&&(pieces[king1x][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)){
  1509. fullKingCheckWhite(king1x,king1y+1);
  1510. }
  1511. if ((checkWhite==1)&&(pieces[king1x+1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x+1>1)&&(king1x+1<10)){
  1512. fullKingCheckWhite(king1x+1,king1y+1);
  1513. }
  1514. if ((checkWhite==1)&&(pieces[king1x-1][king1y]<'a')&&(king1x-1>1)&&(king1x-1<10)){
  1515. fullKingCheckWhite(king1x-1,king1y);
  1516. }
  1517. if ((checkWhite==1)&&(pieces[king1x][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)){
  1518. fullKingCheckWhite(king1x,king1y-1);
  1519. }
  1520. if ((checkWhite==1)&&(pieces[king1x-1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x-1>1)&&(king1x-1<10)){
  1521. fullKingCheckWhite(king1x-1,king1y-1);
  1522. }
  1523. if ((checkWhite==1)&&(pieces[king1x+1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x+1>1)&&(king1x+1<10)){
  1524. fullKingCheckWhite(king1x+1,king1y-1);
  1525. }
  1526. if ((checkWhite==1)&&(pieces[king1x-1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x-1>1)&&(king1x-1<10)){
  1527. fullKingCheckWhite(king1x-1,king1y+1);
  1528. }
  1529. if (checkWhite==1){
  1530. checkMateBlack=0;
  1531. //check if any of his own pieces can move
  1532. for (k=2;k<=9;k++){
  1533. for (l=2;l<=9;l++){
  1534. if (pieces[k][l]<'a'){
  1535. fullPieceCheckBlack(k,l);
  1536. if (checkMateBlack==1){
  1537. return;
  1538. }
  1539. }
  1540. }
  1541. }
  1542. }
  1543. }
  1544.  
  1545. void kingBlackStaleMate(){
  1546. checkBlack=1;
  1547. checkMateWhite=1;
  1548. if (((pieces[king2x+1][king2y]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2x+1>1)&&(king2x+1<10)){
  1549. fullKingCheckBlack(king2x+1,king2y);
  1550. }
  1551. if ((checkBlack==1)&&((pieces[king2x][king2y+1]>='a')||(pieces[king2x][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)){
  1552. fullKingCheckBlack(king2x,king2y+1);
  1553. }
  1554. if ((checkBlack==1)&&((pieces[king2x+1][king2y+1]>='a')||(pieces[king2x+1][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x+1>1)&&(king2x+1<10)){
  1555. fullKingCheckBlack(king2x+1,king2y+1);
  1556. }
  1557. if ((checkBlack==1)&&((pieces[king2x-1][king2y]>='a')||(pieces[king2x-1][king2y]=='\0'))&&(king2x-1>1)&&(king2x-1<10)){
  1558. fullKingCheckBlack(king2x-1,king2y);
  1559. }
  1560. if ((checkBlack==1)&&((pieces[king2x][king2y-1]>='a')||(pieces[king2x][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)){
  1561. fullKingCheckBlack(king2x,king2y-1);
  1562. }
  1563. if ((checkBlack==1)&&((pieces[king2x-1][king2y-1]>='a')||(pieces[king2x-1][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x-1>1)&&(king2x-1<10)){
  1564. fullKingCheckBlack(king2x-1,king2y-1);
  1565. }
  1566. if ((checkBlack==1)&&((pieces[king2x+1][king2y-1]>='a')||(pieces[king2x+1][king2y-1]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x+1>1)&&(king2x+1<10)){
  1567. fullKingCheckBlack(king2x+1,king2y-1);
  1568. }
  1569. if ((checkBlack==1)&&((pieces[king2x-1][king2y+1]>='a')||(pieces[king2x-1][king2y+1]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x-1>1)&&(king2x-1<10)){
  1570. fullKingCheckBlack(king2x-1,king2y+1);
  1571. }
  1572. if (checkBlack==1){
  1573. checkMateWhite=0;
  1574. for (k=2;k<=9;k++){
  1575. for (l=2;l<=9;l++){
  1576. if ((pieces[k][l]=='\0')||(pieces[k][l]>='a')){
  1577. fullPieceCheckWhite(k,l);
  1578. if (checkMateWhite==1){
  1579. return;
  1580. }
  1581. }
  1582. }
  1583. }
  1584. }
  1585. }
  1586.  
  1587. void fullPieceCheckBlack(int x,int y){//same as fullKingCheck
  1588. if ((pieces[x-1][y+2]=='n')||(pieces[x+1][y+2]=='n')||(pieces[x-1][y-2]=='n')||(pieces[x+1][y-2]=='n')||(pieces[x+2][y-1]=='n')||(pieces[x+2][y+1]=='n')||(pieces[x-2][y-1]=='n')||(pieces[x-2][y+1]=='n')){
  1589. checkBlack=1;
  1590. checkMateBlack=1;
  1591. return;
  1592. }
  1593. else if (((pieces[x+1][y-1]=='p')||(pieces[x+1][y+1]=='p'))&&(pieces[x][y]>='A')&&(pieces[x][y]<='Z')){
  1594. checkBlack=1;
  1595. checkMateBlack=1;
  1596. return;
  1597. }
  1598. else if ((pieces[x+1][y]=='p')&&(pieces[x][y]=='\0')){
  1599. checkBlack=1;
  1600. checkMateBlack=1;
  1601. return;
  1602. }
  1603. else{
  1604. for(i=1; i<=7;i++){
  1605. if(x+i>9){
  1606. break;
  1607. }
  1608. else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='r' && pieces[x+i][y]!='q')){
  1609. checkBlack=0;
  1610. checkMateBlack=0;
  1611. break;
  1612. }
  1613. else if(pieces[x+i][y]=='r'||pieces[x+i][y]=='q'){
  1614. checkBlack=1;
  1615. checkMateBlack=1;
  1616. return;
  1617. }
  1618. }
  1619. for(i=1; i<=7;i++){
  1620. if(x-i<2){
  1621. break;
  1622. }
  1623. else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='r' && pieces[x-i][y]!='q')){
  1624. checkBlack=0;
  1625. checkMateBlack=0;
  1626. break;
  1627. }
  1628. else if(pieces[x-i][y]=='r'||pieces[x-i][y]=='q'){
  1629. checkBlack=1;
  1630. checkMateBlack=1;
  1631. return;
  1632. }
  1633. }
  1634. for(i=1; i<=7;i++){
  1635. if(y+i>9){
  1636. break;
  1637. }
  1638. else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='r' && pieces[x][y+i]!='q')){
  1639. checkBlack=0;
  1640. checkMateBlack=0;
  1641. break;
  1642. }
  1643. else if(pieces[x][y+i]=='r'||pieces[x][y+i]=='q'){
  1644. checkBlack=1;
  1645. checkMateBlack=1;
  1646. return;
  1647. }
  1648. }
  1649. for(i=1; i<=7;i++){
  1650. if(y-i<2){
  1651. break;
  1652. }
  1653. else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='r' && pieces[x][y-i]!='q')){
  1654. checkBlack=0;
  1655. checkMateBlack=0;
  1656. break;
  1657. }
  1658. else if(pieces[x][y-i]=='r'||pieces[x][y-i]=='q'){
  1659. checkBlack=1;
  1660. checkMateBlack=1;
  1661. return;
  1662. }
  1663. }
  1664. for(i=1; i<=7;i++){
  1665. if(x+i>9 && y+i>9){
  1666. break;
  1667. }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='b' && pieces[x+i][y+i]!='q')){
  1668. checkBlack=0;
  1669. checkMateBlack=0;
  1670. break;
  1671. }
  1672. else if(pieces[x+i][y+i]=='b'||pieces[x+i][y+i]=='q'){
  1673. checkBlack=1;
  1674. checkMateBlack=1;
  1675. return;
  1676. }
  1677. }
  1678. for(i=1; i<=7;i++){
  1679. if(x-i<2 && y+i>9){
  1680. break;
  1681. }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='b' && pieces[x-i][y+i]!='q')){
  1682. checkBlack=0;
  1683. checkMateBlack=0;
  1684. break;
  1685. }
  1686. else if(pieces[x-i][y+i]=='b'||pieces[x-i][y+i]=='q'){
  1687. checkBlack=1;
  1688. checkMateBlack=1;
  1689. return;
  1690. }
  1691. }
  1692. for(i=1; i<=7;i++){
  1693. if(x-i<2 && y-i<2){
  1694. break;
  1695. }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='b' && pieces[x-i][y-i]!='q')){
  1696. checkBlack=0;
  1697. checkMateBlack=0;
  1698. break;
  1699. }
  1700. else if(pieces[x-i][y-i]=='b'||pieces[x-i][y-i]=='q'){
  1701. checkBlack=1;
  1702. checkMateBlack=1;
  1703. return;
  1704. }
  1705. }
  1706. for(i=1; i<=7;i++){
  1707. if(x+i>9 && y-i<2){
  1708. break;
  1709. }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='b' && pieces[x+i][y-i]!='q')){
  1710. checkBlack=0;
  1711. checkMateBlack=0;
  1712. break;
  1713. }
  1714. else if(pieces[x+i][y-i]=='b'||pieces[x+i][y-i]=='q'){
  1715. checkBlack=1;
  1716. checkMateBlack=1;
  1717. return;
  1718. }
  1719. }
  1720. }
  1721. }
  1722.  
  1723. void fullPieceCheckWhite(int x,int y){
  1724. if ((pieces[x-1][y+2]=='N')||(pieces[x+1][y+2]=='N')||(pieces[x-1][y-2]=='N')||(pieces[x+1][y-2]=='N')||(pieces[x+2][y-1]=='N')||(pieces[x+2][y+1]=='N')||(pieces[x-2][y-1]=='N')||(pieces[x-2][y+1]=='N')){
  1725. checkWhite=1;
  1726. checkMateWhite=1;
  1727. return;
  1728. }
  1729. else if (((pieces[x-1][y-1]=='P')||(pieces[x-1][y+1]=='p'))&&(pieces[x][y]>='a')&&(pieces[x][y]<='z')){
  1730. checkBlack=1;
  1731. checkMateBlack=1;
  1732. return;
  1733. }
  1734. else if ((pieces[x-1][y]=='P')&&(pieces[x][y]=='\0')){
  1735. checkBlack=1;
  1736. checkMateBlack=1;
  1737. return;
  1738. }
  1739. else{
  1740. for(i=1; i<=7;i++){
  1741. if(x+i>9){
  1742. break;
  1743. }
  1744. else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='R' && pieces[x+i][y]!='Q')){
  1745. checkWhite=0;
  1746. checkMateWhite=0;
  1747. break;
  1748. }
  1749. else if(pieces[x+i][y]=='R'||pieces[x+i][y]=='Q'){
  1750. checkWhite=1;
  1751. checkMateWhite=1;
  1752. return;
  1753. }
  1754. }
  1755. for(i=1; i<=7;i++){
  1756. if(x-i<2){
  1757. break;
  1758. }
  1759. else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='R' && pieces[x-i][y]!='Q')){
  1760. checkWhite=0;
  1761. checkMateWhite=0;
  1762. break;
  1763. }
  1764. else if(pieces[x-i][y]=='R'||pieces[x-i][y]=='Q'){
  1765. checkWhite=1;
  1766. checkMateWhite=1;
  1767. return;
  1768. }
  1769. }
  1770. for(i=1; i<=7;i++){
  1771. if(y+i>9){
  1772. break;
  1773. }
  1774. else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='R' && pieces[x][y+i]!='Q')){
  1775. checkWhite=0;
  1776. checkMateWhite=0;
  1777. break;
  1778. }
  1779. else if(pieces[x][y+i]=='R'||pieces[x][y+i]=='Q'){
  1780. checkWhite=1;
  1781. checkMateWhite=1;
  1782. return;
  1783. }
  1784. }
  1785. for(i=1; i<=7;i++){
  1786. if(y-i<2){
  1787. break;
  1788. }
  1789. else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='R' && pieces[x][y-i]!='Q')){
  1790. checkWhite=0;
  1791. checkMateWhite=0;
  1792. break;
  1793. }
  1794. else if(pieces[x][y-i]=='R'||pieces[x][y-i]=='Q'){
  1795. checkWhite=1;
  1796. checkMateWhite=1;
  1797. return;
  1798. }
  1799. }
  1800. for(i=1; i<=7;i++){
  1801. if(x+i>9 && y+i>9){
  1802. break;
  1803. }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='B' && pieces[x+i][y+i]!='Q')){
  1804. checkWhite=0;
  1805. checkMateWhite=0;
  1806. break;
  1807. }
  1808. else if(pieces[x+i][y+i]=='B'||pieces[x+i][y+i]=='Q'){
  1809. checkWhite=1;
  1810. checkMateWhite=1;
  1811. return;
  1812. }
  1813. }
  1814. for(i=1; i<=7;i++){
  1815. if(x-i<2 && y+i>9){
  1816. break;
  1817. }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='B' && pieces[x-i][y+i]!='Q')){
  1818. checkWhite=0;
  1819. checkMateWhite=0;
  1820. break;
  1821. }
  1822. else if(pieces[x-i][y+i]=='B'||pieces[x-i][y+i]=='Q'){
  1823. checkWhite=1;
  1824. checkMateWhite=1;
  1825. return;
  1826. }
  1827. }
  1828. for(i=1; i<=7;i++){
  1829. if(x-i<2 && y-i<2){
  1830. break;
  1831. }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='B' && pieces[x-i][y-i]!='Q')){
  1832. checkWhite=0;
  1833. checkMateWhite=0;
  1834. break;
  1835. }
  1836. else if(pieces[x-i][y-i]=='B'||pieces[x-i][y-i]=='Q'){
  1837. checkWhite=1;
  1838. checkMateWhite=1;
  1839. return;
  1840. }
  1841. }
  1842. for(i=1; i<=7;i++){
  1843. if(x+i>9 && y-i<2){
  1844. break;
  1845. }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='B' && pieces[x+i][y-i]!='Q')){
  1846. checkWhite=0;
  1847. checkMateWhite=0;
  1848. break;
  1849. }
  1850. else if(pieces[x+i][y-i]=='B'||pieces[x+i][y-i]=='Q'){
  1851. checkWhite=1;
  1852. checkMateWhite=1;
  1853. return;
  1854. }
  1855. }
  1856. }
  1857. }
  1858.  
  1859. void gameOver(){
  1860. printf("For new game press \"N\" / to load a game press \"L\" / to exit press \"E\".\n");
  1861. do {readLine(play,5);}
  1862. while ((play[1]!='\0')||((play[0]!='N')&&(play[0]!='L')&&(play[0]!='E')));
  1863. }
  1864.  
  1865. void saveGame(){
  1866. printf("Enter the file name.\n");
  1867. char saveName[100];
  1868. readLine(saveName,100);
  1869. fPointer = fopen(saveName,"w");
  1870. //saving the current turn
  1871. fputc(turn,fPointer);
  1872. //saving pieces
  1873. for (i=2;i<=9;i++){
  1874. for (j=2;j<=9;j++){
  1875. fputc(pieces[i][j],fPointer);
  1876. }
  1877. }
  1878. //saving dead pieces
  1879. for (i=0;i<37;i++){
  1880. fputc(deadPieces[i],fPointer);
  1881. }
  1882. //saving undo
  1883. for (i=0;i<500;i++){
  1884. for (j=0;j<5;j++){
  1885. fputc(undo[i][j],fPointer);
  1886. }
  1887. }
  1888. //saving redo
  1889. for (i=0;i<500;i++){
  1890. for (j=0;j<4;j++){
  1891. fputc(redo[i][j],fPointer);
  1892. }
  1893. }
  1894. //saving deadPieces index
  1895. fputc(died,fPointer);
  1896. fclose(fPointer);
  1897. printf("Game successfully saved.\n");
  1898. }
  1899.  
  1900. void loadGame(){
  1901. //loading turn
  1902. turn=fgetc(fPointer);
  1903. //load pieces
  1904. for (i=2;i<=9;i++){
  1905. for (j=2;j<=9;j++){
  1906. pieces[i][j]=fgetc(fPointer);
  1907. }
  1908. }
  1909. //load deadPieces
  1910. for (i=0;i<37;i++){
  1911. deadPieces[i]=fgetc(fPointer);
  1912. }
  1913. //load undo
  1914. for (i=0;i<500;i++){
  1915. for (j=0;j<5;j++){
  1916. undo[i][j]=fgetc(fPointer);
  1917. }
  1918. }
  1919. //load redo
  1920. for (i=0;i<500;i++){
  1921. for (j=0;j<4;j++){
  1922. redo[i][j]=fgetc(fPointer);
  1923. }
  1924. }
  1925. //load diedIndex
  1926. died=fgetc(fPointer);
  1927. fclose(fPointer);
  1928. printGame();
  1929. printf("Game loaded successfully.\n");
  1930. fullKingCheckBlack(king2x,king2y);
  1931. if (checkBlack==1){
  1932. printf("Check black.\n");
  1933. }
  1934. fullKingCheckWhite(king1x,king1y);
  1935. if (checkWhite==1){
  1936. printf("Check white.\n");
  1937. }
  1938. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement