-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathHigherLower.cs
77 lines (71 loc) · 2.16 KB
/
HigherLower.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
using System;
class HigherLower{
static void Main(string[] args){
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("<----------Higher/Lower Game---------->");
Console.WriteLine("Created by Morasiu ([email protected])");
Console.Write("Press any key to start.");
Console.ReadKey();
StartGame();
}
static void StartGame(){
int maxNumber = GetDifficultLevel();
Random rand = new Random();
//First number is included, but second is exluded so I needed to add 1.
int randomNumber = rand.Next(1, maxNumber + 1);
int number = 0;
int attempt = 0;
Console.WriteLine("\nGame Start!");
//Start guessing loop.
do {
Console.Write("Enter valid number: ");
//Try parse string from Console to int.
try {
number = int.Parse(Console.ReadLine());
} catch {
Console.WriteLine("Not a valid number.");
continue;
}
if(number == randomNumber){
attempt++;
Console.WriteLine("Hurray! You won :) Attempts: " + attempt.ToString());
} else if(number > randomNumber){
attempt++;
Console.WriteLine("Lower");
} else if (number < randomNumber){
attempt++;
Console.WriteLine("Higher");
}
} while(number != randomNumber);
//Ask if player want to play again.
PlayAgain();
}
static void PlayAgain(){
Console.Write("Do you want to play again? (y/n): ");
string decision = Console.ReadLine();
if (decision == "y")
StartGame();
else if (decision == "n"){
Console.WriteLine("Thanks for playing. :)");
} else {
Console.WriteLine("Wrong option");
PlayAgain();
}
}
static int GetDifficultLevel(){
Console.WriteLine("\n1. Easy (1-10)\n2. Medium (1-100)\n3. Hard (1-1000)\nPick difficult level: ");
int maxNumber = 0;
var difficult = Console.ReadLine();
if (difficult == "1")
maxNumber = 10;
else if (difficult == "2")
maxNumber = 100;
else if(difficult == "3")
maxNumber = 1000;
else {
Console.WriteLine("Wrong option.");
maxNumber = GetDifficultLevel();
}
return maxNumber;
}
}