Converted everything to orgmode
converted everything to orgmode and added solution to the README files
This commit is contained in:
parent
3013d7ad47
commit
311cb8434c
@ -13,11 +13,11 @@ For example, in the following game player **A** won:
|
|||||||
|
|
||||||
* Your task
|
* Your task
|
||||||
|
|
||||||
The file `game.cpp` contains a partial implementation of the game. The main game loop is implemented in the function `play_game(grid)`. This function is called from `main.cpp` and gets the game grid as an argument. The game grid is already fully implemented. We describe the functionality it provides in a following section below.
|
The file =game.cpp= contains a partial implementation of the game. The main game loop is implemented in the function =play_game(grid)=. This function is called from =main.cpp= and gets the game grid as an argument. The game grid is already fully implemented. We describe the functionality it provides in a following section below.
|
||||||
|
|
||||||
Your task is extending the game implementation in file `game.cpp` such that it works the way described above. We provide some functions in this file and thus suggest a certain structure -- but you are completely free to do it your way. Add, remove or change anything you want. The only constraint is that you must implement the function play_game which takes the game grid as argument. You are of course free to change the body of this function in whatever way you like.
|
Your task is extending the game implementation in file =game.cpp= such that it works the way described above. We provide some functions in this file and thus suggest a certain structure -- but you are completely free to do it your way. Add, remove or change anything you want. The only constraint is that you must implement the function play_game which takes the game grid as argument. You are of course free to change the body of this function in whatever way you like.
|
||||||
|
|
||||||
If you do decide to change the body of the play_game function be careful to not accidentally change the format of the outputs it generates. For example: If you change the output `std::cout << "Game finished" << std::endl;` to `std::cout << "Game Finished" << std::endl;` you will not pass some test cases. The autograder is very picky when checking whether or not your outputs are correct. You can get all the points in this exercise without changing any of the output lines in the template.
|
If you do decide to change the body of the play_game function be careful to not accidentally change the format of the outputs it generates. For example: If you change the output =std::cout << "Game finished" << std::endl;= to =std::cout << "Game Finished" << std::endl;= you will not pass some test cases. The autograder is very picky when checking whether or not your outputs are correct. You can get all the points in this exercise without changing any of the output lines in the template.
|
||||||
|
|
||||||
/Note/: More Information on the the concept of the game on Code Expert.
|
/Note/: More Information on the the concept of the game on Code Expert.
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
* Task
|
* Task
|
||||||
|
|
||||||
Let `a`, `b`, `c`, and `d` be variables of type `int`.
|
Let =a=, =b=, =c=, and =d= be variables of type =int=.
|
||||||
|
|
||||||
- Which of the following character sequences are valid expressions in the sense that they are accepted by a C++ Compiler? Explain your answer.
|
- Which of the following character sequences are valid expressions in the sense that they are accepted by a C++ Compiler? Explain your answer.
|
||||||
|
|
||||||
@ -15,7 +15,7 @@ Let `a`, `b`, `c`, and `d` be variables of type `int`.
|
|||||||
|
|
||||||
Assume that all the variables have been defined and correctly initialized and that all expressions are completely independent from each other.
|
Assume that all the variables have been defined and correctly initialized and that all expressions are completely independent from each other.
|
||||||
|
|
||||||
- For each of the expressions that you have identified as valid, decide whether the entire expression is an `lvalue` or an `rvalue`, and explain your decision.
|
- For each of the expressions that you have identified as valid, decide whether the entire expression is an =lvalue= or an =rvalue=, and explain your decision.
|
||||||
|
|
||||||
- Determine the values of the expressions that you have identified as valid and explain how these values are obtained.
|
- Determine the values of the expressions that you have identified as valid and explain how these values are obtained.
|
||||||
|
|
||||||
|
@ -3,11 +3,11 @@
|
|||||||
|
|
||||||
* Task
|
* Task
|
||||||
|
|
||||||
/This task is a text-based task but mostly automatically checked. You are required to write your answers into `submission.txt` by replacing the question marks with the correct solution. Please, do not change the line formating./
|
/This task is a text-based task but mostly automatically checked. You are required to write your answers into =submission.txt= by replacing the question marks with the correct solution. Please, do not change the line formating./
|
||||||
|
|
||||||
/You can check whether your solution is correct by clicking on the test button./
|
/You can check whether your solution is correct by clicking on the test button./
|
||||||
|
|
||||||
Numbers can be provided in various formats in C++. Literals prefixed with `0b` indicate binary encoding. Assume unsigned arithmetics with sufficient numbers of bits, i.e. no overflows. Convert the following binary numbers into decimal numbers (1-4) and decimal numbers to binary (5-8):
|
Numbers can be provided in various formats in C++. Literals prefixed with =0b= indicate binary encoding. Assume unsigned arithmetics with sufficient numbers of bits, i.e. no overflows. Convert the following binary numbers into decimal numbers (1-4) and decimal numbers to binary (5-8):
|
||||||
|
|
||||||
#+begin_src txt
|
#+begin_src txt
|
||||||
# Lines starting with # are comments. Spaces is ignored.
|
# Lines starting with # are comments. Spaces is ignored.
|
||||||
|
@ -16,9 +16,9 @@ We assume that \(R_1\), \(R_2\), \(R_3\), and \(R_4\) have an integer valued res
|
|||||||
|
|
||||||
You can find formulas for computing the total resistance in this [[https://en.wikipedia.org/wiki/Resistor#Series_and_parallel_resistors][Wikipedia article]].
|
You can find formulas for computing the total resistance in this [[https://en.wikipedia.org/wiki/Resistor#Series_and_parallel_resistors][Wikipedia article]].
|
||||||
|
|
||||||
*Important:* using anything other than `int` (e.g., floating point numbers, `long`, or `double long`) is forbidden.
|
*Important:* using anything other than =int= (e.g., floating point numbers, =long=, or =double long=) is forbidden.
|
||||||
|
|
||||||
*Important:* using `if-else` and any other branches is forbidden.
|
*Important:* using =if-else= and any other branches is forbidden.
|
||||||
|
|
||||||
* Solution
|
* Solution
|
||||||
|
|
||||||
|
@ -6,14 +6,14 @@ This task is a text based task. You do not need to write any program/C++ file: t
|
|||||||
|
|
||||||
* Task
|
* Task
|
||||||
|
|
||||||
Which of the following expressions evaluate to `true`, which to `false`?
|
Which of the following expressions evaluate to =true=, which to =false=?
|
||||||
|
|
||||||
1. `3 >= 3`
|
1. =3 >= 3=
|
||||||
1. `true || false && false`
|
1. =true || false && false=
|
||||||
1. `(true || false) && false`
|
1. =(true || false) && false=
|
||||||
1. `3 > (1 < true)`
|
1. =3 > (1 < true)=
|
||||||
1. `8 > 4 > 2 > 1`
|
1. =8 > 4 > 2 > 1=
|
||||||
1. `2 < a < 4` (a is a variable of type int)
|
1. =2 < a < 4= (a is a variable of type int)
|
||||||
|
|
||||||
* Solutions
|
* Solutions
|
||||||
|
|
||||||
|
@ -3,9 +3,9 @@
|
|||||||
|
|
||||||
* Task
|
* Task
|
||||||
|
|
||||||
Translate the following natural language expressions to `C++` expressions. Assume that all the variables are non-negative integers or boolean (of value `true` or `false`).
|
Translate the following natural language expressions to =C++= expressions. Assume that all the variables are non-negative integers or boolean (of value =true= or =false=).
|
||||||
|
|
||||||
/For this task you need to write the solutions in the `solutions.cpp` file, by filling the various functions that have been defined for each subtasks./
|
/For this task you need to write the solutions in the =solutions.cpp= file, by filling the various functions that have been defined for each subtasks./
|
||||||
|
|
||||||
*Example:* \(a\) is greater than \(3\) and smaller than \(5\). \(\Longrightarrow\) *Solution:*
|
*Example:* \(a\) is greater than \(3\) and smaller than \(5\). \(\Longrightarrow\) *Solution:*
|
||||||
|
|
||||||
@ -13,7 +13,7 @@ Translate the following natural language expressions to `C++` expressions. Assum
|
|||||||
return a > 3 && a < 5;
|
return a > 3 && a < 5;
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
/Note:/ Do not confuse the bitwise logical operators (e.g., `&`) with their binary logical counterparts (`&&`). The semantics are slightly different — bitwise operators do not exhibit short circuit evaluation.
|
/Note:/ Do not confuse the bitwise logical operators (e.g., =&=) with their binary logical counterparts (=&&=). The semantics are slightly different — bitwise operators do not exhibit short circuit evaluation.
|
||||||
|
|
||||||
1. \(a\) greater than \(b\) and the difference between \(a\) and \(b\) is smaller than \(15\).
|
1. \(a\) greater than \(b\) and the difference between \(a\) and \(b\) is smaller than \(15\).
|
||||||
1. \(a\) is an even natural number greater than \(a\).
|
1. \(a\) is an even natural number greater than \(a\).
|
||||||
@ -23,9 +23,9 @@ return a > 3 && a < 5;
|
|||||||
|
|
||||||
** Input
|
** Input
|
||||||
|
|
||||||
The program expects the task number as the first input followed by the parameters to the chosen task. For example, `3 5 1 1` selects task `3` with `a = 5`, `b = 1`, and `c = 1`.
|
The program expects the task number as the first input followed by the parameters to the chosen task. For example, =3 5 1 1= selects task =3= with =a = 5=, =b = 1=, and =c = 1=.
|
||||||
|
|
||||||
Note that boolean parameters for tasks 4 and 5 are entered as true and false. For example `4 true false true` would run task `4` with `a = true`, `b = false`, and `c = true`.
|
Note that boolean parameters for tasks 4 and 5 are entered as true and false. For example =4 true false true= would run task =4= with =a = true=, =b = false=, and =c = true=.
|
||||||
|
|
||||||
* Solutions
|
* Solutions
|
||||||
|
|
||||||
@ -80,3 +80,7 @@ bool task5(bool a, int b) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
|
Made by JirR02 in Switzerland 🇨🇭
|
||||||
|
@ -20,7 +20,7 @@ If your program is asked to print the Fibonacci primes between $0$ and $14$ the
|
|||||||
Found 4 Fibonacci primes
|
Found 4 Fibonacci primes
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
*Important:* using anything other than `int` (e.g., `unsigned int`, floating point numbers, `long`, or double `long`) is forbidden.
|
*Important:* using anything other than =int= (e.g., =unsigned int=, floating point numbers, =long=, or double =long=) is forbidden.
|
||||||
|
|
||||||
* Solutions
|
* Solutions
|
||||||
|
|
||||||
|
@ -7,17 +7,17 @@
|
|||||||
|
|
||||||
* Task
|
* Task
|
||||||
|
|
||||||
Fibonacci numbers grow fast, and can thus easily exceed the value range of 32-bit `int`. Think of a general way how you can check if the result of an addition would exceed the range of a 32-bit `int` (i.e. overflow) *without actually performing the addition causing the overflow.*
|
Fibonacci numbers grow fast, and can thus easily exceed the value range of 32-bit =int=. Think of a general way how you can check if the result of an addition would exceed the range of a 32-bit =int= (i.e. overflow) *without actually performing the addition causing the overflow.*
|
||||||
|
|
||||||
Remember that we consider *signed integers.* Because only half of the numbers are positive, this leaves us with 31 bits to store the actual positive number value.
|
Remember that we consider *signed integers.* Because only half of the numbers are positive, this leaves us with 31 bits to store the actual positive number value.
|
||||||
|
|
||||||
Write a program that asks the user for an integer \(n\) and then prints the first $n$ Fibonacci numbers, each number on a new line. Use an `int` (we assume 32 bits, including the sign) to represent the current Fibonacci number. *Most importantly:* exit the print loop as soon as you detect that an overflow /would occur./
|
Write a program that asks the user for an integer \(n\) and then prints the first $n$ Fibonacci numbers, each number on a new line. Use an =int= (we assume 32 bits, including the sign) to represent the current Fibonacci number. *Most importantly:* exit the print loop as soon as you detect that an overflow /would occur./
|
||||||
|
|
||||||
Finally, again on a new line, output the count \(c\) of Fibonacci numbers previously printed, and the initial input \(n\) from the user, in the format: `c of n`.
|
Finally, again on a new line, output the count \(c\) of Fibonacci numbers previously printed, and the initial input \(n\) from the user, in the format: =c of n=.
|
||||||
|
|
||||||
** Example:
|
** Example:
|
||||||
|
|
||||||
Let's (wrongly!) assume that \(5\) cannot be represented using a 32 bit `int`. This means that \(3\) is the largest 32-bit Fibonacci number. If your program is asked to print the first \(4\) Fibonacci numbers the output should look as follows:
|
Let's (wrongly!) assume that \(5\) cannot be represented using a 32 bit =int=. This means that \(3\) is the largest 32-bit Fibonacci number. If your program is asked to print the first \(4\) Fibonacci numbers the output should look as follows:
|
||||||
|
|
||||||
#+begin_src shell
|
#+begin_src shell
|
||||||
0
|
0
|
||||||
@ -38,7 +38,7 @@ If you instead ask it to print the first 100 Fibonacci numbers the output should
|
|||||||
Printed 5 of 100 Fibonacci numbers
|
Printed 5 of 100 Fibonacci numbers
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
*Important:* using anything other than `int` (e.g., `unsigned int`, floating point numbers, `long`, or double `long`) is forbidden.
|
*Important:* using anything other than =int= (e.g., =unsigned int=, floating point numbers, =long=, or double =long=) is forbidden.
|
||||||
|
|
||||||
*Restrictions:*
|
*Restrictions:*
|
||||||
|
|
||||||
@ -56,7 +56,7 @@ Printed 5 of 100 Fibonacci numbers
|
|||||||
|
|
||||||
* Mistakes
|
* Mistakes
|
||||||
|
|
||||||
- The variable `j` goes into overflow which is not allowed!
|
- The variable =j= goes into overflow which is not allowed!
|
||||||
|
|
||||||
* Solution
|
* Solution
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
* Task
|
* Task
|
||||||
|
|
||||||
Write a program that inputs a non-negative integer `n` (but store it as `int`) and outputs the binary digits of `n` in the /correct/ order (i.e., starting with the most significant bit). Do not output the leading zeros or the sign.
|
Write a program that inputs a non-negative integer =n= (but store it as =int=) and outputs the binary digits of =n= in the /correct/ order (i.e., starting with the most significant bit). Do not output the leading zeros or the sign.
|
||||||
|
|
||||||
*Hint:* In order to find the largest integer \(k\) such that \(2^k \leq x\), you can utilize that \(k\) is the smallest integer such that \(2^k > \frac{x}{2}\). This observation is particularly useful to avoid an overflow for the expression \(2^k\) when searching for the most significant bit to represent \(x\).
|
*Hint:* In order to find the largest integer \(k\) such that \(2^k \leq x\), you can utilize that \(k\) is the smallest integer such that \(2^k > \frac{x}{2}\). This observation is particularly useful to avoid an overflow for the expression \(2^k\) when searching for the most significant bit to represent \(x\).
|
||||||
|
|
||||||
|
@ -86,3 +86,7 @@ void loop() {
|
|||||||
std::cout << x << std::endl;
|
std::cout << x << std::endl;
|
||||||
}
|
}
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
|
Made by JirR02 in Switzerland 🇨🇭
|
||||||
|
@ -19,7 +19,7 @@ A natural number \(m\) (\(m \geq 1\)).
|
|||||||
|
|
||||||
The approximation of $\pi$ given by $m$ terms, rounded to 6 significant digits. Note that 6 significant digits is the default precision of C++ for printing floating-point values. Use a double variable to store the sum.
|
The approximation of $\pi$ given by $m$ terms, rounded to 6 significant digits. Note that 6 significant digits is the default precision of C++ for printing floating-point values. Use a double variable to store the sum.
|
||||||
|
|
||||||
**Important**: the use of functions from the math library (e.g., `pow`) is prohibited.
|
**Important**: the use of functions from the math library (e.g., =pow=) is prohibited.
|
||||||
|
|
||||||
* Solution
|
* Solution
|
||||||
|
|
||||||
|
@ -5,13 +5,13 @@
|
|||||||
|
|
||||||
Consider a parabola \((\m{P})\) defined as \(y = g(x)\), with \(g(x) = 0.9 \cdot x^2 + 1.3 \cdot x - 0.7\).
|
Consider a parabola \((\m{P})\) defined as \(y = g(x)\), with \(g(x) = 0.9 \cdot x^2 + 1.3 \cdot x - 0.7\).
|
||||||
|
|
||||||
Write a program that determines if a point \((x,y)\) lies on parabola \((\m{P})\) or not. The input is provided by two decimal numbers in the sequence \(x,y\). The program must output `yes`, if the point lies on the parabola, otherwise `no`. Use datatype `double` for all variables and numbers used in the calculation of \(g(x)\).
|
Write a program that determines if a point \((x,y)\) lies on parabola \((\m{P})\) or not. The input is provided by two decimal numbers in the sequence \(x,y\). The program must output =yes=, if the point lies on the parabola, otherwise =no=. Use datatype =double= for all variables and numbers used in the calculation of \(g(x)\).
|
||||||
|
|
||||||
You will notice that a straight forward approach (comparing for equality) does not work, i.e., for some points that clearly should be on parabola $g$ such an approach returns result `no`.
|
You will notice that a straight forward approach (comparing for equality) does not work, i.e., for some points that clearly should be on parabola $g$ such an approach returns result =no=.
|
||||||
|
|
||||||
/Hint/: Look at the difference between the exact values of the function and the values that your program calculates. Change the program so that it works properly for all points the submission system uses as test input without hard-coding the points. Expect an epsilon within the range \([ 10^{-6}, 10^{-3}]\)$. Experiment yourself to find the epsilon required to pass the test cases.
|
/Hint/: Look at the difference between the exact values of the function and the values that your program calculates. Change the program so that it works properly for all points the submission system uses as test input without hard-coding the points. Expect an epsilon within the range \([ 10^{-6}, 10^{-3}]\)$. Experiment yourself to find the epsilon required to pass the test cases.
|
||||||
|
|
||||||
**Note**: Output only with `std::cout << "no" << std::endl;` or `std::cout << "yes" << std::endl;`, as the autograder will only accept output that exactly matches `yes\n` or `no\n`. For all other messages, use `std::cerr` as in:
|
**Note**: Output only with =std::cout << "no" << std::endl;= or =std::cout << "yes" << std::endl;=, as the autograder will only accept output that exactly matches =yes\n= or =no\n=. For all other messages, use =std::cerr= as in:
|
||||||
|
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
std::cerr << "This is a test message\n"
|
std::cerr << "This is a test message\n"
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
* Task
|
* Task
|
||||||
|
|
||||||
Implement the following rounding function that rounds a 64-bit floating point number (type `double`) to the nearest 32-bit integer (type `int`). You may assume that the type `double` complies with the IEEE standard 754. The function is only required to work correctly if the nearest integer is in the value range of the type `int`, otherwise, the return value of the function is undefined.
|
Implement the following rounding function that rounds a 64-bit floating point number (type =double=) to the nearest 32-bit integer (type =int=). You may assume that the type =double= complies with the IEEE standard 754. The function is only required to work correctly if the nearest integer is in the value range of the type =int=, otherwise, the return value of the function is undefined.
|
||||||
|
|
||||||
**Note: Usage of library rounding functions (standard or others) is not allowed.**
|
**Note: Usage of library rounding functions (standard or others) is not allowed.**
|
||||||
|
|
||||||
@ -14,9 +14,9 @@ Implement the following rounding function that rounds a 64-bit floating point nu
|
|||||||
int round_number(double x);
|
int round_number(double x);
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
Write your solution in `rounding.h`.
|
Write your solution in =rounding.h=.
|
||||||
|
|
||||||
/Hint/: In `C++`, when you convert a `float` or `double` to an `int`, the fractional part gets cut off (truncated). Think about how you can use the truncated number to solve the exercise.
|
/Hint/: In =C++=, when you convert a =float= or =double= to an =int=, the fractional part gets cut off (truncated). Think about how you can use the truncated number to solve the exercise.
|
||||||
|
|
||||||
* Solution
|
* Solution
|
||||||
|
|
||||||
|
@ -40,9 +40,9 @@ void print_weekday(int day, int month, int year);
|
|||||||
|
|
||||||
To complete the task, you have to provide the definition of the aforementioned functions.
|
To complete the task, you have to provide the definition of the aforementioned functions.
|
||||||
|
|
||||||
The `calendar.cpp` file contains skeletons of the functions to be implemented. The `main.cpp` file contains testing functions and the main function. In the main function, a menu is printed to select the function that has to be tested.
|
The =calendar.cpp= file contains skeletons of the functions to be implemented. The =main.cpp= file contains testing functions and the main function. In the main function, a menu is printed to select the function that has to be tested.
|
||||||
|
|
||||||
**Important** the main is not editable. Required functions must be implemented in `calendar.cpp`.
|
**Important** the main is not editable. Required functions must be implemented in =calendar.cpp=.
|
||||||
|
|
||||||
**Additional notes:**
|
**Additional notes:**
|
||||||
|
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
/This task is a text-based task. You do not need to write any program/C++ file: the answer should be written in functions.cpp./
|
/This task is a text-based task. You do not need to write any program/C++ file: the answer should be written in functions.cpp./
|
||||||
|
|
||||||
Consider the functions implemented in `functions.cpp`. For each function, add proper pre- and post-conditions.
|
Consider the functions implemented in =functions.cpp=. For each function, add proper pre- and post-conditions.
|
||||||
|
|
||||||
- If no pre-condition is needed, you can simply write "n/a".
|
- If no pre-condition is needed, you can simply write "n/a".
|
||||||
- The post-condition does not have to be a mathematical formula, e.g. it can be an informal description, but it must completely characterize the results and effects of the functions depending on the provided parameters.
|
- The post-condition does not have to be a mathematical formula, e.g. it can be an informal description, but it must completely characterize the results and effects of the functions depending on the provided parameters.
|
||||||
|
@ -7,12 +7,12 @@ Run-length encoding is a simple data compression technique that represents \(N\)
|
|||||||
|
|
||||||
[[./pictures/encode_decode.png]]
|
[[./pictures/encode_decode.png]]
|
||||||
|
|
||||||
Write a program that implements run-length encoding and decoding of a byte sequence as described above. By a byte, we mean an integer value in the range \([ 0; 255 ]\). Use the stepwise refinement method to implement the program. **Your solution must consist of at least two functions, encode and decode. Please implement them in `run_length.cpp`.**
|
Write a program that implements run-length encoding and decoding of a byte sequence as described above. By a byte, we mean an integer value in the range \([ 0; 255 ]\). Use the stepwise refinement method to implement the program. **Your solution must consist of at least two functions, encode and decode. Please implement them in =run_length.cpp=.**
|
||||||
|
|
||||||
The _input_ is structured as follows:
|
The _input_ is structured as follows:
|
||||||
|
|
||||||
1. One integer that determines whether to encode: \(0\) or decode: \(1\).
|
1. One integer that determines whether to encode: \(0\) or decode: \(1\).
|
||||||
1. Byte sequence to be encoded or decoded (of arbitrary length). If a value outside the range \([ 0; 255 ]\) (except $-1$) is entered, output `error` and stop the en- or decoding.
|
1. Byte sequence to be encoded or decoded (of arbitrary length). If a value outside the range \([ 0; 255 ]\) (except $-1$) is entered, output =error= and stop the en- or decoding.
|
||||||
1. Integer -1 signaling the end of the byte sequence. Any extra input should be ignored.
|
1. Integer -1 signaling the end of the byte sequence. Any extra input should be ignored.
|
||||||
|
|
||||||
For the example above, the inputs are:
|
For the example above, the inputs are:
|
||||||
@ -39,13 +39,13 @@ I.e., you can 'reuse' the output as the input.
|
|||||||
|
|
||||||
**Note 1):** As the encoded sequence must be a _byte_ sequence, runs of length 256 or longer need to be split into multiple runs of length 255 at most.
|
**Note 1):** As the encoded sequence must be a _byte_ sequence, runs of length 256 or longer need to be split into multiple runs of length 255 at most.
|
||||||
|
|
||||||
**Note 2):** The first input element (the integer that determines wether to encode or decode), is already consumed by the `main`, that calls either the encode or decode function.
|
**Note 2):** The first input element (the integer that determines wether to encode or decode), is already consumed by the =main=, that calls either the encode or decode function.
|
||||||
|
|
||||||
**Note 3):** Your output should not be followed by new line (i.e., do not use `std::endl` or `\n` at the end of your printout)
|
**Note 3):** Your output should not be followed by new line (i.e., do not use =std::endl= or =\n= at the end of your printout)
|
||||||
|
|
||||||
**Note 4):** The program will print your output (the result of the decoding or encoding), surrounded by asterisks. You don't have to worry about them, the autograder can safely recognize your solution
|
**Note 4):** The program will print your output (the result of the decoding or encoding), surrounded by asterisks. You don't have to worry about them, the autograder can safely recognize your solution
|
||||||
|
|
||||||
**Note 5):** Output only what is strictly required (the encoded or decoded sequence). The autograder will only accept output that exactly matches the expected result. For all other messages, use `std::cerr` as in:
|
**Note 5):** Output only what is strictly required (the encoded or decoded sequence). The autograder will only accept output that exactly matches the expected result. For all other messages, use =std::cerr= as in:
|
||||||
|
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
std::cerr << "This is a test message\n"
|
std::cerr << "This is a test message\n"
|
||||||
@ -55,10 +55,10 @@ Those will be ignored by the autograder.
|
|||||||
|
|
||||||
**Special cases**: While decoding a byte sequence two special cases can occur. These must be handled as follows:
|
**Special cases**: While decoding a byte sequence two special cases can occur. These must be handled as follows:
|
||||||
|
|
||||||
1. If a byte sequence ends in the middle of a tuple, stop printing the output of en- or decoding and output `error`.
|
1. If a byte sequence ends in the middle of a tuple, stop printing the output of en- or decoding and output =error=.
|
||||||
1. Tuples of run-length 0 are possible. For such tuples, output only the leading indicator (\(0\)/\(1\)) and the trailing \(-1\).
|
1. Tuples of run-length 0 are possible. For such tuples, output only the leading indicator (\(0\)/\(1\)) and the trailing \(-1\).
|
||||||
|
|
||||||
**Hint**: You can enter multiple numbers at once separated with a space on the console, e.g, you can copy and paste the above examples, and sequentially read them using multiple `std::cin >> _var_` calls.
|
**Hint**: You can enter multiple numbers at once separated with a space on the console, e.g, you can copy and paste the above examples, and sequentially read them using multiple =std::cin >> _var_= calls.
|
||||||
|
|
||||||
Also note that, even though the program's output and the user input are both shown in the same console, they are processed separately internally, so you do not have to worry that the two will mix: if your program outputs something to the console before reading another value, it will only read the user input and not the previous output value. See the Calculator code in the Lecture 4 handout for an example of reading input and producing output in a loop.
|
Also note that, even though the program's output and the user input are both shown in the same console, they are processed separately internally, so you do not have to worry that the two will mix: if your program outputs something to the console before reading another value, it will only read the user input and not the previous output value. See the Calculator code in the Lecture 4 handout for an example of reading input and producing output in a loop.
|
||||||
|
|
||||||
|
@ -104,51 +104,20 @@ You can also see the results for your submission on the "Enrolled Courses" tab o
|
|||||||
|
|
||||||
* Solution
|
* Solution
|
||||||
|
|
||||||
Front End to import libraries and files.
|
|
||||||
|
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "guess_a_number.h"
|
#include "guess_a_number.h"
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Main function where the code runs.
|
|
||||||
|
|
||||||
#+begin_src cpp
|
|
||||||
int main() {
|
int main() {
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Declaring Variable which will be used.
|
|
||||||
#+begin_src cpp
|
|
||||||
int number_to_guess; // The number to guess
|
int number_to_guess; // The number to guess
|
||||||
int guess; // The guessed number
|
int guess; // The guessed number
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Front end terminal output.
|
|
||||||
|
|
||||||
#+begin_src cpp
|
|
||||||
std::cout << "Number to guess: ";
|
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);
|
number_to_guess = choose_a_number(3);
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Input front end.
|
|
||||||
#+begin_src cpp
|
|
||||||
std::cin >> guess;
|
std::cin >> guess;
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Output the input of the user.
|
|
||||||
|
|
||||||
#+begin_src cpp
|
|
||||||
std::cout << "Your guess: ";
|
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) {
|
if (guess == number_to_guess) {
|
||||||
std::cout << "Congratulations, you correctly guessed " << number_to_guess << "!";
|
std::cout << "Congratulations, you correctly guessed " << number_to_guess << "!";
|
||||||
} else {
|
} else {
|
||||||
|
@ -13,11 +13,11 @@ The first project consists of two tasks: *task 1* was to reimplement the first v
|
|||||||
|
|
||||||
The program template of task 2 differs from that of task 1 in two important ways:
|
The program template of task 2 differs from that of task 1 in two important ways:
|
||||||
|
|
||||||
1. A goal of task 1 was to show you how important — and potentially cumbersome — it is to precisely fulfil output requirements and to pass the automated tests. This is no longer the focus of task 2, and you are therefore given functions such as `print_you_won(...)` that generate the expected output for you.
|
1. A goal of task 1 was to show you how important — and potentially cumbersome — it is to precisely fulfil output requirements and to pass the automated tests. This is no longer the focus of task 2, and you are therefore given functions such as =print_you_won(...)= that generate the expected output for you.
|
||||||
|
|
||||||
1. The final program consists of three major parts, and the template has three corresponding "holes" where you have to fill in code: inputting the next guess, checking the guess, and finishing the current round (we refer to each guess as one round of the game).
|
1. The final program consists of three major parts, and the template has three corresponding "holes" where you have to fill in code: inputting the next guess, checking the guess, and finishing the current round (we refer to each guess as one round of the game).
|
||||||
|
|
||||||
To make it easier for you to develop your solution step by step, you are given a "master implementation" for these steps, e.g `PART1_read_next_guess(...)`, which you must replace with your own code. Use the following workflow:
|
To make it easier for you to develop your solution step by step, you are given a "master implementation" for these steps, e.g =PART1_read_next_guess(...)=, which you must replace with your own code. Use the following workflow:
|
||||||
|
|
||||||
1. Run the tests: everything should be fine (thanks to the master implementations)
|
1. Run the tests: everything should be fine (thanks to the master implementations)
|
||||||
|
|
||||||
@ -31,7 +31,7 @@ To make it easier for you to develop your solution step by step, you are given a
|
|||||||
|
|
||||||
* Your task
|
* Your task
|
||||||
|
|
||||||
As mentioned above, you can develop the final solution step by step, by iteratively replacing each call to a `STEP...` master implementation by your own code.
|
As mentioned above, you can develop the final solution step by step, by iteratively replacing each call to a =STEP...= master implementation by your own code.
|
||||||
|
|
||||||
** Step 1: input the next guess
|
** Step 1: input the next guess
|
||||||
|
|
||||||
@ -59,7 +59,7 @@ As mentioned above, you can develop the final solution step by step, by iterativ
|
|||||||
PART2_handle_guess(guess, number_to_guess, play);
|
PART2_handle_guess(guess, number_to_guess, play);
|
||||||
#+end_src
|
#+end_src
|
||||||
|
|
||||||
1. Write code that compares the user-made guess with the number to guess: if the two are equal, call `print_you_won(guess)` to generate a you-won message, and set variable `play` to `false` to end the game. Otherwise, call `print_wrong_guess(guess)` to generate a wrong-guess message (and let the game continue).
|
1. Write code that compares the user-made guess with the number to guess: if the two are equal, call =print_you_won(guess)= to generate a you-won message, and set variable =play= to =false= to end the game. Otherwise, call =print_wrong_guess(guess)= to generate a wrong-guess message (and let the game continue).
|
||||||
|
|
||||||
** Step 3: handle the guess the user made
|
** Step 3: handle the guess the user made
|
||||||
|
|
||||||
@ -102,20 +102,15 @@ You lost after 2 attempt(s) :-( The number to guess was 2.
|
|||||||
|
|
||||||
* Submitting your solution
|
* Submitting your solution
|
||||||
|
|
||||||
*Important*: you /must/ replace all three master implementations (calls to functions `PART1/2/3`) with your own code! Your submission /will not be accepted/ if it still uses the master implementations, regardless of how many tests pass when /you/ run them.
|
*Important*: you /must/ replace all three master implementations (calls to functions =PART1/2/3=) with your own code! Your submission /will not be accepted/ if it still uses the master implementations, regardless of how many tests pass when /you/ run them.
|
||||||
|
|
||||||
-----
|
-----
|
||||||
* Solutions
|
* Solutions
|
||||||
|
|
||||||
Fronted to import libraries and the code from Task 1
|
|
||||||
#+begin_src cpp
|
#+begin_src cpp
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "guess_a_number.h"
|
#include "guess_a_number.h"
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Main funtion where the magic happens. We define two variables: `number_to_guess` and `max_attempts` and we assign them.
|
|
||||||
|
|
||||||
#+begin_src cpp
|
|
||||||
int main() {
|
int main() {
|
||||||
int number_to_guess;
|
int number_to_guess;
|
||||||
int max_attempts;
|
int max_attempts;
|
||||||
@ -124,20 +119,12 @@ int main() {
|
|||||||
number_to_guess = choose_a_number(10);
|
number_to_guess = choose_a_number(10);
|
||||||
|
|
||||||
std::cout << "Number of attempts: ";
|
std::cout << "Number of attempts: ";
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Afterwards we make sure that the player has at least one attempts and create a variable `attempts` to count the attempts the player had.
|
|
||||||
|
|
||||||
#+begin_src cpp
|
|
||||||
if (max_attempts < 1) max_attempts = 1;
|
if (max_attempts < 1) max_attempts = 1;
|
||||||
|
|
||||||
int attempts = 0; // Attempts made so far
|
int attempts = 0; // Attempts made so far
|
||||||
bool play = true; // false once the game is over
|
bool play = true; // false once the game is over
|
||||||
#+end_src
|
|
||||||
|
|
||||||
Now in a while loop, we solve `PART 1` to `3` commenting out the master functions.
|
|
||||||
|
|
||||||
#+begin_src cpp
|
|
||||||
while (play) {
|
while (play) {
|
||||||
print_attempts_left(max_attempts - attempts);
|
print_attempts_left(max_attempts - attempts);
|
||||||
|
|
||||||
|
@ -1,50 +0,0 @@
|
|||||||
# Projekt 2: Hangman
|
|
||||||
|
|
||||||
## Layers
|
|
||||||
|
|
||||||
### Start (Optional)
|
|
||||||
|
|
||||||
Beim Starten des Spiels soll ein Welcome screen erscheinen mit den Optionen, das Spiel zu starten und das Spiel zu beenden. Damit der Welcome screen gut aussieht soll es Terminal Art beinhalten. Nach dem Start Vorgang wird der Anzahl der Spieler gefragt.
|
|
||||||
Daraus entstehen 2 Szenarien:
|
|
||||||
|
|
||||||
1. Falls es im Einzelspieler Modus ist, wird ein Wort aus der Liste ausgesucht.
|
|
||||||
1. Falls im Mehrspieler Modus, darf der andere Spieler ein Wort zum Raten auswählen.
|
|
||||||
|
|
||||||
- [ ] Start input
|
|
||||||
- [ ] End input
|
|
||||||
- [ ] Invalid Input
|
|
||||||
- [ ] Single or Multiplayer
|
|
||||||
- [ ] Terminal Art
|
|
||||||
|
|
||||||
### Game
|
|
||||||
|
|
||||||
Das zu ratende Wort wird verdeckt im Terminal gezeigt. Es wird dann ein Input als Buchstabe verlangt. Wenn der Input zu lang oder ein invalid character ist, wird der Spieler nochmals dazu aufgefordert, ein Buchstabe einzugeben.
|
|
||||||
Daraus entstehen 2 Szenarien:
|
|
||||||
|
|
||||||
1. Ist der Buchstabe in der Zahl enthalten, wird der Buchstabe aufgedeckt und ein positiver Satz erscheint im Terminal.
|
|
||||||
1. Ist der Buchstabe falsch, so wird ein Leben abgezogen und ein negativer Satz wird ausgespuckt.
|
|
||||||
|
|
||||||
Während des ganzen Spiels wird der Terminal Art aktualisiert.
|
|
||||||
|
|
||||||
- [ ] Wort verdeckt im Terminal anzeigen
|
|
||||||
- [ ] Input von einem Buchstaben verlangen
|
|
||||||
- [ ] Input kontrollieren
|
|
||||||
- [ ] Buchstabe kontrollieren
|
|
||||||
- [ ] Positiver Satz
|
|
||||||
- [ ] Buchstabe aufdecken
|
|
||||||
- [ ] Negativer Satz
|
|
||||||
- [ ] (Optional) Terminal Art
|
|
||||||
|
|
||||||
### End
|
|
||||||
|
|
||||||
Es entstehen daraus zwei Endszenarien:
|
|
||||||
|
|
||||||
1. Wurden alle Buchstaben eraten, so wird ein Gewinner Satz ausgesprochen und gefragt ob das Spiel neugestartet werden soll.
|
|
||||||
1. Wurden alle Versuche verbraucht, so wird ein verlierer Satz ausgesprochen, das Wort aufgelöst und gefragt, ob das Spiel neugestartet werden soll.
|
|
||||||
|
|
||||||
Falls das Programm geschlossen wird, wird ein Abschiedssatz gezeigt.
|
|
||||||
|
|
||||||
- [ ] Gewinner Satz
|
|
||||||
- [ ] Verlierer Satz
|
|
||||||
- [ ] (Optional) Fragen für eine neue Runde
|
|
||||||
- [ ] Abschiedssatz
|
|
258
Vorkurs/Projekt_2/README.org
Normal file
258
Vorkurs/Projekt_2/README.org
Normal file
@ -0,0 +1,258 @@
|
|||||||
|
#+TITLE: Project 2: Hangman
|
||||||
|
#+AUTHOR: JirR02
|
||||||
|
|
||||||
|
* Project Overview
|
||||||
|
|
||||||
|
The goal of this project is to implement a version of the popular guessing game known as Hangman (German: Galgenmännchen). The rules of the game are simple: the first player picks a word which the second player has to guess character by character — with a limited amount of guesses.
|
||||||
|
|
||||||
|
When played on paper, a snapshot of the game typically looks as follows:
|
||||||
|
|
||||||
|
[[]]
|
||||||
|
|
||||||
|
or your version, we focus on the essence of the game:
|
||||||
|
|
||||||
|
1. Choosing a word and initialising the game
|
||||||
|
1. Repeatedly guessing characters, and uncovering them if they occur in the chosen word
|
||||||
|
1. Eventually announcing that the player won ... or lost
|
||||||
|
|
||||||
|
** A primer on strings and chars
|
||||||
|
|
||||||
|
In =C++= (and in most other programming languages), sequences of characters, such as the word the player needs to guess, can be represented as strings. Strings will be introduced properly in your later, regular computer science course; for now, you only need to know the following about strings in order to implement Hangman:
|
||||||
|
|
||||||
|
1. Strings and operations thereon are provided by the *string* library:
|
||||||
|
|
||||||
|
#+begin_src cpp
|
||||||
|
#include <string>
|
||||||
|
using std::string;
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
The first line allows us to use the =c++= *string* library, the second line allows us to directly use =string= as a data type, instead of having to write =std::string= everywhere.
|
||||||
|
|
||||||
|
1. A variable named 'word' of type string can be declared as follows:
|
||||||
|
|
||||||
|
#+begin_src cpp
|
||||||
|
string word;
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
1. String-typed variables can be set as follows:
|
||||||
|
|
||||||
|
- By using string literals, which are enclosed in quotation marks:
|
||||||
|
|
||||||
|
#+begin_src cpp
|
||||||
|
word = "potato";
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
- By reading a string from the keyboard:
|
||||||
|
|
||||||
|
#+begin_src cpp
|
||||||
|
std::cin >> word;
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
- By assigning it another string
|
||||||
|
|
||||||
|
#+begin_src cpp
|
||||||
|
word = chooseWord();
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
The function chooseWord() returns a string value which is then assigned to the variable word.
|
||||||
|
|
||||||
|
1. The length (type unsigned int) of a string can be determined by using the length function, which returns the number of characters in the string:
|
||||||
|
|
||||||
|
|
||||||
|
#+begin_src cpp
|
||||||
|
string word = "hangman";
|
||||||
|
std::cout << word.length(); // outputs: 7
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
1. The nth character (type char) of a string can be accessed using the at function:
|
||||||
|
|
||||||
|
#+begin_src cpp
|
||||||
|
string word = "hangman";
|
||||||
|
std::cout << word.at(0); // Output: h (the first character)
|
||||||
|
std::cout << word.at(6); // Output: n (the last character)
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
Note that the first character is at position 0 and the last character at position length() - 1!
|
||||||
|
|
||||||
|
The nth character can also be replaced by using the at function, e.g.
|
||||||
|
|
||||||
|
#+begin_src cpp
|
||||||
|
string workingCopy = "_______";
|
||||||
|
workingCopy.at(3) = 'n';
|
||||||
|
std::cout << workingCopy; // Output: __n____
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
where '_' denotes a single character (of type char).
|
||||||
|
|
||||||
|
1. Similar to whole strings, you can also declare variables for single characters (type char) and read them from the keyboard:
|
||||||
|
|
||||||
|
#+begin_src cpp
|
||||||
|
char guess;
|
||||||
|
std::cin >> guess;
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
** Stepwise development
|
||||||
|
|
||||||
|
The program template that you start with has been developed using the /stepwise refinement/ technique, and just like for project 1, task 2, contains master implementations (functions =PART1=... etc.) that you must replace with your own code.
|
||||||
|
|
||||||
|
We also provide you with output-generating functions (e.g. =printYouWon()=) that make it easier for you to please the tests (and thus the autograder).
|
||||||
|
|
||||||
|
As before, we suggest that you develop your program step-by-step, and run or test it often. I.e. implement a single part, such as reading the next guess from the keyboard or updating the working copy, and then run and test the program to see if it (still) works as intended.
|
||||||
|
|
||||||
|
** Implementing the game
|
||||||
|
|
||||||
|
Your job is to implement three major parts of the actual algorithm to play the game. As before, each part can be implemented separately.
|
||||||
|
|
||||||
|
The following variables are already declared in the template given to you:
|
||||||
|
|
||||||
|
- =string= word: This variable contains the word that must be guessed
|
||||||
|
|
||||||
|
- =string= workingCopy: This variable is initialized with a string that has the same size as the string word. Each character in this string is initialized with an underscore (_).
|
||||||
|
|
||||||
|
- =bool= done: If this variable is set to true the program will not play another round of the game (both in the case of winning and losing).
|
||||||
|
|
||||||
|
- =int= wrongGuesses: This variable is initialized with 0 and must be incremented by one every time a guessed character doesn't occur in the word to guess.
|
||||||
|
|
||||||
|
- =int= maxWrongGuesses: This variable is initialized with constant 6. Don't change it.
|
||||||
|
|
||||||
|
- =bool= found: This variable indicates whether (true) or not (false) the guessed character occurs in the word to guess.
|
||||||
|
|
||||||
|
*Note:* Each of the following parts is already implemented in a master implementation function (=PART=...), which you need to replace with your own code. The corresponding places in the code are marked by TODO comments.
|
||||||
|
|
||||||
|
*** Part 1
|
||||||
|
|
||||||
|
This part is about reading the next guess from the keyboard.
|
||||||
|
|
||||||
|
Comment out the call to =PART1_readCharacter= and replace it by an implementation that performs the following steps:
|
||||||
|
|
||||||
|
1. Output the text " =Your guess:= "
|
||||||
|
1. Read a single character from the keyboard into variable guess.
|
||||||
|
|
||||||
|
You already know how to output text; see the string primer above (point 6) for how to input a single character.
|
||||||
|
|
||||||
|
*** Part 2
|
||||||
|
|
||||||
|
This part involves checking if the guessed character occurs in the word, and updating the =workingCopy= accordingly: by replacing the underlines at the respective position(s) by the guessed character. Variable found indicates if the guessed character was found in the word.
|
||||||
|
|
||||||
|
Comment out the call to =PART2_updateWorkingCopy= and replace it by an implementation that performs the requested operation.
|
||||||
|
|
||||||
|
*Example (found):* If variable word is set to "success", variable =workingCopy= still contains the initial underlines ("=_______=") and the current guess is '=c=', then two actions must be performed:
|
||||||
|
|
||||||
|
1. Variable found must be set to =true=, because '=c=' occurs in the word "success"
|
||||||
|
1. All occurrences of the letter '=c=' must be uncovered in the working copy, thus variable =workingCopy= must be changed to have the content "=__cc___="
|
||||||
|
|
||||||
|
*Example (not found):* If variable word is set to "success", variable =workingCopy= still contains the initial underlines ("=_______=") and the current guess is '=x=', then the following is important:
|
||||||
|
|
||||||
|
1. Variable found must remain =false= (because '=x=' does not occur in "success"
|
||||||
|
1. Variable =workingCopy= must not be changed.
|
||||||
|
|
||||||
|
*** Part 3
|
||||||
|
|
||||||
|
This part is about determining if the game continues, or if it is already won or lost.
|
||||||
|
|
||||||
|
Comment out the call to =PART3_updateGameState= and replace it by an equivalent implementation of your own, as described next.
|
||||||
|
|
||||||
|
If the player made a correct guess and we uncovered at least one further character in the working copy (in which case variable found has value =true=), and if variable =workingCopy= now equals variable word, then the game has been won. Two things must happen in this case:
|
||||||
|
|
||||||
|
1. Set variable done to true, to indicate that the game must not continue.
|
||||||
|
1. Call =printYouWon(word)=, which will output the result of the game (in a format required by the autograder).
|
||||||
|
|
||||||
|
Otherwise, if the player guessed incorrectly (in which case variable found is =false=), variable =wrongGuesses= must be incremented by one. If this variable is afterwards equal to =maxWrongGuesses= then the game is lost, in which case the following two things must happen:
|
||||||
|
|
||||||
|
1. Set variable done to =true=, to indicate that the game must not continue.
|
||||||
|
1. Call =printYouLost(word)=, which will output the result of the game (in a format required by the autograder).
|
||||||
|
|
||||||
|
In all other situations, just make sure that variable done remains =false=. This ensures that the game continues.
|
||||||
|
|
||||||
|
** Submitting your solution
|
||||||
|
|
||||||
|
*Important:* you must replace all three master implementations (calls to functions =PART1/2/3=) with your own code! Your submission /will not be accepted/ if it still uses the master implementations, regardless of how many tests pass when you run them.
|
||||||
|
|
||||||
|
To successfully complete this project, your solution needs to pass 12 out of 17 tests, i.e. you don't need to score 100%.
|
||||||
|
|
||||||
|
** Hints
|
||||||
|
|
||||||
|
- Checking for equality of strings can be done using the ==== operator, the same way you would check equality of numbers or booleans.
|
||||||
|
- Pimp your implementation by calling =showHangman(i)= to draw the gallows after =i= incorrect guesses (for i between 0 and 6)
|
||||||
|
- You don't need to account for German umlauts, French accents etc. (such as ö or é)
|
||||||
|
- You do not need to handle duplicated guesses in any special way. E.g. if the player guesses X twice, then either nothing happens the second time around (if X occurs in the word) or the player loses a point each time (if X does not occur in the word).
|
||||||
|
- If you're interested (and brave enough), you can find more information about strings at https://en.cppreference.com/w/cpp/string
|
||||||
|
|
||||||
|
|
||||||
|
* Solution
|
||||||
|
|
||||||
|
#+begin_src cpp
|
||||||
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
#include "hangman.h"
|
||||||
|
|
||||||
|
using std::string;
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Word the player needs to guess (randomly selected)
|
||||||
|
string word = chooseWord();
|
||||||
|
//string word = "success";
|
||||||
|
|
||||||
|
// Initialise the "uncovered" word that is shown to the player: the uncovered
|
||||||
|
// word is obtained by replacing each letter from the original word (variable
|
||||||
|
// word) with an underscore (i.e. _).
|
||||||
|
string workingCopy = createWorkingCopy(word);
|
||||||
|
|
||||||
|
// This variable indicates whether or not the game is over
|
||||||
|
bool done = false;
|
||||||
|
|
||||||
|
int wrongGuesses = 0; // Number of wrong guesses
|
||||||
|
int maxWrongGuesses = 6; // Maximum number of wrong guesses (don't change)
|
||||||
|
|
||||||
|
// Draw the empty gallow
|
||||||
|
showHangman(0);
|
||||||
|
|
||||||
|
// Game loop (each iteration is a round of the game)
|
||||||
|
while (!done) {
|
||||||
|
printGameState(maxWrongGuesses, wrongGuesses);
|
||||||
|
printWorkingCopy(workingCopy);
|
||||||
|
|
||||||
|
|
||||||
|
/** Part 1: input next guess **********************************************/
|
||||||
|
char guess = '\0';
|
||||||
|
// TODO: replace the following line with your implementation
|
||||||
|
//PART1_readCharacter(guess);
|
||||||
|
std::cout << "Your guess: ";
|
||||||
|
std::cin >> guess;
|
||||||
|
|
||||||
|
|
||||||
|
/** Part 2: update working copy *******************************************/
|
||||||
|
bool found = false;
|
||||||
|
// TODO: replace the following line with your implementation
|
||||||
|
//PART2_updateWorkingCopy(word, guess, workingCopy, found);
|
||||||
|
for (int i = 0; i != word.length(); i++) {
|
||||||
|
if (guess == word.at(i)) {
|
||||||
|
found = true;
|
||||||
|
workingCopy.at(i) = guess;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/** Part 3: update game state *********************************************/
|
||||||
|
// TODO: replace the following line with your implementation
|
||||||
|
//PART3_updateGameState(word, workingCopy, found, maxWrongGuesses, done, wrongGuesses);
|
||||||
|
if (workingCopy == word) {
|
||||||
|
done = true;
|
||||||
|
printYouWon(word);
|
||||||
|
} else if (found == false) {
|
||||||
|
wrongGuesses += 1;
|
||||||
|
}
|
||||||
|
if (wrongGuesses == maxWrongGuesses) {
|
||||||
|
done = true;
|
||||||
|
printYouLost(word);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
#+end_src
|
||||||
|
|
||||||
|
-----
|
||||||
|
|
||||||
|
Made by JirR02 in Switzerland 🇨🇭
|
@ -1,69 +0,0 @@
|
|||||||
#include "hangman.h"
|
|
||||||
#include "termcolor.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 string action = std::getenv("ACTION");
|
|
||||||
const string words[] = {
|
|
||||||
#include "words.csv"
|
|
||||||
"sentinel"
|
|
||||||
};
|
|
||||||
|
|
||||||
string color(string c) {
|
|
||||||
return action == "run" ? c : "";
|
|
||||||
}
|
|
||||||
|
|
||||||
string chooseWord() {
|
|
||||||
string word;
|
|
||||||
if (action == "test" || action == "submit") {
|
|
||||||
std::cin >> word;
|
|
||||||
} else {
|
|
||||||
int i = rand() % (sizeof(words)/sizeof(*words) - 1); // don't take the last word (which is 'sentinel')
|
|
||||||
word = words[i];
|
|
||||||
std::cout << "A random (english) word with " << word.length() << " characters has been chosen." << std::endl;
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
return word;
|
|
||||||
}
|
|
||||||
|
|
||||||
string createWorkingCopy(string word){
|
|
||||||
string result = word;
|
|
||||||
for (unsigned int i = 0; i < result.length(); ++i) {
|
|
||||||
result.at(i) = '_';
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
void showHangman(int wrongGuesses) {
|
|
||||||
std::cout
|
|
||||||
<< "<cx:html>\n"
|
|
||||||
// << "<p>Attempts left: " << maxWrongGuesses - wrongGuesses << "</p>\n"
|
|
||||||
<< "<img alt='' src='https://lec.inf.ethz.ch/mavt/et/2019/img/hangman/hang_" << wrongGuesses + 1 << ".gif'/>\n"
|
|
||||||
<< "</cx:html>" << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void printGameState(int maxWrongGuesses, int wrongGuesses){
|
|
||||||
std::cout << color(gray) << "\nAttempts left: " << (maxWrongGuesses - wrongGuesses) << color(reset) << "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
void printWorkingCopy(string workingCopy){
|
|
||||||
std::cout << color(blue) << "[ " ;
|
|
||||||
for (unsigned int i = 0; i < workingCopy.length(); ++i) {
|
|
||||||
std::cout << workingCopy.at(i) << " ";
|
|
||||||
}
|
|
||||||
std::cout << "]\n" << color(reset);
|
|
||||||
}
|
|
||||||
|
|
||||||
void printYouLost(string word){
|
|
||||||
std::cout << "The word was: " << color(white) << word << color(red) << "\nYou lost!\n" << color(reset);
|
|
||||||
}
|
|
||||||
|
|
||||||
void printYouWon(string word){
|
|
||||||
std::cout << "\n";
|
|
||||||
printWorkingCopy(word);
|
|
||||||
std::cout << color(green) << "You won!\n" << color(reset);
|
|
||||||
}
|
|
@ -1,97 +0,0 @@
|
|||||||
#ifndef HANGMAN_H
|
|
||||||
#define HANGMAN_H
|
|
||||||
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
using std::string;
|
|
||||||
|
|
||||||
// NOTE: You cannot change this file. It "only" contains declarations and
|
|
||||||
// short descriptions of the functionality we provided to you.
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function returns a random english word. Use this to generate a new
|
|
||||||
* word to guess. It is imperative that you use this function to get a word,
|
|
||||||
* otherwise, the auto-grader will not work properly when testing or
|
|
||||||
* submitting your project.
|
|
||||||
*/
|
|
||||||
string chooseWord();
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function creates a "working copy" based on a given word. It returns a
|
|
||||||
* string with the same amount of characters than the word, but all of them
|
|
||||||
* are initially set to "_" (underscore)
|
|
||||||
*/
|
|
||||||
string createWorkingCopy(string word);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* You may call this function to render a little hangman figure in the HTML view.
|
|
||||||
* This is completely optional, the tests don't rely on this function being
|
|
||||||
* called. As argument, the function takes the number of wrong guesses and
|
|
||||||
* selects the correct hangman picture to show.
|
|
||||||
*/
|
|
||||||
void showHangman(int wrongGuesses);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function prints the number of remaining attempts (based on the provided
|
|
||||||
* number of wrong guesses. Call this method before each attempt.
|
|
||||||
*
|
|
||||||
* Example: The call 'printGameState(2)' will output: "Attempts left: 4"
|
|
||||||
* because MAX_WRONG_GUESSES is 6, and 6 - 2 = 4
|
|
||||||
*/
|
|
||||||
void printGameState(int maxWrongGuesses, int wrongGuesses);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function prints the partly uncovered word (the working copy) in the
|
|
||||||
* desired format.
|
|
||||||
*
|
|
||||||
* Example: If workingCopy is "_xp_rt", a call to printWorkingCopy(workingCopy)
|
|
||||||
* will print "[ _ x p _ r t ]" - this is the format that is expected by the
|
|
||||||
* autograder.
|
|
||||||
*/
|
|
||||||
void printWorkingCopy(string workingCopy);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function must be called if the game was lost (that is, on the 6th
|
|
||||||
* wrong guess).
|
|
||||||
*
|
|
||||||
* Example: If the correct word was "expert", its outputs
|
|
||||||
* "The word was: expert
|
|
||||||
* You lost!"
|
|
||||||
*/
|
|
||||||
void printYouLost(string word);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This function must be called if the game was won (that is, the word was
|
|
||||||
* guessed with less than 6 wrong guesses).
|
|
||||||
*
|
|
||||||
* Example: If the correct word was "expert", its outputs
|
|
||||||
* "[ e x p e r t ]
|
|
||||||
* You won!"
|
|
||||||
*/
|
|
||||||
void printYouWon(string word);
|
|
||||||
|
|
||||||
|
|
||||||
// THE FOLLOWING FUNCTIONS CAN BE USED INTERACTIVELY TO IMPLEMENT THE INDIVIDUAL
|
|
||||||
// PARTS, BUT THEY DONT'T WORK DURING SUBMISSION
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Part 1: Ask the user to enter a character and update parameter 'guess'
|
|
||||||
*/
|
|
||||||
void PART1_readCharacter(char& guess);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Part 2: Set the guessed character in the working copy. Updates parameter
|
|
||||||
* 'workingCopy' and sets parameter 'found' to either true or false
|
|
||||||
*/
|
|
||||||
void PART2_updateWorkingCopy(string word, char guess, string& workingCopy, bool& found);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Part 3: Check if game is finished and update wrongGuesses variable.
|
|
||||||
* Print the approriate messages in the console. Updates parameters 'done' and
|
|
||||||
* 'wrongGuesses'
|
|
||||||
*/
|
|
||||||
void PART3_updateGameState(string word, string workingCopy, bool found, int maxWrongGuesses, bool& done, int& wrongGuesses);
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,69 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include <string>
|
|
||||||
#include "hangman.h"
|
|
||||||
|
|
||||||
using std::string;
|
|
||||||
|
|
||||||
int main() {
|
|
||||||
// Word the player needs to guess (randomly selected)
|
|
||||||
string word = chooseWord();
|
|
||||||
//string word = "success";
|
|
||||||
|
|
||||||
// Initialise the "uncovered" word that is shown to the player: the uncovered
|
|
||||||
// word is obtained by replacing each letter from the original word (variable
|
|
||||||
// word) with an underscore (i.e. _).
|
|
||||||
string workingCopy = createWorkingCopy(word);
|
|
||||||
|
|
||||||
// This variable indicates whether or not the game is over
|
|
||||||
bool done = false;
|
|
||||||
|
|
||||||
int wrongGuesses = 0; // Number of wrong guesses
|
|
||||||
int maxWrongGuesses = 6; // Maximum number of wrong guesses (don't change)
|
|
||||||
|
|
||||||
// Draw the empty gallow
|
|
||||||
showHangman(0);
|
|
||||||
|
|
||||||
// Game loop (each iteration is a round of the game)
|
|
||||||
while (!done) {
|
|
||||||
printGameState(maxWrongGuesses, wrongGuesses);
|
|
||||||
printWorkingCopy(workingCopy);
|
|
||||||
|
|
||||||
|
|
||||||
/** Part 1: input next guess **********************************************/
|
|
||||||
char guess = '\0';
|
|
||||||
// TODO: replace the following line with your implementation
|
|
||||||
//PART1_readCharacter(guess);
|
|
||||||
std::cout << "Your guess: ";
|
|
||||||
std::cin >> guess;
|
|
||||||
|
|
||||||
|
|
||||||
/** Part 2: update working copy *******************************************/
|
|
||||||
bool found = false;
|
|
||||||
// TODO: replace the following line with your implementation
|
|
||||||
//PART2_updateWorkingCopy(word, guess, workingCopy, found);
|
|
||||||
for (int i = 0; i != word.length(); i++) {
|
|
||||||
if (guess == word.at(i)) {
|
|
||||||
found = true;
|
|
||||||
workingCopy.at(i) = guess;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** Part 3: update game state *********************************************/
|
|
||||||
// TODO: replace the following line with your implementation
|
|
||||||
//PART3_updateGameState(word, workingCopy, found, maxWrongGuesses, done, wrongGuesses);
|
|
||||||
if (workingCopy == word) {
|
|
||||||
done = true;
|
|
||||||
printYouWon(word);
|
|
||||||
} else if (found == false) {
|
|
||||||
wrongGuesses += 1;
|
|
||||||
}
|
|
||||||
if (wrongGuesses == maxWrongGuesses) {
|
|
||||||
done = true;
|
|
||||||
printYouLost(word);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
@ -1,20 +0,0 @@
|
|||||||
#ifndef TERMCOLOR_H
|
|
||||||
#define TERMCOLOR_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 auto red = "\033[31;1m";
|
|
||||||
const auto green = "\033[32;1m";
|
|
||||||
const auto yellow = "\033[33;1m";
|
|
||||||
const auto blue = "\033[34;1m";
|
|
||||||
const auto magenta = "\033[35;1m";
|
|
||||||
const auto cyan = "\033[36;1m";
|
|
||||||
const auto gray = "\033[39;2m";
|
|
||||||
const auto white = "\033[39;1m";
|
|
||||||
const auto reset = "\033[0m";
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,100 +0,0 @@
|
|||||||
"available",
|
|
||||||
"exaggerate",
|
|
||||||
"ancestor",
|
|
||||||
"architect",
|
|
||||||
"neighborhood",
|
|
||||||
"curriculum",
|
|
||||||
"promotion",
|
|
||||||
"opera",
|
|
||||||
"frequency",
|
|
||||||
"excavation",
|
|
||||||
"guarantee",
|
|
||||||
"reflection",
|
|
||||||
"benefit",
|
|
||||||
"development",
|
|
||||||
"average",
|
|
||||||
"ghostwriter",
|
|
||||||
"unlikely",
|
|
||||||
"disturbance",
|
|
||||||
"initiative",
|
|
||||||
"hospitality",
|
|
||||||
"mastermind",
|
|
||||||
"eyebrow",
|
|
||||||
"consciousness",
|
|
||||||
"operational",
|
|
||||||
"vehicle",
|
|
||||||
"housewife",
|
|
||||||
"capital",
|
|
||||||
"execution",
|
|
||||||
"terrify",
|
|
||||||
"disagree",
|
|
||||||
"exclusive",
|
|
||||||
"equinox",
|
|
||||||
"essential",
|
|
||||||
"imperial",
|
|
||||||
"publicity",
|
|
||||||
"secretary",
|
|
||||||
"nationalist",
|
|
||||||
"attention",
|
|
||||||
"established",
|
|
||||||
"magnitude",
|
|
||||||
"orientation",
|
|
||||||
"contraction",
|
|
||||||
"intention",
|
|
||||||
"seminar",
|
|
||||||
"forecast",
|
|
||||||
"manufacturer",
|
|
||||||
"reception",
|
|
||||||
"fabricate",
|
|
||||||
"mosquito",
|
|
||||||
"cooperative",
|
|
||||||
"parachute",
|
|
||||||
"exotic",
|
|
||||||
"demonstrate",
|
|
||||||
"production",
|
|
||||||
"spontaneous",
|
|
||||||
"minimum",
|
|
||||||
"abolish",
|
|
||||||
"holiday",
|
|
||||||
"formation",
|
|
||||||
"admission",
|
|
||||||
"handicap",
|
|
||||||
"continuous",
|
|
||||||
"presentation",
|
|
||||||
"constituency",
|
|
||||||
"unique",
|
|
||||||
"violation",
|
|
||||||
"radical",
|
|
||||||
"notebook",
|
|
||||||
"custody",
|
|
||||||
"dictionary",
|
|
||||||
"comprehensive",
|
|
||||||
"dominant",
|
|
||||||
"requirement",
|
|
||||||
"opponent",
|
|
||||||
"business",
|
|
||||||
"national",
|
|
||||||
"manufacture",
|
|
||||||
"nominate",
|
|
||||||
"liberal",
|
|
||||||
"continuation",
|
|
||||||
"galaxy",
|
|
||||||
"interest",
|
|
||||||
"ignorant",
|
|
||||||
"indirect",
|
|
||||||
"illustrate",
|
|
||||||
"proportion",
|
|
||||||
"projection",
|
|
||||||
"philosophy",
|
|
||||||
"acceptable",
|
|
||||||
"aluminium",
|
|
||||||
"continental",
|
|
||||||
"potential",
|
|
||||||
"vegetarian",
|
|
||||||
"elephant",
|
|
||||||
"advantage",
|
|
||||||
"recording",
|
|
||||||
"agenda",
|
|
||||||
"electronics",
|
|
||||||
"engagement",
|
|
||||||
"lonely",
|
|
|
Loading…
x
Reference in New Issue
Block a user