Advertisement
Seif45

Untitled

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