First Exercise
This commit is contained in:
@@ -1,9 +0,0 @@
|
||||
# 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.
|
@@ -1,33 +0,0 @@
|
||||
#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;
|
||||
}
|
||||
}
|
@@ -1,15 +0,0 @@
|
||||
#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
|
@@ -1,22 +0,0 @@
|
||||
#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.";
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user