From 398d9373203066b0e455555af65a86b7b9ec78ba Mon Sep 17 00:00:00 2001 From: fdlt3885 Date: Fri, 10 Feb 2023 12:14:19 +0000 Subject: [PATCH] Added Hangman game and headers --- src/main/quizproject.c | 75 ++++++++++++++++++++++++++++++++++++++++-- src/main/quizproject.h | 5 +++ 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/src/main/quizproject.c b/src/main/quizproject.c index a197cee..af61b8f 100644 --- a/src/main/quizproject.c +++ b/src/main/quizproject.c @@ -2831,6 +2831,72 @@ void b_celebrity(){ } + +//Hangman + +void b_hangman() { + char word[MAX_LEN + 1]; + char display[MAX_LEN + 1]; + char used[26]; + int num_wrong_guesses; + + printf("Welcome to Hangman!\n"); + + strcpy(word, "hangman"); + int i; + for (i = 0; i < strlen(word); i++) { + display[i] = '-'; + } + display[i] = '\0'; + num_wrong_guesses = 0; + memset(used, 0, sizeof used); + + char guess[MAX_LEN + 1]; + int good_guess, already_guessed; + while (num_wrong_guesses < MAX_GUESSES && strchr(display, '-') != NULL) { + printf("Current word: %s\n", display); + printf("You have %d incorrect guesses left.\n", MAX_GUESSES - num_wrong_guesses); + printf("Used letters: %s\n", used); + printf("Enter your guess: "); + scanf("%s", guess); + if (strlen(guess) != 1 || !isalpha(*guess)) { + printf("Invalid guess. Please enter a single letter.\n"); + continue; + } + already_guessed = 0; + for (i = 0; i < 26; i++) { + if (tolower(guess[0]) == used[i]) { + printf("You already used the letter '%c'.\n", tolower(guess[0])); + already_guessed = 1; + break; + } + } + if (already_guessed) { + continue; + } + used[i] = tolower(guess[0]); + good_guess = 0; + for (i = 0; i < strlen(word); i++) { + if (tolower(guess[0]) == tolower(word[i])) { + display[i] = word[i]; + good_guess = 1; + } + } + if (!good_guess) { + printf("Incorrect! The letter '%c' is not in the word.\n", tolower(guess[0])); + num_wrong_guesses++; + } + } + if (num_wrong_guesses == MAX_GUESSES) { + printf("You lose! The word was '%s'.\n", word); + } else { + printf("You win! The word was '%s'.\n", word); + } + +} + + + void v_play_rockpapersciss(){ int player_choice, computer_choice, player_score = 0, computer_score = 0; srand(time(NULL)); @@ -3426,7 +3492,7 @@ int main(int argc, char *argv[]) { int jump_to_menu = 0; - while (choice != 24 || jump_to_menu) { + while (choice != 25 || jump_to_menu) { printf("Welcome to the Game Menu!\n"); printf("1. QuizGame\n"); printf("2. Fact or Lie?\n"); @@ -3451,7 +3517,8 @@ int main(int argc, char *argv[]) { printf("21. Brain whiz\n"); printf("22. Personality Quiz!\n"); printf("23. Guess the Celebrity!\n"); - printf("24. Exit\n"); + printf("24. Hangman!\n"); + printf("25. Exit\n"); printf("Enter your choice: "); scanf("%d", &choice); v_progress_bar(argc,argv); @@ -3550,6 +3617,10 @@ int main(int argc, char *argv[]) { jump_to_menu = 1; break; case 24: + b_hangman(); + jump_to_menu = 1; + break; + case 25: printf("\nThank you for trying our C code!\n"); break; default: diff --git a/src/main/quizproject.h b/src/main/quizproject.h index e9bb61a..e029b0b 100644 --- a/src/main/quizproject.h +++ b/src/main/quizproject.h @@ -150,6 +150,9 @@ void b_celebrity(); char celebrity[20]; char answer; +//Hangman Headers +void b_hangman(); + //-------brain_whiz_header_begin------------ void for_loop_print_question(char [][140], char [][140], char []); @@ -175,5 +178,7 @@ int* randomNumber1(); #define ROCK 1 #define PAPER 2 #define SCISSORS 3 +#define MAX_LEN 30 +#define MAX_GUESSES 6 #endif