Newer
Older
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
using System.Text.RegularExpressions;
using System;
namespace AttemptAnConsoleApp
{
class Program
{
static void Main(string[] args)
{
Random r = new Random();
bool game = true;
Console.WriteLine("Welcome to the game, anytime you want to quit, type 'q'");
while (game)
{
int answer = r.Next(1, 9);
bool cont = true;
while (cont)
{
Console.WriteLine("Guess a number between 1 and 9");
char userInput = Console.ReadKey().KeyChar;
if (!IsValidInput(userInput))
{
continue;
}
if(userInput == 'q')
{
game = false;
break;
}
int IntUserInput = int.Parse(userInput.ToString());
if (IntUserInput == answer)
{
string message = Environment.NewLine + "That is correct!";
Console.WriteLine(message);
cont = false;
}
else
{
string message = Environment.NewLine + "That is not the number I thought of!";
Console.WriteLine(message);
Console.WriteLine("Try again!");
}
}
if (game == false)
{
Console.WriteLine();
break;
}
Console.WriteLine(Environment.NewLine + "Let's play more! Remember you can type q to exit!");
Console.WriteLine();
}
Console.WriteLine("Thanks for playing, exiting...");
}
static bool IsValidInput(char character)
{
Regex ValidInput = new Regex("([1-9]|q)");
Match Inputmatch = ValidInput.Match(character.ToString());
if (Inputmatch.Success)
{
return true;
}
Console.WriteLine();
Console.WriteLine("that is not a valid input, input a number between 1 and 9, or 'q' to quit");
return false;
}
}
}