-
Notifications
You must be signed in to change notification settings - Fork 0
/
guessing_game.cs
100 lines (84 loc) · 3.15 KB
/
guessing_game.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
// Default C# library.
using System;
// Defines the class Program.
// This allows me to use this whole class in a different file.
// It also shows that there can be a whole lot of layers of
// abstraction.
class Program
{
// Defines the Main function.
static void Main()
{
// This is where the game will be played.
// This makes a new game.
var game = new NumberGuessingGame();
// Calls the play function to begin!
game.Play();
}
}
// Defines the class NumberGuessingGame.
class NumberGuessingGame
{
// Creates the Random functionality.
// This is done by utilizing a library that another programmer
// has created to easily generate random numbers.
private readonly Random _random = new Random();
// Creates the private int.
private readonly int _numberToGuess;
// Creates the private int.
private int _remainingGuesses;
// Creates a new instance of NumberGuessingGame class to redefine
// the _remainingGuesses and _numberToGuess.
// You can always change these numbers to make it more difficult or easier
// depending on the person playing.
public NumberGuessingGame()
{
// Defines the _numberToGuess variable by setting a number between
// 1 or 100. These can be changed to make the game easier or harder.
_numberToGuess = _random.Next(1, 101);
// This defines the _remainingGuesses variable by setting how many
// guesses you have to try and guess the number!
_remainingGuesses = 5;
}
// Defines the Play function.
public void Play()
{
// Makes a loop to tell the player how many remaining
// guesses they have.
while (_remainingGuesses > 0)
{
// Updates the user on how many guesses they have remaining,
// each time the program loops.
Console.WriteLine($"Guess a number between 1 and 100 ({_remainingGuesses} guesses remaining):");
var guess = int.Parse(Console.ReadLine());
// If the user guesses the numnber...
if (guess == _numberToGuess)
{
// Prints the following line:
Console.WriteLine("Congratulations, you guessed the number!");
return;
}
// Subrtacts the _remainingGuesses by one each loop.
_remainingGuesses--;
// If _remaingGuesses is 0...
if (_remainingGuesses == 0)
{
// You lose!
Console.WriteLine($"Sorry, you ran out of guesses. The number was {_numberToGuess}.");
return;
}
// If guess is less than _numberToGuess...
if (guess < _numberToGuess)
{
// Prints the following statement:
Console.WriteLine("Too low!");
}
// If guess is greater than _numberToGuess...
else
{
// Prints the following statement:
Console.WriteLine("Too high!");
}
}
}
}