Advertisement
Seif45

Checkmate protection

Dec 13th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.79 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. char play[5];
  9. int index[4];
  10. int died=1;
  11. int diedLast;
  12. int i,j;
  13. int valid;
  14. int turn;
  15. int king1x, king1y, king2x, king2y;
  16. int checkWhite, checkBlack;
  17. int checkMateBlack,checkMateWhite;
  18. void checkMateWhitePath(int x,int y);
  19.  
  20. void readLine()
  21. {
  22. int ch, i=0;
  23. while((ch=getchar()) != '\n'){
  24. if(i<4)
  25. {
  26. play[i++]=ch;
  27. }
  28. }
  29. play[i]='\0';
  30. }
  31.  
  32. void gameBoardBorders();
  33. void checkerBoard();
  34. void startingChessBoard();
  35. void printGame ();
  36. void scanAndConvert();
  37. void checkValidMove();
  38. void swapElements ();
  39. void reverseSwapElement();
  40. void rook();
  41. void knight();
  42. void king();
  43. void king();
  44. void bishop();
  45. void queen();
  46. void pawn();
  47. void moveSet();
  48. void fullKingCheckWhite(int x,int y);
  49. void fullKingCheckBlack(int x,int y);
  50. void king1CheckMate();
  51. void king2CheckMate();
  52. void checkMateBlackPath(int x,int y);
  53.  
  54. int main(){
  55. startingChessBoard();
  56. gameBoardBorders();
  57. checkerBoard();
  58. printGame();
  59. king1x=9, king1y=6, king2x=2, king2y=6;
  60. turn=1;
  61. diedLast=0;
  62. checkWhite=0;
  63. checkBlack=0;
  64. while (1){
  65. valid = 0;
  66. checkMateBlack=-1;checkMateWhite=-1;
  67. if (turn==1){
  68. while ((valid==0)||(checkWhite==1)){
  69. printf("\nWhite's turn: ");
  70. scanAndConvert();
  71. if(valid==1){
  72. checkValidMove();
  73. }
  74. if(valid==1){
  75. moveSet();
  76. }
  77. if (valid==1){
  78. swapElements();
  79. }
  80. if((valid==1)&&(checkWhite==1)){
  81. fullKingCheckWhite(king1x,king1y);
  82. }
  83. if ((valid==1)&&(checkWhite==1)){
  84. printf("Invalid move.");
  85. reverseSwapElement();
  86. }
  87. }
  88. printf("\n");
  89. printGame();
  90. fullKingCheckBlack(king2x,king2y);
  91. if (checkMateBlack==1){
  92. king2CheckMate();
  93. if (checkMateWhite==0){
  94. printf("Check mate.\n");
  95. exit(EXIT_SUCCESS);
  96. }
  97. }
  98. fullKingCheckBlack(king2x,king2y);
  99. if (checkBlack==1){
  100. printf("Check black.\n");
  101. }
  102. turn=2;
  103. }
  104. else {
  105. while ((valid==0)||(checkBlack==1)){
  106. printf("\nBlack's turn: ");
  107. scanAndConvert();
  108. if(valid==1){
  109. checkValidMove();
  110. }
  111. if(valid==1){
  112. moveSet();
  113. }
  114. if(valid==1){
  115. swapElements();
  116. }
  117. if(checkBlack==1 && valid==1){
  118. fullKingCheckBlack(king2x,king2y);
  119. }
  120. if(checkBlack==1 && valid==1){
  121. printf("Invalid move.");
  122. reverseSwapElement();
  123. }
  124. }
  125. printf("\n");
  126. printGame();
  127. fullKingCheckWhite(king1x,king1y);
  128. if (checkMateWhite==1){
  129. king1CheckMate();
  130. if (checkMateBlack==0){
  131. printf("Check mate.\n");
  132. exit(EXIT_SUCCESS);
  133. }
  134. }
  135. fullKingCheckWhite(king1x,king1y);
  136. if (checkWhite==1){
  137. printf("Check white.\n");
  138. }
  139. turn=1;
  140. }
  141. }
  142. return 0;
  143. }
  144.  
  145. void gameBoardBorders(){
  146. for (i=0;i<12;i++){
  147. for (j=0;j<12;j++){
  148. gameBoard[i][j]='\0';
  149. }
  150. }
  151. for (i=0;i<12;i+=11){
  152. for (j=2;j<10;j++){
  153. gameBoard[i][j]='A'+(j-2);
  154. }
  155. }
  156. for (j=2;j<10;j++){
  157. gameBoard[1][j]='-';
  158. gameBoard[10][j]='-';
  159. }
  160. for (i=2;i<10;i++){
  161. for (j=0;j<12;j+=11){
  162. gameBoard[i][j]='8'-(i-2);
  163. }
  164. }
  165. for (i=2;i<10;i++){
  166. gameBoard[i][1]='|';
  167. gameBoard[i][10]='|';
  168. }
  169.  
  170. }
  171.  
  172. void checkerBoard(){
  173. for (i=2;i<10;i++){
  174. for (j=2;j<10;j+=2){
  175. if (i%2==0){
  176. gameBoard[i][j]='.';
  177. }
  178. else {
  179. gameBoard[i][j]='_';
  180. }
  181. }
  182. }
  183. for (i=2;i<10;i++){
  184. for (j=3;j<10;j+=2){
  185. if (i%2==0){
  186. gameBoard[i][j]='_';
  187. }
  188. else {
  189. gameBoard[i][j]='.';
  190. }
  191. }
  192. }
  193. }
  194.  
  195. void startingChessBoard(){
  196. for (i=0;i<12;i++){
  197. for(j=0;j<12;j++){
  198. pieces[i][j]='\0';
  199. }
  200. }
  201. pieces[2][2]= 'R';
  202. pieces[2][3]= 'N';
  203. pieces[2][4]= 'B';
  204. pieces[2][5]= 'Q';
  205. pieces[2][6]= 'K';
  206. pieces[2][7]= 'B';
  207. pieces[2][8]= 'N';
  208. pieces[2][9]= 'R';
  209.  
  210. pieces[9][2]= 'r';
  211. pieces[9][3]= 'n';
  212. pieces[9][4]= 'b';
  213. pieces[9][5]= 'q';
  214. pieces[9][6]= 'k';
  215. pieces[9][7]= 'b';
  216. pieces[9][8]= 'n';
  217. pieces[9][9]= 'r';
  218.  
  219. for(j=2;j<10;j++){
  220. pieces[3][j]= 'P';
  221. }
  222.  
  223. for(j=2;j<10;j++){
  224. pieces[8][j]= 'p';
  225. }
  226. for(i=0;i<36;i++){
  227. deadPieces[i]='\0';
  228. }
  229. }
  230.  
  231. void printGame (){
  232. system("cls");
  233. printf("\n\t\t\t\t\t\tDead\n\n");
  234. for (i=0;i<12;i++){
  235. for (j=0;j<12;j++){
  236. printf("%c",gameBoard[i][j]);
  237. printf("%c ", pieces[i][j]);
  238. }
  239. printf("\t\t%c %c %c", deadPieces[i+1], deadPieces[i+13], deadPieces[i+25]);
  240. printf("\n\n");
  241. }
  242. printf("\n");
  243. }
  244.  
  245. void scanAndConvert(){
  246. readLine();
  247. while (play[4]!='\0'){
  248. printf("Invalid move.");
  249. valid=0;
  250. return;
  251. }
  252. while ((play[0]<'A')||(play[0]>'H')||(play[1]<'1')||(play[1]>'8')||(play[2]<'A')||(play[2]>'H')||(play[3]<'1')||(play[3]>'8')){
  253. printf("Invalid move.");
  254. valid=0;
  255. return;
  256. }
  257. index[0]=58-play[1];
  258. index[1]=play[0]-63;
  259. index[2]=58-play[3];
  260. index[3]=play[2]-63;
  261. valid=1;
  262. }
  263.  
  264. void checkValidMove(){
  265. if (turn==1){
  266. if ((pieces[index[0]][index[1]]<'a')||(pieces[index[0]][index[1]]>'z')){
  267. printf("Invalid move.");
  268. valid=0;
  269. return;
  270. }
  271. if ((pieces[index[2]][index[3]]>='a')&&(pieces[index[2]][index[3]]<='z')){
  272. printf("Invalid move.");
  273. valid=0;
  274. return;
  275. }
  276. }
  277. else if (turn==2){
  278. if ((pieces[index[0]][index[1]]<'A')||(pieces[index[0]][index[1]]>'Z')){
  279. printf("Invalid move.");
  280. valid=0;
  281. return;
  282. }
  283. if((pieces[index[2]][index[3]]>='A')&&(pieces[index[2]][index[3]]<='Z')){
  284. printf("Invalid move.");
  285. valid=0;
  286. return;
  287. }
  288. }
  289. valid=1;
  290. }
  291.  
  292. void swapElements (){
  293. if ((pieces[index[2]][index[3]])!='\0'){
  294. deadPieces[died]= pieces[index[2]][index[3]];
  295. died++;
  296. pieces[index[2]][index[3]]='\0';
  297. diedLast=1;
  298. }else{
  299. diedLast=0;
  300. }
  301. char temp=pieces[index[0]][index[1]];
  302. pieces[index[0]][index[1]]=pieces[index[2]][index[3]];
  303. pieces[index[2]][index[3]]=temp;
  304. }
  305.  
  306. void reverseSwapElement(){
  307. if(diedLast==1){
  308. if(pieces[index[0]][index[1]]=='\0' || deadPieces[died-1]!='\0'){
  309. pieces[index[0]][index[1]]=deadPieces[died-1];
  310. died--;
  311. deadPieces[died]='\0';
  312. }
  313. }
  314. char temp=pieces[index[2]][index[3]];
  315. pieces[index[2]][index[3]]=pieces[index[0]][index[1]];
  316. pieces[index[0]][index[1]]=temp;
  317. }
  318.  
  319. void rook(){
  320. if((index[3]-index[1])==0 && (index[2]-index[0]>0)){
  321. for (i=1;i<index[2]-index[0];i++){
  322. if (pieces[index[0]+i][index[1]]!='\0'){
  323. printf("Invalid move.");
  324. valid=0;
  325. return;
  326. }
  327. }
  328. if(index[0]+i==index[2]){
  329. valid=1;
  330. }
  331. }
  332. else if ((index[3]-index[1])==0 && (index[0]-index[2]>0)){
  333. for (i=1;i<index[0]-index[2];i++){
  334. if (pieces[index[0]-i][index[1]]!='\0'){
  335. printf("Invalid move.");
  336. valid=0;
  337. return;
  338. }
  339. }
  340. if (index[0]-i==index[2]){
  341. valid=1;
  342. }
  343. }
  344. else if ((index[3]-index[1])>0 && (index[2]-index[0]==0)){
  345. for (i=1;i<index[3]-index[1];i++){
  346. if (pieces[index[0]][index[1]+i]!='\0'){
  347. printf("Invalid move.");
  348. valid=0;
  349. return;
  350. }
  351. }
  352. if(index[1]+i==index[3]){
  353. valid=1;
  354. }
  355. }
  356. else if ((index[1]-index[3])>0 && (index[2]-index[0]==0)){
  357. for (i=1;i<index[1]-index[3];i++){
  358. if (pieces[index[0]][index[1]-i]!='\0'){
  359. printf("Invalid move.");
  360. valid=0;
  361. return;
  362. }
  363. }
  364. if(index[1]-i==index[3]){
  365. valid=1;
  366. }
  367. }
  368. else{
  369. printf("Invalid move.");
  370. valid=0;
  371. return;
  372. }
  373. }
  374.  
  375. void knight(){
  376. 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)))
  377. {
  378. valid=1;
  379. }else{
  380. printf("Invalid move.");
  381. valid=0;
  382. return;
  383. }
  384. }
  385.  
  386. void king(){
  387. 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)))
  388. {
  389. if (turn==1){
  390. king1x=index[2];
  391. king1y=index[3];
  392. }
  393. else{
  394. king2x=index[2];
  395. king2y=index[3];
  396. }
  397. valid=1;
  398. }else{
  399. printf("Invalid move.");
  400. valid=0;
  401. return;
  402. }
  403. }
  404.  
  405. void bishop(){
  406. if (((index[0]-index[2])==(index[1]-index[3]))&&((index[0]-index[2])>0)){
  407. for (i=1;i<index[0]-index[2];i++){
  408. if ((pieces[index[0]-i][index[1]-i])!='\0'){
  409. printf("Invalid move.");
  410. valid=0;
  411. return;
  412. }
  413. }
  414. if ((index[0]-i==index[2])&&(index[1]-i==index[3])){
  415. valid=1;
  416. }
  417. }
  418. else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[2]-index[0])>0)){
  419. for (i=1;i<index[2]-index[0];i++){
  420. if ((pieces[index[0]+i][index[1]+i])!='\0'){
  421. printf("Invalid move.");
  422. valid=0;
  423. return;
  424. }
  425. }
  426. if ((index[0]+i==index[2])&&(index[1]+i==index[3])){
  427. valid=1;
  428. }
  429. }
  430. else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[0]-index[2])>0)){
  431. for (i=1;i<index[0]-index[2];i++){
  432. if ((pieces[index[0]-i][index[1]+i])!='\0'){
  433. printf("Invalid move.");
  434. valid=0;
  435. return;
  436. }
  437. }
  438. if ((index[0]-i==index[2])&&(index[1]+i==index[3])){
  439. valid=1;
  440. }
  441. }
  442. else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[2]-index[0])>0)){
  443. for (i=1;i<index[0]-index[2];i++){
  444. if ((pieces[index[0]+i][index[1]-i])!='\0'){
  445. printf("Invalid move.");
  446. valid=0;
  447. return;
  448. }
  449. }
  450. if ((index[0]+i==index[2])&&(index[1]-i==index[3])){
  451. valid=1;
  452. }
  453. }
  454. else{
  455. printf("Invalid move.");
  456. valid=0;
  457. return;
  458. }
  459. }
  460.  
  461. void queen(){
  462. if((index[3]-index[1])==0 && (index[2]-index[0]>0)){
  463. for (i=1;i<index[2]-index[0];i++){
  464. if (pieces[index[0]+i][index[1]]!='\0'){
  465. printf("Invalid move.");
  466. valid=0;
  467. return;
  468. }
  469. }
  470. if(index[0]+i==index[2]){
  471. valid=1;
  472. }
  473. }
  474. else if ((index[3]-index[1])==0 && (index[0]-index[2]>0)){
  475. for (i=1;i<index[0]-index[2];i++){
  476. if (pieces[index[0]-i][index[1]]!='\0'){
  477. printf("Invalid move.");
  478. valid=0;
  479. return;
  480. }
  481. }
  482. if (index[0]-i==index[2]){
  483. valid=1;
  484. }
  485. }
  486. else if ((index[3]-index[1])>0 && (index[2]-index[0]==0)){
  487. for (i=1;i<index[3]-index[1];i++){
  488. if (pieces[index[0]][index[1]+i]!='\0'){
  489. printf("Invalid move.");
  490. valid=0;
  491. return;
  492. }
  493. }
  494. if(index[1]+i==index[3]){
  495. valid=1;
  496. }
  497. }
  498. else if ((index[1]-index[3])>0 && (index[2]-index[0]==0)){
  499. for (i=1;i<index[1]-index[3];i++){
  500. if (pieces[index[0]][index[1]-i]!='\0'){
  501. printf("Invalid move.");
  502. valid=0;
  503. return;
  504. }
  505. }
  506. if(index[1]-i==index[3]){
  507. valid=1;
  508. }
  509. }
  510. else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[0]-index[2])>0)){
  511. for (i=1;i<index[0]-index[2];i++){
  512. if ((pieces[index[0]-i][index[1]-i])!='\0'){
  513. printf("Invalid move.");
  514. valid=0;
  515. return;
  516. }
  517. }
  518. if ((index[0]-i==index[2])&&(index[1]-i==index[3])){
  519. valid=1;
  520. }
  521. }
  522. else if (((index[0]-index[2])==(index[1]-index[3]))&&((index[2]-index[0])>0)){
  523. for (i=1;i<index[2]-index[0];i++){
  524. if ((pieces[index[0]+i][index[1]+i])!='\0'){
  525. printf("Invalid move.");
  526. valid=0;
  527. return;
  528. }
  529. }
  530. if ((index[0]+i==index[2])&&(index[1]+i==index[3])){
  531. valid=1;
  532. }
  533. }
  534. else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[0]-index[2])>0)){
  535. for (i=1;i<index[0]-index[2];i++){
  536. if ((pieces[index[0]-i][index[1]+i])!='\0'){
  537. printf("Invalid move.");
  538. valid=0;
  539. return;
  540. }
  541. }
  542. if ((index[0]-i==index[2])&&(index[1]+i==index[3])){
  543. valid=1;
  544. }
  545. }
  546. else if (((index[0]-index[2])==(index[3]-index[1]))&&((index[2]-index[0])>0)){
  547. for (i=1;i<index[0]-index[2];i++){
  548. if ((pieces[index[0]+i][index[1]-i])!='\0'){
  549. printf("Invalid move.");
  550. valid=0;
  551. return;
  552. }
  553. }
  554. if ((index[0]+i==index[2])&&(index[1]-i==index[3])){
  555. valid=1;
  556. }
  557. }
  558. else{
  559. printf("Invalid move.");
  560. valid=0;
  561. return;
  562. }
  563. }
  564.  
  565. void pawn(){
  566. char promote;
  567. if (pieces[index[0]][index[1]]=='p'){
  568. if(index[0]==8){
  569. if(((index[2]-index[0]==-1)||(index[2]-index[0]==-2)) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
  570. valid = 1;
  571. }
  572. else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
  573. valid = 1;
  574. }
  575. else if ((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
  576. valid=1;
  577. }
  578. else{
  579. printf("Invalid move.");
  580. valid=0;
  581. return;
  582. }
  583. }else{
  584. if((index[2]-index[0]==-1) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
  585. valid = 1;
  586. }
  587. else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
  588. valid = 1;
  589. }
  590. else{
  591. printf("Invalid move.");
  592. valid=0;
  593. return;
  594. }
  595. }
  596. if (index[2]==2){
  597. printf("Promote to: ");
  598. scanf(" %c", &promote);
  599. while(promote != 'R' && promote != 'N' && promote != 'B' && promote != 'Q' ){
  600. printf("Wrong promotion\n");
  601. printf("Promote to: ");
  602. scanf(" %c", &promote);
  603. }
  604. if(promote == 'R'){
  605. pieces[index[2]][index[3]]='r';
  606. }
  607. else if(promote == 'N'){
  608. pieces[index[2]][index[3]]='n';
  609. }
  610. else if(promote == 'B'){
  611. pieces[index[2]][index[3]]='b';
  612. }
  613. else if(promote == 'Q'){
  614. pieces[index[2]][index[3]]='q';
  615. }
  616. }
  617. }
  618. else{
  619. if(index[0]==3){
  620. if(((index[2]-index[0]==1)||(index[2]-index[0]==2)) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
  621. valid = 1;
  622. }
  623. else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==-1)&& (abs(index[3]-index[1])==1))){
  624. valid = 1;
  625. }
  626. else if ((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==1)&& (abs(index[3]-index[1])==1))){
  627. valid=1;
  628. }
  629. else{
  630. printf("Invalid move.");
  631. valid=0;
  632. return;
  633. }
  634. }else{
  635. if((index[2]-index[0]==1) && (index[3]-index[1]==0) && (pieces[index[2]][index[3]]=='\0')){
  636. valid = 1;
  637. }
  638. else if((pieces[index[2]][index[3]]!='\0')&& ((index[2]-index[0]==1)&& (abs(index[3]-index[1])==1))){
  639. valid = 1;
  640. }
  641. else{
  642. printf("Invalid move.");
  643. valid=0;
  644. return;
  645. }
  646. }
  647. if (index[2]==9){
  648. printf("Promote to: ");
  649. scanf(" %c", &promote);
  650. while(promote != 'R' && promote != 'N' && promote != 'B' && promote != 'Q' ){
  651. printf("Wrong promotion\n");
  652. printf("Promote to: ");
  653. scanf(" %c", &promote);
  654. }
  655. if(promote == 'R'){
  656. pieces[index[2]][index[3]]='R';
  657. }
  658. else if(promote == 'N'){
  659. pieces[index[2]][index[3]]='N';
  660. }
  661. else if(promote == 'B'){
  662. pieces[index[2]][index[3]]='B';
  663. }
  664. else if(promote == 'Q'){
  665. pieces[index[2]][index[3]]='Q';
  666. }
  667. }
  668. }
  669. }
  670.  
  671. void moveSet(){
  672. if ((pieces[index[0]][index[1]]=='p')||(pieces[index[0]][index[1]]=='P')){
  673. pawn();
  674. }
  675. else if ((pieces[index[0]][index[1]]=='r')||(pieces[index[0]][index[1]]=='R')){
  676. rook();
  677. }
  678. else if ((pieces[index[0]][index[1]]=='n')||(pieces[index[0]][index[1]]=='N')){
  679. knight();
  680. }
  681. else if ((pieces[index[0]][index[1]]=='b')||(pieces[index[0]][index[1]]=='B')){
  682. bishop();
  683. }
  684. else if ((pieces[index[0]][index[1]]=='q')||(pieces[index[0]][index[1]]=='Q')){
  685. queen();
  686. }
  687. else if ((pieces[index[0]][index[1]]=='k')||(pieces[index[0]][index[1]]=='K')){
  688. king();
  689. }
  690. }
  691.  
  692.  
  693. void fullKingCheckWhite(int x,int y){
  694. 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')){
  695. checkWhite=1;
  696. checkMateWhite=1;
  697. return;
  698. }
  699. else if ((pieces[x-1][y-1]=='P')||(pieces[x-1][y+1]=='P')){
  700. checkWhite=1;
  701. checkMateWhite=1;
  702. return;
  703. }
  704. else{
  705. for(i=1; i<=7;i++){
  706. if(x+i>9){
  707. break;
  708. }
  709. else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='R' && pieces[x+i][y]!='Q')){
  710. checkWhite=0;
  711. checkMateWhite=0;
  712. break;
  713. }
  714. else if(pieces[x+i][y]=='R'||pieces[x+i][y]=='Q'){
  715. checkWhite=1;
  716. checkMateWhite=1;
  717. return;
  718. }
  719. }
  720. for(i=1; i<=7;i++){
  721. if(x-i<2){
  722. break;
  723. }
  724. else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='R' && pieces[x-i][y]!='Q')){
  725. checkWhite=0;
  726. checkMateWhite=0;
  727. break;
  728. }
  729. else if(pieces[x-i][y]=='R'||pieces[x-i][y]=='Q'){
  730. checkWhite=1;
  731. checkMateWhite=1;
  732. return;
  733. }
  734. }
  735. for(i=1; i<=7;i++){
  736. if(y+i>9){
  737. break;
  738. }
  739. else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='R' && pieces[x][y+i]!='Q')){
  740. checkWhite=0;
  741. checkMateWhite=0;
  742. break;
  743. }
  744. else if(pieces[x][y+i]=='R'||pieces[x][y+i]=='Q'){
  745. checkWhite=1;
  746. checkMateWhite=1;
  747. return;
  748. }
  749. }
  750. for(i=1; i<=7;i++){
  751. if(y-i<2){
  752. break;
  753. }
  754. else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='R' && pieces[x][y-i]!='Q')){
  755. checkWhite=0;
  756. checkMateWhite=0;
  757. break;
  758. }
  759. else if(pieces[x][y-i]=='R'||pieces[x][y-i]=='Q'){
  760. checkWhite=1;
  761. checkMateWhite=1;
  762. return;
  763. }
  764. }
  765. for(i=1; i<=7;i++){
  766. if(x+i>9 && y+i>9){
  767. break;
  768. }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='B' && pieces[x+i][y+i]!='Q')){
  769. checkWhite=0;
  770. checkMateWhite=0;
  771. break;
  772. }
  773. else if(pieces[x+i][y+i]=='B'||pieces[x+i][y+i]=='Q'){
  774. checkWhite=1;
  775. checkMateWhite=1;
  776. return;
  777. }
  778. }
  779. for(i=1; i<=7;i++){
  780. if(x-i<2 && y+i>9){
  781. break;
  782. }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='B' && pieces[x-i][y+i]!='Q')){
  783. checkWhite=0;
  784. checkMateWhite=0;
  785. break;
  786. }
  787. else if(pieces[x-i][y+i]=='B'||pieces[x-i][y+i]=='Q'){
  788. checkWhite=1;
  789. checkMateWhite=1;
  790. return;
  791. }
  792. }
  793. for(i=1; i<=7;i++){
  794. if(x-i<2 && y-i<2){
  795. break;
  796. }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='B' && pieces[x-i][y-i]!='Q')){
  797. checkWhite=0;
  798. checkMateWhite=0;
  799. break;
  800. }
  801. else if(pieces[x-i][y-i]=='B'||pieces[x-i][y-i]=='Q'){
  802. checkWhite=1;
  803. checkMateWhite=1;
  804. return;
  805. }
  806. }
  807. for(i=1; i<=7;i++){
  808. if(x+i>9 && y-i<2){
  809. break;
  810. }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='B' && pieces[x+i][y-i]!='Q')){
  811. checkWhite=0;
  812. checkMateWhite=0;
  813. break;
  814. }
  815. else if(pieces[x+i][y-i]=='B'||pieces[x+i][y-i]=='Q'){
  816. checkWhite=1;
  817. checkMateWhite=1;
  818. return;
  819. }
  820. }
  821. }
  822. }
  823.  
  824. void fullKingCheckBlack(int x,int y){
  825. 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')){
  826. checkBlack=1;
  827. checkMateBlack=1;
  828. return;
  829. }
  830. else if ((pieces[x+1][y-1]=='p')||(pieces[x+1][y+1]=='p')){
  831. checkBlack=1;
  832. checkMateBlack=1;
  833. return;
  834. }
  835. else{
  836. for(i=1; i<=7;i++){
  837. if(x+i>9){
  838. break;
  839. }
  840. else if(pieces[x+i][y]!='\0' && (pieces[x+i][y]!='r' && pieces[x+i][y]!='q')){
  841. checkBlack=0;
  842. checkMateBlack=0;
  843. break;
  844. }
  845. else if(pieces[x+i][y]=='r'||pieces[x+i][y]=='q'){
  846. checkBlack=1;
  847. checkMateBlack=1;
  848. return;
  849. }
  850. }
  851. for(i=1; i<=7;i++){
  852. if(x-i<2){
  853. break;
  854. }
  855. else if(pieces[x-i][y]!='\0' && (pieces[x-i][y]!='r' && pieces[x-i][y]!='q')){
  856. checkBlack=0;
  857. checkMateBlack=0;
  858. break;
  859. }
  860. else if(pieces[x-i][y]=='r'||pieces[x-i][y]=='q'){
  861. checkBlack=1;
  862. checkMateBlack=1;
  863. return;
  864. }
  865. }
  866. for(i=1; i<=7;i++){
  867. if(y+i>9){
  868. break;
  869. }
  870. else if(pieces[x][y+i]!='\0' && (pieces[x][y+i]!='r' && pieces[x][y+i]!='q')){
  871. checkBlack=0;
  872. checkMateBlack=0;
  873. break;
  874. }
  875. else if(pieces[x][y+i]=='r'||pieces[x][y+i]=='q'){
  876. checkBlack=1;
  877. checkMateBlack=1;
  878. return;
  879. }
  880. }
  881. for(i=1; i<=7;i++){
  882. if(y-i<2){
  883. break;
  884. }
  885. else if(pieces[x][y-i]!='\0' && (pieces[x][y-i]!='r' && pieces[x][y-i]!='q')){
  886. checkBlack=0;
  887. checkMateBlack=0;
  888. break;
  889. }
  890. else if(pieces[x][y-i]=='r'||pieces[x][y-i]=='q'){
  891. checkBlack=1;
  892. checkMateBlack=1;
  893. return;
  894. }
  895. }
  896. for(i=1; i<=7;i++){
  897. if(x+i>9 && y+i>9){
  898. break;
  899. }else if(pieces[x+i][y+i]!='\0' && (pieces[x+i][y+i]!='b' && pieces[x+i][y+i]!='q')){
  900. checkBlack=0;
  901. checkMateBlack=0;
  902. break;
  903. }
  904. else if(pieces[x+i][y+i]=='b'||pieces[x+i][y+i]=='q'){
  905. checkBlack=1;
  906. checkMateBlack=1;
  907. return;
  908. }
  909. }
  910. for(i=1; i<=7;i++){
  911. if(x-i<2 && y+i>9){
  912. break;
  913. }else if(pieces[x-i][y+i]!='\0' && (pieces[x-i][y+i]!='b' && pieces[x-i][y+i]!='q')){
  914. checkBlack=0;
  915. checkMateBlack=0;
  916. break;
  917. }
  918. else if(pieces[x-i][y+i]=='b'||pieces[x-i][y+i]=='q'){
  919. checkBlack=1;
  920. checkMateBlack=1;
  921. return;
  922. }
  923. }
  924. for(i=1; i<=7;i++){
  925. if(x-i<2 && y-i<2){
  926. break;
  927. }else if(pieces[x-i][y-i]!='\0' && (pieces[x-i][y-i]!='b' && pieces[x-i][y-i]!='q')){
  928. checkBlack=0;
  929. checkMateBlack=0;
  930. break;
  931. }
  932. else if(pieces[x-i][y-i]=='b'||pieces[x-i][y-i]=='q'){
  933. checkBlack=1;
  934. checkMateBlack=1;
  935. return;
  936. }
  937. }
  938. for(i=1; i<=7;i++){
  939. if(x+i>9 && y-i<2){
  940. break;
  941. }else if(pieces[x+i][y-i]!='\0' && (pieces[x+i][y-i]!='b' && pieces[x+i][y-i]!='q')){
  942. checkBlack=0;
  943. checkMateBlack=0;
  944. break;
  945. }
  946. else if(pieces[x+i][y-i]=='b'||pieces[x+i][y-i]=='q'){
  947. checkBlack=1;
  948. checkMateBlack=1;
  949. return;
  950. }
  951. }
  952. }
  953. }
  954.  
  955. void king1CheckMate(){
  956. if ((pieces[king1x+1][king1y]<'a')&&(king1x+1>1)&&(king1x+1<10)){
  957. fullKingCheckWhite(king1x+1,king1y);
  958. }
  959. if ((checkMateWhite==1)&&(pieces[king1x][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)){
  960. fullKingCheckWhite(king1x,king1y+1);
  961. }
  962. if ((checkMateWhite==1)&&(pieces[king1x+1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x+1>1)&&(king1x+1<10)){
  963. fullKingCheckWhite(king1x+1,king1y+1);
  964. }
  965. if ((checkMateWhite==1)&&(pieces[king1x-1][king1y]<'a')&&(king1x-1>1)&&(king1x-1<10)){
  966. fullKingCheckWhite(king1x-1,king1y);
  967. }
  968. if ((checkMateWhite==1)&&(pieces[king1x][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)){
  969. fullKingCheckWhite(king1x,king1y-1);
  970. }
  971. if ((checkMateWhite==1)&&(pieces[king1x-1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x-1>1)&&(king1x-1<10)){
  972. fullKingCheckWhite(king1x-1,king1y-1);
  973. }
  974. if ((checkMateWhite==1)&&(pieces[king1x+1][king1y-1]<'a')&&(king1y-1>1)&&(king1y-1<10)&&(king1x+1>1)&&(king1x+1<10)){
  975. fullKingCheckWhite(king1x+1,king1y-1);
  976. }
  977. if ((checkMateWhite==1)&&(pieces[king1x-1][king1y+1]<'a')&&(king1y+1>1)&&(king1y+1<10)&&(king1x-1>1)&&(king1x-1<10)){
  978. fullKingCheckWhite(king1x-1,king1y+1);
  979. }
  980. if (checkMateWhite==1){
  981. fullKingCheckBlack(index[2],index[3]);
  982. }
  983. if (checkMateBlack==0){
  984. checkMateBlackPath(king1x,king1y);
  985. }
  986. }
  987.  
  988. void king2CheckMate(){
  989. if (((pieces[king2x+1][king2y]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2x+1>1)&&(king2x+1<10)){
  990. fullKingCheckBlack(king2x+1,king2y);
  991. }
  992. if (((checkMateBlack==1)&&(pieces[king2x][king2y+1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y+1>1)&&(king2y+1<10)){
  993. fullKingCheckBlack(king2x,king2y+1);
  994. }
  995. if (((checkMateBlack==1)&&(pieces[king2x+1][king2y+1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x+1>1)&&(king2x+1<10)){
  996. fullKingCheckBlack(king2x+1,king2y+1);
  997. }
  998. if (((checkMateBlack==1)&&(pieces[king2x-1][king2y]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2x-1>1)&&(king2x-1<10)){
  999. fullKingCheckBlack(king2x-1,king2y);
  1000. }
  1001. if (((checkMateBlack==1)&&(pieces[king2x][king2y-1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y-1>1)&&(king2y-1<10)){
  1002. fullKingCheckBlack(king2x,king2y-1);
  1003. }
  1004. if (((checkMateBlack==1)&&(pieces[king2x-1][king2y-1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x-1>1)&&(king2x-1<10)){
  1005. fullKingCheckBlack(king2x-1,king2y-1);
  1006. }
  1007. if (((checkMateBlack==1)&&(pieces[king2x+1][king2y-1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y-1>1)&&(king2y-1<10)&&(king2x+1>1)&&(king2x+1<10)){
  1008. fullKingCheckBlack(king2x+1,king2y-1);
  1009. }
  1010. if (((checkMateBlack==1)&&(pieces[king2x-1][king2y+1]>='a')||(pieces[king2x+1][king2y]=='\0'))&&(king2y+1>1)&&(king2y+1<10)&&(king2x-1>1)&&(king2x-1<10)){
  1011. fullKingCheckBlack(king2x-1,king2y+1);
  1012. }
  1013. if (checkMateBlack==1){
  1014. fullKingCheckWhite(index[2],index[3]);
  1015. }
  1016. if (checkMateWhite==0){
  1017. checkMateWhitePath(king2x,king2y);
  1018. }
  1019. }
  1020.  
  1021. void checkMateWhitePath(int x,int y){
  1022. if((index[3]-y)==0 && (index[2]-x>0)){
  1023. for (i=1;i<index[2]-x;i++){
  1024. fullKingCheckBlack(x+i,y);
  1025. if (checkMateBlack==1){
  1026. break;
  1027. }
  1028. }
  1029. }
  1030. else if ((index[3]-y)==0 && (x-index[2]>0)){
  1031. for (i=1;i<x-index[2];i++){
  1032. fullKingCheckBlack(x-i,y);
  1033. if (checkMateBlack==1){
  1034. break;
  1035. }
  1036. }
  1037. }
  1038. else if ((index[3]-y)>0 && (index[2]-x==0)){
  1039. for (i=1;i<index[3]-y;i++){
  1040. fullKingCheckBlack(x,y+i);
  1041. if (checkMateBlack==1){
  1042. break;
  1043. }
  1044. }
  1045. }
  1046. else if ((y-index[3])>0 && (index[2]-x==0)){
  1047. for (i=1;i<y-index[3];i++){
  1048. fullKingCheckBlack(x,y-i);
  1049. if (checkMateBlack==1){
  1050. break;
  1051. }
  1052. }
  1053. }
  1054. else if (((x-index[2])==(y-index[3]))&&((x-index[2])>0)){
  1055. for (i=1;i<x-index[2];i++){
  1056. fullKingCheckBlack(x-i,y-i);
  1057. if (checkMateBlack==1){
  1058. break;
  1059. }
  1060. }
  1061. }
  1062. else if (((x-index[2])==(y-index[3]))&&((index[2]-x)>0)){
  1063. for (i=1;i<index[2]-x;i++){
  1064. fullKingCheckBlack(x+i,y+i);
  1065. if (checkMateBlack==1){
  1066. break;
  1067. }
  1068. }
  1069. }
  1070. else if (((x-index[2])==(index[3]-y))&&((x-index[2])>0)){
  1071. for (i=1;i<x-index[2];i++){
  1072. fullKingCheckBlack(x-i,y+i);
  1073. if (checkMateBlack==1){
  1074. break;
  1075. }
  1076. }
  1077. }
  1078. else if (((x-index[2])==(index[3]-y))&&((index[2]-x)>0)){
  1079. for (i=1;i<index[0]-index[2];i++){
  1080. fullKingCheckBlack(x+i,y-i);
  1081. if (checkMateBlack==1){
  1082. break;
  1083. }
  1084. }
  1085. }
  1086. }
  1087.  
  1088. void checkMateBlackPath(int x,int y){
  1089. if((index[3]-y)==0 && (index[2]-x>0)){
  1090. for (i=1;i<index[2]-x;i++){
  1091. fullKingCheckBlack(x+i,y);
  1092. if (checkMateBlack==1){
  1093. break;
  1094. }
  1095. }
  1096. }
  1097. else if ((index[3]-y)==0 && (x-index[2]>0)){
  1098. for (i=1;i<x-index[2];i++){
  1099. fullKingCheckBlack(x-i,y);
  1100. if (checkMateBlack==1){
  1101. break;
  1102. }
  1103. }
  1104. }
  1105. else if ((index[3]-y)>0 && (index[2]-x==0)){
  1106. for (i=1;i<index[3]-y;i++){
  1107. fullKingCheckBlack(x,y+i);
  1108. if (checkMateBlack==1){
  1109. break;
  1110. }
  1111. }
  1112. }
  1113. else if ((y-index[3])>0 && (index[2]-x==0)){
  1114. for (i=1;i<y-index[3];i++){
  1115. fullKingCheckBlack(x,y-i);
  1116. if (checkMateBlack==1){
  1117. break;
  1118. }
  1119. }
  1120. }
  1121. else if (((x-index[2])==(y-index[3]))&&((x-index[2])>0)){
  1122. for (i=1;i<x-index[2];i++){
  1123. fullKingCheckBlack(x-i,y-i);
  1124. if (checkMateBlack==1){
  1125. break;
  1126. }
  1127. }
  1128. }
  1129. else if (((x-index[2])==(y-index[3]))&&((index[2]-x)>0)){
  1130. for (i=1;i<index[2]-x;i++){
  1131. fullKingCheckBlack(x+i,y+i);
  1132. if (checkMateBlack==1){
  1133. break;
  1134. }
  1135. }
  1136. }
  1137. else if (((x-index[2])==(index[3]-y))&&((x-index[2])>0)){
  1138. for (i=1;i<x-index[2];i++){
  1139. fullKingCheckBlack(x-i,y+i);
  1140. if (checkMateBlack==1){
  1141. break;
  1142. }
  1143. }
  1144. }
  1145. else if (((x-index[2])==(index[3]-y))&&((index[2]-x)>0)){
  1146. for (i=1;i<index[0]-index[2];i++){
  1147. fullKingCheckBlack(x+i,y-i);
  1148. if (checkMateBlack==1){
  1149. break;
  1150. }
  1151. }
  1152. }
  1153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement