Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- #include <windows.h>
- #include <dos.h> // contains the sound function
- #include <conio.h> // it does include getch()
- struct player { // to define the player's properties
- char name[25];
- char color[25];
- int moves;
- int seconds;
- // the pieces that the player has taken
- int Pawn;
- int Rook;
- int Knight;
- int Bishop;
- int Queen;
- } player[2] = {{"Ahmed", "White", 0, 0, 0, 0, 0, 0, 0},
- {"Ahmed", "Black", 0, 0, 0, 0, 0, 0, 0}};
- char maingrid[8][8] = {
- {'R', 'N', 'B', 'Q', 'K', 'B', 'N', 'R'}, /*capital character are for black */
- {'P', 'P', 'P', 'P', 'P', 'P', 'P', 'P'},
- {'0', '0', '0', '0', '0', '0', '0', '0'},
- {'0', '0', '0', '0', '0', '0', '0', '0'},
- {'0', '0', '0', '0', '0', '0', '0', '0'},/*0 means that this place is empty */
- {'0', '0', '0', '0', '0', '0', '0', '0'},
- {'p', 'p', 'p', 'p', 'p', 'p', 'p', 'p'},
- {'r', 'n', 'b', 'q', 'k', 'b', 'n', 'r'}, /*small character are for white */
- };
- //********************************************************************************************************************************//
- int mainPage();
- void showMainPage(int highLightedElement, int color);
- int getSpecialInput();
- int isCertainInput(int ASCII1, int ASCII2);
- //********************************************************************************************************************************//
- void newGame();
- int detailsPage();
- //********************************************************************************************************************************//
- void save(char fileName[], int playerIndex);
- void loadLast(char fileName[], int *playerIndex);
- int lineLen(char fileName[]);
- void winner(int playerIndex);
- //********************************************************************************************************************************//
- int validateCommand(char *command, int playerIndex);
- void help();
- int editGrid(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
- char getDecision(int playerIndex);
- void giveToPlayer(int playerIndex, char piece);
- void ChangeDisplayColor(int color);
- int pawn(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
- int rook(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
- int rookInEmptyPath(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX);
- int knight(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
- int bishop(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
- int bishopInEmptyPath(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX);
- int queen(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
- int king(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex);
- //********************************************************************************************************************************//
- void printAtXY(int x, int y, const char *__fmt);
- void SendToPrintGrade(int length, int weight, char MainGrade_local[length][weight], int positionATX, int positionATY);
- void printGrid(int row, int colom, char *object);
- void PrintColoredLinesIndride(int NumOFline);
- void TODetermineThePosition(int PlayerIndex);
- void PrintRowsNumber(int row);
- //********************************************* main() the mother of all functions******************************************************************//
- int KingWightPositionX=5;
- int KingWightPositionY=7;
- int KingBlackPositionX=5;
- int KingBlackPositionY=0;
- int playnow=0;
- int flagPrintInvalidMove=0;
- int main() {
- /*
- SendToPrintGrade(8, 8, maingrid, -1, -1);
- validateCommand("a7a5", 1);
- Sleep(2000);
- system("@cls||clear");
- SendToPrintGrade(8, 8, maingrid, -1, -1);
- validateCommand("a5a4", 1);
- Sleep(2000);
- system("@cls||clear");
- SendToPrintGrade(8, 8, maingrid, -1, -1);
- validateCommand("a4a3", 1);
- Sleep(2000);
- system("@cls||clear");
- SendToPrintGrade(8, 8, maingrid, -1, -1);
- validateCommand("a3b2", 1);
- Sleep(2000);
- system("@cls||clear");
- SendToPrintGrade(8, 8, maingrid, -1, -1);
- validateCommand("b2a1", 1);
- Sleep(2000);
- system("@cls||clear");
- SendToPrintGrade(8, 8, maingrid, -1, -1);
- */
- /*int playerIndex = 0;
- //FILE * filePointer = fopen("gameRecord.txt", "w");
- loadLast("gameRecord.txt", &playerIndex);
- //
- printf("%s,%d,%d,%d,%d,%d,%d,%d\n",player[0].name, // printing the first player details
- player[0].seconds,player[0].moves,player[0].Pawn, player[0].Rook,player[0].Knight,player[0].Bishop,player[0].Queen);
- printf("%s,%d,%d,%d,%d,%d,%d,%d",player[1].name, // printing the second player details
- player[1].seconds,player[1].moves,player[1].Pawn,player[1].Rook,player[1].Knight,player[1].Bishop,player[1].Queen);
- printf("\nthis the current player%d",playerIndex);
- //
- //winner(0);*/
- newGame();
- int x=IsSavePosition(8,8,maingrid,5,2,0);
- printf("%d",x);
- // int IsSavePosition(int a8, int b8, char grid[a8][b8],int positionY ,int PositionX,int playerindex){ //
- }
- //**************************************************main menu***************************************************
- int mainPage() { // returns the the choice the user had made in order to navigate through the pages
- register int keyPressed, position = 0, temp;
- showMainPage(0, 4);
- do {
- keyPressed = getSpecialInput();
- switch (keyPressed) {
- case 0: // up
- if (position ==
- 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
- continue;
- }
- temp = position - 1;
- position = ((position == 0) ? position : temp);
- system("@cls||clear");
- showMainPage(position % 4, 4);
- break;
- case 1: // down
- if (position == 3) {
- continue;
- }
- temp = position + 1;
- position = (position == 3) ? position : temp;
- system("@cls||clear");
- showMainPage(position % 4, 4);
- break;
- case 4: // enter
- system("@cls||clear");
- switch (position) {
- case 0:
- newGame();
- break;
- case 1:
- printf("load last game called");
- break; // loadGame(); or return 1;
- case 2:
- printf("show credits called");
- break; // credits(); or return 2;
- case 3:
- printAtXY(100,50,"THANK YOU FOR PLAYING WITH OUR SMALL GAME :) ");
- exit(0);
- break;
- }
- }
- } while (keyPressed != 4);
- return 0;
- }
- void showMainPage(int highLightedElement, int color) { //prints the elements in the main page but one highlighted
- register int i;
- char elements[][16] = {"New game", "Load last game", "Credits", "Exit"};
- printAtXY(10, 10, "\n");
- for (i = 0; i < 4; i++) { // strlen(elements)
- if (i == highLightedElement) {
- ChangeDisplayColor(color);
- printAtXY(50, 0, elements[i]);
- printf("\n");
- ChangeDisplayColor(15);
- } else {
- printAtXY(50, 0, elements[i]);
- printf("\n");
- }
- }
- }
- int getSpecialInput() { // 0 -> up , 1 -> down , 2 -> right , 3 -> left , 4 -> enter , 5 -> space , 6 -> delete ,
- // 7 -> escape , 8 -> delete , 9 -> control + z , 10 -> control + s , 11 -> control + u
- // 12 -> shift + h
- int ASCII = getch();
- if (ASCII == 224 || ASCII == 0) {
- switch (getch()) {
- case 72:
- return 0;
- case 80:
- return 1;
- case 77:
- return 2;
- case 75:
- return 3;
- case 83:
- return 8;
- default:
- return -1; //not identified
- }
- } else {
- switch (ASCII) {
- case 13:
- return 4;
- case 32:
- return 5;
- case 8:
- return 6;
- case 27:
- return 7;
- case 26:
- return 9; /*undo*/
- case 19:
- return 10; /*cotrol s */
- case 21:
- return 11;
- case 72:
- return 12; /* shift h */
- default:
- return -1;
- }
- }
- }
- int isCertainInput(int ASCII1, int ASCII2) { // returns 1 if and only the typed button holds the
- if (ASCII2 != -1) { // ASCII code of the passed as an argument
- while (1) {
- if (ASCII1 == getch()) {
- if (ASCII2 == getch()) {
- return 1;
- }
- }
- }
- } else {
- while (1) {
- if (ASCII1 == getch()) {
- return 1;
- }
- }
- }
- /* remember these ASCII int decimal
- arrows
- up 224 72
- down 224 80
- right 224 77
- left 224 75
- delete 224 83
- enter 13
- space 32
- back space 8
- escape 27
- */
- }
- //**************************************************newGame function*********************************************************
- void newGame() {
- FILE * fp = fopen("gameRecord.txt","w");
- fclose(fp);
- switch (detailsPage()){
- case 0:
- arrowsgame();
- break;
- case 1:
- commandGame();
- break;
- }
- }
- int count=0;
- void arrowsgame(){
- HANDLE console;
- console = GetStdHandle(STD_OUTPUT_HANDLE);
- while(1){
- if (count%2==0)/*that's means white about to plat*/
- {
- count++;
- TODetermineThePosition(0);
- if( IsSavePosition(8,8,maingrid,KingWightPositionY,KingWightPositionX,1)){
- SetConsoleTextAttribute(console, 20);
- printf("chick!!\a\a\a\a\a\a");
- }
- }else if (count%2==1){
- count++;
- TODetermineThePosition(1);
- if( IsSavePosition(8,8,maingrid,KingBlackPositionY,KingBlackPositionX,0)){
- SetConsoleTextAttribute(console, 20);
- printf("chick!!\a\a\aa\a\aa\a\a");
- }
- }
- }}
- void commandGame(){
- }
- int detailsPage() { // returns the users choice <1\0> and scan their in formation
- char temp[25] ;
- char choice ;
- printAtXY(30, 0, "player one\n");
- printAtXY(0, 0, "ENTER YOUR FIRST NAME: ");
- scanf(" %s", temp);
- do {
- printAtXY(0, 0, "DO YOU WANT TO PLAY WITH WHITE OR BLACK: <W/B> ");
- scanf("%c", &choice);
- choice = tolower(choice);
- } while (choice != 'w' && choice != 'b');
- switch (choice) {
- case 'b':
- strcpy(player[1].name, temp);
- case 'w':
- strcpy(player[0].name, temp);
- }
- system("cls||clear");
- printAtXY(30, 0, "player two\n");
- printAtXY(0, 0, "ENTER YOUR FIRST NAME: ");
- scanf(" %s", (choice == 'w') ? player[1].name : player[0].name);
- system("cls||clear");
- do {
- printAtXY(0, 0, "do you want it with arrows <a> or with keyboard <k>: ");
- scanf(" %c", &choice);
- }while ( choice != 'a' && choice != 'k');
- system("@cls||clear");
- switch (choice){
- case 'a':
- return 0; // he choose the arrows
- case 'k':
- return 1; // he choose the keyboard
- }
- }
- //****************************************************data management************************************************************************
- void save(char fileName[],int currentPlayerIndex){ // save the current data for the game
- register int i, j;
- FILE * filePointer = fopen(fileName, "a+"); // this is the file gameRecord.txt opened in reading and appending mode
- fseek(filePointer,0,SEEK_END); // go to the end for the file to append new things
- fprintf(filePointer,"\n");
- for (i = 0;i < 8; i++){ // saving the mainGrid in one line of 64 character
- for (j = 0;j < 8; j++){
- fprintf(filePointer,"%c",maingrid[i][j]);
- }
- }
- fprintf(filePointer,", %s ,%d,%d,%d,%d,%d,%d,%d:",player[0].name, // printing the first player details
- player[0].seconds,player[0].moves,player[0].Pawn, player[0].Rook,player[0].Knight,player[0].Bishop,player[0].Queen);
- fprintf(filePointer," %s ,%d,%d,%d,%d,%d,%d,%d,",player[1].name, // printing the second player details
- player[1].seconds,player[1].moves,player[1].Pawn,player[1].Rook,player[1].Knight,player[1].Bishop,player[1].Queen);
- fprintf(filePointer,"%d",currentPlayerIndex); // printing int last of the line whose the last move belongs to?
- fclose(filePointer); // a good programming practice :)
- }
- void loadLast(char fileName[],int *playerIndex){ //it reads from the text file
- register int i = 0;
- char temp = '\0';
- FILE * filePointer = fopen(fileName, "r"); // this is the file gameRecord.txt opened in reading mode
- fseek(filePointer,-lineLen(fileName),SEEK_END); // go to the start of the last line of the file to start reading old things and
- while (i < 64){ // this is to load the mainGrid
- fscanf(filePointer,"%c",&temp);
- maingrid[i/8][i%8] = temp;
- i++;
- }
- 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
- &player[0].moves, &player[0].Pawn, &player[0].Rook, &player[0].Knight, &player[0].Bishop, // and before the string formate
- &player[0].Queen, player[1].name, &player[1].seconds, &player[1].moves, &player[1].Pawn,
- &player[1].Rook, &player[1].Knight, &player[1].Bishop, &player[1].Queen ,playerIndex);
- fclose(filePointer); // a good programming practice :)
- }
- 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
- 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
- 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 .
- FILE * fp = fopen(fileName,"r");
- while (temp != '\n'){
- fseek(fp,-(i++),SEEK_END);
- fscanf(fp,"%c",&temp);
- }
- fclose(fp); // a good programming practice :)
- return i - 2;
- }
- void winner(int playerIndex){
- register int i , key;
- char lineWin[256], lineHOFame[256];
- int colors[3] = {1,4,11};
- FILE * fp= fopen("WIN.txt","r");
- fseek(fp,0,SEEK_SET);
- printAtXY(0,10,"");
- for (i = 1; i < 10 ; i++){ // printing win gradually
- fgets(lineWin,256,fp);
- ChangeDisplayColor(i);
- printAtXY(20,0,"");
- printf("%s", lineWin);
- Beep(2000,100);
- }
- for (i = 0; i < 3 ; i++){ // printing three win words
- Beep(1000 * (i + 1),500);
- fseek(fp,0,SEEK_SET);
- system("cls||clear");
- ChangeDisplayColor(colors[i]);
- printAtXY(0,10,"");
- //Beep(1000 * (i + 1),500);
- while (!feof(fp)){
- fgets(lineWin,256,fp);
- printAtXY(20,0,"");
- printf("%s",lineWin);
- }
- }
- fclose(fp); // closing the pointer of the WIN file
- Sleep(500);
- ChangeDisplayColor(6);
- printAtXY(30,2,"CONGRATUATIONS ");
- printf("%s YOU HAVE WON THE GAME :) \n",player[playerIndex].name);
- FILE * filePointer = fopen("hall of fame.txt","a+");
- int score = player[playerIndex].Pawn + // pawn worth one point
- player[playerIndex].Rook * 5 + // Rook worth five point
- player[playerIndex].Knight * 3 + // Knight worth three point
- player[playerIndex].Bishop * 3 + // Bishop worth three point
- player[playerIndex].Queen * 9 ; // Queen worth nine point
- fseek(filePointer,0,SEEK_END);
- fprintf(filePointer,"\nNAME:\t%s\t\tSCORE:\t%d\t\tMOVES:\t%d\t\t\t",player[playerIndex].name,score,player[playerIndex].moves);
- fseek(filePointer,0,SEEK_SET);
- while (!feof(filePointer)){
- fgets(lineHOFame,256,filePointer);
- printAtXY(10,0,"");
- printf("%s",lineHOFame);
- }
- fclose(filePointer); // closing the pointer of hale of fame file
- ChangeDisplayColor(15);
- printf("\n\n\nPress ESC to go to the main menu:");
- do{
- key = getSpecialInput();
- if (key == 7){
- system("cls||clear");
- mainPage();
- }
- }while (key != 7);
- }
- //**************************************************validations and editing the grid*********************************************************
- int validateCommand(char *command,
- 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
- if (!strcmpi(command,
- "help")) { // note that the strcmpi() returns 0 if the two strings are the same , and strcmpi() is not case sensitive
- // call help()
- help();
- return 1; // 1 means success
- } else if (!strcmpi(command, "undo")) {
- // call undo()
- printf("undo called");
- return 1;
- } else if (!strcmpi(command, "exit")) {
- // call mainPage()
- mainPage();
- return 1;
- } else if (strlen(command) >=
- 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
- int preX, preY, newX, newY; // now translating the string into numbers
- preX = tolower(command[0]) - 'a'; // thug life with ASCII :)
- preY = 8 - (command[1] - '0'); // as the user start counting form down but i count from up and i count form 0 :)
- newX = tolower(command[2]) - 'a';
- newY = 8 - (command[3] - '0');
- if ((preX >= 0 && preX <= 7) && (preY >= 0 && preY <= 7) && (newX >= 0 && newX <= 7) &&
- (newY >= 0 && newY <= 7)) {
- editGrid(8, 8, maingrid, preY, preX, newY, newX,
- playerIndex); // editing the grid with the validated command
- return 1; // success
- }
- }
- return 0; // 0 means failed
- }
- void help() {
- printf("You can enter to the command line the following commands only:\n"\
- "undo -> to return to the last move.\n"\
- "help -> to show the commands you can enter.\n"\
- "exit -> to exit the game and go to the main menu.\n"\
- "the move command may be enter in the following formate:\n"\
- "current column + current row + new column + new row\n"\
- "note you may enter the game command in only such way, and if any mistake is made, the command wont be accepted.\n"\
- "note that: an case of promotion you will be asked to enter the kind of promotion you desire\n"\
- "have a happy game :)\n");
- }
- int editGrid(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
- int playerIndex) { //it edits the mainGrid as asked and how the arguments say
- switch (grid[preY][preX]) {
- case 'p': // white pawn
- if (pawn(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- grid[preY][preX] = '0'; // remove the pawn from his old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- if (newY == 0) { // check for promotion
- grid[newY][newX] = getDecision(playerIndex); // tell me what kind of promotion you may need
- player[playerIndex].moves++;
- return; // only to terminate the function thats all
- }
- grid[newY][newX] = 'p';
- player[playerIndex].moves++;
- return 1;
- }
- break;
- case 'r': // white rook
- if (rook(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- grid[preY][preX] = '0'; // remove the rook from his old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- grid[newY][newX] = 'r';
- player[playerIndex].moves++;
- return 1;
- }
- break;
- case 'n': // white knight
- if (knight(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- grid[preY][preX] = '0'; // remove the knight from his old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- grid[newY][newX] = 'n';
- player[playerIndex].moves++;
- return 1;
- }
- break;
- case 'b': // white bishop
- if (bishop(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- grid[preY][preX] = '0'; // remove the bishop from his old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- grid[newY][newX] = 'b';
- player[playerIndex].moves++;
- return 1;
- }
- break;
- case 'q': // white queen
- if (queen(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- grid[preY][preX] = '0'; // remove the queen from her old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- grid[newY][newX] = 'q';
- player[playerIndex].moves++;
- return 1;
- }
- break;
- case 'k': // white king
- if (king(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- if (newX == preX + 2) {
- grid[preY][preX] = '0';
- giveToPlayer(playerIndex, tolower(grid[newY][newX]));
- grid[newY][newX] = 'k';
- grid[7][7] = '0';
- grid[7][5] = 'r';
- KingWightPositionX=newX;
- KingWightPositionY=newY;
- return 1;
- // give the player something if he removed a piece
- } else if (newX == preX - 2) {
- grid[preY][preX] = '0';
- giveToPlayer(playerIndex, tolower(grid[newY][newX]));
- grid[newY][newX] = 'k';
- grid[7][0] = '0';
- grid[7][3] = 'r';
- KingWightPositionX=newX;
- KingWightPositionY=newY;
- return 1;
- } else {
- grid[preY][preX] = '0'; // remove the king from his old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- grid[newY][newX] = 'k';
- player[playerIndex].moves++;
- KingWightPositionX=newX;
- KingWightPositionY=newY;
- return 1;
- }
- }
- break;
- case 'P': // black pawn
- if (pawn(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- grid[preY][preX] = '0'; // remove the pawn from his old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- if (newY == 7) { // check for promotion
- grid[newY][newX] = getDecision(playerIndex); // tell me what kind of promotion you may need
- player[playerIndex].moves++;
- return 1; // only to terminate the function thats all
- }
- grid[newY][newX] = 'P';
- player[playerIndex].moves++;
- return 1;
- }
- break;
- case 'R': // black rook
- if (rook(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- grid[preY][preX] = '0'; // remove the rook from his old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- grid[newY][newX] = 'R';
- player[playerIndex].moves++;
- return 1;
- }
- break;
- case 'N': // black knight
- if (knight(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- grid[preY][preX] = '0'; // remove the knight from his old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- grid[newY][newX] = 'N';
- player[playerIndex].moves++;
- return 1;
- }
- break;
- case 'B': // black bishop
- if (bishop(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- grid[preY][preX] = '0'; // remove the bishop from his old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- grid[newY][newX] = 'B';
- player[playerIndex].moves++;
- return 1;
- }
- break;
- case 'Q': // black queen
- if (queen(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- grid[preY][preX] = '0'; // remove the queen from her old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- grid[newY][newX] = 'Q';
- player[playerIndex].moves++;
- return 1;
- }
- break;
- case 'K': // black king
- if (king(a8, b8, grid, preY, preX, newY, newX, playerIndex)) {
- if (newX == preX + 2) {
- grid[preY][preX] = '0';
- giveToPlayer(playerIndex, tolower(grid[newY][newX]));
- grid[newY][newX] = 'K';
- grid[0][7] = '0';
- grid[0][5] = 'R';
- KingBlackPositionX=newX;
- KingBlackPositionY=newY;
- return 1;
- } else if (newX == preX - 2) {
- grid[preY][preX] = '0';
- giveToPlayer(playerIndex, tolower(grid[newY][newX]));
- grid[newY][newX] = 'K';
- grid[0][0] = '0';
- grid[0][3] = 'R';
- KingBlackPositionX=newX;
- KingBlackPositionY=newY;
- return 1;
- } else {
- grid[preY][preX] = '0'; // remove the king from his old place
- giveToPlayer(playerIndex,
- tolower(grid[newY][newX])); // give the player something if he removed a piece
- grid[newY][newX] = 'K';
- player[playerIndex].moves++;
- KingBlackPositionX=newX;
- KingBlackPositionY=newY;
- return 1;
- }
- }
- break;
- default: return 0;
- }
- }
- void giveToPlayer(int playerIndex, char piece) { // it gives a certain player what he gained
- switch (piece) {
- case 'p':
- player[playerIndex].Pawn++;
- break;
- case 'r':
- player[playerIndex].Rook++;
- break;
- case 'n':
- player[playerIndex].Knight++;
- break;
- case 'b':
- player[playerIndex].Bishop++;
- break;
- case 'q':
- player[playerIndex].Queen++;
- break;
- default:
- break;
- }
- }
- char getDecision(int playerIndex) { // requires ctype.h it gets the kind of promotion the user needs
- char decision;
- printf("Enter the type of promotion you want: <q,b,n,r> ");
- scanf(" %c", &decision);
- decision = tolower(decision);
- if (decision != 'q' && decision != 'n' && decision != 'r' && decision != 'b') {
- printf("WORNG INPUT!!\n");
- return getDecision(playerIndex);
- }// returning the promotion based on the color of the player
- if (!strcmpi(player[playerIndex].color, "white")) { // note that strcmpi() is not case sensitive
- return tolower(decision);
- } else if (!strcmpi(player[playerIndex].color, "black")) { // note that strcmpi() is not case sensitive
- return toupper(decision);
- }
- printf("SORRY, SOMETHING WENT WRONG!!");
- return getDecision(playerIndex);
- }
- // the validation functions for each piece
- int pawn(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
- int playerIndex) { // contains the validation for the pawn
- // write the validation
- if (playerIndex == 0) { /*white player */
- if (grid[preY][preX] == 'p') {/* it must be p as lower */
- if (isupper(grid[newY][newX]) || grid[newY][newX] == '0') { /* next position must be black or empty */
- if ((newY == preY - 1 && grid[newY][newX] == '0' &&
- newX == preX/*this condition if white want to move one piece to empty cell*/) ||
- (newY == preY - 2 && grid[newY][newX] == '0' && preY == 6 && newX == preX && grid[preY - 1][preX] ==
- '0'/*this condition if white to move two cell only if it is the first move of it*/ )) {
- return 1;
- } else if (newY == preY - 1 && (newX == preX + 1 || newX == preX - 1) && isupper(grid[newY][newX]) &&
- grid[newY][newX] != '0') { /*this if white want to eat black piece*/
- return 1;
- } else return 0;
- } else return 0;
- } else return 0;
- } else if (playerIndex == 1) {/*black player */
- if (grid[preY][preX] == 'P') {/* it must be p as upper */
- if (islower(grid[newY][newX]) || grid[newY][newX] == '0') { /* next position must be white or empty */
- if ((newY == preY + 1 && grid[newY][newX] == '0' &&
- newX == preX/*this condition if black want to move one piece to empty cell*/) ||
- (newY == preY + 2 && grid[newY][newX] == '0' && preY == 1 && newX == preX && grid[preY + 1][preX] ==
- '0'/*this condition if black to move two cell only if it the first position of it*/ )) {
- return 1;
- } else if (newY == preY + 1 && (newX == preX + 1 || newX == preX - 1) && islower(grid[newY][newX]) &&
- grid[newY][newX] != '0') { /*this if black want to eat white piece*/
- return 1;
- } else return 0;
- } else return 0;
- } else return 0;
- }
- return 0;
- }
- int RookWhiteMoved[2] = {0, 0};
- int RookBlackMoved[2] = {0, 0};
- int rook(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
- int playerIndex) { // contains the validation for the rook
- if (isalpha(grid[preY][preX])) { // check for the content if the index first
- if (islower(grid[preY][preX])) { // the element is white (small)
- if (isupper(grid[newY][newX]) || grid[newY][newX] ==
- '0') { // as the white (small litters) can only move to empty or black (capital) positions
- if (rookInEmptyPath(8, 8, grid, preY, preX, newY,
- newX)) {// the two positions are in the same horizontal or vertical line
- if (preX == 7 && preY == 7)RookWhiteMoved[1] = 1;
- else if (preX == 0 && preY == 7)RookWhiteMoved[0] = 1;
- return 1;
- }
- }
- } else {
- if (islower(grid[newY][newX]) || grid[newY][newX] ==
- '0') { // as the black (capital litters) can only move to empty or white (small) positions
- if (rookInEmptyPath(8, 8, grid, preY, preX, newY,
- newX)) { // the two positions are in the same horizontal or vertical line
- if (preX == 7 && preY == 0)RookBlackMoved[1] = 1;
- else if (preX == 0 && preY == 0)RookBlackMoved[0] = 1;
- return 1;
- }
- }
- }
- }
- return 0; // rook can't move to this place
- }
- int rookInEmptyPath(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY,
- int newX) { // used to tell the whether the pass is empty 1 or not 0
- register int i;
- int emptyPass = 1; //is a variable to tell if the pass between the two positions is clean
- if (newX == preX) { // in the same vertical line
- if (preY < newY) { // new place is down the previous place
- for (i = preY + 1; i <
- newY; i++) { // looping through the pass between the two elements to check for the existence if another element in between
- if (grid[i][preX] != '0') {
- emptyPass = 0; // pass isn't empty
- break;
- }
- }
- } else if (preY > newY) { // new place is up the previous place
- for (i = preY - 1; i >
- newY; i--) { // looping through the pass between the two elements to check for the existence if another element in between
- if (grid[i][preX] != '0') {
- emptyPass = 0; // pass isn't empty
- break;
- }
- }
- }
- } else if (preY == newY) {
- if (preX < newX) { // new place is to the right of the previous place
- for (i = preX + 1; i <
- newX; i++) { // looping through the pass between the two elements to check for the existence if another element in between
- if (grid[preY][i] != '0') {
- emptyPass = 0; // pass isn't empty
- break;
- }
- }
- } else if (preX > newX) { // new place is to the left of the previous place
- for (i = preX - 1; i >
- newX; i--) { // looping through the pass between the two elements to check for the existence if another element in between
- if (grid[i][preX] != '0') {
- emptyPass = 0; // pass isn't empty
- break;
- }
- }
- }
- } else if ((preY != newY) || (preX != newX)) {
- emptyPass = 0;
- }
- return emptyPass;
- }
- int knight(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
- int playerIndex) { // contains the validation for the knight
- if ((playerIndex == 0 && islower(grid[preY][preX]) && grid[preY][preX] != '0' &&
- (grid[newY][newX] == '0' || isupper(grid[newY][newX]))) /*white about to plat*/
- || (playerIndex == 1 && isupper(grid[preY][preX]) && grid[preY][preX] != '0' &&
- (grid[newY][newX] == '0' || islower(grid[newY][newX])))) { /*black about to move*/
- if (newX == preX + 1 && newY == preY + 2)
- return 1;
- else if (newX == preX + 2 && newY == preY + 1)
- return 1;
- else if (newX == preX + 2 && newY == preY - 1)
- return 1;
- else if (newX == preX + 1 && newY == preY - 2)
- return 1;
- else if (newX == preX - 1 && newY == preY - 2) /*there are 8 possiple moves for knight */
- return 1;
- else if (newX == preX - 2 && newY == preY - 1)
- return 1;
- else if (newX == preX - 2 && newY == preY + 1)
- return 1;
- else if (newX == preX - 1 && newY == preY + 2)
- return 1;
- else return 0;
- } else return 0;
- }
- // write the validation
- int bishop(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
- int playerIndex) { // contains the validation for the bishop
- // write the validation
- if (isalpha(grid[preY][preX])) { // the old place is not empty
- if (islower(grid[preY][preX])) { // the element is white (small)
- if (isupper(grid[newY][newX]) || grid[newY][newX] ==
- '0') { // as the white (small litters) can only move to empty or black (capital) positions
- if (bishopInEmptyPath(8, 8, grid, preY, preX, newY,
- newX)) { // the two positions are synchronized the way the bishop needs with nothing in between
- return 1;
- }
- }
- } else { // the element is black (capital)
- if (islower(grid[newY][newX]) || grid[newY][newX] ==
- '0') { // as the black (capital litters) can only move to empty or white (small) positions
- if (bishopInEmptyPath(8, 8, grid, preY, preX, newY,
- newX)) { // the two positions are synchronized the way the bishop needs with nothing in between
- return 1;
- }
- }
- }
- }
- return 0; // bishop can't move to this place
- }
- int bishopInEmptyPath(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY,
- int newX) { // check if there is nothing between the two place
- register int i; // iteration variable
- for (i = 1; i < 8; i++) {
- if (((preX + i == newX) &&
- (preY + i == newY))) { // figure it out draw the grid to understand otherwise you won't
- return 1;
- } else if (grid[preY + i][preX + i] != '0') {
- break; // it fails and breaks because there is something in the path between the two items
- }
- }
- for (i = 1; i < 8; i++) {
- if (((preX - i == newX) &&
- (preY - i == newY))) { // figure it out draw the grid to understand otherwise you won't
- return 1;
- } else if (grid[preY - i][preX - i] != '0') {
- break; // it fails because there is something in the path between the two items
- }
- }
- for (i = 1; i < 8; i++) {
- if (((preX + i == newX) &&
- (preY - i == newY))) { // figure it out draw the grid to understand otherwise you won't
- return 1;
- } else if (grid[preY - i][preX + i] != '0') {
- break; // it fails because there is something in the path between the two items
- }
- }
- for (i = 1; i < 8; i++) {
- if (((preX - i == newX) &&
- (preY + i == newY))) { // figure it out draw the grid to understand otherwise you won't
- return 1;
- } else if (grid[preY + i][preX - i] != '0') {
- break; // it fails because there is something in the path between the two items
- }
- }
- return 0;
- }
- int queen(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX,
- int playerIndex) { // contains the validation for the queen
- // write the validation
- if (isalpha(grid[preY][preX])) { // the old place is not empty
- if (islower(grid[preY][preX])) { // the element is white (small)
- if (isupper(grid[newY][newX]) || grid[newY][newX] ==
- '0') { // as the white (small litters) can only move to empty or black (capital) positions
- if (bishopInEmptyPath(8, 8, grid, preY, preX, newY, newX) ||
- rookInEmptyPath(8, 8, grid, preY, preX, newY,
- 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
- return 1;
- }
- }
- } else { // the element is black (capital)
- if (islower(grid[newY][newX]) || grid[newY][newX] ==
- '0') { // as the black (capital litters) can only move to empty or white (small) positions
- if (bishopInEmptyPath(8, 8, grid, preY, preX, newY, newX) ||
- rookInEmptyPath(8, 8, grid, preY, preX, newY,
- 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
- return 1;
- }
- }
- }
- }
- return 0; // this return statement can be removed later when writing this function
- }
- int king(int a8, int b8, char grid[a8][b8], int preY, int preX, int newY, int newX, int playerIndex) {
- // contains the validation for the king
- static int KingWightMoved = 0;
- static int KingBlackMoved = 0;
- if (playerIndex == 0 && KingWightMoved == 0 && grid[newY][newX] == '0'&&preY==newY) {
- if (newX == preX + 2 && RookWhiteMoved[1] == 0 && grid[newY][preX + 1] == '0') {
- KingWightMoved = 1;
- return 1;
- } else if (newX == preX - 2 && RookWhiteMoved[0] == 0 && grid[newY][preX - 1] == '0') {
- KingWightMoved = 1;
- return 1;
- }
- } else if (playerIndex == 1 && KingBlackMoved == 0 && grid[newY][newX] == '0'&&preY==newY) {
- if (newX == preX + 2 && RookBlackMoved[1] == 0 && grid[newY][preX + 1] == '0') {
- KingBlackMoved = 1;
- return 1;
- } else if (newX == preX - 2 && RookBlackMoved[0] == 0 && grid[newY][preX - 1] == '0') {
- KingBlackMoved = 1;
- return 1;
- }
- }
- if (playerIndex == 0 && grid[preY][preX] != '0' && islower(grid[preY][preX]) &&
- (grid[newY][newX] == '0' || isupper(grid[newY][newX]))) {/*white about ti move */
- if (newX == preX && newY == preY + 1) {
- KingWightMoved = 1;
- return 1;
- } else if (newX == preX && newY == preY - 1) {
- KingWightMoved = 1;
- return 1;
- } else if (newY == preY && newX == preX + 1) {
- KingWightMoved = 1;
- return 1;
- } else if (newY == preY && newX == preX - 1) {
- KingWightMoved = 1;
- return 1;
- } else if (newY == preY + 1 && newX == preX + 1) {
- KingWightMoved = 1;
- return 1;
- } else if (newY == preY - 1 && newX == preX - 1) {
- KingWightMoved = 1;
- return 1;
- } else if (newY == preY - 1 && newX == preX + 1) {
- KingWightMoved = 1;
- return 1;
- } else if (newY == preY + 1 && newX == preX - 1) {
- KingWightMoved = 1;
- return 1;
- } else return 0;
- } else if (playerIndex == 1 && grid[preY][preX] != '0' && isupper(grid[preY][preX]) &&
- (grid[newY][newX] == '0' || islower(grid[newY][newX]))) {
- if (newX == preX && newY == preY + 1) {
- KingBlackMoved = 1;
- return 1;
- } else if (newX == preX && newY == preY - 1) {
- KingBlackMoved = 1;
- return 1;
- } else if (newY == preY && newX == preX + 1) {
- KingBlackMoved = 1;
- return 1;
- } else if (newY == preY && newX == preX - 1) {
- KingBlackMoved = 1;
- return 1;
- } else if (newY == preY + 1 && newX == preX + 1) {
- KingBlackMoved = 1;
- return 1;
- } else if (newY == preY - 1 && newX == preX - 1) {
- KingBlackMoved = 1;
- return 1;
- } else if (newY == preY - 1 && newX == preX + 1) {
- KingBlackMoved = 1;
- return 1;
- } else if (newY == preY + 1 && newX == preX - 1) {
- KingBlackMoved = 1;
- return 1;
- } else return 0;
- }
- return 0;
- }
- int IsSavePosition(int a8, int b8, char grid[a8][b8],int positionY ,int PositionX,int playerindex){ //
- if (playerindex==0){
- for(int count1=0;count1<a8;count1++){
- for (int count2=0;count2<b8;count2++){
- if(isupper(grid[count1][count2])){
- switch (grid[count1][count2]){
- case 'P':
- if(pawn( a8, b8, grid, count1, count2, positionY,PositionX , 1)){
- return 1;
- }
- break;
- case 'R':
- if(rook( a8, b8, grid, count1, count2, positionY,PositionX , 1)){
- return 1;
- }break;
- case 'B':
- if(bishop( a8, b8, grid, count1, count2, positionY,PositionX , 1)){
- return 1;
- }break;
- case 'Q':
- if(queen( a8, b8, grid, count1, count2, positionY,PositionX , 1)){
- return 1;}
- break;
- case 'N':
- if(knight( a8, b8, grid, count1, count2, positionY,PositionX , 1)){
- return 1;}
- break;
- case 'K':
- if(king(a8, b8, grid, count1, count2, positionY,PositionX , 1)){
- // return 1;
- }break;
- }
- }
- }
- }return 0;
- }else if(playerindex==1){
- for(int count1=0;count1<a8;count1++){
- for (int count2=0;count2<b8;count2++){
- if(islower(grid[count1][count2])){
- switch (grid[count1][count2]){
- case 'p':
- if(pawn( a8, b8, grid, count1, count2, positionY,PositionX , 0)){
- return 1;
- }
- break;
- case 'r':
- if(rook( a8, b8, grid, count1, count2, positionY,PositionX , 0)){
- return 1;
- }break;
- case 'b':
- if(bishop( a8, b8, grid, count1, count2, positionY,PositionX , 0)){
- return 1;
- }break;
- case 'q':
- if(queen( a8, b8, grid, count1, count2, positionY,PositionX , 0)){
- return 1;}
- break;
- case 'n':
- if(knight( a8, b8, grid, count1, count2, positionY,PositionX , 0)){
- return 1;}
- break;
- case 'K':
- if(king(a8, b8, grid, count1, count2, positionY,PositionX , 0)){
- return 1;
- }break;
- }
- }
- }
- }
- }
- return 0;
- }
- void ChangeDisplayColor(
- int color) { // source is {https://www.youtube.com/watch?v=gRLzd-2sqIQ} don't laugh at me library is windows.h
- HANDLE console;
- console = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleTextAttribute(console, color);
- /*
- 0 BLACK
- 1 BLUE
- 2 GREEN
- 3 CYAN
- 4 RED
- 5 MAGENTA
- 6 BROWN
- 7 LIGHTGRAY
- 8 DARKGRAY
- 9 LIGHTBLUE
- 10 LIGHTGREEN
- 11 LIGHTCYAN
- 12 LIGHTRED
- 13 LIGHTMAGENTA
- 14 YELLOW
- 15 WHITE
- */
- }
- // **************************************************main grid************************************************************ //
- 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
- 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
- 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
- flags for each coloum */
- HANDLE console;
- console = GetStdHandle(STD_OUTPUT_HANDLE);
- printAtXY(90, 5, "");
- for (int count1 = 0; count1 < 8; count1++) {
- PrintColoredLinesIndride(count1); /*to print two imaginary line */
- for (int count2 = 0; count2 < 8; count2++) {
- if (count1 == positionATY &&
- count2 == positionATX) { /*if the cell is highlighted we will deal with it with anther condition */
- if (islower(MainGrade_local[count1][count2]) &&
- MainGrade_local[count1][count2] != '0') {/*to check if highlighted is white */
- SetConsoleTextAttribute(console, 167);
- printf(" %c ", MainGrade_local[count1][count2]);
- if (count2 == 7) { /*if the cell is the last one on row we will print the number of next row */
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- PrintRowsNumber(count1);
- }
- } else if (MainGrade_local[count1][count2] != '0') { /*if the piece is black*/
- SetConsoleTextAttribute(console, 160);
- printf(" %c ",
- (MainGrade_local[count1][count2] == 0) ? ' ' : MainGrade_local[count1][count2]);
- if (count2 == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- PrintRowsNumber(count1);
- }
- } else { /*there is no piece */
- SetConsoleTextAttribute(console, 160);
- printf(" ");
- if (count2 == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- PrintRowsNumber(count1);
- }
- }
- } else printGrid(count1, count2, &MainGrade_local[count1][count2]);
- }
- PrintColoredLinesIndride(count1);
- }
- char determine_coloun_with_char[8] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
- SetConsoleTextAttribute(console, 10);
- printf("\n");
- printAtXY(90, 0, "");
- for (int count = 0; count < 8; count++)
- printf(" %c ", determine_coloun_with_char[count]);
- SetConsoleTextAttribute(console, 15);
- if(flagPrintInvalidMove==1){
- SetConsoleTextAttribute(console, 20);
- printf("Invalid Move !!!");
- flagPrintInvalidMove=0;
- SetConsoleTextAttribute(console, 0);
- }
- // printf("\n");
- }
- void printGrid(int row, int colom, char *object) {
- /*this function is to print each cells and class it into 5 cases 1_ white piece on white cell
- 2_ white piece on black cell1
- 3 _ black piece on white cell
- 4_ black piece on black cell1
- 5_empty cell
- 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
- cell will be white , else it will be black*/
- HANDLE console;
- console = GetStdHandle(STD_OUTPUT_HANDLE);
- if ((row + colom) % 2 == 0 && *object != '0' && isupper(*object)) {
- SetConsoleTextAttribute(console, 112); /*this show when the place is white and the piece is black */
- printf(" %c ", *object);
- if (colom == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- PrintRowsNumber(row);
- }
- } else if ((row + colom) % 2 == 1 && *object != '0' && isupper(*object)) {
- SetConsoleTextAttribute(console, 64); /*this show when the place is black and the piece is black */
- printf(" %c ", *object);
- if (colom == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- PrintRowsNumber(row);
- }
- } else if ((row + colom) % 2 == 0 && *object != '0' && islower(*object)) {
- SetConsoleTextAttribute(console, 120); /*this show when the place is white and the piece is white */
- printf(" %c ", *object);
- if (colom == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- PrintRowsNumber(row);
- }
- } else if ((row + colom) % 2 == 1 && *object != '0' && islower(*object)) {
- SetConsoleTextAttribute(console, 71); /*this show when the place is white and the piece is white */
- printf(" %c ", *object);
- if (colom == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- PrintRowsNumber(row);
- }
- } else if (*object == '0' && (row + colom) % 2 == 1) {
- SetConsoleTextAttribute(console, 71); /*this show when the place is white and there is no piece */
- printf(" ");
- if (colom == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- PrintRowsNumber(row);
- }
- } else {
- SetConsoleTextAttribute(console, 120); /*this show when the place is white and there is no piece */
- printf(" ");
- if (colom == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- PrintRowsNumber(row);
- }
- }
- }
- void printAtXY(int x, int y, const char *__fmt) { // you need to use sprintf or snprintf to formate the text to be taken
- register int i;
- for (i = 0; i < y; i++) {
- printf("\n");
- }
- for (i = 0; i < x; i++) {
- printf(" ");
- }
- printf("%s", __fmt);
- }
- void PrintColoredLinesIndride(
- 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 */
- HANDLE console;
- console = GetStdHandle(STD_OUTPUT_HANDLE);
- 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*/
- /*print imaginary begin with white and end with with black */
- for (int count = 0; count < 8; count++) {
- if ((NumOFline % 2 ==
- 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*/
- if (count % 2 ==
- 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 */
- SetConsoleTextAttribute(console, 120);
- printf(" ");
- if (count == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n"); /*to make new line after end of each row*/
- printAtXY(90, 0, "");
- }
- } else { /*to print black cell if count (number of coloum )is even */
- SetConsoleTextAttribute(console, 71);
- printf(" ");
- if (count == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- printAtXY(90, 0, "");
- }
- }
- } else {/*this else means that the begin of this rows is black */
- if (count % 2 == 1) { /*that's mean this cell is white */
- SetConsoleTextAttribute(console, 120);
- printf(" ");
- if (count == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- printAtXY(90, 0, "");
- }
- } else {/*that's mean this cell is black */
- SetConsoleTextAttribute(console, 71);
- printf(" ");
- if (count == 7) {
- SetConsoleTextAttribute(console, 0);
- printf(" |\n");
- printAtXY(90, 0, "");
- }
- }
- }
- }
- }
- }
- void TODetermineThePosition(
- 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*/
- HANDLE console;
- console = GetStdHandle(STD_OUTPUT_HANDLE);
- int preX = 4;
- int preY = 4;
- int newX = 4;
- int newY = 4;
- int ExitWhileLoop = 0;
- static int positionATX = 4; /*this three line to print the begin grade */
- static int positionATY = 4;
- SendToPrintGrade(8, 8, maingrid, positionATX, positionATY);
- while (1) {
- switch (getSpecialInput()) {
- case 0: /*4 next cases to determine the position of pointer */
- if (positionATY > 0) { /*to make sure y position won't be negative*/
- positionATY--;
- system("@cls||clear");
- SendToPrintGrade(8, 8, maingrid, positionATX, positionATY);
- }
- break;
- case 1:
- if (positionATY < 7) {/*to make sure y position won't be more than 7*/
- positionATY++;
- system("@cls||clear");
- SendToPrintGrade(8, 8, maingrid, positionATX, positionATY);
- }
- break;
- case 2:
- if (positionATX < 7) { /*to make sure x position won't be more than 7*/
- positionATX++;
- system("@cls||clear");
- SendToPrintGrade(8, 8, maingrid, positionATX, positionATY);
- printf("%d", positionATX);
- }
- break;
- case 3:
- if (positionATX > 0) { /*to make sure x position won't be negative*/
- positionATX--; /*to get x position */
- system("@cls||clear");
- SendToPrintGrade(8, 8, maingrid, positionATX, positionATY);
- }
- break;
- case 4:
- if (ExitWhileLoop == 0) {/*this line to save last x , y */
- ExitWhileLoop++;
- preX = positionATX;
- preY = positionATY;
- } else if (ExitWhileLoop == 1) {/*this line to save new x , y */
- ExitWhileLoop++;
- newX = positionATX;
- newY = positionATY;
- }
- break;
- }
- if (ExitWhileLoop == 2) {
- SetConsoleTextAttribute(console, 10);
- ExitWhileLoop = 0;
- SetConsoleTextAttribute(console, 10);
- printf("%d",PlayerIndex); /*after two enter , we send it to edit grid*/
- if( editGrid(8, 8, maingrid, preY, preX, newY, newX, PlayerIndex)){;
- system("@cls||clear");
- break;
- }else {
- system("@cls||clear");
- flagPrintInvalidMove=1;
- SetConsoleTextAttribute(console, 10);
- printf("assssssssssssssfsfasfa");
- count--;
- break;
- }
- }
- }
- }
- void PrintRowsNumber(int row) { /*this function was called to print some thing beside the grid*/
- HANDLE console;
- console = GetStdHandle(STD_OUTPUT_HANDLE);
- printAtXY(88, 0, "");
- SetConsoleTextAttribute(console, 10); /*to change the color to green */
- int Rows[8] = {8, 7, 6, 5, 4, 3, 2, 1};
- printf("%d ", Rows[row]);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement