-
-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathObjectNotFound.razor
More file actions
167 lines (154 loc) · 4.87 KB
/
Copy pathObjectNotFound.razor
File metadata and controls
167 lines (154 loc) · 4.87 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
@using System.Security.Cryptography
<div class="m-auto text-center">
<h1 class="fs-1">404 - o((⊙﹏⊙))o</h1>
<br />
<p>I really looked hard but I couldn't find the page you are looking for.</p>
<p>Go back to <a href="/">safety</a></p>
<hr />
<h3>Play a Number Guessing Game</h3>
<p>Since you are here, why not play a number guessing game?</p>
@if (!isGameStarted)
{
<p>Select a difficulty level to start:</p>
<div class="btn-group mb-3" role="group">
<button class="btn btn-success" @onclick="() => StartGame(DifficultyLevel.Easy)">Easy</button>
<button class="btn btn-warning" @onclick="() => StartGame(DifficultyLevel.Medium)">Medium</button>
<button class="btn btn-danger" @onclick="() => StartGame(DifficultyLevel.Hard)">Hard</button>
</div>
}
else
{
<p>I'm thinking of a number between 1 and @maxNumber. Can you guess it?</p>
<p>You have <strong>@remainingGuesses</strong> guesses left.</p>
<div class="input-group mb-3 w-25 mx-auto">
<input type="number" class="form-control" @bind="userGuess" min="1" max="@maxNumber" @onkeyup="HandleKeyPress" />
<button class="btn btn-primary" @onclick="CheckGuess">Guess</button>
</div>
<p>Total guesses: <strong>@guessCount</strong></p>
@if (!string.IsNullOrEmpty(message))
{
<div class="alert @alertClass mt-3" role="alert">
@message
</div>
@if (gameDone)
{
<button class="btn btn-success mt-3" @onclick="ResetGame">Play Again</button>
}
}
}
</div>
@code {
private int targetNumber;
private int userGuess;
private int guessCount;
private int maxNumber;
private int maxGuesses;
private int remainingGuesses;
private string message = string.Empty;
private string alertClass = "";
private bool gameDone = false;
private bool isGameStarted = false;
private enum DifficultyLevel
{
Easy,
Medium,
Hard
}
private void StartGame(DifficultyLevel difficulty)
{
isGameStarted = true;
gameDone = false;
guessCount = 0;
message = string.Empty;
userGuess = 0;
switch (difficulty)
{
case DifficultyLevel.Easy:
maxNumber = 10;
maxGuesses = 3;
break;
case DifficultyLevel.Medium:
maxNumber = 100;
maxGuesses = 7;
break;
case DifficultyLevel.Hard:
maxNumber = 1000;
maxGuesses = 10;
break;
default:
throw new ArgumentOutOfRangeException(nameof(difficulty), difficulty, null);
}
remainingGuesses = maxGuesses;
targetNumber = RandomNumberGenerator.GetInt32(1, maxNumber + 1);
}
private void CheckGuess()
{
if (gameDone || !isGameStarted)
return;
if (userGuess < 1 || userGuess > maxNumber)
{
message = $"Please enter a number between 1 and {maxNumber}.";
alertClass = "alert-warning";
return;
}
guessCount++;
remainingGuesses--;
int difference = Math.Abs(userGuess - targetNumber);
double proximity = (double)difference / maxNumber;
if (userGuess == targetNumber)
{
gameDone = true;
message = "🎉 Congratulations! You guessed the number!";
alertClass = "alert-success";
}
else if (remainingGuesses == 0)
{
gameDone = true;
message = $"😞 Game over! You've run out of guesses. The number was {targetNumber}.";
alertClass = "alert-danger";
}
else
{
switch (proximity)
{
case < 0.05:
message = "🔥 Scalding Hot!";
alertClass = "alert-danger";
break;
case < 0.1:
message = "🌡️ Very Hot!";
alertClass = "alert-warning";
break;
case < 0.2:
message = "🌞 Warm.";
alertClass = "alert-info";
break;
case < 0.3:
message = "🌤️ Cool.";
alertClass = "alert-secondary";
break;
default:
message = "❄️ Cold!";
alertClass = "alert-secondary";
break;
}
}
}
private void ResetGame()
{
isGameStarted = false;
gameDone = false;
message = string.Empty;
alertClass = "";
userGuess = 0;
guessCount = 0;
remainingGuesses = 0;
}
private void HandleKeyPress(KeyboardEventArgs e)
{
if (e.Key == "Enter" && !gameDone && isGameStarted)
{
CheckGuess();
}
}
}