-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWords.cs
47 lines (39 loc) · 1.23 KB
/
Words.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
using System;
using System.IO;
namespace HangmanGame
{
// Manages the words that are part of the game.
class Words
{
// Name of the file where the words are. Words must be one on each line.
private const string FileName = "words.txt";
// Random number generator.
private Random random = new Random();
// Array with the words that are part of the game.
private string[] words;
// Constructor.
public Words()
{
// Reads the words.
ReadWords();
}
// Reads the words in the file.
private void ReadWords()
{
// Read the words into the array.
this.words = File.ReadAllLines(FileName);
// Converts all letters of words to uppercase.
for (int i = 0; i < words.Length; i++)
{
words[i] = words[i].ToUpper();
}
}
// Draw a new word.
public string Pick()
{
// Generates a number between 0 and the size of the words array.
int index = random.Next(words.Length);
return words[index];
}
}
}