-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
189 lines (153 loc) · 5.54 KB
/
Program.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
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.Collections.Generic;
namespace NumberGuesser
{
class Program
{
static void Main(string[] args)
{
greeting();
//keep track of questions
int questions = 0;
//create list of numbers
var numbers = new List<int>();
//current guess
int guess = 0;
//player answer
string answer = "";
//keep track of number of games
int numberOfGames = 0;
//keep track of average guesses
//int averageGuesses = 0;
createList(numbers);
Console.WriteLine($"Please think of a number from {numbers[0]} to {numbers.Count}.\nI will guess your number by asking you a few questions.\n");
//gamePlay();
do
{
numberOfGames++;
//Select random number from list
guess = randomGuess(numbers);
//ask if higher, lower, or correct
Console.WriteLine($"My guess is {guess}. Is your number higher, lower, or am I correct?");
top:
answer = Console.ReadLine();
//ensure response is all lower before moving on
answer.ToLower();
questions++;
if (answer == "higher")
{
removeLower(numbers, guess);
//Status
status(numbers, questions);
}
else if (answer == "lower")
{
removeHigher(numbers, guess);
//Status
status(numbers, questions);
}
else if (answer == "correct")
{
Console.WriteLine($"I correctly guessed your number in {questions} tries. Amazing!");
}
else
{
Console.WriteLine("Invalid response. Please enter higher, lower, or correct.");
goto top;
}
} while (answer != "correct");
endOfGame();
}
static void greeting()
{
Console.Write("Welcome... \nI am a number guessing Wizard. ");
}
static List<int> createList(List<int> numbers)
{
for (int i = 1; i < 1025; i++)
{
numbers.Add(i);
}
return numbers;
}
/*
gamePlay(){
do
{
numberOfGames++;
//Select random number from list
guess = randomGuess(numbers);
//ask if higher, lower, or correct
Console.WriteLine($"My guess is {guess}. Is your number higher, lower, or am I correct?");
answer = Console.ReadLine();
//ensure response is all lower before moving on
answer.ToLower();
if(answer == "higher"){
removeLower(numbers, guess);
//Status
status(numbers, questions);
}
else if (answer == "lower"){
removeHigher(numbers, guess);
//Status
status(numbers, questions);
}
else if(answer == "correct"){
questions++;
Console.WriteLine($"I correctly guessed your number in {questions} questions. Amazing!");
}
else{
Console.WriteLine("Invalid response. Please enter higher, lower, or correct.");
}
} while (answer != "correct");
}
*/
static int randomGuess(List<int> numbers)
{
//draw random number from 1-1024 using guess var
var rnd = new Random();
int numIndex = rnd.Next(numbers.Count);
return numbers[numIndex];
}
static List<int> removeHigher(List<int> numbers, int guess)
{
Console.WriteLine($"I will now remove all numbers at and above {guess}");
int range = numbers.Count - guess;
int min = guess - 1;
numbers.RemoveRange(min, range);
return numbers;
}
static List<int> removeLower(List<int> numbers, int guess)
{
Console.WriteLine($"I will now remove all numbers at and below {guess}");
numbers.RemoveRange(0, guess);
return numbers;
}
static void status(List<int> numbers, int questions)
{
Console.WriteLine(numbers.Count + " numbers remaining.");
Console.WriteLine($"I have asked {questions} questions.");
}
static void endOfGame()
{
Console.WriteLine("Thank you for playing. Would you like to play again?\n");
top:
string playAgain = Console.ReadLine();
//ensure response is all lower before moving on
playAgain.ToLower();
if (playAgain == "yes")
{
Console.WriteLine("We need to figure this out!");
}
else if (playAgain == "no")
{
Console.WriteLine("Come back soon!");
}
else
{
Console.WriteLine("Invalid response. Please enter yes or no.");
goto top;
}
}
}
}