Advertisement
zayady

Untitled

Dec 9th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 61.89 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <windows.h>
  6. #include <dos.h> // contains the sound function
  7. #include <conio.h> // it does include getch()
  8.  
  9. struct player { // to define the player's properties
  10. char name[25];
  11. char color[25];
  12. int moves;
  13. int seconds;
  14. // the pieces that the player has taken
  15. int Pawn;
  16. int Rook;
  17. int Knight;
  18. int Bishop;
  19. int Queen;
  20. } player[2] = {{"Ahmed", "White", 0, 0, 0, 0, 0, 0, 0},
  21. {"Ahmed", "Black", 0, 0, 0, 0, 0, 0, 0}};
  22.  
  23. char maingrid[8][8] = {
  24. {'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, /*capital character are for black */
  25. {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
  26. {'0', '0', '0', '0', '0', '0', '0', '0'},
  27. {'0', '0', '0', '0', '0', '0', '0', '0'},
  28. {'0', '0', '0', '0', '0', '0', '0', '0'},/*0 means that this place is empty */
  29. {'0', '0', '0', '0', '0', '0', '0', '0'},
  30. {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
  31. {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'}, /*small character are for white */
  32. };
  33.  
  34. //********************************************************************************************************************************//
  35. int mainPage();
  36.  
  37. void showMainPage(int highLightedElement, int color);
  38.  
  39. int getSpecialInput();
  40.  
  41. int isCertainInput(int ASCII1, int ASCII2);
  42.  
  43. //********************************************************************************************************************************//
  44. void newGame();
  45.  
  46. int detailsPage();
  47.  
  48. //********************************************************************************************************************************//
  49. void save(char fileName[], int playerIndex);
  50.  
  51. void loadLast(char fileName[], int *playerIndex);
  52.  
  53. int lineLen(char fileName[]);
  54.  
  55. void winner(int playerIndex);
  56.  
  57. //********************************************************************************************************************************//
  58. int validateCommand(char *command, int playerIndex);
  59.  
  60. void help();
  61.  
  62. int editGrid(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
  63.  
  64. char getDecision(int playerIndex);
  65.  
  66. void giveToPlayer(int playerIndex, char piece);
  67.  
  68. void ChangeDisplayColor(int color);
  69.  
  70. int pawn(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
  71.  
  72. int rook(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
  73.  
  74. int rookInEmptyPath(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX);
  75.  
  76. int knight(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
  77.  
  78. int bishop(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
  79.  
  80. int bishopInEmptyPath(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX);
  81.  
  82. int queen(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
  83.  
  84. int king(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
  85.  
  86. //********************************************************************************************************************************//
  87. void printAtXY(int x, int y, const char *__fmt);
  88.  
  89. void SendToPrintGrade(int length, int weight, char MainGrade_local[length][weight], int positionATX, int positionATY);
  90.  
  91. void printGrid(int row, int colom, char *object);
  92.  
  93. void PrintColoredLinesIndride(int NumOFline);
  94.  
  95. void TODetermineThePosition(int PlayerIndex);
  96.  
  97. void PrintRowsNumber(int row);
  98.  
  99. //********************************************* main() the mother of all functions******************************************************************//
  100. int KingWightPositionX=5;
  101. int KingWightPositionY=7;
  102. int KingBlackPositionX=5;
  103. int KingBlackPositionY=0;
  104. int playnow=0;
  105. int flagPrintInvalidMove=0;
  106.  
  107. int main() {
  108. /*
  109. SendToPrintGrade(8, 8, maingrid, -1, -1);
  110. validateCommand("a7a5", 1);
  111. Sleep(2000);
  112. system("@cls||clear");
  113. SendToPrintGrade(8, 8, maingrid, -1, -1);
  114. validateCommand("a5a4", 1);
  115. Sleep(2000);
  116. system("@cls||clear");
  117. SendToPrintGrade(8, 8, maingrid, -1, -1);
  118. validateCommand("a4a3", 1);
  119. Sleep(2000);
  120. system("@cls||clear");
  121. SendToPrintGrade(8, 8, maingrid, -1, -1);
  122. validateCommand("a3b2", 1);
  123. Sleep(2000);
  124. system("@cls||clear");
  125. SendToPrintGrade(8, 8, maingrid, -1, -1);
  126. validateCommand("b2a1", 1);
  127. Sleep(2000);
  128. system("@cls||clear");
  129. SendToPrintGrade(8, 8, maingrid, -1, -1);
  130. */
  131. /*int playerIndex = 0;
  132. //FILE * filePointer = fopen("gameRecord.txt", "w");
  133. loadLast("gameRecord.txt", &playerIndex);
  134. //
  135. printf("%s,%d,%d,%d,%d,%d,%d,%d\n",player[0].name, // printing the first player details
  136. player[0].seconds,player[0].moves,player[0].Pawn, player[0].Rook,player[0].Knight,player[0].Bishop,player[0].Queen);
  137. printf("%s,%d,%d,%d,%d,%d,%d,%d",player[1].name, // printing the second player details
  138. player[1].seconds,player[1].moves,player[1].Pawn,player[1].Rook,player[1].Knight,player[1].Bishop,player[1].Queen);
  139. printf("\nthis the current player%d",playerIndex);
  140. //
  141.  
  142. //winner(0);*/
  143. newGame();
  144.  
  145. int x=IsSavePosition(8,8,maingrid,5,2,0);
  146. printf("%d",x);
  147.  
  148. // int IsSavePosition(int a8, int b8, char grid[a8][b8],int positionY ,int PositionX,int playerindex){ //
  149.  
  150. }
  151. //**************************************************main menu***************************************************
  152.  
  153. int mainPage() { // returns the the choice the user had made in order to navigate through the pages
  154. register int keyPressed, position = 0, temp;
  155. showMainPage(0, 4);
  156. do {
  157. keyPressed = getSpecialInput();
  158. switch (keyPressed) {
  159. case 0: // up
  160. if (position ==
  161. 0) { // this condition is only needed because Zaiady wanted the console to stop refreshing when we reach the top or the bottom of the menu
  162. continue;
  163. }
  164. temp = position - 1;
  165. position = ((position == 0) ? position : temp);
  166. system("@cls||clear");
  167. showMainPage(position % 4, 4);
  168. break;
  169. case 1: // down
  170. if (position == 3) {
  171. continue;
  172. }
  173. temp = position + 1;
  174. position = (position == 3) ? position : temp;
  175. system("@cls||clear");
  176. showMainPage(position % 4, 4);
  177. break;
  178. case 4: // enter
  179. system("@cls||clear");
  180. switch (position) {
  181. case 0:
  182. newGame();
  183. break;
  184. case 1:
  185. printf("load last game called");
  186. break; // loadGame(); or return 1;
  187. case 2:
  188. printf("show credits called");
  189. break; // credits(); or return 2;
  190. case 3:
  191. printAtXY(100,50,"THANK YOU FOR PLAYING WITH OUR SMALL GAME :) ");
  192. exit(0);
  193. break;
  194. }
  195. }
  196. } while (keyPressed != 4);
  197. return 0;
  198. }
  199.  
  200. void showMainPage(int highLightedElement, int color) { //prints the elements in the main page but one highlighted
  201. register int i;
  202. char elements[][16] = {"New game", "Load last game", "Credits", "Exit"};
  203. printAtXY(10, 10, "\n");
  204. for (i = 0; i < 4; i++) { // strlen(elements)
  205. if (i == highLightedElement) {
  206. ChangeDisplayColor(color);
  207. printAtXY(50, 0, elements[i]);
  208. printf("\n");
  209. ChangeDisplayColor(15);
  210. } else {
  211. printAtXY(50, 0, elements[i]);
  212. printf("\n");
  213. }
  214. }
  215. }
  216.  
  217. int getSpecialInput() { // 0 -> up , 1 -> down , 2 -> right , 3 -> left , 4 -> enter , 5 -> space , 6 -> delete ,
  218. // 7 -> escape , 8 -> delete , 9 -> control + z , 10 -> control + s , 11 -> control + u
  219. // 12 -> shift + h
  220. int ASCII = getch();
  221. if (ASCII == 224 || ASCII == 0) {
  222. switch (getch()) {
  223. case 72:
  224. return 0;
  225. case 80:
  226. return 1;
  227. case 77:
  228. return 2;
  229. case 75:
  230. return 3;
  231. case 83:
  232. return 8;
  233. default:
  234. return -1; //not identified
  235. }
  236. } else {
  237. switch (ASCII) {
  238. case 13:
  239. return 4;
  240. case 32:
  241. return 5;
  242. case 8:
  243. return 6;
  244. case 27:
  245. return 7;
  246. case 26:
  247. return 9; /*undo*/
  248. case 19:
  249. return 10; /*cotrol s */
  250. case 21:
  251. return 11;
  252. case 72:
  253. return 12; /* shift h */
  254. default:
  255. return -1;
  256. }
  257. }
  258. }
  259.  
  260. int isCertainInput(int ASCII1, int ASCII2) { // returns 1 if and only the typed button holds the
  261. if (ASCII2 != -1) { // ASCII code of the passed as an argument
  262. while (1) {
  263. if (ASCII1 == getch()) {
  264. if (ASCII2 == getch()) {
  265. return 1;
  266. }
  267. }
  268. }
  269. } else {
  270. while (1) {
  271. if (ASCII1 == getch()) {
  272. return 1;
  273. }
  274. }
  275. }
  276. /* remember these ASCII int decimal
  277. arrows
  278. up 224 72
  279. down 224 80
  280. right 224 77
  281. left 224 75
  282. delete 224 83
  283. enter 13
  284. space 32
  285. back space 8
  286. escape 27
  287. */
  288. }
  289.  
  290. //**************************************************newGame function*********************************************************
  291. void newGame() {
  292. FILE * fp = fopen("gameRecord.txt","w");
  293. fclose(fp);
  294. switch (detailsPage()){
  295. case 0:
  296. arrowsgame();
  297. break;
  298.  
  299. case 1:
  300. commandGame();
  301. break;
  302.  
  303. }
  304. }
  305. int count=0;
  306. void arrowsgame(){
  307.  
  308. HANDLE console;
  309. console = GetStdHandle(STD_OUTPUT_HANDLE);
  310. while(1){
  311. if (count%2==0)/*that's means white about to plat*/
  312. {
  313. count++;
  314.  
  315. TODetermineThePosition(0);
  316.  
  317. if( IsSavePosition(8,8,maingrid,KingWightPositionY,KingWightPositionX,1)){
  318. SetConsoleTextAttribute(console, 20);
  319. printf("chick!!\a\a\a\a\a\a");
  320.  
  321. }
  322.  
  323. }else if (count%2==1){
  324. count++;
  325.  
  326. TODetermineThePosition(1);
  327. if( IsSavePosition(8,8,maingrid,KingBlackPositionY,KingBlackPositionX,0)){
  328. SetConsoleTextAttribute(console, 20);
  329. printf("chick!!\a\a\aa\a\aa\a\a");
  330.  
  331. }
  332.  
  333. }
  334.  
  335. }}
  336. void commandGame(){
  337.  
  338. }
  339.  
  340. int detailsPage() { // returns the users choice <1\0> and scan their in formation
  341. char temp[25] ;
  342. char choice ;
  343. printAtXY(30, 0, "player one\n");
  344. printAtXY(0, 0, "ENTER YOUR FIRST NAME: ");
  345. scanf(" %s", temp);
  346. do {
  347. printAtXY(0, 0, "DO YOU WANT TO PLAY WITH WHITE OR BLACK: <W/B> ");
  348. scanf("%c", &choice);
  349. choice = tolower(choice);
  350. } while (choice != 'w' && choice != 'b');
  351. switch (choice) {
  352. case 'b':
  353. strcpy(player[1].name, temp);
  354. case 'w':
  355. strcpy(player[0].name, temp);
  356. }
  357. system("cls||clear");
  358. printAtXY(30, 0, "player two\n");
  359. printAtXY(0, 0, "ENTER YOUR FIRST NAME: ");
  360. scanf(" %s", (choice == 'w') ? player[1].name : player[0].name);
  361. system("cls||clear");
  362.  
  363. do {
  364. printAtXY(0, 0, "do you want it with arrows <a> or with keyboard <k>: ");
  365. scanf(" %c", &choice);
  366. }while ( choice != 'a' && choice != 'k');
  367. system("@cls||clear");
  368. switch (choice){
  369. case 'a':
  370. return 0; // he choose the arrows
  371. case 'k':
  372. return 1; // he choose the keyboard
  373. }
  374. }
  375.  
  376. //****************************************************data management************************************************************************
  377. void save(char fileName[],int currentPlayerIndex){ // save the current data for the game
  378. register int i, j;
  379. FILE * filePointer = fopen(fileName, "a+"); // this is the file gameRecord.txt opened in reading and appending mode
  380. fseek(filePointer,0,SEEK_END); // go to the end for the file to append new things
  381. fprintf(filePointer,"\n");
  382. for (i = 0;i < 8; i++){ // saving the mainGrid in one line of 64 character
  383. for (j = 0;j < 8; j++){
  384. fprintf(filePointer,"%c",maingrid[i][j]);
  385. }
  386. }
  387. fprintf(filePointer,", %s ,%d,%d,%d,%d,%d,%d,%d:",player[0].name, // printing the first player details
  388. player[0].seconds,player[0].moves,player[0].Pawn, player[0].Rook,player[0].Knight,player[0].Bishop,player[0].Queen);
  389. fprintf(filePointer," %s ,%d,%d,%d,%d,%d,%d,%d,",player[1].name, // printing the second player details
  390. player[1].seconds,player[1].moves,player[1].Pawn,player[1].Rook,player[1].Knight,player[1].Bishop,player[1].Queen);
  391. fprintf(filePointer,"%d",currentPlayerIndex); // printing int last of the line whose the last move belongs to?
  392. fclose(filePointer); // a good programming practice :)
  393. }
  394.  
  395. void loadLast(char fileName[],int *playerIndex){ //it reads from the text file
  396. register int i = 0;
  397. char temp = '\0';
  398. FILE * filePointer = fopen(fileName, "r"); // this is the file gameRecord.txt opened in reading mode
  399. fseek(filePointer,-lineLen(fileName),SEEK_END); // go to the start of the last line of the file to start reading old things and
  400. while (i < 64){ // this is to load the mainGrid
  401. fscanf(filePointer,"%c",&temp);
  402. maingrid[i/8][i%8] = temp;
  403. i++;
  404. }
  405. fscanf(filePointer,", %s ,%d,%d,%d,%d,%d,%d,%d: %s ,%d,%d,%d,%d,%d,%d,%d,%d",player[0].name, &player[0].seconds, // note the i add spaces after
  406. &player[0].moves, &player[0].Pawn, &player[0].Rook, &player[0].Knight, &player[0].Bishop, // and before the string formate
  407. &player[0].Queen, player[1].name, &player[1].seconds, &player[1].moves, &player[1].Pawn,
  408. &player[1].Rook, &player[1].Knight, &player[1].Bishop, &player[1].Queen ,playerIndex);
  409.  
  410. fclose(filePointer); // a good programming practice :)
  411. }
  412.  
  413. int lineLen(char fileName[]){ // this function go to the end of the file and returns how many character are there between the end of the
  414. char temp = '\0'; // file and new line character '\n' , note that i am taking two of the i in the return statement as there is
  415. register int i = 0; // a character in the end of any file (i don't know i don't care) ,and there is the new line character .
  416. FILE * fp = fopen(fileName,"r");
  417. while (temp != '\n'){
  418. fseek(fp,-(i++),SEEK_END);
  419. fscanf(fp,"%c",&temp);
  420. }
  421. fclose(fp); // a good programming practice :)
  422. return i - 2;
  423. }
  424.  
  425. void winner(int playerIndex){
  426. register int i , key;
  427. char lineWin[256], lineHOFame[256];
  428. int colors[3] = {1,4,11};
  429. FILE * fp= fopen("WIN.txt","r");
  430. fseek(fp,0,SEEK_SET);
  431. printAtXY(0,10,"");
  432. for (i = 1; i < 10 ; i++){ // printing win gradually
  433. fgets(lineWin,256,fp);
  434. ChangeDisplayColor(i);
  435. printAtXY(20,0,"");
  436. printf("%s", lineWin);
  437. Beep(2000,100);
  438. }
  439. for (i = 0; i < 3 ; i++){ // printing three win words
  440. Beep(1000 * (i + 1),500);
  441. fseek(fp,0,SEEK_SET);
  442. system("cls||clear");
  443. ChangeDisplayColor(colors[i]);
  444. printAtXY(0,10,"");
  445. //Beep(1000 * (i + 1),500);
  446. while (!feof(fp)){
  447. fgets(lineWin,256,fp);
  448. printAtXY(20,0,"");
  449. printf("%s",lineWin);
  450. }
  451. }
  452. fclose(fp); // closing the pointer of the WIN file
  453. Sleep(500);
  454. ChangeDisplayColor(6);
  455. printAtXY(30,2,"CONGRATUATIONS ");
  456. printf("%s YOU HAVE WON THE GAME :) \n",player[playerIndex].name);
  457. FILE * filePointer = fopen("hall of fame.txt","a+");
  458. int score = player[playerIndex].Pawn + // pawn worth one point
  459. player[playerIndex].Rook * 5 + // Rook worth five point
  460. player[playerIndex].Knight * 3 + // Knight worth three point
  461. player[playerIndex].Bishop * 3 + // Bishop worth three point
  462. player[playerIndex].Queen * 9 ; // Queen worth nine point
  463. fseek(filePointer,0,SEEK_END);
  464. fprintf(filePointer,"\nNAME:\t%s\t\tSCORE:\t%d\t\tMOVES:\t%d\t\t\t",player[playerIndex].name,score,player[playerIndex].moves);
  465. fseek(filePointer,0,SEEK_SET);
  466. while (!feof(filePointer)){
  467. fgets(lineHOFame,256,filePointer);
  468. printAtXY(10,0,"");
  469. printf("%s",lineHOFame);
  470. }
  471. fclose(filePointer); // closing the pointer of hale of fame file
  472. ChangeDisplayColor(15);
  473. printf("\n\n\nPress ESC to go to the main menu:");
  474. do{
  475. key = getSpecialInput();
  476. if (key == 7){
  477. system("cls||clear");
  478. mainPage();
  479. }
  480. }while (key != 7);
  481. }
  482.  
  483. //**************************************************validations and editing the grid*********************************************************
  484.  
  485. int validateCommand(char *command,
  486. int playerIndex) { //this function will validate the input and analyze it and call the editGrid function within it if the validation are success <return 1>, if not it will return 0
  487. if (!strcmpi(command,
  488. "help")) { // note that the strcmpi() returns 0 if the two strings are the same , and strcmpi() is not case sensitive
  489. // call help()
  490. help();
  491. return 1; // 1 means success
  492. } else if (!strcmpi(command, "undo")) {
  493. // call undo()
  494. printf("undo called");
  495. return 1;
  496. } else if (!strcmpi(command, "exit")) {
  497. // call mainPage()
  498. mainPage();
  499. return 1;
  500. } else if (strlen(command) >=
  501. 4) { // the purpose of the nested if statements is to reduce the condition that is validated i mean if it fails the first one it doesn't need to validate any more
  502. int preX, preY, newX, newY; // now translating the string into numbers
  503. preX = tolower(command[0]) - 'a'; // thug life with ASCII :)
  504. preY = 8 - (command[1] - '0'); // as the user start counting form down but i count from up and i count form 0 :)
  505. newX = tolower(command[2]) - 'a';
  506. newY = 8 - (command[3] - '0');
  507. if ((preX >= 0 && preX <= 7) && (preY >= 0 && preY <= 7) && (newX >= 0 && newX <= 7) &&
  508. (newY >= 0 && newY <= 7)) {
  509. editGrid(8, 8, maingrid, preY, preX, newY, newX,
  510. playerIndex); // editing the grid with the validated command
  511. return 1; // success
  512. }
  513. }
  514. return 0; // 0 means failed
  515. }
  516.  
  517. void help() {
  518. printf("You can enter to the command line the following commands only:\n"\
  519. "undo -> to return to the last move.\n"\
  520. "help -> to show the commands you can enter.\n"\
  521. "exit -> to exit the game and go to the main menu.\n"\
  522. "the move command may be enter in the following formate:\n"\
  523. "current column + current row + new column + new row\n"\
  524. "note you may enter the game command in only such way, and if any mistake is made, the command wont be accepted.\n"\
  525. "note that: an case of promotion you will be asked to enter the kind of promotion you desire\n"\
  526. "have a happy game :)\n");
  527. }
  528.  
  529. int editGrid(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
  530. int playerIndex) { //it edits the mainGrid as asked and how the arguments say
  531. switch (grid[preY][preX]) {
  532. case 'p': // white pawn
  533. if (pawn(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  534. grid[preY][preX] = '0'; // remove the pawn from his old place
  535. giveToPlayer(playerIndex,
  536. tolower(grid[newY][newX])); // give the player something if he removed a piece
  537. if (newY == 0) { // check for promotion
  538. grid[newY][newX] = getDecision(playerIndex); // tell me what kind of promotion you may need
  539. player[playerIndex].moves++;
  540. return; // only to terminate the function thats all
  541. }
  542. grid[newY][newX] = 'p';
  543. player[playerIndex].moves++;
  544. return 1;
  545. }
  546. break;
  547. case 'r': // white rook
  548. if (rook(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  549. grid[preY][preX] = '0'; // remove the rook from his old place
  550. giveToPlayer(playerIndex,
  551. tolower(grid[newY][newX])); // give the player something if he removed a piece
  552. grid[newY][newX] = 'r';
  553. player[playerIndex].moves++;
  554. return 1;
  555.  
  556. }
  557. break;
  558. case 'n': // white knight
  559. if (knight(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  560. grid[preY][preX] = '0'; // remove the knight from his old place
  561. giveToPlayer(playerIndex,
  562. tolower(grid[newY][newX])); // give the player something if he removed a piece
  563. grid[newY][newX] = 'n';
  564. player[playerIndex].moves++;
  565. return 1;
  566.  
  567. }
  568. break;
  569. case 'b': // white bishop
  570. if (bishop(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  571. grid[preY][preX] = '0'; // remove the bishop from his old place
  572. giveToPlayer(playerIndex,
  573. tolower(grid[newY][newX])); // give the player something if he removed a piece
  574. grid[newY][newX] = 'b';
  575. player[playerIndex].moves++;
  576. return 1;
  577.  
  578. }
  579. break;
  580. case 'q': // white queen
  581. if (queen(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  582. grid[preY][preX] = '0'; // remove the queen from her old place
  583. giveToPlayer(playerIndex,
  584. tolower(grid[newY][newX])); // give the player something if he removed a piece
  585. grid[newY][newX] = 'q';
  586. player[playerIndex].moves++;
  587. return 1;
  588.  
  589. }
  590. break;
  591. case 'k': // white king
  592. if (king(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  593. if (newX == preX + 2) {
  594. grid[preY][preX] = '0';
  595. giveToPlayer(playerIndex, tolower(grid[newY][newX]));
  596. grid[newY][newX] = 'k';
  597. grid[7][7] = '0';
  598. grid[7][5] = 'r';
  599. KingWightPositionX=newX;
  600. KingWightPositionY=newY;
  601. return 1;
  602.  
  603. // give the player something if he removed a piece
  604.  
  605.  
  606. } else if (newX == preX - 2) {
  607. grid[preY][preX] = '0';
  608. giveToPlayer(playerIndex, tolower(grid[newY][newX]));
  609. grid[newY][newX] = 'k';
  610. grid[7][0] = '0';
  611. grid[7][3] = 'r';
  612. KingWightPositionX=newX;
  613. KingWightPositionY=newY;
  614. return 1;
  615.  
  616.  
  617.  
  618. } else {
  619. grid[preY][preX] = '0'; // remove the king from his old place
  620. giveToPlayer(playerIndex,
  621. tolower(grid[newY][newX])); // give the player something if he removed a piece
  622. grid[newY][newX] = 'k';
  623. player[playerIndex].moves++;
  624. KingWightPositionX=newX;
  625. KingWightPositionY=newY;
  626. return 1;
  627.  
  628. }
  629. }
  630. break;
  631. case 'P': // black pawn
  632. if (pawn(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  633. grid[preY][preX] = '0'; // remove the pawn from his old place
  634. giveToPlayer(playerIndex,
  635. tolower(grid[newY][newX])); // give the player something if he removed a piece
  636. if (newY == 7) { // check for promotion
  637. grid[newY][newX] = getDecision(playerIndex); // tell me what kind of promotion you may need
  638. player[playerIndex].moves++;
  639. return 1; // only to terminate the function thats all
  640. }
  641. grid[newY][newX] = 'P';
  642. player[playerIndex].moves++;
  643. return 1;
  644.  
  645. }
  646. break;
  647. case 'R': // black rook
  648. if (rook(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  649. grid[preY][preX] = '0'; // remove the rook from his old place
  650. giveToPlayer(playerIndex,
  651. tolower(grid[newY][newX])); // give the player something if he removed a piece
  652. grid[newY][newX] = 'R';
  653. player[playerIndex].moves++;
  654. return 1;
  655.  
  656. }
  657. break;
  658. case 'N': // black knight
  659. if (knight(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  660. grid[preY][preX] = '0'; // remove the knight from his old place
  661. giveToPlayer(playerIndex,
  662. tolower(grid[newY][newX])); // give the player something if he removed a piece
  663. grid[newY][newX] = 'N';
  664. player[playerIndex].moves++;
  665. return 1;
  666.  
  667. }
  668. break;
  669. case 'B': // black bishop
  670. if (bishop(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  671. grid[preY][preX] = '0'; // remove the bishop from his old place
  672. giveToPlayer(playerIndex,
  673. tolower(grid[newY][newX])); // give the player something if he removed a piece
  674. grid[newY][newX] = 'B';
  675. player[playerIndex].moves++;
  676. return 1;
  677.  
  678. }
  679. break;
  680. case 'Q': // black queen
  681. if (queen(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  682. grid[preY][preX] = '0'; // remove the queen from her old place
  683. giveToPlayer(playerIndex,
  684. tolower(grid[newY][newX])); // give the player something if he removed a piece
  685. grid[newY][newX] = 'Q';
  686. player[playerIndex].moves++;
  687. return 1;
  688.  
  689. }
  690. break;
  691. case 'K': // black king
  692. if (king(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
  693. if (newX == preX + 2) {
  694. grid[preY][preX] = '0';
  695. giveToPlayer(playerIndex, tolower(grid[newY][newX]));
  696. grid[newY][newX] = 'K';
  697. grid[0][7] = '0';
  698. grid[0][5] = 'R';
  699.  
  700. KingBlackPositionX=newX;
  701. KingBlackPositionY=newY;
  702. return 1;
  703.  
  704. } else if (newX == preX - 2) {
  705. grid[preY][preX] = '0';
  706. giveToPlayer(playerIndex, tolower(grid[newY][newX]));
  707. grid[newY][newX] = 'K';
  708. grid[0][0] = '0';
  709. grid[0][3] = 'R';
  710.  
  711. KingBlackPositionX=newX;
  712. KingBlackPositionY=newY;
  713. return 1;
  714.  
  715.  
  716. } else {
  717. grid[preY][preX] = '0'; // remove the king from his old place
  718. giveToPlayer(playerIndex,
  719. tolower(grid[newY][newX])); // give the player something if he removed a piece
  720. grid[newY][newX] = 'K';
  721. player[playerIndex].moves++;
  722.  
  723. KingBlackPositionX=newX;
  724. KingBlackPositionY=newY;
  725. return 1;
  726.  
  727. }
  728. }
  729. break;
  730. default: return 0;
  731.  
  732. }
  733. }
  734.  
  735. void giveToPlayer(int playerIndex, char piece) { // it gives a certain player what he gained
  736. switch (piece) {
  737. case 'p':
  738. player[playerIndex].Pawn++;
  739. break;
  740. case 'r':
  741. player[playerIndex].Rook++;
  742. break;
  743. case 'n':
  744. player[playerIndex].Knight++;
  745. break;
  746. case 'b':
  747. player[playerIndex].Bishop++;
  748. break;
  749. case 'q':
  750. player[playerIndex].Queen++;
  751. break;
  752. default:
  753. break;
  754. }
  755. }
  756.  
  757. char getDecision(int playerIndex) { // requires ctype.h it gets the kind of promotion the user needs
  758. char decision;
  759. printf("Enter the type of promotion you want: <q,b,n,r> ");
  760. scanf(" %c", &decision);
  761. decision = tolower(decision);
  762. if (decision != 'q' && decision != 'n' && decision != 'r' && decision != 'b') {
  763. printf("WORNG INPUT!!\n");
  764. return getDecision(playerIndex);
  765. }// returning the promotion based on the color of the player
  766. if (!strcmpi(player[playerIndex].color, "white")) { // note that strcmpi() is not case sensitive
  767. return tolower(decision);
  768. } else if (!strcmpi(player[playerIndex].color, "black")) { // note that strcmpi() is not case sensitive
  769. return toupper(decision);
  770. }
  771. printf("SORRY, SOMETHING WENT WRONG!!");
  772. return getDecision(playerIndex);
  773. }
  774.  
  775. // the validation functions for each piece
  776. int pawn(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
  777. int playerIndex) { // contains the validation for the pawn
  778. // write the validation
  779. if (playerIndex == 0) { /*white player */
  780. if (grid[preY][preX] == 'p') {/* it must be p as lower */
  781. if (isupper(grid[newY][newX]) || grid[newY][newX] == '0') { /* next position must be black or empty */
  782. if ((newY == preY - 1 && grid[newY][newX] == '0' &&
  783. newX == preX/*this condition if white want to move one piece to empty cell*/) ||
  784. (newY == preY - 2 && grid[newY][newX] == '0' && preY == 6 && newX == preX && grid[preY - 1][preX] ==
  785. '0'/*this condition if white to move two cell only if it is the first move of it*/ )) {
  786. return 1;
  787. } else if (newY == preY - 1 && (newX == preX + 1 || newX == preX - 1) && isupper(grid[newY][newX]) &&
  788. grid[newY][newX] != '0') { /*this if white want to eat black piece*/
  789. return 1;
  790. } else return 0;
  791. } else return 0;
  792.  
  793. } else return 0;
  794.  
  795. } else if (playerIndex == 1) {/*black player */
  796. if (grid[preY][preX] == 'P') {/* it must be p as upper */
  797. if (islower(grid[newY][newX]) || grid[newY][newX] == '0') { /* next position must be white or empty */
  798. if ((newY == preY + 1 && grid[newY][newX] == '0' &&
  799. newX == preX/*this condition if black want to move one piece to empty cell*/) ||
  800. (newY == preY + 2 && grid[newY][newX] == '0' && preY == 1 && newX == preX && grid[preY + 1][preX] ==
  801. '0'/*this condition if black to move two cell only if it the first position of it*/ )) {
  802. return 1;
  803. } else if (newY == preY + 1 && (newX == preX + 1 || newX == preX - 1) && islower(grid[newY][newX]) &&
  804. grid[newY][newX] != '0') { /*this if black want to eat white piece*/
  805. return 1;
  806. } else return 0;
  807.  
  808. } else return 0;
  809. } else return 0;
  810. }
  811. return 0;
  812. }
  813.  
  814. int RookWhiteMoved[2] = {0, 0};
  815. int RookBlackMoved[2] = {0, 0};
  816.  
  817. int rook(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
  818. int playerIndex) { // contains the validation for the rook
  819. if (isalpha(grid[preY][preX])) { // check for the content if the index first
  820. if (islower(grid[preY][preX])) { // the element is white (small)
  821. if (isupper(grid[newY][newX]) || grid[newY][newX] ==
  822. '0') { // as the white (small litters) can only move to empty or black (capital) positions
  823. if (rookInEmptyPath(8, 8, grid, preY, preX, newY,
  824. newX)) {// the two positions are in the same horizontal or vertical line
  825. if (preX == 7 && preY == 7)RookWhiteMoved[1] = 1;
  826. else if (preX == 0 && preY == 7)RookWhiteMoved[0] = 1;
  827. return 1;
  828. }
  829. }
  830. } else {
  831. if (islower(grid[newY][newX]) || grid[newY][newX] ==
  832. '0') { // as the black (capital litters) can only move to empty or white (small) positions
  833. if (rookInEmptyPath(8, 8, grid, preY, preX, newY,
  834. newX)) { // the two positions are in the same horizontal or vertical line
  835. if (preX == 7 && preY == 0)RookBlackMoved[1] = 1;
  836. else if (preX == 0 && preY == 0)RookBlackMoved[0] = 1;
  837. return 1;
  838. }
  839. }
  840. }
  841. }
  842. return 0; // rook can't move to this place
  843. }
  844.  
  845. int rookInEmptyPath(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY,
  846. int newX) { // used to tell the whether the pass is empty 1 or not 0
  847. register int i;
  848. int emptyPass = 1; //is a variable to tell if the pass between the two positions is clean
  849. if (newX == preX) { // in the same vertical line
  850. if (preY < newY) { // new place is down the previous place
  851. for (i = preY + 1; i <
  852. newY; i++) { // looping through the pass between the two elements to check for the existence if another element in between
  853. if (grid[i][preX] != '0') {
  854. emptyPass = 0; // pass isn't empty
  855. break;
  856. }
  857. }
  858. } else if (preY > newY) { // new place is up the previous place
  859. for (i = preY - 1; i >
  860. newY; i--) { // looping through the pass between the two elements to check for the existence if another element in between
  861. if (grid[i][preX] != '0') {
  862. emptyPass = 0; // pass isn't empty
  863. break;
  864. }
  865. }
  866. }
  867. } else if (preY == newY) {
  868. if (preX < newX) { // new place is to the right of the previous place
  869. for (i = preX + 1; i <
  870. newX; i++) { // looping through the pass between the two elements to check for the existence if another element in between
  871. if (grid[preY][i] != '0') {
  872. emptyPass = 0; // pass isn't empty
  873. break;
  874. }
  875. }
  876. } else if (preX > newX) { // new place is to the left of the previous place
  877. for (i = preX - 1; i >
  878. newX; i--) { // looping through the pass between the two elements to check for the existence if another element in between
  879. if (grid[i][preX] != '0') {
  880. emptyPass = 0; // pass isn't empty
  881. break;
  882. }
  883. }
  884. }
  885. } else if ((preY != newY) || (preX != newX)) {
  886. emptyPass = 0;
  887. }
  888. return emptyPass;
  889. }
  890.  
  891. int knight(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
  892. int playerIndex) { // contains the validation for the knight
  893. if ((playerIndex == 0 && islower(grid[preY][preX]) && grid[preY][preX] != '0' &&
  894. (grid[newY][newX] == '0' || isupper(grid[newY][newX]))) /*white about to plat*/
  895. || (playerIndex == 1 && isupper(grid[preY][preX]) && grid[preY][preX] != '0' &&
  896. (grid[newY][newX] == '0' || islower(grid[newY][newX])))) { /*black about to move*/
  897. if (newX == preX + 1 && newY == preY + 2)
  898. return 1;
  899. else if (newX == preX + 2 && newY == preY + 1)
  900. return 1;
  901. else if (newX == preX + 2 && newY == preY - 1)
  902. return 1;
  903. else if (newX == preX + 1 && newY == preY - 2)
  904. return 1;
  905. else if (newX == preX - 1 && newY == preY - 2) /*there are 8 possiple moves for knight */
  906. return 1;
  907. else if (newX == preX - 2 && newY == preY - 1)
  908. return 1;
  909. else if (newX == preX - 2 && newY == preY + 1)
  910. return 1;
  911. else if (newX == preX - 1 && newY == preY + 2)
  912. return 1;
  913.  
  914. else return 0;
  915. } else return 0;
  916. }
  917.  
  918.  
  919. // write the validation
  920.  
  921.  
  922. int bishop(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
  923. int playerIndex) { // contains the validation for the bishop
  924. // write the validation
  925. if (isalpha(grid[preY][preX])) { // the old place is not empty
  926. if (islower(grid[preY][preX])) { // the element is white (small)
  927. if (isupper(grid[newY][newX]) || grid[newY][newX] ==
  928. '0') { // as the white (small litters) can only move to empty or black (capital) positions
  929. if (bishopInEmptyPath(8, 8, grid, preY, preX, newY,
  930. newX)) { // the two positions are synchronized the way the bishop needs with nothing in between
  931. return 1;
  932. }
  933. }
  934. } else { // the element is black (capital)
  935. if (islower(grid[newY][newX]) || grid[newY][newX] ==
  936. '0') { // as the black (capital litters) can only move to empty or white (small) positions
  937. if (bishopInEmptyPath(8, 8, grid, preY, preX, newY,
  938. newX)) { // the two positions are synchronized the way the bishop needs with nothing in between
  939. return 1;
  940. }
  941. }
  942. }
  943. }
  944. return 0; // bishop can't move to this place
  945. }
  946.  
  947. int bishopInEmptyPath(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY,
  948. int newX) { // check if there is nothing between the two place
  949. register int i; // iteration variable
  950. for (i = 1; i < 8; i++) {
  951. if (((preX + i == newX) &&
  952. (preY + i == newY))) { // figure it out draw the grid to understand otherwise you won't
  953. return 1;
  954. } else if (grid[preY + i][preX + i] != '0') {
  955. break; // it fails and breaks because there is something in the path between the two items
  956. }
  957. }
  958. for (i = 1; i < 8; i++) {
  959. if (((preX - i == newX) &&
  960. (preY - i == newY))) { // figure it out draw the grid to understand otherwise you won't
  961. return 1;
  962. } else if (grid[preY - i][preX - i] != '0') {
  963. break; // it fails because there is something in the path between the two items
  964. }
  965. }
  966. for (i = 1; i < 8; i++) {
  967. if (((preX + i == newX) &&
  968. (preY - i == newY))) { // figure it out draw the grid to understand otherwise you won't
  969. return 1;
  970. } else if (grid[preY - i][preX + i] != '0') {
  971. break; // it fails because there is something in the path between the two items
  972. }
  973. }
  974. for (i = 1; i < 8; i++) {
  975. if (((preX - i == newX) &&
  976. (preY + i == newY))) { // figure it out draw the grid to understand otherwise you won't
  977. return 1;
  978. } else if (grid[preY + i][preX - i] != '0') {
  979. break; // it fails because there is something in the path between the two items
  980. }
  981. }
  982. return 0;
  983. }
  984.  
  985. int queen(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
  986. int playerIndex) { // contains the validation for the queen
  987. // write the validation
  988. if (isalpha(grid[preY][preX])) { // the old place is not empty
  989. if (islower(grid[preY][preX])) { // the element is white (small)
  990. if (isupper(grid[newY][newX]) || grid[newY][newX] ==
  991. '0') { // as the white (small litters) can only move to empty or black (capital) positions
  992. if (bishopInEmptyPath(8, 8, grid, preY, preX, newY, newX) ||
  993. rookInEmptyPath(8, 8, grid, preY, preX, newY,
  994. newX)) { // the two positions are in the same horizontal or vertical line or they are synchronized the way the bishop needs with nothing in between
  995. return 1;
  996. }
  997. }
  998. } else { // the element is black (capital)
  999. if (islower(grid[newY][newX]) || grid[newY][newX] ==
  1000. '0') { // as the black (capital litters) can only move to empty or white (small) positions
  1001. if (bishopInEmptyPath(8, 8, grid, preY, preX, newY, newX) ||
  1002. rookInEmptyPath(8, 8, grid, preY, preX, newY,
  1003. newX)) { // the two positions are in the same horizontal or vertical line or they are synchronized the way the bishop needs with nothing in between
  1004. return 1;
  1005. }
  1006. }
  1007. }
  1008. }
  1009. return 0; // this return statement can be removed later when writing this function
  1010. }
  1011.  
  1012. int king(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex) {
  1013. // contains the validation for the king
  1014. static int KingWightMoved = 0;
  1015. static int KingBlackMoved = 0;
  1016. if (playerIndex == 0 && KingWightMoved == 0 && grid[newY][newX] == '0'&&preY==newY) {
  1017. if (newX == preX + 2 && RookWhiteMoved[1] == 0 && grid[newY][preX + 1] == '0') {
  1018.  
  1019. KingWightMoved = 1;
  1020. return 1;
  1021.  
  1022. } else if (newX == preX - 2 && RookWhiteMoved[0] == 0 && grid[newY][preX - 1] == '0') {
  1023. KingWightMoved = 1;
  1024. return 1;
  1025. }
  1026.  
  1027. } else if (playerIndex == 1 && KingBlackMoved == 0 && grid[newY][newX] == '0'&&preY==newY) {
  1028. if (newX == preX + 2 && RookBlackMoved[1] == 0 && grid[newY][preX + 1] == '0') {
  1029. KingBlackMoved = 1;
  1030. return 1;
  1031.  
  1032.  
  1033. } else if (newX == preX - 2 && RookBlackMoved[0] == 0 && grid[newY][preX - 1] == '0') {
  1034. KingBlackMoved = 1;
  1035. return 1;
  1036.  
  1037.  
  1038. }
  1039. }
  1040.  
  1041. if (playerIndex == 0 && grid[preY][preX] != '0' && islower(grid[preY][preX]) &&
  1042. (grid[newY][newX] == '0' || isupper(grid[newY][newX]))) {/*white about ti move */
  1043.  
  1044. if (newX == preX && newY == preY + 1) {
  1045. KingWightMoved = 1;
  1046. return 1;
  1047. } else if (newX == preX && newY == preY - 1) {
  1048. KingWightMoved = 1;
  1049. return 1;
  1050. } else if (newY == preY && newX == preX + 1) {
  1051. KingWightMoved = 1;
  1052. return 1;
  1053. } else if (newY == preY && newX == preX - 1) {
  1054. KingWightMoved = 1;
  1055. return 1;
  1056. } else if (newY == preY + 1 && newX == preX + 1) {
  1057. KingWightMoved = 1;
  1058. return 1;
  1059. } else if (newY == preY - 1 && newX == preX - 1) {
  1060. KingWightMoved = 1;
  1061. return 1;
  1062. } else if (newY == preY - 1 && newX == preX + 1) {
  1063. KingWightMoved = 1;
  1064. return 1;
  1065. } else if (newY == preY + 1 && newX == preX - 1) {
  1066. KingWightMoved = 1;
  1067. return 1;
  1068. } else return 0;
  1069.  
  1070. } else if (playerIndex == 1 && grid[preY][preX] != '0' && isupper(grid[preY][preX]) &&
  1071. (grid[newY][newX] == '0' || islower(grid[newY][newX]))) {
  1072.  
  1073. if (newX == preX && newY == preY + 1) {
  1074. KingBlackMoved = 1;
  1075. return 1;
  1076. } else if (newX == preX && newY == preY - 1) {
  1077. KingBlackMoved = 1;
  1078. return 1;
  1079. } else if (newY == preY && newX == preX + 1) {
  1080. KingBlackMoved = 1;
  1081. return 1;
  1082. } else if (newY == preY && newX == preX - 1) {
  1083. KingBlackMoved = 1;
  1084. return 1;
  1085. } else if (newY == preY + 1 && newX == preX + 1) {
  1086. KingBlackMoved = 1;
  1087. return 1;
  1088. } else if (newY == preY - 1 && newX == preX - 1) {
  1089. KingBlackMoved = 1;
  1090. return 1;
  1091. } else if (newY == preY - 1 && newX == preX + 1) {
  1092. KingBlackMoved = 1;
  1093. return 1;
  1094. } else if (newY == preY + 1 && newX == preX - 1) {
  1095. KingBlackMoved = 1;
  1096. return 1;
  1097. } else return 0;
  1098. }
  1099. return 0;
  1100. }
  1101.  
  1102. int IsSavePosition(int a8, int b8, char grid[a8][b8],int positionY ,int PositionX,int playerindex){ //
  1103. if (playerindex==0){
  1104. for(int count1=0;count1<a8;count1++){
  1105. for (int count2=0;count2<b8;count2++){
  1106. if(isupper(grid[count1][count2])){
  1107. switch (grid[count1][count2]){
  1108. case 'P':
  1109. if(pawn( a8, b8, grid, count1, count2, positionY,PositionX , 1)){
  1110. return 1;
  1111. }
  1112. break;
  1113. case 'R':
  1114. if(rook( a8, b8, grid, count1, count2, positionY,PositionX , 1)){
  1115. return 1;
  1116. }break;
  1117. case 'B':
  1118. if(bishop( a8, b8, grid, count1, count2, positionY,PositionX , 1)){
  1119. return 1;
  1120.  
  1121. }break;
  1122. case 'Q':
  1123. if(queen( a8, b8, grid, count1, count2, positionY,PositionX , 1)){
  1124. return 1;}
  1125. break;
  1126.  
  1127. case 'N':
  1128. if(knight( a8, b8, grid, count1, count2, positionY,PositionX , 1)){
  1129. return 1;}
  1130. break;
  1131. case 'K':
  1132. if(king(a8, b8, grid, count1, count2, positionY,PositionX , 1)){
  1133. // return 1;
  1134. }break;
  1135. }
  1136. }
  1137. }
  1138. }return 0;
  1139.  
  1140. }else if(playerindex==1){
  1141. for(int count1=0;count1<a8;count1++){
  1142. for (int count2=0;count2<b8;count2++){
  1143. if(islower(grid[count1][count2])){
  1144. switch (grid[count1][count2]){
  1145. case 'p':
  1146. if(pawn( a8, b8, grid, count1, count2, positionY,PositionX , 0)){
  1147. return 1;
  1148.  
  1149. }
  1150. break;
  1151. case 'r':
  1152. if(rook( a8, b8, grid, count1, count2, positionY,PositionX , 0)){
  1153. return 1;
  1154.  
  1155. }break;
  1156. case 'b':
  1157. if(bishop( a8, b8, grid, count1, count2, positionY,PositionX , 0)){
  1158. return 1;
  1159. }break;
  1160. case 'q':
  1161. if(queen( a8, b8, grid, count1, count2, positionY,PositionX , 0)){
  1162. return 1;}
  1163. break;
  1164. case 'n':
  1165. if(knight( a8, b8, grid, count1, count2, positionY,PositionX , 0)){
  1166. return 1;}
  1167. break;
  1168. case 'K':
  1169. if(king(a8, b8, grid, count1, count2, positionY,PositionX , 0)){
  1170. return 1;
  1171. }break;
  1172. }
  1173. }
  1174. }
  1175. }
  1176. }
  1177. return 0;
  1178. }
  1179.  
  1180. void ChangeDisplayColor(
  1181. int color) { // source is {https://www.youtube.com/watch?v=gRLzd-2sqIQ} don't laugh at me library is windows.h
  1182. HANDLE console;
  1183. console = GetStdHandle(STD_OUTPUT_HANDLE);
  1184. SetConsoleTextAttribute(console, color);
  1185. /*
  1186. 0 BLACK
  1187. 1 BLUE
  1188. 2 GREEN
  1189. 3 CYAN
  1190. 4 RED
  1191. 5 MAGENTA
  1192. 6 BROWN
  1193. 7 LIGHTGRAY
  1194. 8 DARKGRAY
  1195. 9 LIGHTBLUE
  1196. 10 LIGHTGREEN
  1197. 11 LIGHTCYAN
  1198. 12 LIGHTRED
  1199. 13 LIGHTMAGENTA
  1200. 14 YELLOW
  1201. 15 WHITE
  1202. */
  1203. }
  1204.  
  1205. // **************************************************main grid************************************************************ //
  1206.  
  1207. void SendToPrintGrade(int length, int weight, char MainGrade_local[length][weight], int positionATX, int positionATY) { /* this function is used to print grid but with two ways if player choose arrows this function will get the position of pointer at x and y then
  1208. it will highlight this cell with green , if user choose keyboard this function will get -1 and -1 for x and y so there is no highlighted cell
  1209. then it will call printgrid function and send the number rows and coulum of each cell , in the end of this function it will print a to h as
  1210. flags for each coloum */
  1211. HANDLE console;
  1212. console = GetStdHandle(STD_OUTPUT_HANDLE);
  1213. printAtXY(90, 5, "");
  1214. for (int count1 = 0; count1 < 8; count1++) {
  1215. PrintColoredLinesIndride(count1); /*to print two imaginary line */
  1216. for (int count2 = 0; count2 < 8; count2++) {
  1217. if (count1 == positionATY &&
  1218. count2 == positionATX) { /*if the cell is highlighted we will deal with it with anther condition */
  1219. if (islower(MainGrade_local[count1][count2]) &&
  1220. MainGrade_local[count1][count2] != '0') {/*to check if highlighted is white */
  1221. SetConsoleTextAttribute(console, 167);
  1222. printf(" %c ", MainGrade_local[count1][count2]);
  1223. if (count2 == 7) { /*if the cell is the last one on row we will print the number of next row */
  1224. SetConsoleTextAttribute(console, 0);
  1225. printf(" |\n");
  1226. PrintRowsNumber(count1);
  1227.  
  1228. }
  1229. } else if (MainGrade_local[count1][count2] != '0') { /*if the piece is black*/
  1230.  
  1231. SetConsoleTextAttribute(console, 160);
  1232. printf(" %c ",
  1233. (MainGrade_local[count1][count2] == 0) ? ' ' : MainGrade_local[count1][count2]);
  1234. if (count2 == 7) {
  1235. SetConsoleTextAttribute(console, 0);
  1236. printf(" |\n");
  1237. PrintRowsNumber(count1);
  1238.  
  1239. }
  1240. } else { /*there is no piece */
  1241. SetConsoleTextAttribute(console, 160);
  1242. printf(" ");
  1243. if (count2 == 7) {
  1244. SetConsoleTextAttribute(console, 0);
  1245. printf(" |\n");
  1246. PrintRowsNumber(count1);
  1247.  
  1248. }
  1249.  
  1250. }
  1251.  
  1252. } else printGrid(count1, count2, &MainGrade_local[count1][count2]);
  1253. }
  1254. PrintColoredLinesIndride(count1);
  1255.  
  1256. }
  1257.  
  1258. char determine_coloun_with_char[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
  1259. SetConsoleTextAttribute(console, 10);
  1260. printf("\n");
  1261. printAtXY(90, 0, "");
  1262. for (int count = 0; count < 8; count++)
  1263. printf(" %c ", determine_coloun_with_char[count]);
  1264. SetConsoleTextAttribute(console, 15);
  1265. if(flagPrintInvalidMove==1){
  1266. SetConsoleTextAttribute(console, 20);
  1267. printf("Invalid Move !!!");
  1268. flagPrintInvalidMove=0;
  1269. SetConsoleTextAttribute(console, 0);
  1270.  
  1271. }
  1272.  
  1273. // printf("\n");
  1274.  
  1275. }
  1276.  
  1277. void printGrid(int row, int colom, char *object) {
  1278. /*this function is to print each cells and class it into 5 cases 1_ white piece on white cell
  1279. 2_ white piece on black cell1
  1280. 3 _ black piece on white cell
  1281. 4_ black piece on black cell1
  1282. 5_empty cell
  1283. and this classification depends on the value of each cell lower -> black ,, upper ->white , o-> empty ;; and if the sum of rows and colum is even the
  1284. cell will be white , else it will be black*/
  1285. HANDLE console;
  1286. console = GetStdHandle(STD_OUTPUT_HANDLE);
  1287. if ((row + colom) % 2 == 0 && *object != '0' && isupper(*object)) {
  1288.  
  1289. SetConsoleTextAttribute(console, 112); /*this show when the place is white and the piece is black */
  1290. printf(" %c ", *object);
  1291. if (colom == 7) {
  1292. SetConsoleTextAttribute(console, 0);
  1293. printf(" |\n");
  1294. PrintRowsNumber(row);
  1295.  
  1296. }
  1297.  
  1298. } else if ((row + colom) % 2 == 1 && *object != '0' && isupper(*object)) {
  1299. SetConsoleTextAttribute(console, 64); /*this show when the place is black and the piece is black */
  1300. printf(" %c ", *object);
  1301. if (colom == 7) {
  1302. SetConsoleTextAttribute(console, 0);
  1303. printf(" |\n");
  1304. PrintRowsNumber(row);
  1305.  
  1306. }
  1307. } else if ((row + colom) % 2 == 0 && *object != '0' && islower(*object)) {
  1308. SetConsoleTextAttribute(console, 120); /*this show when the place is white and the piece is white */
  1309. printf(" %c ", *object);
  1310. if (colom == 7) {
  1311. SetConsoleTextAttribute(console, 0);
  1312. printf(" |\n");
  1313. PrintRowsNumber(row);
  1314.  
  1315. }
  1316.  
  1317. } else if ((row + colom) % 2 == 1 && *object != '0' && islower(*object)) {
  1318. SetConsoleTextAttribute(console, 71); /*this show when the place is white and the piece is white */
  1319. printf(" %c ", *object);
  1320. if (colom == 7) {
  1321. SetConsoleTextAttribute(console, 0);
  1322. printf(" |\n");
  1323. PrintRowsNumber(row);
  1324.  
  1325. }
  1326. } else if (*object == '0' && (row + colom) % 2 == 1) {
  1327. SetConsoleTextAttribute(console, 71); /*this show when the place is white and there is no piece */
  1328. printf(" ");
  1329. if (colom == 7) {
  1330. SetConsoleTextAttribute(console, 0);
  1331. printf(" |\n");
  1332. PrintRowsNumber(row);
  1333.  
  1334. }
  1335. } else {
  1336. SetConsoleTextAttribute(console, 120); /*this show when the place is white and there is no piece */
  1337. printf(" ");
  1338. if (colom == 7) {
  1339. SetConsoleTextAttribute(console, 0);
  1340. printf(" |\n");
  1341. PrintRowsNumber(row);
  1342.  
  1343. }
  1344. }
  1345. }
  1346.  
  1347.  
  1348. void printAtXY(int x, int y, const char *__fmt) { // you need to use sprintf or snprintf to formate the text to be taken
  1349. register int i;
  1350. for (i = 0; i < y; i++) {
  1351. printf("\n");
  1352. }
  1353. for (i = 0; i < x; i++) {
  1354. printf(" ");
  1355. }
  1356. printf("%s", __fmt);
  1357. }
  1358.  
  1359. void PrintColoredLinesIndride(
  1360. int NumOFline) { /* function print grade print only each of cells is one line only , and this function is to make each cells 5 lines in length */
  1361. HANDLE console;
  1362. console = GetStdHandle(STD_OUTPUT_HANDLE);
  1363. for (int i = 0; i < 2; i++) { /*the count is 4 as i want to print 4 imaginary line up and down the main line*/
  1364. /*print imaginary begin with white and end with with black */
  1365. for (int count = 0; count < 8; count++) {
  1366. if ((NumOFline % 2 ==
  1367. 0)) {/*this line to determine the begin it is white or black as next cells will depend on it*//*in this case it will be white as number of row is even*/
  1368. if (count % 2 ==
  1369. 0) { /*this line to determine the color of each cell*//*after knowing the begin color of each row we change the color of each cell */
  1370. SetConsoleTextAttribute(console, 120);
  1371. printf(" ");
  1372. if (count == 7) {
  1373. SetConsoleTextAttribute(console, 0);
  1374. printf(" |\n"); /*to make new line after end of each row*/
  1375. printAtXY(90, 0, "");
  1376. }
  1377. } else { /*to print black cell if count (number of coloum )is even */
  1378. SetConsoleTextAttribute(console, 71);
  1379. printf(" ");
  1380. if (count == 7) {
  1381. SetConsoleTextAttribute(console, 0);
  1382. printf(" |\n");
  1383. printAtXY(90, 0, "");
  1384. }
  1385. }
  1386. } else {/*this else means that the begin of this rows is black */
  1387. if (count % 2 == 1) { /*that's mean this cell is white */
  1388.  
  1389. SetConsoleTextAttribute(console, 120);
  1390. printf(" ");
  1391. if (count == 7) {
  1392. SetConsoleTextAttribute(console, 0);
  1393. printf(" |\n");
  1394. printAtXY(90, 0, "");
  1395. }
  1396. } else {/*that's mean this cell is black */
  1397. SetConsoleTextAttribute(console, 71);
  1398. printf(" ");
  1399. if (count == 7) {
  1400. SetConsoleTextAttribute(console, 0);
  1401. printf(" |\n");
  1402. printAtXY(90, 0, "");
  1403. }
  1404. }
  1405. }
  1406. }
  1407. }
  1408. }
  1409.  
  1410. void TODetermineThePosition(
  1411. int PlayerIndex) { /*this function was called when user choose playing with arrows and return the position of the pointer bounding limitation on it 0 to 7 only*/
  1412. HANDLE console;
  1413. console = GetStdHandle(STD_OUTPUT_HANDLE);
  1414. int preX = 4;
  1415.  
  1416. int preY = 4;
  1417. int newX = 4;
  1418. int newY = 4;
  1419. int ExitWhileLoop = 0;
  1420. static int positionATX = 4; /*this three line to print the begin grade */
  1421. static int positionATY = 4;
  1422. SendToPrintGrade(8, 8, maingrid, positionATX, positionATY);
  1423. while (1) {
  1424. switch (getSpecialInput()) {
  1425.  
  1426. case 0: /*4 next cases to determine the position of pointer */
  1427. if (positionATY > 0) { /*to make sure y position won't be negative*/
  1428. positionATY--;
  1429. system("@cls||clear");
  1430. SendToPrintGrade(8, 8, maingrid, positionATX, positionATY);
  1431.  
  1432. }
  1433. break;
  1434. case 1:
  1435. if (positionATY < 7) {/*to make sure y position won't be more than 7*/
  1436. positionATY++;
  1437. system("@cls||clear");
  1438. SendToPrintGrade(8, 8, maingrid, positionATX, positionATY);
  1439. }
  1440. break;
  1441. case 2:
  1442. if (positionATX < 7) { /*to make sure x position won't be more than 7*/
  1443. positionATX++;
  1444. system("@cls||clear");
  1445. SendToPrintGrade(8, 8, maingrid, positionATX, positionATY);
  1446. printf("%d", positionATX);
  1447. }
  1448. break;
  1449.  
  1450. case 3:
  1451. if (positionATX > 0) { /*to make sure x position won't be negative*/
  1452. positionATX--; /*to get x position */
  1453. system("@cls||clear");
  1454. SendToPrintGrade(8, 8, maingrid, positionATX, positionATY);
  1455. }
  1456. break;
  1457. case 4:
  1458. if (ExitWhileLoop == 0) {/*this line to save last x , y */
  1459.  
  1460.  
  1461. ExitWhileLoop++;
  1462. preX = positionATX;
  1463. preY = positionATY;
  1464. } else if (ExitWhileLoop == 1) {/*this line to save new x , y */
  1465.  
  1466.  
  1467. ExitWhileLoop++;
  1468. newX = positionATX;
  1469. newY = positionATY;
  1470. }
  1471. break;
  1472. }
  1473. if (ExitWhileLoop == 2) {
  1474. SetConsoleTextAttribute(console, 10);
  1475. ExitWhileLoop = 0;
  1476. SetConsoleTextAttribute(console, 10);
  1477.  
  1478. printf("%d",PlayerIndex); /*after two enter , we send it to edit grid*/
  1479. if( editGrid(8, 8, maingrid, preY, preX, newY, newX, PlayerIndex)){;
  1480. system("@cls||clear");
  1481.  
  1482. break;
  1483. }else {
  1484. system("@cls||clear");
  1485. flagPrintInvalidMove=1;
  1486. SetConsoleTextAttribute(console, 10);
  1487.  
  1488. printf("assssssssssssssfsfasfa");
  1489. count--;
  1490. break;
  1491.  
  1492. }
  1493. }
  1494. }
  1495. }
  1496.  
  1497.  
  1498. void PrintRowsNumber(int row) { /*this function was called to print some thing beside the grid*/
  1499. HANDLE console;
  1500. console = GetStdHandle(STD_OUTPUT_HANDLE);
  1501. printAtXY(88, 0, "");
  1502. SetConsoleTextAttribute(console, 10); /*to change the color to green */
  1503. int Rows[8] = {8, 7, 6, 5, 4, 3, 2, 1};
  1504. printf("%d ", Rows[row]);
  1505. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement