Skip to content

Commit 155e839

Browse files
committed
Improving Hangman game error handling
1 parent 0e52d16 commit 155e839

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

HangmanGame/Program.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using DesignPatternsInCSharp.Memento;
44
using System;
55
using System.Collections.Generic;
6+
using System.Threading;
67

78
namespace HangmanGameApp
89
{
@@ -56,14 +57,27 @@ static void Main(string[] args)
5657
}
5758
}
5859
#endif
59-
game.Guess(entry);
60+
try
61+
{
62+
game.Guess(entry);
6063
#if SupportUndo
61-
gameHistory.Push(game.CreateSetPoint());
64+
gameHistory.Push(game.CreateSetPoint());
6265
#endif
63-
Console.WriteLine();
66+
Console.WriteLine();
67+
}
68+
catch (DuplicateGuessException)
69+
{
70+
OutputError("You already guessed that.");
71+
continue;
72+
}
73+
catch (InvalidGuessException)
74+
{
75+
OutputError("Sorry, invalid guess.");
76+
continue;
77+
}
6478
}
6579

66-
if(game.Result == GameResult.Won)
80+
if (game.Result == GameResult.Won)
6781
{
6882
Console.WriteLine("CONGRATS! YOU WON!");
6983
}
@@ -73,5 +87,14 @@ static void Main(string[] args)
7387
Console.WriteLine("SORRY, You lost this time. Try again!");
7488
}
7589
}
90+
91+
private static void OutputError(string message)
92+
{
93+
Console.WriteLine();
94+
Console.ForegroundColor = ConsoleColor.Red;
95+
Console.WriteLine(message);
96+
Console.ResetColor();
97+
Thread.Sleep(3000);
98+
}
7699
}
77100
}

0 commit comments

Comments
 (0)