-
-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add game when user navigated to 404
- Loading branch information
1 parent
5a7e04c
commit 1882a4d
Showing
3 changed files
with
123 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
src/LinkDotNet.Blog.Web/Features/Components/ObjectNotFound.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<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> | ||
<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)) | ||
{ | ||
<p>@message</p> | ||
@if (gameDone) | ||
{ | ||
<button class="btn btn-success" @onclick="ResetGame">Play Again</button> | ||
} | ||
} | ||
} | ||
</div> | ||
|
||
@code { | ||
private int targetNumber; | ||
private int userGuess; | ||
private int guessCount; | ||
private int maxNumber; | ||
private string message = string.Empty; | ||
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; | ||
break; | ||
case DifficultyLevel.Medium: | ||
maxNumber = 100; | ||
break; | ||
case DifficultyLevel.Hard: | ||
maxNumber = 1000; | ||
break; | ||
} | ||
|
||
var random = new Random(); | ||
targetNumber = random.Next(1, maxNumber + 1); | ||
Check failure on line 76 in src/LinkDotNet.Blog.Web/Features/Components/ObjectNotFound.razor
|
||
} | ||
|
||
private void CheckGuess() | ||
{ | ||
if (gameDone || !isGameStarted) | ||
return; | ||
|
||
if (userGuess < targetNumber) | ||
{ | ||
guessCount++; | ||
message = "Too low, try again!"; | ||
} | ||
else if (userGuess > targetNumber) | ||
{ | ||
guessCount++; | ||
message = "Too high, try again!"; | ||
} | ||
else | ||
{ | ||
guessCount++; | ||
gameDone = true; | ||
message = "🎉 Congratulations! You guessed the number!"; | ||
} | ||
} | ||
|
||
private void ResetGame() | ||
{ | ||
isGameStarted = false; | ||
gameDone = false; | ||
message = string.Empty; | ||
userGuess = 0; | ||
guessCount = 0; | ||
} | ||
|
||
private void HandleKeyPress(KeyboardEventArgs e) | ||
{ | ||
if (e.Key == "Enter" && !gameDone && isGameStarted) | ||
{ | ||
CheckGuess(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters