Browse Source

Merge branch 'Saba' into 'main'

Hangman added to main

See merge request pmuw_projekt/pmuw_projekt_notebinder!36
remotes/origin/Saba
fdai8032 11 months ago
parent
commit
b21f9e1d20
  1. 89
      src/main/c/Hangman/drawHangman.c
  2. 6
      src/main/c/Hangman/drawHangman.h
  3. 44
      src/main/c/Hangman/initializeHangman.c
  4. 6
      src/main/c/Hangman/initializeHangman.h
  5. 89
      src/main/c/Hangman/playHangman.c
  6. 23
      src/main/c/Hangman/playHangman.h
  7. 33
      src/main/c/Hangman/word_selector.c
  8. 12
      src/main/c/Hangman/word_selector.h
  9. 6
      src/main/c/main.c
  10. 40
      test/Hangman/test_drawHangman.c
  11. 4
      test/Hangman/test_drawHangman.h
  12. 56
      test/Hangman/test_playHangman.c

89
src/main/c/Hangman/drawHangman.c

@ -0,0 +1,89 @@
#include "drawHangman.h"
void drawHangman(int incorrectGuesses) {
if (incorrectGuesses == 0) {
printf(" +---+\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("=========\n");
}
if (incorrectGuesses == 1) {
printf(" +---+\n");
printf(" | |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("=========\n");
}
if (incorrectGuesses == 2) {
printf(" +---+\n");
printf(" | |\n");
printf(" O |\n");
printf(" |\n");
printf(" |\n");
printf(" |\n");
printf("=========\n");
}
if (incorrectGuesses == 3) {
printf(" +---+\n");
printf(" | |\n");
printf(" O |\n");
printf(" | |\n");
printf(" |\n");
printf(" |\n");
printf("=========\n");
}
if (incorrectGuesses == 4) {
printf(" +---+\n");
printf(" | |\n");
printf(" O |\n");
printf(" /| |\n");
printf(" |\n");
printf(" |\n");
printf("=========\n");
}
if (incorrectGuesses == 5) {
printf(" +---+\n");
printf(" | |\n");
printf(" O |\n");
printf(" /|\\ |\n");
printf(" |\n");
printf(" |\n");
printf("=========\n");
}
if (incorrectGuesses == 6) {
printf(" +---+\n");
printf(" | |\n");
printf(" O |\n");
printf(" /|\\ |\n");
printf(" / \\ |\n");
printf(" |\n");
printf("=========\n");
}
if (incorrectGuesses >= 6) {
printf("\n"
" ___ ___ _ \n"
" / __|__ _ _ __ ___ / _ \\__ _____ _ _| |\n"
" | (_ / _` | ' \\/ -_) | (_) \\ V / -_) '_|_|\n"
" \\___\\__,_|_|_|_\\___| \\___/ \\_/\\___|_| (_)\n"
" \n");
}
}
void currentState(char *currentGuess, int mistakes) {
printf("Current Word: %s \n", currentGuess);
printf("Mistakes Made: %d/6 \n", mistakes);
drawHangman(mistakes);
}

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

@ -0,0 +1,6 @@
#ifndef PMUW_PROJEKT_NOTEBINDER_DRAWHANGMAN_H
#define PMUW_PROJEKT_NOTEBINDER_DRAWHANGMAN_H
#include <stdio.h>
#endif //PMUW_PROJEKT_NOTEBINDER_DRAWHANGMAN_H

44
src/main/c/Hangman/initializeHangman.c

@ -0,0 +1,44 @@
#include "initializeHangman.h"
void initializeHangman(char *wordToGuess, char *currentGuess) {
int wordLength = strlen(wordToGuess);
for (int i = 0; i < wordLength; ++i) {
if (isalpha(wordToGuess[i])) {
currentGuess[i] = '_';
} else {
currentGuess[i] = wordToGuess[i];
}
}
currentGuess[wordLength] = '\0';
}
void printRules(){
printf("Welcome to hangman game!\n");
printf("How to play: \n");
printf(" - A word be randomly selected from the list of words.\n");
printf(" - A series of dashes will be displayed, each representing a letter in the secret word.\n");
printf(" - You are allowed a maximum of 6 mistakes.\n");
printf(" - You will start guessing letters one at a time.\n");
printf(" - Each guess should be a single letter of the alphabet, either uppercase or lowercase.\n");
printf(" - After each guess, the computer will check if the letter you guessed is in the secret word.\n");
printf(" - If the guessed letter is in the word, the computer will reveal all occurrences of that letter in the word.\n");
printf(" - If the guessed letter is not in the word, the computer will count the incorrect guesses.\n");
printf(" - Wrong guesses are represented by drawing parts of a hangman figure.\n");
printf(" - You are allowed a maximum of 6 mistakes.\n");
}
void displayRules(){
int readRules;
printf("Do you know how to play? (type number 1 or 2): \n");
printf("1. YES \n");
printf("2. NO \n");
scanf("%d", &readRules);
if(readRules == 2){
printRules();
} else if (readRules == 1) {
printf("Let's get started!\n");
} else {
printf("Please enter either 1 or 2 to continue: \n");
displayRules();
}
}

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

@ -0,0 +1,6 @@
#ifndef PMUW_PROJEKT_NOTEBINDER_INITIALIZEHANGMAN_H
#define PMUW_PROJEKT_NOTEBINDER_INITIALIZEHANGMAN_H
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#endif //PMUW_PROJEKT_NOTEBINDER_INITIALIZEHANGMAN_H

89
src/main/c/Hangman/playHangman.c

@ -0,0 +1,89 @@
#include "playHangman.h"
// Converts the letter to lower case
char toLower(char ch) {
// Convert uppercase letter to lowercase manually
if (ch >= 'A' && ch <= 'Z') {
return ch + ('a' - 'A');
}
return ch;
}
void playHangman(char *wordToGuess) {
int mistakes = 0;
char guessedLetters[30] = ""; //Guessed letters
char currentGuess[50]; //Current state of the guessed word
displayRules();
// Initialize the current guess and print the rules
initializeHangman(wordToGuess, currentGuess);
currentState(currentGuess, mistakes);
while (mistakes < MAX_MISTAKES) {
// Get a letter from the user
char input[20];
printf("Enter your guess: \n");
scanf("%s", input);
if(strlen(input) > 1) {
printf("Invalid input. Please enter a single letter.\n");
continue;
}
char guess = input[0];
if (!isalpha(guess)) {
printf("Please enter a valid alphabet.\n");
continue;
}
// Convert uppercase letter to lowercase
guess = toLower(guess);
// Check if the letter has already been guessed by the player
if (strchr(guessedLetters, guess) != NULL) {
printf("You already guessed that letter. Try another letter.\n");
continue;
}
// Add the guessed letter to the list
guessedLetters[strlen(guessedLetters)] = guess;
// Check if the guessed letter is in the word
int found = 0;
for (int i = 0; i < strlen(wordToGuess); ++i) {
if (wordToGuess[i] == guess) {
currentGuess[i] = guess;
found = 1;
}
}
// Update mistakes (if the guess is wrong)
if (!found) {
mistakes++;
}
// Win: Check if the player guessed all the letters
if (strcmp(currentGuess, wordToGuess) == 0) {
currentState(currentGuess, mistakes);
printf("Bravo! You guessed the word: %s \n", wordToGuess);
printf("\n"
" __ __ __ __ _ \n"
" \\ \\ / /__ _ _ \\ \\ / /__ _ _ | |\n"
" \\ V / _ \\ || | \\ \\/\\/ / _ \\ ' \\|_|\n"
" |_|\\___/\\_,_| \\_/\\_/\\___/_||_(_)\n"
" \n");
break;
}
// Lose: Print the answer + game over
if (mistakes == MAX_MISTAKES) {
currentState(currentGuess, mistakes);
printf("Oops! You have no more guesses :( \n The answer was: %s \n", wordToGuess);
break;
}
currentState(currentGuess, mistakes);
}
}

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

@ -0,0 +1,23 @@
#ifndef PMUW_PROJEKT_NOTEBINDER_PLAYHANGMAN_H
#define PMUW_PROJEKT_NOTEBINDER_PLAYHANGMAN_H
#include <stdio.h>
// Include declarations from corresponding header files
#include "initializeHangman.h"
#include "drawHangman.h"
#include "word_selector.h"
// 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
#endif // PMUW_PROJEKT_NOTEBINDER_PLAYHANGMAN_H

33
src/main/c/Hangman/word_selector.c

@ -0,0 +1,33 @@
#include "word_selector.h"
const char wordsList[NUM_WORDS][MAX_WORD_LENGTH + 1] = {
"skill",
"world",
"difference",
"celebration",
"association",
"customer",
"mood",
"agreement",
"audience",
"professor",
"year",
"dealer",
"patience",
"table tennis",
"pollution",
"awareness",
"problem",
"vehicle",
"death",
"cousin"
};
const char* selectRandomWord() {
// pick a random number and assign it to index of wordsList array
srand((unsigned int)time(NULL));
int randomIndex = rand() % NUM_WORDS;
return wordsList[randomIndex];
}

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

@ -0,0 +1,12 @@
#ifndef PMUW_PROJEKT_NOTEBINDER_WORD_SELECTOR_H
#define PMUW_PROJEKT_NOTEBINDER_WORD_SELECTOR_H
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX_WORD_LENGTH 20
#define NUM_WORDS 20
#endif //PMUW_PROJEKT_NOTEBINDER_WORD_SELECTOR_H

6
src/main/c/main.c

@ -2,7 +2,7 @@
#include <stdlib.h>
#include <stdbool.h>
#include "Template/game100.h"
#include "GameTic_Tac_Toe/tictactoe.h"
#include "Snake/snake_start.h"
#include "Minesweeper/minesweeper_start.h"
@ -13,9 +13,9 @@ int main(){
bool running = true;
while (running){
int option = 0;
system("clear");
printf("Waehlen Sie eine Option:\n");
printf("\t1.Hangman starten\n");

40
test/Hangman/test_drawHangman.c

@ -0,0 +1,40 @@
#include <stdio.h>
#include <assert.h>
#include "drawHangman.h"
#include "playHangman.h"
void test_drawHangman() {
// Test each case of incorrectGuesses
printf("Testing drawHangman(0):\n");
drawHangman(0);
printf("\n");
printf("Testing drawHangman(1):\n");
drawHangman(1);
printf("\n");
printf("Testing drawHangman(2):\n");
drawHangman(2);
printf("\n");
printf("Testing drawHangman(3):\n");
drawHangman(3);
printf("\n");
printf("Testing drawHangman(4):\n");
drawHangman(4);
printf("\n");
printf("Testing drawHangman(5):\n");
drawHangman(5);
printf("\n");
printf("Testing drawHangman(6):\n");
drawHangman(6);
printf("\n");
printf("Testing drawHangman(7):\n");
drawHangman(7);
printf("\n");
}

4
test/Hangman/test_drawHangman.h

@ -0,0 +1,4 @@
#ifndef PMUW_PROJEKT_NOTEBINDER_TEST_DRAWHANGMAN_H
#define PMUW_PROJEKT_NOTEBINDER_TEST_DRAWHANGMAN_H
#include "drawHangman.c"
#endif //PMUW_PROJEKT_NOTEBINDER_TEST_DRAWHANGMAN_H

56
test/Hangman/test_playHangman.c

@ -0,0 +1,56 @@
#include <stdio.h>
#include "playHangman.c"
#include <assert.h>
void test_displayRules() {
// Simulate user input: 1, 2, and an invalid input
printf("Test Case 1: User enters '1' (YES)\n");
printf("Expected Output: \"Let's get started!\"\n");
printf("Actual Output: ");
displayRules(); // Call displayRules() with input 1
printf("\nTest Case 2: User enters '2' (NO)\n");
printf("Expected Output: Output of printRules()\n");
printf("Actual Output: ");
displayRules(); // Call displayRules() with input 2
printf("\nTest Case 3: User enters an invalid input\n");
printf("Expected Output: \"Please enter either 1 or 2 to continue:\"\n");
printf("Actual Output: ");
displayRules(); // Call displayRules() with invalid input
}
void test_toLower() {
printf("Test Case 1: Lowercase letter 'a'\n");
printf("Expected Output: 'a'\n");
char result = toLower('a');
printf("Actual Output: '%c'\n", result);
assert(result == 'a');
printf("\nTest Case 2: Uppercase letter 'B'\n");
printf("Expected Output: 'b'\n");
result = toLower('B');
printf("Actual Output: '%c'\n", result);
assert(result == 'b');
printf("\nTest Case 3: Non-alphabetic character '#'\n");
printf("Expected Output: '#'\n");
result = toLower('?');
printf("Actual Output: '%c'\n", result);
assert(result == '?');
}
void test_initializeHangman() {
// Test case 1: Word with alphabets only
char wordToGuess1[] = "hangman";
char currentGuess1[8]; // Make sure this size is enough to accommodate the wordToGuess and the null terminator
initializeHangman(wordToGuess1, currentGuess1);
assert(strcmp(currentGuess1, "_______") == 0);
// Test case 2: Word with spaces (e.g: table tennis)
char wordToGuess2[] = "hello world";
char currentGuess2[12]; // Make sure this size is enough to accommodate the wordToGuess and the null terminator
initializeHangman(wordToGuess2, currentGuess2);
assert(strcmp(currentGuess2, "_____ _____") == 0);
printf("initializeHangman test passed!\n");
}
Loading…
Cancel
Save