JirR02 88e0b5ed69 Converted everything to orgmode
converted everything to orgmode and added solution to the README files
2025-03-31 08:40:43 +02:00
..
2025-03-31 08:40:43 +02:00

Project 1: Guess A Number (Task 1)

About this task

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 (this task) is to reimplement the first version of the game that was presented in the lecture, task 2 is to implement the full game.

General development advice

Develop your program step-by-step, and save, run and compile it often. I.e. implement a single feature, such as inputting the guess, or comparing the guess and the correct number, and then compile and run the program to see if your code (still) works as intended. Small changes and repeated testing make it easier for you to observe problems, to work out what causes them, and to finally solve them.

Fulfilling requirements and testing programs

A particular goal of this task is to make you understand how strongly connected requirements and testing (and thus grading submissions) are. E.g. if the task description requires output of the shape "I␣saw␣\(n\)␣cat(s).", where $n$ is a number and ␣ represents blanks/whitespaces, your program will not be considered correct if it outputs a text that is "basically the same", but does not precisely match the requirements. E.g. the following outputs, while very close to the expected output, do not match the shape specified above: "i␣saw␣3␣cat(s).", "I␣saw␣3␣cats.", "I␣saw␣1␣cat.", "I␣saw␣2␣cat(s)" and "i␣saw␣2␣␣cat(s)␣.".

Meeting such requirements precisely is necessary for testing: we automatically (at least to some extent) test correctness of your programs by comparing the expected output to the output produced by your program. Since these comparisons are performed by a computer, it is much much easier to do them syntactically, i.e. letter-by-letter.

However, being able to precisely fulfil requirements is more generally important, in particular when working with customers: imagine you order a black smartphone, but then get delivered a dark blue one (same make and model). Although "basically the same", it's just not what you ordered. The same holds for software — it's the details that matter.

Your task

To solve this task, proceed as follows:

  1. Look at the template program, in particular main.cpp, and see what's already there (e.g. variable declarations) and what's still missing. The latter is hinted at by TODO comments.
  2. The template already contains code for picking the number to guess. By default, the number is randomly choosen from the interval \([1,3]\):
number_to_guess = choose_a_number(3);

During development, you might want to change this line: e.g. read the number from the keyboard:

std::cin >> number_to_guess;

or even hard-code (i.e. fix) a specific number, e.g.

number_to_guess = 3;

However, in order to run the automatic tests, and when submitting your final version, your program must either read the word from the keyboard or use the choose_a_number function.

  1. Now address the first TODO comment: by outputting the text "Your␣guess:␣" (as before, ␣ denotes a blank/whitespace character), followed by inputting the guess from the keyboard into variable guess.
  2. Now address the second TODO comment: by comparing the two numbers for equality. If they are, output the line
Congratulations,␣you␣correctly␣guessed␣X!

where X is the correctly guessed number. Since the output is expected to be a line, don't forget to end it with either \n or std::endl.

If the guess was wrong, however, output the line

Sorry,␣but␣Y␣is␣wrong,␣X␣was␣the␣number␣to␣guess.

where Y is the incorrectly guessed number.

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:

Number to guess: ?
Your guess: 3
Congratulations, you correctly guessed 3!

And a lost game:

Number to guess: ?
Your guess: 2
Sorry, but 2 is wrong, 1 was the number to guess.

Testing your program

You can always test your program manually: click the "play" button in the bottom panel, run your program and see if it behaves as expected.

Relevant for your final submission, however, is if it passes the automated tests: to run those, click the "chemistry flask" button in the bottom panel and wait for the output to appear. If your program passes all tests — everything is green and your score is 100% — then your program is ready to be submitted.

Otherwise, carefully compare the expected output to the actual output to find out what went wrong.

Reminder: The devil is in the detail! Pay attention to whitespace and newline characters, and in general check that your output fulfils all requirements, even the "boring" ones.

Submitting your solution

Finally, submit your solution (your program) by clicking the corresponding button in the top right corner of the Code Expert IDE (open Task/History first). Your program will be tested automatically, and your score will be shown in the "History" view, which can be opened by clicking on the corresponding tab on the right of the Code Expert IDE. Note that you can submit arbitrarily often (before the exercise deadline, of course), and your last submission will be considered for grading.

For this task, all tests need to pass in order to successfully solve this task. This should be rather easy, though, since there isn't much that can go wrong.

You can also see the results for your submission on the "Enrolled Courses" tab of Code Expert, as green or red percentage values to the left of the task's name.

Solution

#include <iostream>
#include "guess_a_number.h"

int main() {
  int number_to_guess; // The number to guess
  int guess;           // The guessed number

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

  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.";
  }
}