-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWord.cs
98 lines (82 loc) · 2.91 KB
/
Word.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
using System;
namespace HangmanGame
{
// Manages the active word of the game.
public class Word
{
// Character to be displayed in place of the unguessed letters.
private const char WildCard = '*';
// Object that manages the words of the game.
private Words words = new Words();
// Characters of the complete word.
private char[] completeWordChars;
// Partial word characters, which may contain wildcards.
private char[] partialWordChars;
// Full word.
public string CompleteWord { get; private set; }
// Partial word.
public string PartialWord
{
get
{
// Creates a string using the character array.
return new string(partialWordChars);
}
}
// Indicates whether the game has ended (if the word was completely guessed).
public bool Finished
{
get
{
// Checks whether the partial word equals the complete word
return PartialWord == CompleteWord;
}
}
// Constructor.
public Word()
{
// When the object is built, it searches for the first word.
Next();
}
// Get a new word.
public void Next()
{
// Draw a new word.
this.CompleteWord = words.Pick();
// Converts the word into a char[].
this.completeWordChars = this.CompleteWord.ToCharArray();
//Instantiates the partial word character array and assigns the wildcard to all positions.
this.partialWordChars = new char[completeWordChars.Length];
for (int i = 0; i < partialWordChars.Length; i++)
{
if (completeWordChars[i] != ' ')
{
partialWordChars[i] = WildCard;
}
else
{
partialWordChars[i] = ' ';
}
}
}
// Try to guess a letter.
public bool Guess(char letter)
{
bool found = false;
// Converts the character to uppercase, as the words are all uppercase.
letter = Char.ToUpper(letter);
// Search for the letter in the word.
for (int i = 0; i < completeWordChars.Length; i++)
{
if (completeWordChars[i] == letter)
{
// If the letter is found, the partial word character array has the
// corresponding wildcard replaced by the letter.
partialWordChars[i] = letter;
found = true;
}
}
return found;
}
}
}