Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.96 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <windows.h>
  5. #include <math.h>
  6. /*Used Variables c,k,n,char temp*/
  7. int k=0,n=0,g=2,xk=8,xK=1;
  8. char board[8][8],yk='E',yK='E';
  9. int c=1;
  10. int x1,x2;
  11. char eaten1[16]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
  12. char eaten2[16]={' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' ',' '};
  13. char index1[8]={'A','B','C','D','E','F','G','H'};
  14. int index2[8]={1,2,3,4,5,6,7,8};
  15. int valid(int x1,char y1,int x2,char y2);
  16.  
  17. int check (int xk,char yk,int xK,char yK);
  18. void gotoxy(int x, int y)
  19. {
  20. COORD coord;
  21. coord.X = x;
  22. coord.Y = y;
  23. SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
  24. }
  25. void delay(unsigned int mseconds)
  26. {
  27. clock_t goal = mseconds + clock();
  28. while (goal > clock());
  29. }
  30. int main()
  31. {
  32. char y1,y2,mode,temp;
  33. /*Initializing the initial board*/
  34. for(int i=0;i<2;i++)
  35. {
  36. if(i==1)
  37. {
  38. for(int j=0;j<8;j++)
  39. {
  40. board[i][j]='P';
  41. }
  42. }
  43. else
  44. {
  45. for(int j=0;j<8;j++)
  46. {
  47. switch (j)
  48. {
  49. case 0: board[i][j]='R'; break;
  50. case 1: board[i][j]='N'; break;
  51. case 2: board[i][j]='B'; break;
  52. case 3: board[i][j]='Q'; break;
  53. case 4: board[i][j]='K'; break;
  54. case 5: board[i][j]='B'; break;
  55. case 6: board[i][j]='N'; break;
  56. case 7: board[i][j]='R'; break;
  57. }
  58. }
  59. }
  60. }
  61. for(int i=6;i<8;i++)
  62. {
  63. if(i==6)
  64. {
  65. for(int j=0;j<8;j++)
  66. {
  67. board[i][j]='p';
  68. }
  69. }
  70. else
  71. {
  72. for(int j=0;j<8;j++)
  73. {
  74. switch (j)
  75. {
  76. case 0: board[i][j]='r'; break;
  77. case 1: board[i][j]='n'; break;
  78. case 2: board[i][j]='b'; break;
  79. case 3: board[i][j]='q'; break;
  80. case 4: board[i][j]='k'; break;
  81. case 5: board[i][j]='b'; break;
  82. case 6: board[i][j]='n'; break;
  83. case 7: board[i][j]='r'; break;
  84. }
  85. }
  86. }
  87. }
  88. for(int i=2;i<6;i++)
  89. {
  90. for(int j=0;j<8;j++)
  91. {
  92. if ((i+j)%2==1)
  93. board[i][j]=' ';
  94. else
  95. board[i][j]=219;
  96. }
  97. }
  98. /*End of Assiging Values*/
  99. /*Printing the board at the begining*/
  100. printf("\n\n");
  101. printf(" Eaten Pieces Of Player (1) ");
  102. for(int i=0;i<16;i++)
  103. {
  104. printf("%c|",eaten1[i]);
  105. }
  106. printf("\n");
  107. printf(" Eaten Pieces Of Player (2) ");
  108. for(int i=0;i<16;i++)
  109. {
  110. printf("%c|",eaten2[i]);
  111. }
  112. printf("\n\n\n\n");
  113. printf(" Player {2} Black\n\n");
  114.  
  115. printf(" ");
  116. for(int i=0;i<8;i++)
  117. {
  118. printf(" .%c.",index1[i]);
  119. }
  120. printf("\n");
  121. for(int row=0;row<8;row++)
  122. {
  123. /* Draw the top line */
  124. printf(" ");
  125. for(int column=0;column<8;column++)
  126. printf(" ---");
  127. putchar('\n');
  128. /* Draw the squares */
  129. printf(" ");
  130. printf("%d",c++);
  131. for(int column=0;column<8;column++)
  132. {
  133. printf("| %c ",board[row][column]);
  134. }
  135. printf("|\n");
  136. }
  137. printf(" ");
  138. for(int column=0;column<8;column++)
  139. printf(" ---");
  140. printf("\n\n");
  141. printf(" Player {1} White ");
  142. printf("\n\n");
  143. /* End of Printing the board before playing*//*Printing the board before playing*//*Printing the board before playing*/
  144. printf("Enter the desired piece\n");
  145. scanf("%d %c",&x1,&y1);
  146. printf("Enter the desired destination\n");
  147. scanf("%d %c",&x2,&y2);
  148.  
  149. while(valid(x1,y1,x2,y2)==0){
  150. printf("NOT VALID MOVE\n");
  151. printf("Enter the desired piece\n");
  152. scanf("%d %c",&x1,&y1);
  153. printf("Enter the desired destination\n");
  154. scanf("%d %c",&x2,&y2);}
  155. g++;
  156. x1=x1-1;
  157. x2=x2-1;
  158. y1=y1-65;
  159. y2=y2-65;
  160. temp=board[x2][y2];
  161. board[x2][y2]=board[x1][y1];
  162. if((x1+y1)%2==1)
  163. {
  164. board[x1][y1]=' ';
  165. }
  166. else
  167. {
  168. board[x1][y1]=219;
  169. }
  170. /*Fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooor*/
  171. for(;;)
  172. {
  173.  
  174. system("cls");
  175. if(check(xk,yk,xK,yK)==1||check(xk,yk,xK,yK)==2){printf("CHECK!");}
  176. int c=1;
  177. /*Printing The Board At Every Move*/
  178. printf("\n\n");
  179. printf(" Eaten Pieces Of Player (1) ");
  180. for(int i=0;i<16;i++)
  181. {
  182. printf("%c|",eaten1[i]);
  183. }
  184. printf("\n");
  185. printf(" Eaten Pieces Of Player (2) ");
  186. for(int i=0;i<16;i++)
  187. {
  188. printf("%c|",eaten2[i]);
  189. }
  190. printf("\n\n\n\n");
  191. printf(" Player {2} Black\n\n");
  192.  
  193. printf(" ");
  194. for(int i=0;i<8;i++)
  195. {
  196. printf(" .%c.",index1[i]);
  197. }
  198. printf("\n");
  199. for(int row=0;row<8;row++)
  200. {
  201. /* Draw the top line */
  202. printf(" ");
  203. for(int column=0;column<8;column++)
  204. printf(" ---");
  205. putchar('\n');
  206. /* Draw the squares */
  207. printf(" ");
  208. printf("%d",c++);
  209. for(int column=0;column<8;column++)
  210. {
  211. printf("| %c ",board[row][column]);
  212. }
  213. printf("|\n");
  214. }
  215. printf(" ");
  216. for(int column=0;column<8;column++)
  217. printf(" ---");
  218. printf("\n\n");
  219. printf(" Player {1} White ");
  220. printf("\n\n");
  221. /*End of Printing The Board At Every Move*/
  222.  
  223. printf("To Move Press (M) , To Undo Press (U), To Redo Press (R)\n");
  224. mode=getch();
  225. if(mode=='U')
  226. {
  227. board[x1][y1]=board[x2][y2];
  228. board[x2][y2]=temp;
  229. }
  230. else if(mode=='R')
  231. {
  232.  
  233. }
  234. else
  235. {
  236.  
  237. printf("Enter the desired piece\n");
  238. scanf("%d %c",&x1,&y1);
  239. printf("Enter the desired destination\n");
  240. scanf("%d %c",&x2,&y2);
  241.  
  242. while(valid(x1,y1,x2,y2)==0){
  243. printf("NOT VALID MOVE\n");
  244. printf("Enter the desired piece\n");
  245. scanf("%d %c",&x1,&y1);
  246. printf("Enter the desired destination\n");
  247. scanf("%d %c",&x2,&y2);}
  248. if(board[x1-1][y1-65]=='k'){xk=x1;
  249. yk=y1;}
  250. else if(board[x1-1][y1-65]=='K'){xK=x1;
  251. yK=y1;}
  252.  
  253. //check(xk,yk,xK,yK);
  254. g++;
  255. x1=x1-1;
  256. x2=x2-1;
  257. y1=y1-65;
  258. y2=y2-65;
  259. /*Printing Eaten if Exist */
  260. if((board[x2][y2]>=97 && board[x2][y2]<=122))
  261. {
  262. eaten1[k]=board[x2][y2];
  263. k++;
  264. }
  265. if((board[x2][y2]>=65 && board[x2][y2]<=90))
  266. {
  267. eaten2[n]=board[x2][y2];
  268. n++;
  269. }
  270. /*Printing Eaten if Exist */
  271. temp=board[x2][y2];
  272. board[x2][y2]=board[x1][y1];
  273. if((x1+y1)%2==1)
  274. {
  275. board[x1][y1]=' ';
  276. }
  277. else
  278. {
  279. board[x1][y1]=219;
  280. }
  281. }
  282. }
  283. return 0;
  284.  
  285. }
  286.  
  287.  
  288.  
  289.  
  290. int valid(int x1,char y1,int x2,char y2)
  291. {
  292. int condition=1,i,t1=0,t2=0;
  293. y1=y1-65;
  294. y2=y2-65;
  295. x1=x1-1;
  296. x2=x2-1;
  297. char w[6]={'p','r','n','b','k','q'};
  298. char b[6]={'P','R','N','B','K','Q'};
  299. char c=219,tempq;
  300. if(board[x1][y1]=='b'||board[x1][y1]=='B'){
  301. if(abs(x2-x1)!=abs(y2-y1)){condition=0;}}
  302. else if(board[x1][y1]=='n'||board[x1][y1]=='N')
  303. {
  304. if(!((abs(x2-x1)==1&&abs(y2-y1)==2)||(abs(y2-y1)==1&&abs(x2-x1)==2))){condition=0;}
  305. }
  306. else if(board[x1][y1]=='r'||board[x1][y1]=='R')
  307. {
  308. if(x1!=x2&&y1!=y2){condition=0;}
  309. }
  310. else if(board[x1][y1]=='K'||board[x1][y1]=='k')
  311. {
  312. if(abs(x2-x1)>=2||abs(y2-y1)>=2){condition=0;}
  313. }
  314. else if(board[x1][y1]=='p'){
  315. if(!(x1-x2==1&&y1==y2)){condition=0;}
  316. }
  317. else if(board[x1][y1]=='P'){
  318. if(!(x2-x1==1&&y1==y2)){condition=0;}
  319. }
  320. else if(board[x1][y1]=='q'||board[x1][y1]=='Q')
  321. {
  322. if((abs(x2-x1)!=abs(y2-y1))&&(x1!=x2&&y1!=y2)){condition=0;}
  323. }
  324. for(i=0;i<6;i++){
  325. if(board[x1][y1]==w[i]){t1=1;}}
  326. for(i=0;i<6;i++){
  327. if(board[x2][y2]==w[i]){t2=1;}}
  328. if(t1==1&&t2==1){condition=0;}
  329. t1=0;
  330. t2=0;
  331. for(i=0;i<6;i++){
  332. if(board[x1][y1]==b[i]){t1=1;}}
  333. for(i=0;i<6;i++){
  334. if(board[x2][y2]==b[i]){t2=1;}}
  335. if(t1==1&&t2==1){condition=0;}
  336. if(board[x1][y1]==' '||board[x1][y1]==c){condition=0;}
  337. t1=0;
  338. t2=0;
  339. if(board[x1][y1]=='p'&&(x1-x2)==1&&abs(y2-y1)==1)
  340. {
  341. for(i=0;i<6;i++){
  342. if(board[x2][y2]==b[i]){condition=1;}}}
  343. if(board[x1][y1]=='P'&&(x2-x1)==1&&abs(y2-y1)==1)
  344. {
  345. for(i=0;i<6;i++){
  346. if(board[x2][y2]==w[i]){condition=1;}}}
  347. i=1,t1=0,t2=0;
  348. if((board[x1][y1]=='b'||board[x1][y1]=='B')&&condition==1)
  349. {
  350. if(x2>x1&&y2>y1){
  351. while(x1+i!=x2){
  352. if(board[x1+i][y1+i]!=' '&&board[x1+i][y1+i]!=c){t1=1;}
  353. i++;}
  354. if(t1==1){condition=0;}}
  355.  
  356. else if(x2>x1&&y2<y1){
  357. t1=0,i=1;
  358. while(x1+i!=x2){
  359. if(board[x1+i][y1-i]!=' '&&board[x1+i][y1-i]!=c){t1=1;}
  360. i++;}
  361. if(t1==1){condition=0;}}
  362.  
  363. else if(x2<x1&&y2<y1){
  364. t1=0,i=1;
  365. while(x1-i!=x2){
  366. if(board[x1-i][y1-i]!=' '&&board[x1-i][y1-i]!=c){t1=1;}
  367. i++;}
  368. if(t1==1){condition=0;}}
  369.  
  370. else if(x2<x1&&y2>y1){
  371. t1=0,i=1;
  372. while(x1-i!=x2){
  373. if(board[x1-i][y1+i]!=' '&&board[x1-i][y1+i]!=c){t1=1;}
  374. i++;}
  375. if(t1==1){condition=0;}}
  376. t1=0,i=1;}
  377. if((board[x1][y1]=='r'||board[x1][y1]=='R')&&condition==1){
  378. if(x2>x1){
  379. i=1,t1=0;
  380. while(x1+i!=x2){
  381. if(board[x1+i][y1]!=' '&&board[x1+i][y1]!=c){t1=1;}
  382. i++;}
  383. if(t1==1){condition=0;}}
  384. if(x2<x1){
  385. i=1,t1=0;
  386. while(x1-i!=x2){
  387. if(board[x1-i][y1]!=' '&&board[x1-i][y1]!=c){t1=1;}
  388. i++;}
  389. if(t1==1){condition=0;}}
  390. if(y2>y1){
  391. i=1,t1=0;
  392. while(y1+i!=y2){
  393. if(board[x1][y1+i]!=' '&&board[x1][y1+i]!=c){t1=1;}
  394. i++;}
  395. if(t1==1){condition=0;}}
  396. if(y2<y1){
  397. i=1,t1=0;
  398. while(y1-i!=y2){
  399. if(board[x1][y1-i]!=' '&&board[x1][y1-i]!=c){t1=1;}
  400. i++;}
  401. if(t1==1){condition=0;}}}
  402. if((board[x1][y1]=='q'||board[x1][y1]=='Q')&&condition==1){
  403. if(abs(x2-x1)==abs(y2-y1)){
  404. tempq=board[x1][y1];
  405. board[x1][y1]='b';
  406. if(valid(x1+1,y1+65,x2+1,y2+65)==0){condition=0;}
  407. board[x1][y1]=tempq;}
  408. else {
  409. tempq=board[x1][y1];
  410. board[x1][y1]='r';
  411. if(valid(x1+1,y1+65,x2+1,y2+65)==0){condition=0;}
  412. board[x1][y1]=tempq;}}
  413.  
  414. if(x1>8||x1<0||y1>8||y1<0||x2>8||x2<0||y2>8||y2<0){condition=0;}
  415.  
  416. if(board[x1][y1]=='P'&&condition==0){
  417. if((x2-x1)==2&&x1==1){condition=1;}}
  418.  
  419. if(board[x1][y1]=='p'&&condition==0){
  420. if((x1-x2)==2&&x1==6){condition=1;}}
  421.  
  422. if(board[x1][y1]=='P'&&condition==1&&y1==y2)
  423. {
  424. if(((x2-x1)==1)&&(board[x2][y2]!=' '&&board[x2][y2]!=c)){condition=0;}
  425. }
  426.  
  427. if(board[x1][y1]=='p'&&condition==1&&y1==y2)
  428. {
  429. if(((x1-x2)==1)&&(board[x2][y2]!=' '&&board[x2][y2]!=c)){condition=0;}
  430. }
  431. if((board[x1][y1]=='k')&&check(x2+1,y2+65,xK,yK)==2){condition=0;}
  432.  
  433. if((board[x1][y1]=='K')&&check(xk,yk,x2+1,y2+65)==1){condition=0;}
  434.  
  435. for(i=0;i<6;i++){
  436. if((board[x1][y1]==w[i])&&check(xk,yk,xK,yK)==2){condition=0;}}
  437. for(i=0;i<6;i++){
  438. if((board[x1][y1]==b[i])&&check(xk,yk,xK,yK)==1){condition=0;}}
  439.  
  440.  
  441. /*Validity of turns*/
  442.  
  443. // if((board[x1][y1]>=65 && board[x1][y1]<=90) && (g%2)==0){condition=0;}
  444. // if((board[x1][y1]>=97 && board[x1][y1]<=122)&&(g%2)==1){condition=0;}
  445.  
  446.  
  447.  
  448.  
  449. return condition;
  450. }
  451. int check(int xk,char yk,int xK,char yK)
  452. { int i=0,j=0,k,check=0,checkmate=1,t1=0,t2=0;
  453. char temp;
  454. char w[6]={'q','r','n','b','k','p'};
  455. char b[6]={'Q','R','N','B','K','P'};
  456. for(k=0;k<5;k++){
  457. for(i=0;i<8;i++){
  458. for(j=0;j<8;j++){
  459. if(w[k]==board[i][j]){
  460. if(valid(i+1,j+65,xK,yK)==1){check=1;
  461. break;}}
  462. if(b[k]==board[i][j]){
  463. if(valid(i+1,j+65,xk,yk)==1){check=2;
  464. break;}}}}}
  465.  
  466.  
  467.  
  468.  
  469.  
  470.  
  471. return check;
  472.  
  473. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement