-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPuzzle.cs
More file actions
89 lines (76 loc) · 2.27 KB
/
Puzzle.cs
File metadata and controls
89 lines (76 loc) · 2.27 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HangMan
{
class Puzzle
{
private string wordToGuess;
private List<char> wrongGuesses;
private char[] wordToDisplay;
public Puzzle()
{
wordToGuess = new PuzzleLibrary().getWordForPuzzle();
wrongGuesses = new List<char>();
wordToDisplay = Enumerable.Repeat('-', wordToGuess.Length).ToArray();
}
public override string ToString()
{
var result = new StringBuilder();
if (wrongGuesses.Count > 0)
{
result.Append("Wrong Guesses: " + new string(wrongGuesses.ToArray()));
result.Append(Environment.NewLine);
}
result.Append(new string(wordToDisplay));
return result.ToString();
}
public void checkGuessedLetter(char letter)
{
bool isMatch = false;
foreach (char l in wordToGuess)
{
if (l.Equals(letter))
{
isMatch = true;
}
}
if (isMatch)
{
changeDisplay(letter);
} else
{
handleWrongGuess(letter);
}
}
public bool checkForSolve(string guess)
{
if (wordToGuess.Equals(guess))
{
wordToDisplay = wordToGuess.ToCharArray();
return true;
} else
{
return false;
}
}
void handleWrongGuess(char letter)
{
wrongGuesses.Add(letter);
Console.WriteLine(Environment.NewLine + "Sorry, your guess was incorrect. Try again!" + Environment.NewLine);
}
void changeDisplay(char letter)
{
for (int i = 0; i < wordToGuess.Length; i++)
{
if (wordToGuess[i].Equals(letter))
{
wordToDisplay[i] = letter;
Console.WriteLine(Environment.NewLine + "You guessed correctly!" + Environment.NewLine);
}
}
}
}
}