-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
77 lines (65 loc) · 2.35 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace NumbersGame
{
internal class Program
{
static void Main(string[] args)
{
// Initialize a new instance of the Random class to generate random numbers
Random random = new Random();
int answer = random.Next(1, 20);
Console.WriteLine("Välkommen! Jag tänker på ett nummer. Kan du gissa vilket? Du får fem försök.");
// Initialize the number of attempts the player has made
int tries = 0;
// Loop until the player has had five tries
while (tries < 5)
{
int input; //Storage the input from the user and resets after every loop till tries equals 5
bool succes = int.TryParse(Console.ReadLine(), out input); // Check value is valid
if (succes)
{
//Call method ChecGuess and after validating the input return tries to main method
tries = CheckGuess(input, answer, tries);
// if input equals answer than break the while loop.
if (input== answer)
{
break;
}
}
else
{
Console.WriteLine("Fel inmatning försök igen");
}
}
// If the user guess wrong five times, display the answer
if (tries>=5)
{
Console.WriteLine($"Det nu har du gjort fem försök, svaret var {answer}");
}
}
// Method to check if the player's guess is correct or too high/low
static int CheckGuess(int input, int answer,int tries)
{
if (input == answer)
{
Console.WriteLine("Wohoo! Du klarade det!");
}
else if (input > answer)
{
Console.WriteLine("Ditt tal är för hög");
tries++;
}
else
{
Console.WriteLine("Ditt tal är för låg");
tries++;
}
// Return the updated number of attempts
return tries;
}
}
}