Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
210 lines (208 loc) · 9.2 KB
/
Copy pathProgram.cs
File metadata and controls
210 lines (208 loc) · 9.2 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System.Diagnostics;
namespace WhichNumber {
class Program {
static void Main(string[] args) {
try {
Console.Clear();
Console.Title = "Which Number?";
Banner();
switch (args.Length) {
case 0: // No arguments, open menu
Menu();
break;
case 1: // One argument
switch (args[0].ToLower()) {
case "a":
// Run guess the number with no number
GuessTheNumber(-1);
break;
case "b":
// Run binary search with default starting point
BinarySearch(100);
break;
case "c":
Help();
break;
case "/s":
// Runned as screensaver, open menu
Menu();
break;
case "/?":
Help();
break;
case "-?":
Help();
break;
case "--help":
Help();
break;
case "-h":
Help();
break;
case "/h":
Help();
break;
case "/help":
Help();
break;
default:
Console.WriteLine("Invalid first argument!");
Help();
break;
}
break;
case 2: // Two arguments
switch (args[0].ToString().ToLower()[0]) { // First argument, lower case, first character
case 'a':
// Run guess the number with the number provided in the second argument
GuessTheNumber(Int32.Parse(args[1]));
break;
case 'b':
// Run binary search starting with the number provided in the second argument
BinarySearch(Int32.Parse(args[1]));
break;
default: // like for example "-? 100"
Console.WriteLine("Invalid first argument for 2 arguments execution!");
Help();
break;
}
break;
default:
Console.WriteLine("Too many arguments!");
Help();
break;
}
} catch (Exception ex) {
Console.Write($"\nError: {ex.Message}\n");
}
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
static void Help() {
Console.Write("\nOpening project website...");
System.Diagnostics.Process.Start(new ProcessStartInfo {
FileName = "https://jgc777.github.io/WhichNumber",
UseShellExecute = true
});
}
static void Banner() {
Console.WriteLine(@"
██╗ ██████╗ ██████╗███████╗
██║██╔════╝ ██╔════╝╚════██║
██║██║ ███╗██║ ██╔╝
██ ██║██║ ██║██║ ██╔╝
╚█████╔╝╚██████╔╝╚██████╗ ██║
╚════╝ ╚═════╝ ╚═════╝ ╚═╝
");
System.Threading.Thread.Sleep(1000); // Wait 1 second
Console.Clear();
}
static void Menu() {
Console.WriteLine(@"Do you want to:
a) Guess the computer number
b) Make your computer guess your number
c) Get more info");
switch (Console.ReadKey().KeyChar.ToString().ToLower()[0]) {
// Read key, then convert to lower case
case 'a':
GuessTheNumber(-1);
break;
case 'b':
BinarySearch(100);
break;
case 'c':
Help();
break;
default:
Console.WriteLine("Invalid option!");
break;
}
}
static int BinarySearch(int start) {
bool finished = false;
int attempt = 0;
int max = -1;
int min = 0;
int reference = 100;
do {
attempt++;
Console.Clear();
Console.WriteLine("Is your number higher (+) lower (-) or equal (=) to " + reference + "?");
switch (Console.ReadKey().KeyChar) {
case '+':
min = reference + 1;
if (max == -1) reference = reference * 2; // Maximum not set, so double the reference
else reference = (max + min) / 2;
break;
case '-':
max = reference - 1;
reference = (max + min) / 2;
break;
case '=':
Console.Clear();
Console.WriteLine($"Found your number ({reference}) on {attempt} attempts!");
finished = true; // Exit loop
break;
default:
Console.WriteLine("Invalid option!");
attempt--; // Do not count this attempt
break;
}
} while (!finished);
return reference;
}
static int GuessTheNumber(int number) {
string input;
int max = 100;
int min = 0;
int attempt = 0;
int guess = 0;
Stopwatch stopwatch = new Stopwatch();
Console.Clear();
if (Math.Abs(number) != number) {
// Negative number, so generate a random number
Console.Write("Minimum number:\n>");
input = Console.ReadLine();
if (!int.TryParse(input, out min) || string.IsNullOrEmpty(input)) {
// If not a number or empty, else use the number
Console.WriteLine("Invalid/empty input. Using 0");
}
Console.Write("Maximum number:\n>");
input = Console.ReadLine();
if (!int.TryParse(input, out max) || string.IsNullOrEmpty(input)) {
// If not a number or empty, else use the number
Console.WriteLine("Invalid/empty input. Using 100");
}
Random random = new Random();
number = random.Next(min, max + 1);
}
stopwatch.Start();
Console.Clear();
do {
Console.Write("Write your guess:\n>");
input = Console.ReadLine();
if (!int.TryParse(input, out guess) || string.IsNullOrEmpty(input)) {
// If not a number or empty, else use the number
Console.WriteLine("Invalid/empty input.");
} else {
attempt++; // Count attempt
Console.Clear();
Console.WriteLine($"Attempt #{attempt}, {stopwatch.Elapsed:hh\\:mm\\:ss} elapsed"); // Show attempt and time
if (guess == number) {
stopwatch.Stop();
Console.WriteLine($"You found the number {number} in {attempt} attempts ({stopwatch.Elapsed:hh\\:mm\\:ss})!");
break; // Exit loop
} else if (guess > number) {
Console.WriteLine($"Lower than {guess}!");
} else if (guess < number) {
Console.WriteLine($"Higher than {guess}!");
} else {
Console.WriteLine("Error. Try again.");
attempt--; // Do not count this attempt
}
}
} while (true);
return attempt;
}
}
}