====== - Projekt: Hangman ======
**Slides:**
* {{ :ef_informatik:efif_jahr_a_scope_wert_referenztypen.pdf | Slides: Scope, Wert- & Referenztypen}}
* {{ :ef_informatik:efif_jahr_a_model_vs_view_hangman.pdf |Slides: Model vs. View & HangMan}}
**Ziele:**
* Programmiere das bekannte Hangman-Game als **Konsolenapp**.
* **Grösseres Programm** schreiben (ca. $300$ Zeilen Code)
* Lerne, wie man anhand des Programmierparadigmas **Model vs. View** Code sinnvoll strukturiert.
* **Scope** von Variablen verstehen.
* Unterschied **Werttypen vs. Referenztypen** verstehen.
* **Robusten Code** schreiben, der für alle Eventualitäten gewappnet ist.
**Auftrag:**
1. Erstelle ein **neues GitHub-Repo "HangMan"** und teile es mit den Lehrpersonen. Wähle beim erstellen *private* und *Visual Studio* für das GitIgnore-File. Klone das Repo auf deinen Computer und ...
1. ... erstelle darin ein neues C#-Konsolenprogramm mit Namen "HangMan". Wichtig: Wähle beim Erstellen des Projekte **"Erweitert: Keine Anweisungen der obersten Ebene verwenden"** (Engl.: etwas mit "Top-Level Statements") aus.
1. Verwende das **Template** (siehe unten) und ...
1. ... Programmiere damit das Spiel Hangman. Schaue, dass alle **Vorgaben** unten erfüllt sind.
1. **Teste** dein Spiel ausgiebig: Falsche Eingaben machen, Spiel gewinnen, verlieren, ...
1. Wenn du denkst dass du fertig bist: Gehe die **Checkliste** unten durch. Falls ein Punkt nicht erfüllt ist, verbessere diesen und hake diesen ab. Sobald alle Punkte erfüllt und abgehakt sind ...
1. ... **Arbeit abgeben:**
1. Nachricht an Lehrperson in Teams-Chat
1. inkl. Link zu GitHub-Repo
1. inkl. abgehakter und unterschriebener Checkliste
**Zeit:** Zwei EFIF-Nachmittage (6 Lektionen), Rest HA
**Vorgaben:**
* **Template** verwenden und an dieses halten.
* Code muss **robust** sein: Egal was Benutzer eingibt: Code muss damit klarkommen.
* Von Player1 eingegebenes Wort (das, welches Player2 erraten muss) muss folgende Bedingung erfüllen:
* Mind. 3 Buchstsaben
* Nur ASCII-Grossbuchstaben: 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
* also keine Umlaute (Ä,Ö,Ü) usw. (É,...), keine Leerschläge
* Bei unzulässiger Eingabe wird solange aufgefordert, neues einzugeben bis ok
* Nur GROSSBUCHSTABEN. Player1 & 2 können Kleinbuchstaben eingeben, werden dann aber in Grossbuchstaben umgewandelt.
* Erraten der Buchstaben (Player2):
* Wird bereits erratener Buchstabe erraten, zählt dies *nicht* als Fehlversuch. Player2 wird darauf hingewiesen und aufgefordert, andren Buchstaben einzugeben.
* Auch unzulässige Eingaben (Zahl, Umlaut, Sonderzeichen, ...) zählen nicht als Fehlversuche.
==== Template ====
++++Template (ohne Top-Level Statements)|
namespace HangMan; // here use name of your project
class Program
{
// No variable declarations in this area!!
static void Main(string[] args)
{
// Variable declarations allowed here
while (true) // The game repeats until finished by player 1
{
// Variable declarations allowed here
ReadSecretWord(); // Player 1: Enter the secret word to be guessed by player 2
HangTheMan(); // Screen output for a good start
while (true) // Player 2: Make your guesses
{
ReadOneChar(); // Handle input of one char.
EvaluateTheSituation(); // Game Logic goes here
HangTheMan(); // Screen output goes here
}
QuitOrRestart(); // Ask if want to quit or start new game
}
}
static void ReadSecretWord() // Modification of method declaration recommended: Add return value and parameters
// If there are rules and constraints on allowed secrets (e.g. no digits), check them in here
{
// Variable declarations allowed here
// Console.Write() etc. allowed here!
}
static void ReadOneChar() // Modification of method declaration recommended: Add return value and parameters
// If there are rules and constraints on allowed secrets (e.g. no digits), make sure the input is allowed
{
// Variable declarations allowed here
// Console.Write() etc. allowed here!
}
static void EvaluateTheSituation() // Modification of method declaration recommended: Add return value and parameters
// In here, evaluate the char entered: Is it part of the secret word?
// Calculate and return the game status (Hit or missed? Where? List and number of missed chars?...)
{
// Variable declarations allowed here
// NO Console.Write() etc. in here!
}
static void QuitOrRestart() // Modification of method declaration recommended: Add return value and parameters
// If there are rules and constraints on allowed secrets (e.g. no digits), check them in here
{
// Variable declarations allowed here
// Console.Write() etc. allowed here!
}
static void HangTheMan() // Modification of method declaration recommended: Add return value and parameters
// In here, clear the screen and redraw everything reflecting the actual game status
{
// Variable declarations allowed here
// all Console.Write() etc. go here
}
}
++++
// TEMPLATE FOR TOP-LEVEL STATEMENTS
// Variable declarations allowed here
while (true) // The game repeats until finished by player 1
{
// Variable declarations allowed here
ReadSecretWord(); // Player 1: Enter the secret word to be guessed by player 2
HangTheMan(); // Screen output for a good start
while (true) // Player 2: Make your guesses
{
ReadOneChar(); // Handle input of one char.
EvaluateTheSituation(); // Game Logic goes here
HangTheMan(); // Screen output goes here
}
QuitOrRestart(); // Ask if want to quit or start new game
}
void ReadSecretWord() // Modification of method declaration recommended: Add return value and parameters
// If there are rules and constraints on allowed secrets (e.g. no digits), check them in here
{
// Variable declarations allowed here
// Console.Write() etc. allowed here!
}
void ReadOneChar() // Modification of method declaration recommended: Add return value and parameters
// If there are rules and constraints on allowed secrets (e.g. no digits), make sure the input is allowed
{
// Variable declarations allowed here
// Console.Write() etc. allowed here!
}
void EvaluateTheSituation() // Modification of method declaration recommended: Add return value and parameters
// In here, evaluate the char entered: Is it part of the secret word?
// Calculate and return the game status (Hit or missed? Where? List and number of missed chars?...)
{
// Variable declarations allowed here
// NO Console.Write() etc. in here!
}
void QuitOrRestart() // Modification of method declaration recommended: Add return value and parameters
// If there are rules and constraints on allowed secrets (e.g. no digits), check them in here
{
// Variable declarations allowed here
// Console.Write() etc. allowed here!
}
void HangTheMan() // Modification of method declaration recommended: Add return value and parameters
// In here, clear the screen and redraw everything reflecting the actual game status
{
// Variable declarations allowed here
// all Console.Write() etc. go here
}
++++
Console.WriteLine()
, Console.ReadLine()
, Console.ReadKey()
, ... diese gehören nur in die View-Funktionen).EvaluateTheSituation()
- gibt es keinen einzigen Console-Befehl.Main()
deklariert und dann den anderen Funktionen, falls benötigt, als Argument übergeben.ReadOneChar(...)
die Anzahl Fehlversuche oder verbleibende Leben nicht übergeben werden (falls man eine solche hat), da diese irrelevant für die Funktion ist.