Browse Source

Added Hangman game and headers

main
fdlt3885 2 years ago
parent
commit
398d937320
  1. 75
      src/main/quizproject.c
  2. 5
      src/main/quizproject.h

75
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(){ void v_play_rockpapersciss(){
int player_choice, computer_choice, player_score = 0, computer_score = 0; int player_choice, computer_choice, player_score = 0, computer_score = 0;
srand(time(NULL)); srand(time(NULL));
@ -3426,7 +3492,7 @@ int main(int argc, char *argv[]) {
int jump_to_menu = 0; 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("Welcome to the Game Menu!\n");
printf("1. QuizGame\n"); printf("1. QuizGame\n");
printf("2. Fact or Lie?\n"); printf("2. Fact or Lie?\n");
@ -3451,7 +3517,8 @@ int main(int argc, char *argv[]) {
printf("21. Brain whiz\n"); printf("21. Brain whiz\n");
printf("22. Personality Quiz!\n"); printf("22. Personality Quiz!\n");
printf("23. Guess the Celebrity!\n"); printf("23. Guess the Celebrity!\n");
printf("24. Exit\n");
printf("24. Hangman!\n");
printf("25. Exit\n");
printf("Enter your choice: "); printf("Enter your choice: ");
scanf("%d", &choice); scanf("%d", &choice);
v_progress_bar(argc,argv); v_progress_bar(argc,argv);
@ -3550,6 +3617,10 @@ int main(int argc, char *argv[]) {
jump_to_menu = 1; jump_to_menu = 1;
break; break;
case 24: case 24:
b_hangman();
jump_to_menu = 1;
break;
case 25:
printf("\nThank you for trying our C code!\n"); printf("\nThank you for trying our C code!\n");
break; break;
default: default:

5
src/main/quizproject.h

@ -150,6 +150,9 @@ void b_celebrity();
char celebrity[20]; char celebrity[20];
char answer; char answer;
//Hangman Headers
void b_hangman();
//-------brain_whiz_header_begin------------ //-------brain_whiz_header_begin------------
void for_loop_print_question(char [][140], char [][140], char []); void for_loop_print_question(char [][140], char [][140], char []);
@ -175,5 +178,7 @@ int* randomNumber1();
#define ROCK 1 #define ROCK 1
#define PAPER 2 #define PAPER 2
#define SCISSORS 3 #define SCISSORS 3
#define MAX_LEN 30
#define MAX_GUESSES 6
#endif #endif
Loading…
Cancel
Save