Updated to new Semester

This commit is contained in:
2025-02-20 13:54:56 +01:00
parent 35a05a634e
commit 2b700071aa
18 changed files with 36 additions and 51 deletions

View File

@@ -0,0 +1,9 @@
# Guess A Number (Task 1)
## 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, with at most 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 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.

View File

@@ -0,0 +1,33 @@
#include <cstdlib>
#include <iostream>
#include <string>
#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;
}
}

View File

@@ -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

View File

@@ -0,0 +1,22 @@
#include <iostream>
#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.";
}
}