Updated to new Semester
This commit is contained in:
@@ -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
|
@@ -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",
|
|
Reference in New Issue
Block a user