Updated to new Semester
This commit is contained in:
9
Vorkurs/Projekt_1/Task_1/README.md
Normal file
9
Vorkurs/Projekt_1/Task_1/README.md
Normal 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.
|
33
Vorkurs/Projekt_1/Task_1/guess_a_number.cpp
Normal file
33
Vorkurs/Projekt_1/Task_1/guess_a_number.cpp
Normal 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;
|
||||
}
|
||||
}
|
15
Vorkurs/Projekt_1/Task_1/guess_a_number.h
Normal file
15
Vorkurs/Projekt_1/Task_1/guess_a_number.h
Normal 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
|
22
Vorkurs/Projekt_1/Task_1/main.cpp
Normal file
22
Vorkurs/Projekt_1/Task_1/main.cpp
Normal 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.";
|
||||
}
|
||||
}
|
9
Vorkurs/Projekt_1/Task_2/README.md
Normal file
9
Vorkurs/Projekt_1/Task_2/README.md
Normal file
@@ -0,0 +1,9 @@
|
||||
# Guess A Number (Task 2)
|
||||
|
||||
## 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 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.
|
49
Vorkurs/Projekt_1/Task_2/guess_a_number.cpp
Normal file
49
Vorkurs/Projekt_1/Task_2/guess_a_number.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#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");
|
||||
|
||||
void print_attempts_left(int attempts_left) {
|
||||
std::cout << "You have " << attempts_left << " attempt(s) left.\n";
|
||||
}
|
||||
|
||||
void print_you_won(int correct_guess) {
|
||||
std::cout << "Congratulations, you correctly guessed " << correct_guess << "!\n";
|
||||
}
|
||||
|
||||
void print_wrong_guess(int wrong_guess) {
|
||||
std::cout << "Sorry, but " << wrong_guess << " is wrong.\n";
|
||||
}
|
||||
|
||||
void print_you_lost(int number_to_guess, int attempts) {
|
||||
std::cout << "You lost after " << attempts << " attempt(s) :-( The number to guess was " << number_to_guess << ".\n";
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
}
|
82
Vorkurs/Projekt_1/Task_2/guess_a_number.h
Normal file
82
Vorkurs/Projekt_1/Task_2/guess_a_number.h
Normal file
@@ -0,0 +1,82 @@
|
||||
#ifndef PROJECT_H
|
||||
#define PROJECT_H
|
||||
|
||||
|
||||
// NOTE: You cannot change this file. It "only" contains declarations and
|
||||
// short descriptions of the functionality we provided to you.
|
||||
|
||||
|
||||
// This function outputs the number of attempts that are left.
|
||||
//
|
||||
// Example: the function call print_attempts_left(3) results in the output
|
||||
// "You have 3 attempt(s) left".
|
||||
void print_attempts_left(int attempts_left);
|
||||
|
||||
// This function outputs the winning message.
|
||||
//
|
||||
// Example: the function call print_you_won(9) results in the output
|
||||
// "Congratulations, you correctly guessed 9!".
|
||||
void print_you_won(int correct_guess);
|
||||
|
||||
// This function outputs a message telling the user that their guess was wrong.
|
||||
//
|
||||
// Example: the function call print_wrong_guess(1) results in the output
|
||||
// "Sorry, but 1 is wrong.".
|
||||
void print_wrong_guess(int wrong_guess);
|
||||
|
||||
// This function outputs the losing message.
|
||||
//
|
||||
// Example: the function call print_you_lost(2, 5) results in the output
|
||||
// "You lost after 5 attempts :-( The number to guess was 2.".
|
||||
void print_you_lost(int number_to_guess, int attempts);
|
||||
|
||||
|
||||
// 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);
|
||||
|
||||
|
||||
// Master implementation for part 1: reads the next guess from the keyboard
|
||||
// and stores it in the passed variable.
|
||||
//
|
||||
// Example: given the function call PART1_read_next_guess(my_guess) and
|
||||
// assuming that the player enters 1, variable my_guess will afterwards
|
||||
// have the value 1.
|
||||
void PART1_read_next_guess(int& guess);
|
||||
|
||||
// Master implementation for part 2: compares guess to number_to_guess,
|
||||
// outputs a result-depending message (correct/wrong) and updates
|
||||
// variable play to false if the guess was correct.
|
||||
//
|
||||
// Example: given the function call
|
||||
// PART2_handle_guess(1, 2, continue_playing), a wrong-guess message
|
||||
// will be outputted.
|
||||
//
|
||||
// Example: given the function call
|
||||
// PART2_handle_guess(2, 2, continue_playing), a correct-guess message
|
||||
// will be output and variable continue_playing will be set to false.
|
||||
void PART2_handle_guess(int guess, int number_to_guess, bool& play);
|
||||
|
||||
// Master implementation for part 3: increments the number of guessing
|
||||
// attempts the player made (variable attempts), and if the maximum
|
||||
// number of guesses has been made, outputs a corresponding message and
|
||||
// sets variable play to false.
|
||||
//
|
||||
// Example: given the function call
|
||||
// PART3_finish_round(2, 10, performed_attempts, continue_playing), and
|
||||
// assuming that performed_attempts has a value of 5, then the only
|
||||
// effect of the function call is that performed_attempts will be updated
|
||||
// to 6.
|
||||
//
|
||||
// Example: given the function call
|
||||
// PART3_finish_round(2, 10, performed_attempts, continue_playing), and
|
||||
// assuming that performed_attempts has a value of 9, then
|
||||
// performed_attempts will be incremented to 10, a you-lost message will
|
||||
// be output and continue_playing will be set to false.
|
||||
void PART3_finish_round(int number_to_guess, int max_attempts, int& attempts, bool& play);
|
||||
|
||||
#endif
|
48
Vorkurs/Projekt_1/Task_2/main.cpp
Normal file
48
Vorkurs/Projekt_1/Task_2/main.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
#include <iostream>
|
||||
#include "guess_a_number.h"
|
||||
|
||||
int main() {
|
||||
int number_to_guess; // The number to guess
|
||||
int max_attempts; // The guessed number
|
||||
|
||||
std::cout << "Number to guess: ";
|
||||
number_to_guess = choose_a_number(10); // Randomly choose a number from the interval [1, 10]
|
||||
|
||||
std::cout << "Number of attempts: ";
|
||||
std::cin >> max_attempts;
|
||||
|
||||
// Make sure that the player has at least one attempt
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user