From 282bb86783899dea3d652d3706d2e0b34767f2e7 Mon Sep 17 00:00:00 2001 From: JirR02 Date: Wed, 25 Sep 2024 09:00:53 +0200 Subject: [PATCH] Handed in Project 1 --- Projekt_1/.cx_metadata.json | 5 ++ Projekt_1/Task_1/guess_a_number.cpp | 33 ++++++++++++ Projekt_1/Task_1/guess_a_number.h | 15 ++++++ Projekt_1/Task_1/main.cpp | 22 ++++++++ Projekt_1/Task_2/guess_a_number.cpp | 49 +++++++++++++++++ Projekt_1/Task_2/guess_a_number.h | 82 +++++++++++++++++++++++++++++ Projekt_1/Task_2/main.cpp | 48 +++++++++++++++++ 7 files changed, 254 insertions(+) create mode 100644 Projekt_1/.cx_metadata.json create mode 100644 Projekt_1/Task_1/guess_a_number.cpp create mode 100644 Projekt_1/Task_1/guess_a_number.h create mode 100644 Projekt_1/Task_1/main.cpp create mode 100644 Projekt_1/Task_2/guess_a_number.cpp create mode 100644 Projekt_1/Task_2/guess_a_number.h create mode 100644 Projekt_1/Task_2/main.cpp diff --git a/Projekt_1/.cx_metadata.json b/Projekt_1/.cx_metadata.json new file mode 100644 index 0000000..48c30be --- /dev/null +++ b/Projekt_1/.cx_metadata.json @@ -0,0 +1,5 @@ +{ + "main.cpp": "d", + "guess_a_number.cpp": "r", + "guess_a_number.h": "r" +} \ No newline at end of file diff --git a/Projekt_1/Task_1/guess_a_number.cpp b/Projekt_1/Task_1/guess_a_number.cpp new file mode 100644 index 0000000..5568e0a --- /dev/null +++ b/Projekt_1/Task_1/guess_a_number.cpp @@ -0,0 +1,33 @@ +#include +#include +#include +#include "guess_a_number.h" + +// NOTE: You cannot change this file, and you don't need to understand its +// content in order to solve your task. Feel free to look around, however, +// in case you're interested. + +const std::string action = std::getenv("ACTION"); + +// This function returns a randomly chosen integer from the interval [1, max]. +int randomly_choose_a_number(int max) { + return std::rand() % max + 1; +} + +int choose_a_number(int max) { + if (action == "run") { + std::cout << "?\n"; + // Just here to achieve the same output behaviour, in terms of newlines, + // when a user replaces + // std::cin >> number_to_guess; + // by + // number_to_guess = choose_a_number(MAX); + + return randomly_choose_a_number(max); + } else { + int guess; + std::cin >> guess; + + return guess; + } +} diff --git a/Projekt_1/Task_1/guess_a_number.h b/Projekt_1/Task_1/guess_a_number.h new file mode 100644 index 0000000..dee2d61 --- /dev/null +++ b/Projekt_1/Task_1/guess_a_number.h @@ -0,0 +1,15 @@ +#ifndef PROJECT_H +#define PROJECT_H + +// NOTE: You cannot change this file. In "only" contains declarations and +// short descriptions of the functionality we provided to you. + +// This function returns a randomly chosen integer from the interval [0, max]. +int randomly_choose_a_number(int max); + +// This function returns a number from the interval [1, max]. +// The number is randomly chosen if we're in interactive mode and parameter +// choose_randomly is true, and read from the keyboard (std::cin) otherwise. +int choose_a_number(int max); + +#endif \ No newline at end of file diff --git a/Projekt_1/Task_1/main.cpp b/Projekt_1/Task_1/main.cpp new file mode 100644 index 0000000..4506b2d --- /dev/null +++ b/Projekt_1/Task_1/main.cpp @@ -0,0 +1,22 @@ +#include +#include "guess_a_number.h" + +int main() { + // Declare the required variables + int number_to_guess; // The number to guess + int guess; // The guessed number + + // Pick the number to guess + std::cout << "Number to guess: "; + number_to_guess = choose_a_number(3); // Randomly choose a number from the interval [1, 3] + + // Save number inputted by user into variable guess + std::cin >> guess; + std::cout << "Your guess: "; + + if (guess == number_to_guess) { + std::cout << "Congratulations, you correctly guessed " << number_to_guess << "!"; + } else { + std::cout << "Sorry, but " << guess << " is wrong, " << number_to_guess << " was the number to guess."; + } +} diff --git a/Projekt_1/Task_2/guess_a_number.cpp b/Projekt_1/Task_2/guess_a_number.cpp new file mode 100644 index 0000000..91d5091 --- /dev/null +++ b/Projekt_1/Task_2/guess_a_number.cpp @@ -0,0 +1,49 @@ +#include +#include +#include +#include "guess_a_number.h" + +// NOTE: You cannot change this file, and you don't need to understand its +// content in order to solve your task. Feel free to look around, however, +// in case you're interested. + +const std::string action = std::getenv("ACTION"); + +void print_attempts_left(int attempts_left) { + std::cout << "You have " << attempts_left << " attempt(s) left.\n"; +} + +void print_you_won(int correct_guess) { + std::cout << "Congratulations, you correctly guessed " << correct_guess << "!\n"; +} + +void print_wrong_guess(int wrong_guess) { + std::cout << "Sorry, but " << wrong_guess << " is wrong.\n"; +} + +void print_you_lost(int number_to_guess, int attempts) { + std::cout << "You lost after " << attempts << " attempt(s) :-( The number to guess was " << number_to_guess << ".\n"; +} + +// This function returns a randomly chosen integer from the interval [1, max]. +int randomly_choose_a_number(int max) { + return std::rand() % max + 1; +} + +int choose_a_number(int max) { + if (action == "run") { + std::cout << "?\n"; + // Just here to achieve the same output behaviour, in terms of newlines, + // when a user replaces + // std::cin >> number_to_guess; + // by + // number_to_guess = choose_a_number(MAX); + + return randomly_choose_a_number(max); + } else { + int guess; + std::cin >> guess; + + return guess; + } +} diff --git a/Projekt_1/Task_2/guess_a_number.h b/Projekt_1/Task_2/guess_a_number.h new file mode 100644 index 0000000..de25c19 --- /dev/null +++ b/Projekt_1/Task_2/guess_a_number.h @@ -0,0 +1,82 @@ +#ifndef PROJECT_H +#define PROJECT_H + + +// NOTE: You cannot change this file. It "only" contains declarations and +// short descriptions of the functionality we provided to you. + + +// This function outputs the number of attempts that are left. +// +// Example: the function call print_attempts_left(3) results in the output +// "You have 3 attempt(s) left". +void print_attempts_left(int attempts_left); + +// This function outputs the winning message. +// +// Example: the function call print_you_won(9) results in the output +// "Congratulations, you correctly guessed 9!". +void print_you_won(int correct_guess); + +// This function outputs a message telling the user that their guess was wrong. +// +// Example: the function call print_wrong_guess(1) results in the output +// "Sorry, but 1 is wrong.". +void print_wrong_guess(int wrong_guess); + +// This function outputs the losing message. +// +// Example: the function call print_you_lost(2, 5) results in the output +// "You lost after 5 attempts :-( The number to guess was 2.". +void print_you_lost(int number_to_guess, int attempts); + + +// This function returns a randomly chosen integer from the interval [0, max]. +int randomly_choose_a_number(int max); + +// This function returns a number from the interval [1, max]. +// The number is randomly chosen if we're in interactive mode and parameter +// choose_randomly is true, and read from the keyboard (std::cin) otherwise. +int choose_a_number(int max); + + +// Master implementation for part 1: reads the next guess from the keyboard +// and stores it in the passed variable. +// +// Example: given the function call PART1_read_next_guess(my_guess) and +// assuming that the player enters 1, variable my_guess will afterwards +// have the value 1. +void PART1_read_next_guess(int& guess); + +// Master implementation for part 2: compares guess to number_to_guess, +// outputs a result-depending message (correct/wrong) and updates +// variable play to false if the guess was correct. +// +// Example: given the function call +// PART2_handle_guess(1, 2, continue_playing), a wrong-guess message +// will be outputted. +// +// Example: given the function call +// PART2_handle_guess(2, 2, continue_playing), a correct-guess message +// will be output and variable continue_playing will be set to false. +void PART2_handle_guess(int guess, int number_to_guess, bool& play); + +// Master implementation for part 3: increments the number of guessing +// attempts the player made (variable attempts), and if the maximum +// number of guesses has been made, outputs a corresponding message and +// sets variable play to false. +// +// Example: given the function call +// PART3_finish_round(2, 10, performed_attempts, continue_playing), and +// assuming that performed_attempts has a value of 5, then the only +// effect of the function call is that performed_attempts will be updated +// to 6. +// +// Example: given the function call +// PART3_finish_round(2, 10, performed_attempts, continue_playing), and +// assuming that performed_attempts has a value of 9, then +// performed_attempts will be incremented to 10, a you-lost message will +// be output and continue_playing will be set to false. +void PART3_finish_round(int number_to_guess, int max_attempts, int& attempts, bool& play); + +#endif \ No newline at end of file diff --git a/Projekt_1/Task_2/main.cpp b/Projekt_1/Task_2/main.cpp new file mode 100644 index 0000000..b0fbb30 --- /dev/null +++ b/Projekt_1/Task_2/main.cpp @@ -0,0 +1,48 @@ +#include +#include "guess_a_number.h" + +int main() { + int number_to_guess; // The number to guess + int max_attempts; // The guessed number + + std::cout << "Number to guess: "; + number_to_guess = choose_a_number(10); // Randomly choose a number from the interval [1, 10] + + std::cout << "Number of attempts: "; + std::cin >> max_attempts; + + // Make sure that the player has at least one attempt + if (max_attempts < 1) max_attempts = 1; + + int attempts = 0; // Attempts made so far + bool play = true; // false once the game is over + + while (play) { + print_attempts_left(max_attempts - attempts); + + // *** Part 1: input the next guess **************************************** + int guess; // The user's guess + //PART1_read_next_guess(guess); + std::cout << "Your guess: "; + std::cin >> guess; + + // *** Part 2: handle the guess the user made ***************************** + //PART2_handle_guess(guess, number_to_guess, play); + if (guess == number_to_guess) { + print_you_won(guess); + play = false; + } else { + print_wrong_guess(guess); + } + + // *** Part 3: finish up the round **************************************** + if (play) { + //PART3_finish_round(number_to_guess, max_attempts, attempts, play); + attempts += 1; + if (attempts == max_attempts) { + print_you_lost(number_to_guess, attempts); + play = false; + } + } + } +}