Advertisement
Seif45

Final Project

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