Advertisement
Seif45

Untitled

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