Advertisement
Seif45

Untitled

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