-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathProgram.cs
100 lines (89 loc) · 2.85 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#define SupportUndo
using DesignPatternsInCSharp.Memento;
using System;
using System.Collections.Generic;
using System.Threading;
namespace HangmanGameApp
{
/// <summary>
/// This program uses the Memento pattern
/// </summary>
class Program
{
static void Main(string[] args)
{
#if SupportUndo
// MEMENTO NOTES:
// HangmanGameWithUndo == ORIGINATOR
// This Main Program == CARETAKER
// HangmanMemento == MEMENTO
var game = new HangmanGameWithUndo();
var gameHistory = new Stack<HangmanMemento>();
gameHistory.Push(game.CreateSetPoint());
#else
var game = new HangmanGame();
#endif
while (!game.IsOver)
{
Console.Clear();
Console.SetCursorPosition(0, 0);
Console.WriteLine("Welcome to Hangman");
Console.WriteLine(game.CurrentMaskedWord);
Console.WriteLine($"Previous Guesses: {String.Join(',', game.PreviousGuesses.ToArray())}");
Console.WriteLine($"Guesses Left: {game.GuessesRemaining}");
#if SupportUndo
Console.WriteLine("Guess (a-z or '-' to undo last guess): ");
#else
Console.WriteLine("Guess (a-z): ");
#endif
var entry = char.ToUpperInvariant(Console.ReadKey().KeyChar);
#if SupportUndo
if(entry == '-')
{
if(gameHistory.Count > 1)
{
gameHistory.Pop();
game.ResumeFrom(gameHistory.Peek());
Console.WriteLine();
continue;
}
}
#endif
try
{
game.Guess(entry);
#if SupportUndo
gameHistory.Push(game.CreateSetPoint());
#endif
Console.WriteLine();
}
catch (DuplicateGuessException)
{
OutputError("You already guessed that.");
continue;
}
catch (InvalidGuessException)
{
OutputError("Sorry, invalid guess.");
continue;
}
}
if (game.Result == GameResult.Won)
{
Console.WriteLine("CONGRATS! YOU WON!");
}
if (game.Result == GameResult.Lost)
{
Console.WriteLine("SORRY, You lost this time. Try again!");
}
}
private static void OutputError(string message)
{
Console.WriteLine();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ResetColor();
Thread.Sleep(3000);
}
}
}