From ed23f3a13027d50986633918a54f832fa845dbd1 Mon Sep 17 00:00:00 2001 From: devendra dhare Date: Wed, 28 Sep 2022 11:43:39 +0530 Subject: [PATCH] added menu in myGame --- myGame.c | 312 +++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 247 insertions(+), 65 deletions(-) diff --git a/myGame.c b/myGame.c index 6ae8a20..a8c25e7 100644 --- a/myGame.c +++ b/myGame.c @@ -13,6 +13,10 @@ #define ANSI_COLOR_CYAN "\x1b[36m" #define ANSI_COLOR_RESET "\x1b[0m" +#pragma comment(lib, "WinMM.lib") + +int cheat = 1, sound = 1, beep = 1, difficulty = 2; + // create matrix 4*4 void createMatrix(int arr[][4]) { @@ -93,7 +97,28 @@ int readEnteredKey() c = _getch(); if (c == -32) c = _getch(); - + if (beep == 1 && sound == 1) + { + Beep(550, 100); + beep = 0; + return c; + } + beep = 1; + return c; +} +int readEnteredKey2(int index) +{ + char c; + c = _getch(); + if (c == -32) + c = _getch(); + if (beep == 1 && sound == 1) + { + Beep(200 + (100 * (index + 1)), 200); + beep = 0; + return c; + } + beep = 1; return c; } @@ -174,7 +199,7 @@ int shiftLeft(int arr[][4]) } // Game rules -void gameRule(int arr[][4]) +void gameRule() { system("cls"); @@ -198,15 +223,18 @@ void gameRule(int arr[][4]) printf(ANSI_COLOR_YELLOW "---------------------\n" ANSI_COLOR_RESET); - for (i = 0; i < 4; i++) + for (i = 1; i <= 15;) { printf(ANSI_COLOR_YELLOW "| " ANSI_COLOR_RESET); - for (j = 0; j < 4; j++) + for (j = 1; j <= 4; j++) { - if (arr[i][j] != 0) - printf(ANSI_COLOR_YELLOW "%2d | " ANSI_COLOR_RESET, 4 * i + j + 1); - else - printf(ANSI_COLOR_YELLOW " |" ANSI_COLOR_RESET); + if (i == 16) + { + printf(ANSI_COLOR_YELLOW " | " ANSI_COLOR_RESET); + break; + } + printf(ANSI_COLOR_YELLOW "%2d | " ANSI_COLOR_RESET, i); + i++; } printf("\n"); } @@ -216,16 +244,141 @@ void gameRule(int arr[][4]) printf("\nSo Try to win in minimum no of move \n"); - printf("\nEnter any key to start..... "); + printf("\nEnter any key to menu..... "); int x = readEnteredKey(); + system("cls"); +} + +// added by me +void you_lose_sound() +{ + int i; + if (sound == 1) + { + for (i = 600; i >= 300; i -= 50) + { + Beep(i, 200); + } + Beep(350, 1000); + } +} +void you_win_sound() +{ + int i; + if (sound == 1) + { + Beep(300, 300); + Beep(350, 200); + Beep(200, 50); + Beep(300, 200); + Beep(400, 1000); + } +} +void print_menu(int index) +{ + char menu_item[6][30] = {"Play", "Difficulty", "Sound", "Unlimited moves", "How to play", "Quit game"}; + printf("\n\n\t\tgame menu\n"); + for (int i = 0; i < 6; i++) + { + if (i == index) + { + printf(ANSI_COLOR_BLUE "\n--> %s" ANSI_COLOR_RESET, menu_item[i]); + } + else + { + printf("\n %s", menu_item[i]); + } + switch (i) + { + case 1: + printf(ANSI_COLOR_MAGENTA " %d Moves" ANSI_COLOR_RESET, difficulty * 50); + break; + case 2: + if (sound == 1) + printf(ANSI_COLOR_YELLOW " on" ANSI_COLOR_RESET); + else + printf(ANSI_COLOR_MAGENTA " off" ANSI_COLOR_RESET); + break; + case 3: + if (cheat == 1) + printf(ANSI_COLOR_MAGENTA " off" ANSI_COLOR_RESET); + else + printf(ANSI_COLOR_YELLOW " on" ANSI_COLOR_RESET); + break; + default: + break; + } + } + printf("\n\n"); +} + +void game_menu(int *maxtry) +{ + int key, menu_arrow = 0; + system("cls"); + print_menu(menu_arrow); + while (1) + { + key = readEnteredKey2(menu_arrow); + system("cls"); + printf("<%d>", key); + switch (key) + { + case 72: + if (menu_arrow >= 1) + { + menu_arrow--; + } + break; + case 80: + if (menu_arrow <= 4) + { + menu_arrow++; + } + break; + case 13: + beep = 1; + switch (menu_arrow) + { + case 0: + return; + case 1: + if (difficulty >= 4) + { + difficulty = 1; + *maxtry = difficulty * 50; + break; + } + *maxtry = difficulty * 50; + difficulty++; + break; + case 2: + sound = sound == 1 ? 0 : 1; + break; + case 3: + cheat = cheat == 1 ? 0 : 1; + break; + case 4: + gameRule(); + break; + case 5: + exit(0); + default: + break; + } + default: + break; + } + print_menu(menu_arrow); + } } // main function int main() { - int arr[4][4]; // matrix - int maxTry = 4; // maximum move + int arr[4][4], flg; // matrix + int maxTry = difficulty * 50; // maximum move char name[20]; for (int k = 0; k < 3; k++) printf("\n"); @@ -236,70 +389,99 @@ int main() while (1) { - createMatrix(arr); // calling function to create matrix - gameRule(arr); // game rule function calling - - while (!winner(arr)) // checking for winning situation + game_menu(&maxTry); + while (1) { - system("cls"); - if (!maxTry) // for remaining zero move + flg = 1; + createMatrix(arr); // calling function to create matrix + // gameRule(); // game rule function calling + fflush(stdin); + while (!winner(arr)) // checking for winning situation + { + system("cls"); + if (!maxTry) // for remaining zero move + break; + + printf("\n\n Enter e to [e]ixt\n Player Name: %s , Move remaining : %d ", name, cheat==1?maxTry:NULL); + if(cheat == 0) + printf("Unlimited moves\n\n"); + else + printf("\n\n"); + + showMatrix(arr); // show matrix + + int key = readEnteredKey(); // this will return ascii code of user entered key + + switch (key) // maping + { + case 101: // ascii of E + + case 69: // ascii of e + printf("\a\a\a\a\a\a\n Thanks for Playing ! \n\a"); + printf("\nHit 'Enter' to menu \n"); + key = readEnteredKey(); + flg = 0; + break; + + case 72: // arrow up + if (shiftUp(arr) && cheat == 1) + maxTry--; + break; + case 80: // arrow down + if (shiftDown(arr) && cheat == 1) + maxTry--; + break; + case 77: // arrow right + if (shiftRight(arr) && cheat == 1) + maxTry--; + break; + case 75: // arrow left + if (shiftLeft(arr) && cheat == 1) + maxTry--; + break; + default: + if (cheat == 1) + { + printf("\n\n unlimited moves"); + break; + } + printf("\n\n \a\a Not Allowed \a"); + } + if (flg == 0) + break; + } + if (flg == 0) break; - printf("\n\n Player Name: %s , Move remaining : %d\n\n", name, maxTry); - - showMatrix(arr); // show matrix - - int key = readEnteredKey(); // this will return ascii code of user entered key - - switch (key) // maping + if (!maxTry) { - case 101: // ascii of E - - case 69: // ascii of e - printf("\a\a\a\a\a\a\n Thanks for Playing ! \n\a"); - printf("\nHit 'Enter' to exit the game \n"); - key = readEnteredKey(); - return 0; - - case 72: // arrow up - if (shiftUp(arr)) - maxTry--; - break; - case 80: // arrow down - if (shiftDown(arr)) - maxTry--; + printf(ANSI_COLOR_RED "\n\a YOU LOSE ! \a\a" ANSI_COLOR_RESET); + you_lose_sound(); + readEnteredKey(); break; - case 77: // arrow right - if (shiftRight(arr)) - maxTry--; - break; - case 75: // arrow left - if (shiftLeft(arr)) - maxTry--; + } + else + { + printf(ANSI_COLOR_GREEN "\n\a!!!!!!!!!!!!!Congratulations %s for winning this game !!!!!!!!!!!!!\n\a" ANSI_COLOR_RESET, name); + you_win_sound(); + readEnteredKey(); break; - default: - - printf("\n\n \a\a Not Allowed \a"); } + fflush(stdin); // Will clear the buffer + // char check; + // printf(ANSI_COLOR_GREEN "\nWant to play again? (y/n) : " ANSI_COLOR_RESET); + // scanf("%c", &check); + + // // Leave the game here itself ! + // if ((check != 'y') && (check != 'Y')) + // { + // maxTry = 4; + // break; + // } } - if (!maxTry) - printf(ANSI_COLOR_RED "\n\a YOU LOSE ! \a\a\n" ANSI_COLOR_RESET); - else - printf(ANSI_COLOR_GREEN "\n\a!!!!!!!!!!!!!Congratulations %s for winning this game !!!!!!!!!!!!!\n\a" ANSI_COLOR_RESET, name); - - fflush(stdin); // Will clear the buffer - char check; - printf(ANSI_COLOR_GREEN "\nWant to play again ? \n" ANSI_COLOR_RESET); - printf("enter 'y' to play again: "); - scanf("%c", &check); - - // Leave the game here itself ! - if ((check != 'y') && (check != 'Y')) - break; - - maxTry = 4; + maxTry = difficulty * 50; } return 0; }