Advertisement
Guest User

Untitled

a guest
Dec 7th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.60 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include<stdbool.h>
  4. #include<ctype.h>
  5. int max(int a,int b)
  6. {
  7. if(a>b)
  8. {
  9. return a;
  10. }
  11. else
  12. {
  13. return b;
  14. }
  15. }
  16. int min (int a,int b)
  17. {
  18. if(a<b)
  19. {
  20. return a;
  21. }
  22. else
  23. {
  24. return b;
  25. }
  26. }
  27. char board[8][8];
  28. char c[2];
  29. int player=0;
  30. char c2[2];
  31. char white ='-';
  32. char black ='.';
  33. char pawnw='p',pawnb='P',rookw='r',knightw='n',bishopw='b',queenw='q',kingw='k',rookb='R',knightb='N',bishopb='B',queenb='Q',kingb='K';
  34. void firstDisplay(){
  35. for(int i=0;i<8;i++){
  36. for(int j=0;j<8;j++){
  37. if(i==1){
  38. board[i][j]=pawnw;
  39. }
  40. else if (i==6){
  41. board[i][j]=pawnb;
  42. }
  43. else if (i!=7||i!=0||i!=1||i!=6){
  44. if((i==2||i==4)&&(j==0||j==2||j==4||j==6)){
  45. board[i][j]=white;
  46.  
  47. }
  48. else if ((i==3||i==5)&&(j==1||j==3||j==5||j==7)){
  49. board[i][j]=white;
  50. }
  51. else {
  52. board[i][j]=black;
  53. }
  54.  
  55. }
  56.  
  57. }
  58. }
  59.  
  60. board[0][0]=rookw;
  61. board[7][0]=rookb;
  62. board[0][1]=knightw;
  63. board[7][1]=knightb;
  64. board[0][2]=bishopw;
  65. board[7][2]=bishopb;
  66. board[0][3]=queenw;
  67. board[4][3]=queenb;
  68. board[0][4]=kingw;
  69. board[7][4]=kingb;
  70. board[0][5]=bishopw;
  71. board[7][5]=bishopb;
  72. board[0][6]=knightw;
  73. board[7][6]=knightb;
  74. board[0][7]=rookw;
  75. board[7][7]=rookb;
  76. board[6][0]=pawnb;
  77. board[1][0]=pawnw;
  78. //board[2][2]='A';
  79. //board[5][0]='a';
  80.  
  81. }
  82. void printDisplay(){
  83. system("cls");
  84. for(int i=7;i>=0;i--){
  85. for (int j =0;j<8;j++){
  86. printf("%c",board[i][j]);
  87. }
  88. printf("\n");
  89. }
  90. }
  91. int fromc,fromr,tor,toc;
  92. void read_move(char c[],char c2[])
  93. {
  94. gets(c);
  95. fromc=c[0]-97;
  96. fromr=c[1]-'0'-1;
  97. printf("%d %d\n",fromr,fromc);
  98. //printf("%c",board[fromr][fromc]);
  99. printf("To ");
  100. gets(c2);
  101. toc=c2[0]-97;
  102. tor=c2[1]-'0'-1;
  103. printf("%d %d\n",tor,toc);
  104. // make_move(fromr,fromc,tor,toc);
  105. //printf("%c\n",board[tor][toc]);
  106. }
  107. int validinput(int fromr,int fromc,int tor,int toc)
  108. {
  109. if(fromr>7||fromc>7||fromr<0||fromc<0)
  110. {
  111. return 0;
  112. }
  113. else if(tor>7||toc>7||tor<0||toc<0)
  114. {
  115. return 0;
  116. }
  117. else
  118. {
  119. return 1;
  120. }
  121. }
  122. int validpawn(int fromr,int fromc,int tor,int toc)
  123. {
  124.  
  125. if(board[fromr][fromc]=='p')
  126. {
  127. if((fromr==1)&&(fromc==0||fromc==1||fromc==2||fromc==3||fromc==4||fromc==5||fromc==6||fromc==7))
  128. {
  129. if(tor-fromr!=1&&tor-fromr!=2)
  130. {
  131. printf("df");
  132. return 0;
  133. }
  134. }
  135. else if(tor-fromr!=1)
  136. {
  137. printf("df");
  138. return 0;
  139. }
  140. if(toc-fromc!=0&&tor-fromr==1)//coloumns
  141. {
  142. if(toc-fromc==1||toc-fromc==-1)
  143. {
  144. printf("d");
  145. if(board[tor][toc]!='.'&&board[tor][toc]!='-')
  146. {
  147. if(isupper(board[tor][toc]))
  148. {
  149. printf("ad");
  150. return 1;
  151. }
  152. }
  153. else
  154. {
  155. printf("sdff");
  156. return 0;
  157. }
  158. }
  159. else
  160. {
  161. return 0;
  162.  
  163. }
  164. }
  165. if(board[tor][toc]!='-'&&board[tor][toc]!='.')
  166. {
  167. return 0;
  168. }
  169.  
  170. else
  171. {
  172. return 1;
  173. }
  174. }
  175. else if(board[fromr][fromc]=='P')
  176. {
  177. if((fromr==6)&&(fromc==0||fromc==1||fromc==2||fromc==3||fromc==4||fromc==5||fromc==6||fromc==7))
  178. {
  179. if(tor-fromr!=-1&&tor-fromr!=-2)
  180. {
  181. return 0;
  182. }
  183. }
  184. else if(tor-fromr!=-1)
  185. {
  186. return 0;
  187. }
  188. if(toc-fromc!=0&&tor-fromr==-1)
  189. {
  190. if(toc-fromc==1||toc-fromc==-1)
  191. {
  192. if(board[tor][toc]!='.'&&board[tor][toc]!='-')//it just need to add all of the same chess colours as it can't move if one of them is there
  193. {
  194. if(islower(board[tor][toc]))
  195. return 1;
  196. }
  197. else
  198. {
  199. return 0;
  200. }
  201. }
  202. else
  203. {
  204. return 0;
  205.  
  206. }
  207. }
  208. if(board[tor][toc]!='-'&&board[tor][toc]!='.')
  209. {
  210. return 0;
  211. }
  212.  
  213. else
  214. {
  215. return 1;
  216. }
  217. }
  218. }
  219. int validrook(int fromr,int fromc,int tor,int toc){
  220. if(board[fromr][fromc]=='r'||board[fromr][fromc]=='R')
  221. {
  222. if(board[fromr][fromc]=='r'&&islower(board[tor][toc]))
  223. {
  224. return 0;
  225. }
  226. else if(board[fromr][fromc]=='R'&&isupper(board[tor][toc]))
  227. {
  228. return 0;
  229. }
  230. if(tor-fromr==0&&toc-fromc!=0)
  231. {
  232. for(int i=min(toc,fromc)+1;i<max(toc,fromc);i++)
  233. {
  234. if(board[fromr][i]!='-'&&board[fromr][i]!='.')
  235. {
  236. printf("empty");
  237. return 0;
  238. }
  239. }
  240. return 1;
  241. }
  242. else if(tor-fromr!=0&&toc-fromc==0)
  243. {
  244. printf("a7a");
  245. for(int i=min(tor,fromr)+1;i<max(tor,fromr);i++)
  246. {
  247. if(board[i][fromc]!='-'&&board[i][fromc]!='.')
  248. {
  249. printf("empty");
  250. return 0;
  251. }
  252. }
  253. return 1;
  254. }
  255. else
  256. {
  257. return 0;
  258. }
  259. }
  260. }
  261. int validknight(int fromr,int fromc,int tor,int toc){
  262. if(board[fromr][fromc]=='n'||board[fromr][fromc]=='N'){
  263. if(abs(toc-fromc)==2&&abs(tor-fromr)==1||abs(toc-fromc)==1&&abs(tor-fromr)==2){
  264. if (board[tor][toc]=='-'||board[tor][toc]=='.'){
  265. return 1 ;
  266. }
  267. else if (((board[fromr][fromc]=='n')&&isupper((int)board[tor][toc]))==1||((board[fromr][fromc]=='N')&&islower(board[tor][toc]))){
  268. return 1;
  269. }
  270. }
  271. }
  272. return 0 ;
  273. }
  274. int validking(int fromr,int fromc,int tor,int toc){
  275. if(board[fromr][fromc]=='k'||board[fromr][fromc]=='K'){
  276. if(abs(toc-fromc)==1&&tor-fromr==0||abs(tor-fromr)==1&&toc-fromc==0||abs(tor-fromr)==1&&abs(toc-fromc)==1){
  277. if (board[tor][toc]=='-'||board[tor][toc]=='.'){
  278. return 1 ;
  279. }
  280. else if (((board[fromr][fromc]=='k')&&isupper((int)board[tor][toc]))==1||((board[fromr][fromc]=='K')&&islower(board[tor][toc]))){
  281. return 1;
  282. }
  283.  
  284. }
  285.  
  286. }
  287. return 0 ;
  288. }
  289. int validbishop(int fromr,int fromc,int tor,int toc)
  290. {
  291. if(board[fromr][fromc]=='b'||board[fromr][fromc]=='B')
  292. {
  293. if(abs(tor-fromr)==abs(toc-fromc))
  294. {
  295. if(board[fromr][fromc]=='b'&&islower(board[tor][toc]))
  296. {
  297. //printf("a");
  298. return 0;
  299. }
  300. else if(board[fromr][fromc]=='B'&&isupper(board[tor][toc]))
  301. {
  302. return 0;
  303. }
  304. int j=min(toc,fromc)+1;
  305. for(int i=min(fromr,tor)+1;i<max(fromr,tor);i++)
  306. {
  307. if(board[i][j]!='-'&&board[i][j]!='.')
  308. {
  309. return 0;
  310. }
  311. j++;
  312. }
  313. return 1;
  314. }
  315. else
  316. {
  317. return 0;
  318. }
  319. }
  320. }
  321. int validqueen (int fromr,int fromc,int tor,int toc){
  322. if(board[fromr][fromc]=='q'||board[fromr][fromc]=='Q'){
  323. if(abs(tor-fromr)==abs(toc-fromc))
  324. {
  325. if(board[fromr][fromc]=='q'&&islower(board[tor][toc]))
  326. {
  327. //printf("a");
  328. return 0;
  329. }
  330. else if(board[fromr][fromc]=='Q'&&isupper(board[tor][toc]))
  331. {
  332. return 0;
  333. }
  334. int j=min(toc,fromc)+1;
  335. for(int i=min(fromr,tor)+1;i<max(fromr,tor);i++)
  336. {
  337. if(board[i][j]!='-'&&board[i][j]!='.')
  338. {
  339. return 0;
  340. }
  341. j++;
  342. }
  343. return 1;
  344. }
  345. else if(board[fromr][fromc]=='q'&&islower(board[tor][toc]))
  346. {
  347. return 0;
  348. }
  349. else if(board[fromr][fromc]=='Q'&&isupper(board[tor][toc]))
  350. {
  351. return 0;
  352. }
  353. if(tor-fromr==0&&toc-fromc!=0)
  354. {
  355. for(int i=min(toc,fromc)+1;i<max(toc,fromc);i++)
  356. {
  357. if(board[fromr][i]!='-'&&board[fromr][i]!='.')
  358. {
  359. printf("empty");
  360. return 0;
  361. }
  362. }
  363. return 1;
  364. }
  365. else if(tor-fromr!=0&&toc-fromc==0)
  366. {
  367. printf("a7a");
  368. for(int i=min(tor,fromr)+1;i<max(tor,fromr);i++)
  369. {
  370. if(board[i][fromc]!='-'&&board[i][fromc]!='.')
  371. {
  372. printf("empty");
  373. return 0;
  374. }
  375. }
  376. return 1;
  377. }
  378. else
  379. {
  380. return 0;
  381. }
  382.  
  383. }
  384. }
  385. void make_move (int fromr,int fromc,int tor,int toc){
  386. board[tor][toc]=board[fromr][fromc];
  387. board[fromr][fromc]=white;
  388. printDisplay();
  389. /*
  390. if(validinput(fromr,fromc,tor,toc)==1&&validpawn(fromr,fromc,tor,toc)==1){
  391. if(board[fromr][fromc]==pawnw)
  392. {
  393. board[tor][toc]=pawnw;
  394. board[fromr][fromc]=white;
  395. }
  396. else if(board[fromr][fromc]==pawnb)
  397. {
  398. board[tor][toc]=pawnb;
  399. board[fromr][fromc]=white;
  400. }
  401. printDisplay() ;
  402. }
  403. else if(validinput(fromr,fromc,tor,toc)==1&&validrook(fromr,fromc,tor,toc)==1){
  404. if(board[fromr][fromc]=='r'){
  405. board[tor][toc]=rookw;
  406. board[fromr][fromc]=white;
  407. printDisplay();
  408. }
  409. else{board[tor][toc]=rookb;
  410. board[fromr][fromc]=black;
  411. printDisplay();}
  412. }
  413. else if(validinput(fromr,fromc,tor,toc)==1&&validknight(fromr,fromc,tor,toc)==1){
  414. if(board[fromr][fromc]=='n'){
  415. board[tor][toc]=knightw;
  416. board[fromr][fromc]=white;
  417. printDisplay();
  418. }
  419. else{board[tor][toc]=knightb;
  420. board[fromr][fromc]=black;
  421. printDisplay();}
  422. }
  423. else if(validinput(fromr,fromc,tor,toc)==1&&validking(fromr,fromc,tor,toc)==1){
  424. if(board[fromr][fromc]=='k'){
  425. board[tor][toc]=kingw;
  426. board[fromr][fromc]=white;
  427. printDisplay();
  428. }
  429. else{board[tor][toc]=kingb;
  430. board[fromr][fromc]=black;
  431. printDisplay();}
  432. }
  433. else if(validinput(fromr,fromc,tor,toc)==1&&validbishop(fromr,fromc,tor,toc)==1){
  434. if(board[fromr][fromc]=='b'){
  435. board[tor][toc]=bishopw;
  436. board[fromr][fromc]=white;
  437. printDisplay();
  438. }
  439.  
  440. else{board[tor][toc]=bishopb;
  441. board[fromr][fromc]=black;
  442. printDisplay();}
  443. }
  444. else if(validinput(fromr,fromc,tor,toc)==1&&validqueen(fromr,fromc,tor,toc)==1){
  445. if(board[fromr][fromc]=='q'){
  446. board[tor][toc]=queenw;
  447. board[fromr][fromc]=white;
  448. printDisplay();
  449. }
  450.  
  451. else{board[tor][toc]=queenb;
  452. board[fromr][fromc]=black;
  453. printDisplay();}
  454. }
  455. */
  456. }
  457. int validmove(int fromr,int fromc,int tor,int toc)
  458. {
  459. if(islower(board[fromr][fromc])&&player!=1)
  460. {
  461. return 0;
  462. }
  463. else if(isupper(board[fromr][fromc])&&player!=2)
  464. {
  465. return 0;
  466. }
  467. if(validinput(fromr,fromc,tor,toc)==1&&validpawn(fromr,fromc,tor,toc)==1)
  468. {
  469. return 1;
  470. }
  471. else if(validinput(fromr,fromc,tor,toc)==1&&validrook(fromr,fromc,tor,toc)==1)
  472. {
  473. return 1;
  474. }
  475. else if(validinput(fromr,fromc,tor,toc)==1&&validknight(fromr,fromc,tor,toc)==1)
  476. {
  477. return 1;
  478. }
  479. else if(validinput(fromr,fromc,tor,toc)==1&&validking(fromr,fromc,tor,toc)==1)
  480. {
  481. return 1;
  482. }
  483. else if(validinput(fromr,fromc,tor,toc)==1&&validbishop(fromr,fromc,tor,toc)==1)
  484. {
  485. return 1;
  486. }
  487. else if(validinput(fromr,fromc,tor,toc)==1&&validqueen(fromr,fromc,tor,toc)==1)
  488. {
  489. return 1;
  490. }
  491. else
  492. {
  493. return 0;
  494. }
  495.  
  496. }
  497. void playgame()
  498. {
  499. while(true)
  500. {
  501. if(player==1)
  502. {
  503. player=2;
  504. }
  505. else
  506. {
  507. player=1;
  508. }
  509. printf("Player %d's turn: ",player);
  510. read_move(c,c2);
  511. while(validmove(fromr,fromc,tor,toc)!=1)
  512. {
  513. printf("invalid");
  514. read_move(c,c2);
  515. }
  516. make_move(fromr,fromc,tor,toc);
  517. }
  518.  
  519. }
  520.  
  521. int main()
  522. {
  523. firstDisplay();
  524. printDisplay();
  525. playgame();
  526. // read_move(c,c2);
  527. return 0;
  528. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement