#+TITLE: Project 1: Guess the Number (Task 2)
#+AUTHOR: JirR02

* Project overview

The goal of the first project is to program a simple number guessing game: the player needs to correctly guess a number, chosen from an interval\([1,N]\), with at most \(K\)guesses.

In the lecture, a first version of the game was presented, in which the player had only one chance of guessing the correct number. In order to implement the full game, you will have to extend this version by allowing the player to guess up to \(K\) times.

The first project consists of two tasks: *task 1* was to reimplement the first version of the game that was presented in the lecture, *task 2* /(this task)/ is to implement the full game.

* Stepwise development

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. 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:

1. Run the tests: everything should be fine (thanks to the master implementations)

1. Comment the first master implementation; now all tests should fail

1. Write your own code that replaces the commented master implementation

1. Once all tests pass again, continue by commenting and replacing the next master implementation

1. Once you have replaced all master implementations with your own code (and all tests pass), your solution is ready to be submitted. *Read the remarks at the end of this text!*

* 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.

** Step 1: input the next guess

1. Find the code line

   #+begin_src cpp
    PART1_read_next_guess(guess);
   #+end_src

    and comment it, i.e. change it to

    #+begin_src cpp
    // PART1_read_next_guess(guess);
    #+end_src

1. As a replacement for the commented master implementation, write code that outputs "Your␣guess:␣" (as before, ␣ denotes a blank/whitespace character), and then inputs the next guess from the keyboard into variable guess.

1. Run the tests, they should all pass again

** Step 2: handle the guess the user made

1. Find and comment the code line

   #+begin_src cpp
   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).

** Step 3: handle the guess the user made

1. Find and comment the code line

#+begin_src cpp
PART3_finish_round(number_to_guess, max_attempts, attempts, play);
#+end_src   

1. Write code that increments the number of attempts the user made by 1. Furthermore, write code that 1. checks if the maximum number of attempts has been reached, and if so, 2. call print_you_lost(number_to_guess, attempts) and set play to false.

* Examples

As an illustration, consider the following example in- and outputs of two games (in which the number to guess was randomly chosen). A successfully completed game:

#+begin_src shell
Number to guess: ?
Number of attempts: 3
You have 3 attempt(s) left.
Your guess: 7
Sorry, but 7 is wrong.
You have 2 attempt(s) left.
Your guess: 3
Congratulations, you correctly guessed 3!
#+end_src

And a lost game:

#+begin_src shell
Number to guess: ?
Number of attempts: 2
You have 2 attempt(s) left.
Your guess: 123
Sorry, but 123 is wrong.
You have 1 attempt(s) left.
Your guess: 321
Sorry, but 321 is wrong.
You lost after 2 attempt(s) :-( The number to guess was 2.
#+end_src

* 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.

-----
* Solutions

#+begin_src cpp
#include <iostream>
#include "guess_a_number.h"

int main() {
  int number_to_guess;
  int max_attempts;

  std::cout << "Number to guess: ";
  number_to_guess = choose_a_number(10);

  std::cout << "Number of attempts: ";

  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;
      }
    }
  }
}
#+end_src

-----

Made by JirR02 in Switzerland 🇨🇭
