Browse Source

Merge branch 'Saba' into 'main'

hint added

See merge request pmuw_projekt/pmuw_projekt_notebinder!42
remotes/origin/Saba
fdai7875 11 months ago
parent
commit
373d27ff96
  1. 53
      src/main/c/Hangman/playHangman.c

53
src/main/c/Hangman/playHangman.c

@ -9,10 +9,38 @@ char toLower(char ch) {
return ch; return ch;
} }
// Hint function
char *hint = NULL;
int hintAvailable = 1;
char generateHint(char *wordToGuess, char *guessedLetters) {
int length = strlen(wordToGuess);
char availableLetters[length]; // Array to store letters not guessed yet
int numAvailableLetters = 0;
// Check each letter in the word
for (int i = 0; i < length; i++) {
char letter = wordToGuess[i];
// Check if the letter has not been guessed
if (strchr(guessedLetters, letter) == NULL) {
availableLetters[numAvailableLetters++] = letter; // Store the available letter
}
}
// Randomly select a letter from available letters
if (numAvailableLetters > 0) {
int randomIndex = rand() % numAvailableLetters;
return availableLetters[randomIndex];
} else {
// No available letters, return a placeholder value
return '-';
}
}
void playHangman() { void playHangman() {
char *wordToGuess; char *wordToGuess;
int mistakes = 0; int mistakes = 0;
int score = 10;
int score = 6;
char guessedLetters[30] = ""; //Guessed letters char guessedLetters[30] = ""; //Guessed letters
char currentGuess[50]; //Current state of the guessed word char currentGuess[50]; //Current state of the guessed word
int difficulty; int difficulty;
@ -32,6 +60,27 @@ void playHangman() {
while (mistakes < MAX_MISTAKES) { while (mistakes < MAX_MISTAKES) {
// Asks for hint
if (mistakes == 3 && hintAvailable) {
int wantHint;
printf("Do you want a hint? (1 or 2): \n");
printf("1. YES\n");
printf("2. NO\n");
scanf(" %d", &wantHint);
if (tolower(wantHint) == 1) {
char hintLetter = generateHint(wordToGuess, guessedLetters);
if (hintLetter != '-') {
printf("Hint: %c\n", hintLetter); // Provide the hint
hintAvailable = 0; // Hint is provided, mark as unavailable
} else {
printf("No available hint.\n");
}
} else {
printf("No hint provided.\n");
}
}
// Get a letter from the user // Get a letter from the user
char input[20]; char input[20];
printf("Enter your guess: \n"); printf("Enter your guess: \n");
@ -79,7 +128,7 @@ void playHangman() {
if (strcmp(currentGuess, wordToGuess) == 0) { if (strcmp(currentGuess, wordToGuess) == 0) {
currentState(currentGuess, mistakes); currentState(currentGuess, mistakes);
printf("Bravo! You guessed the word: %s \n", wordToGuess); printf("Bravo! You guessed the word: %s \n", wordToGuess);
printf("your score is %d/10\n", score);
printf("your score is %d/6\n", score);
printf("\n" printf("\n"
" __ __ __ __ _ \n" " __ __ __ __ _ \n"
" \\ \\ / /__ _ _ \\ \\ / /__ _ _ | |\n" " \\ \\ / /__ _ _ \\ \\ / /__ _ _ | |\n"

Loading…
Cancel
Save