Advertisement
Seif45

7ARAAAAAAAAAAAAAM

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