Browse Source

Merge branch 'Saba' into 'main'

adding more features

See merge request pmuw_projekt/pmuw_projekt_notebinder!39
remotes/origin/Saba
fdai7875 11 months ago
parent
commit
2c40ecc9e1
  1. 7
      .idea/vcs.xml
  2. 3
      src/main/c/Hangman/drawHangman.h
  3. 5
      src/main/c/Hangman/initializeHangman.h
  4. 26
      src/main/c/Hangman/playHangman.c
  5. 12
      src/main/c/Hangman/playHangman.h
  6. 93
      src/main/c/Hangman/word_selector.c
  7. 4
      src/main/c/Hangman/word_selector.h
  8. 2
      src/main/c/main.c
  9. 23
      test/Hangman/test_initializeHangman.c

7
.idea/vcs.xml

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
<mapping directory="$PROJECT_DIR$/Unity" vcs="Git" />
</component>
</project>

3
src/main/c/Hangman/drawHangman.h

@ -3,4 +3,7 @@
#include <stdio.h> #include <stdio.h>
void drawHangman(int incorrectGuesses);
void currentState(char *currentGuess, int mistakes);
#endif //PMUW_PROJEKT_NOTEBINDER_DRAWHANGMAN_H #endif //PMUW_PROJEKT_NOTEBINDER_DRAWHANGMAN_H

5
src/main/c/Hangman/initializeHangman.h

@ -3,4 +3,9 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <ctype.h> #include <ctype.h>
void initializeHangman(char *wordToGuess, char *currentGuess);
void printRules();
void displayRules();
#endif //PMUW_PROJEKT_NOTEBINDER_INITIALIZEHANGMAN_H #endif //PMUW_PROJEKT_NOTEBINDER_INITIALIZEHANGMAN_H

26
src/main/c/Hangman/playHangman.c

@ -9,13 +9,23 @@ char toLower(char ch) {
return ch; return ch;
} }
void playHangman(char *wordToGuess) {
void playHangman() {
char *wordToGuess;
int mistakes = 0; int mistakes = 0;
int score = 10;
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;
displayRules(); displayRules();
printf("choose the difficulty level: (1, 2 or 3)\n");
printf("1. Easy \n");
printf("2. Hard \n");
printf("3. Super hard \n");
scanf("%d", &difficulty);
wordToGuess = selectRandomWord(difficulty);
// Initialize the current guess and print the rules // Initialize the current guess and print the rules
initializeHangman(wordToGuess, currentGuess); initializeHangman(wordToGuess, currentGuess);
currentState(currentGuess, mistakes); currentState(currentGuess, mistakes);
@ -62,12 +72,14 @@ void playHangman(char *wordToGuess) {
// Update mistakes (if the guess is wrong) // Update mistakes (if the guess is wrong)
if (!found) { if (!found) {
mistakes++; mistakes++;
score--;
} }
// Win: Check if the player guessed all the letters // Win: Check if the player guessed all the letters
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("\n" printf("\n"
" __ __ __ __ _ \n" " __ __ __ __ _ \n"
" \\ \\ / /__ _ _ \\ \\ / /__ _ _ | |\n" " \\ \\ / /__ _ _ \\ \\ / /__ _ _ | |\n"
@ -81,7 +93,17 @@ void playHangman(char *wordToGuess) {
if (mistakes == MAX_MISTAKES) { if (mistakes == MAX_MISTAKES) {
currentState(currentGuess, mistakes); currentState(currentGuess, mistakes);
printf("Oops! You have no more guesses :( \n The answer was: %s \n", wordToGuess); printf("Oops! You have no more guesses :( \n The answer was: %s \n", wordToGuess);
break;
int retry;
printf("Do you want to retry? \n");
printf("1. YES\n");
printf("2. NO\n");
scanf("%d", &retry);
if (retry == 1){
playHangman();
} else {
break;
}
} }
currentState(currentGuess, mistakes); currentState(currentGuess, mistakes);

12
src/main/c/Hangman/playHangman.h

@ -8,16 +8,10 @@
#include "drawHangman.h" #include "drawHangman.h"
#include "word_selector.h" #include "word_selector.h"
#define MAX_MISTAKES 6
// Include function implementations directly from .c files // Include function implementations directly from .c files
void playHangman(char *wordToGuess);
void initializeHangman(char *wordToGuess, char *currentGuess);
void printRules();
void drawHangman(int incorrectGuesses);
void currentState(char *currentGuess, int mistakes);
const char* selectRandomWord();
const char wordsList[NUM_WORDS][MAX_WORD_LENGTH + 1];
void displayRules();
#define MAX_MISTAKES 6
void playHangman();
#endif // PMUW_PROJEKT_NOTEBINDER_PLAYHANGMAN_H #endif // PMUW_PROJEKT_NOTEBINDER_PLAYHANGMAN_H

93
src/main/c/Hangman/word_selector.c

@ -1,33 +1,92 @@
#include "word_selector.h" #include "word_selector.h"
const char wordsList[NUM_WORDS][MAX_WORD_LENGTH + 1] = {
"skill",
// easy words
char wordsList_easy[NUM_WORDS][MAX_WORD_LENGTH + 1] = {
"apple",
"world", "world",
"difference",
"celebration",
"association",
"customer",
"banana",
"cat",
"lemon",
"dog",
"mood", "mood",
"agreement",
"audience",
"professor",
"orange",
"kite",
"nest",
"year", "year",
"dealer",
"patience",
"table tennis",
"pollution",
"awareness",
"grape",
"sun",
"mouse",
"tennis",
"queen",
"problem", "problem",
"vehicle", "vehicle",
"death", "death",
"cousin" "cousin"
}; };
const char* selectRandomWord() {
// hard words
char wordsList_hard[NUM_WORDS][MAX_WORD_LENGTH + 1] = {
"database",
"programming",
"mountain",
"customer",
"table tennis",
"customer",
"laughter",
"expression",
"audience",
"professor",
"twilight",
"dealer",
"symphony",
"table tennis",
"pollution",
"inspiration",
"supermarket",
"difficulty",
"budget",
"quarter"
};
// super hard words
char wordsList_super_hard[NUM_WORDS][MAX_WORD_LENGTH + 1] = {
"Cacophony",
"Rambunctious",
"Serendipity",
"Resilience",
"Discombobulate",
"Pernicious",
"Eccentricity",
"Mellifluous",
"Ineffable",
"Obfuscate",
"Quixotic",
"Paradoxical",
"Esoteric",
"Perspicacious",
"Indubitable",
"Incandescent",
"Vicissitude",
"Juxtaposition",
"Exacerbate",
"Epiphany"
};
char* selectRandomWord(int difficulty) {
// pick a random number and assign it to index of wordsList array
// pick a random number and assign it to index of wordsList_easy array
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
int randomIndex = rand() % NUM_WORDS; int randomIndex = rand() % NUM_WORDS;
return wordsList[randomIndex];
switch(difficulty){
case 1:
return wordsList_easy[randomIndex];
case 2:
return wordsList_hard[randomIndex];
case 3:
return wordsList_super_hard[randomIndex];
default:
printf("Invalid difficulty level. We choose easy level for you.\n");
return wordsList_easy[randomIndex];
}
} }

4
src/main/c/Hangman/word_selector.h

@ -9,4 +9,8 @@
#define MAX_WORD_LENGTH 20 #define MAX_WORD_LENGTH 20
#define NUM_WORDS 20 #define NUM_WORDS 20
char* selectRandomWord();
char wordsList_easy[NUM_WORDS][MAX_WORD_LENGTH + 1];
char wordsList_hard[NUM_WORDS][MAX_WORD_LENGTH + 1];
#endif //PMUW_PROJEKT_NOTEBINDER_WORD_SELECTOR_H #endif //PMUW_PROJEKT_NOTEBINDER_WORD_SELECTOR_H

2
src/main/c/main.c

@ -30,7 +30,7 @@ int main(){
switch (option){ switch (option){
case 1: case 1:
playHangman(selectRandomWord());
playHangman();
break; break;
case 2: case 2:
pong(); pong();

23
test/Hangman/test_initializeHangman.c

@ -0,0 +1,23 @@
#include <stdio.h>
#include "playHangman.h"
void test_displayRules() {
printf("Test Case 1: User inputs '1'\n");
printf("Expected Output: 'Let's get started!'\n");
printf("Actual Output: ");
printf("1\n");
displayRules();
printf("\nTest Case 2: User inputs '2'\n");
printf("Expected Output: Output of printRules() function\n");
printf("Actual Output: ");
printf("2\n");
displayRules();
printf("\nTest Case 3: User inputs invalid value\n");
printf("Expected Output: 'Please enter either 1 or 2 to continue'\n");
printf("Actual Output: ");
printf("invalid\n");
displayRules();
}
Loading…
Cancel
Save