Reorg and orgmode test

This commit is contained in:
JirR02 2025-03-27 09:28:52 +01:00
parent 8719f4c140
commit d57e32a8b8
14 changed files with 169 additions and 72 deletions

View File

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

View File

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

View File

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

View File

@ -1,8 +1,11 @@
# ETH Informatik Projekte #+TITLE: ETH Computer Science Projects
#+AUTHOR: JirR02
* ETH Informatik Projekte
In this respository the computer science project of the ETH of D-ITET 2024 are managed. They can be used for inspiration. In this respository the computer science project of the ETH of D-ITET 2024 are managed. They can be used for inspiration.
The projects are uploaded on [Code Expert](https://expert.ethz.ch/enrolled/AS24/itet0/exercises). The projects are uploaded on [[https://expert.ethz.ch/enrolled/AS24/itet0/exercises][Code Expert]].
## DISCLAIMER!!! ## DISCLAIMER!!!

View File

@ -0,0 +1,164 @@
#+TITLE: Informatik Vorkurs Project 1
#+AUTHOR: JirR02
* Project 1: Guess A Number (Task 1)
** Table of Contents :toc:
- [[#project-1-guess-a-number-task-1][Project 1: Guess A Number (Task 1)]]
- [[#about-this-task][About this task]]
- [[#solution][Solution]]
** 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.
1. The template already contains code for picking the number to guess. By default, the number is randomly choosen from the interval $$\[1,3\]$$:
#+begin_src cpp
number_to_guess = choose_a_number(3);
#+end_src
/During development/, you might want to change this line: e.g. read the number from the keyboard:
#+begin_src cpp
std::cin >> number_to_guess;
#+end_src
or even hard-code (i.e. fix) a specific number, e.g.
#+begin_src cpp
number_to_guess = 3;
#+end_src
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
#+begin_src shell
Congratulations,␣you␣correctly␣guessed␣X!
#+end_src
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
#+begin_src shell
Sorry,␣but␣Y␣is␣wrong,␣X␣was␣the␣number␣to␣guess.
#+end_src
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:
#+begin_src shell
Number to guess: ?
Your guess: 3
Congratulations, you correctly guessed 3!
#+end_src
And a lost game:
#+begin_src shell
Number to guess: ?
Your guess: 2
Sorry, but 2 is wrong, 1 was the number to guess.
#+end_src
*** 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
Front End to import libraries and files.
#+begin_src cpp
#include <iostream>
#include "guess_a_number.h"
#+end_src
Main function where the code runs.
#+begin_src cpp
int main() {
#+end_src
Declaring Variable which will be used.
#+begin_src cpp
int number_to_guess; // The number to guess
int guess; // The guessed number
#+end_src
Front end terminal output.
#+begin_src cpp
std::cout << "Number to guess: ";
#+end_src
~choose_a_number()~ function is called to choose a number between 1 and 3
#+begin_src cpp
number_to_guess = choose_a_number(3);
#+end_src
Input front end.
#+begin_src cpp
std::cin >> guess;
#+end_src
Output the input of the user.
#+begin_src cpp
std::cout << "Your guess: ";
#+end_src
Depending on the guess, the output in the terminal is different, if the input of the user is the same with the random number, it will output ~Congratulations, you correctly guessed x!~. Otherwise it will output something else.
#+begin_src cpp
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.";
}
}
#+end_src