Converted everything to orgmode

converted everything to orgmode and added solution to the README files
This commit is contained in:
2025-03-31 08:37:19 +02:00
parent 3013d7ad47
commit 311cb8434c
25 changed files with 319 additions and 502 deletions

View File

@@ -104,51 +104,20 @@ You can also see the results for your submission on the "Enrolled Courses" tab o
* Solution
Front End to import libraries and files.
#+begin_src cpp
#include <iostream>
#include "guess_a_number.h"
#+end_src
Main function where the code runs.
#+begin_src cpp
int main() {
#+end_src
Declaring Variable which will be used.
#+begin_src cpp
int number_to_guess; // The number to guess
int guess; // The guessed number
#+end_src
Front end terminal output.
#+begin_src cpp
std::cout << "Number to guess: ";
#+end_src
~choose_a_number()~ function is called to choose a number between 1 and 3
#+begin_src cpp
number_to_guess = choose_a_number(3);
#+end_src
Input front end.
#+begin_src cpp
std::cin >> guess;
#+end_src
Output the input of the user.
#+begin_src cpp
std::cout << "Your guess: ";
#+end_src
Depending on the guess, the output in the terminal is different, if the input of the user is the same with the random number, it will output ~Congratulations, you correctly guessed x!~. Otherwise it will output something else.
#+begin_src cpp
if (guess == number_to_guess) {
std::cout << "Congratulations, you correctly guessed " << number_to_guess << "!";
} else {

View File

@@ -13,11 +13,11 @@ The first project consists of two tasks: *task 1* was to reimplement the first v
The program template of task 2 differs from that of task 1 in two important ways:
1. A goal of task 1 was to show you how important — and potentially cumbersome — it is to precisely fulfil output requirements and to pass the automated tests. This is no longer the focus of task 2, and you are therefore given functions such as `print_you_won(...)` that generate the expected output for you.
1. A goal of task 1 was to show you how important — and potentially cumbersome — it is to precisely fulfil output requirements and to pass the automated tests. This is no longer the focus of task 2, and you are therefore given functions such as =print_you_won(...)= that generate the expected output for you.
1. The final program consists of three major parts, and the template has three corresponding "holes" where you have to fill in code: inputting the next guess, checking the guess, and finishing the current round (we refer to each guess as one round of the game).
To make it easier for you to develop your solution step by step, you are given a "master implementation" for these steps, e.g `PART1_read_next_guess(...)`, which you must replace with your own code. Use the following workflow:
To make it easier for you to develop your solution step by step, you are given a "master implementation" for these steps, e.g =PART1_read_next_guess(...)=, which you must replace with your own code. Use the following workflow:
1. Run the tests: everything should be fine (thanks to the master implementations)
@@ -31,7 +31,7 @@ To make it easier for you to develop your solution step by step, you are given a
* Your task
As mentioned above, you can develop the final solution step by step, by iteratively replacing each call to a `STEP...` master implementation by your own code.
As mentioned above, you can develop the final solution step by step, by iteratively replacing each call to a =STEP...= master implementation by your own code.
** Step 1: input the next guess
@@ -59,7 +59,7 @@ As mentioned above, you can develop the final solution step by step, by iterativ
PART2_handle_guess(guess, number_to_guess, play);
#+end_src
1. Write code that compares the user-made guess with the number to guess: if the two are equal, call `print_you_won(guess)` to generate a you-won message, and set variable `play` to `false` to end the game. Otherwise, call `print_wrong_guess(guess)` to generate a wrong-guess message (and let the game continue).
1. Write code that compares the user-made guess with the number to guess: if the two are equal, call =print_you_won(guess)= to generate a you-won message, and set variable =play= to =false= to end the game. Otherwise, call =print_wrong_guess(guess)= to generate a wrong-guess message (and let the game continue).
** Step 3: handle the guess the user made
@@ -102,42 +102,29 @@ You lost after 2 attempt(s) :-( The number to guess was 2.
* Submitting your solution
*Important*: you /must/ replace all three master implementations (calls to functions `PART1/2/3`) with your own code! Your submission /will not be accepted/ if it still uses the master implementations, regardless of how many tests pass when /you/ run them.
*Important*: you /must/ replace all three master implementations (calls to functions =PART1/2/3=) with your own code! Your submission /will not be accepted/ if it still uses the master implementations, regardless of how many tests pass when /you/ run them.
-----
* Solutions
Fronted to import libraries and the code from Task 1
#+begin_src cpp
#include <iostream>
#include "guess_a_number.h"
#+end_src
Main funtion where the magic happens. We define two variables: `number_to_guess` and `max_attempts` and we assign them.
#+begin_src cpp
int main() {
int number_to_guess;
int max_attempts;
int number_to_guess;
int max_attempts;
std::cout << "Number to guess: ";
number_to_guess = choose_a_number(10);
std::cout << "Number of attempts: ";
#+end_src
Afterwards we make sure that the player has at least one attempts and create a variable `attempts` to count the attempts the player had.
#+begin_src cpp
if (max_attempts < 1) max_attempts = 1;
int attempts = 0; // Attempts made so far
bool play = true; // false once the game is over
#+end_src
Now in a while loop, we solve `PART 1` to `3` commenting out the master functions.
#+begin_src cpp
while (play) {
print_attempts_left(max_attempts - attempts);