-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
189 lines (171 loc) · 5.09 KB
/
Program.cs
File metadata and controls
189 lines (171 loc) · 5.09 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
namespace Hangman
{
class Game
{
private int guesses;
private List<string> bank;
private List<char> letters;
private int length;
public Game(int length)
{
guesses = 0;
bank = new List<string>();
letters = new List<char>();
this.length = length;
}
public bool Play()
{
CreateBank();
while (bank.Count != 1)
{
char key = GuessLetter();
int[] indicies = AskUserLetter(key);
RemoveWords(key, indicies);
if (guesses == 6 || bank.Count == 0)
{
return false;
}
}
//ask user if we won
bool win = AskUserWord();
if (win)
{
return true;
}
else
{
return false;
}
}
//can be removed or replaced by Unity input
bool AskUserWord()
{
string word = bank.ToArray()[0];
Console.WriteLine("Is the word {0}?", word);
string answer = Console.ReadLine();
return answer.ToLower().Contains("y");
}
//can be removed or replaced by Unity input
int[] AskUserLetter(char key)
{
Console.WriteLine("Is there a {0}?: ", key);
string answer = Console.ReadLine();
List<int> indicies = new List<int>();
if (answer.ToLower().Contains("y"))
{
int index = 0;
while (index >= 0)
{
Console.WriteLine("Enter indexes where the number exists. -1 to stop: ");
index = Convert.ToInt32(Console.ReadLine());
if (index != -1)
{
indicies.Add(index);
}
else
{
break;
}
}
}
return indicies.ToArray();
}
char GuessLetter()
{
int[] frequency = new int[26];
for (int i = 0; i < frequency.Length; i++)
{
char x = (char)(96 + i);
foreach (string word in bank)
{
if (word.Contains(x.ToString()))
{
frequency[i]++;
}
}
}
char guess = FindMax(frequency);
letters.Add(guess);
return guess;
}
char FindMax(int[] a)
{
int maxIndex = 0;
int max = 0;
for (int i = 0; i < a.Length; i++) {
if (a[i] > max && !letters.Contains((char)(i+96)))
{
max = a[i];
maxIndex = i;
}
}
return (char)(maxIndex + 96);
}
void RemoveWords(char key, int[] indicies)
{
string[] bankCopy = bank.ToArray();
if (indicies.Length == 0)
{
for (int i = 0; i < bankCopy.Length; i++)
{
if (bankCopy[i].Contains(key.ToString()))
{
bank.Remove(bankCopy[i]);
}
}
}
else
{
for (int i = 0; i < bankCopy.Length; i++)
{
foreach (int index in indicies)
{
if (bankCopy[i].ToCharArray()[index] != key)
{
bank.Remove(bankCopy[i]);
}
}
}
}
}
void CreateBank()
{
string[] words = GetWordsFromFile();
foreach (string word in words)
{
if (word.Length == length)
{
bank.Add(word);
}
}
}
string[] GetWordsFromFile()
{
string filePath = @"C:\Users\mpaba\Desktop\wordBank.txt";
return File.ReadAllLines(filePath, Encoding.UTF8);
}
}
class TestDriver
{
static void Main()
{
int length;
Console.WriteLine("Enter a number between 3 and 20 [except 19]: ");
length = Convert.ToInt32(Console.ReadLine());
Game game = new Game(length);
bool result = game.Play();
if (result)
{
Console.WriteLine("I win!");
}
else
{
Console.WriteLine("I lose :(");
}
}
}
}