Advertisement
Seif45

Untitled

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