|
| 1 | +<div class="m-auto text-center"> |
| 2 | + <h1 class="fs-1">404 - o((⊙﹏⊙))o</h1> |
| 3 | + <br /> |
| 4 | + <p>I really looked hard but I couldn't find the page you are looking for.</p> |
| 5 | + <p>Go back to <a href="/">safety</a></p> |
| 6 | + <hr /> |
| 7 | + <h3>Play a Number Guessing Game!</h3> |
| 8 | + <p>Since you are here, why not play a number guessing game?</p> |
| 9 | + |
| 10 | + @if (!isGameStarted) |
| 11 | + { |
| 12 | + <p>Select a difficulty level to start:</p> |
| 13 | + <div class="btn-group mb-3" role="group"> |
| 14 | + <button class="btn btn-success" @onclick="() => StartGame(DifficultyLevel.Easy)">Easy</button> |
| 15 | + <button class="btn btn-warning" @onclick="() => StartGame(DifficultyLevel.Medium)">Medium</button> |
| 16 | + <button class="btn btn-danger" @onclick="() => StartGame(DifficultyLevel.Hard)">Hard</button> |
| 17 | + </div> |
| 18 | + } |
| 19 | + else |
| 20 | + { |
| 21 | + <p>I'm thinking of a number between 1 and @maxNumber. Can you guess it?</p> |
| 22 | + <div class="input-group mb-3 w-25 mx-auto"> |
| 23 | + <input type="number" class="form-control" @bind="userGuess" min="1" max="@maxNumber" @onkeyup="HandleKeyPress" /> |
| 24 | + <button class="btn btn-primary" @onclick="CheckGuess">Guess</button> |
| 25 | + </div> |
| 26 | + <p>Total guesses: <strong>@guessCount</strong></p> |
| 27 | + @if (!string.IsNullOrEmpty(message)) |
| 28 | + { |
| 29 | + <p>@message</p> |
| 30 | + @if (gameDone) |
| 31 | + { |
| 32 | + <button class="btn btn-success" @onclick="ResetGame">Play Again</button> |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | +</div> |
| 37 | + |
| 38 | +@code { |
| 39 | + private int targetNumber; |
| 40 | + private int userGuess; |
| 41 | + private int guessCount; |
| 42 | + private int maxNumber; |
| 43 | + private string message = string.Empty; |
| 44 | + private bool gameDone = false; |
| 45 | + private bool isGameStarted = false; |
| 46 | + |
| 47 | + private enum DifficultyLevel |
| 48 | + { |
| 49 | + Easy, |
| 50 | + Medium, |
| 51 | + Hard |
| 52 | + } |
| 53 | + |
| 54 | + private void StartGame(DifficultyLevel difficulty) |
| 55 | + { |
| 56 | + isGameStarted = true; |
| 57 | + gameDone = false; |
| 58 | + guessCount = 0; |
| 59 | + message = string.Empty; |
| 60 | + userGuess = 0; |
| 61 | + |
| 62 | + switch (difficulty) |
| 63 | + { |
| 64 | + case DifficultyLevel.Easy: |
| 65 | + maxNumber = 10; |
| 66 | + break; |
| 67 | + case DifficultyLevel.Medium: |
| 68 | + maxNumber = 100; |
| 69 | + break; |
| 70 | + case DifficultyLevel.Hard: |
| 71 | + maxNumber = 1000; |
| 72 | + break; |
| 73 | + } |
| 74 | + |
| 75 | + var random = new Random(); |
| 76 | + targetNumber = random.Next(1, maxNumber + 1); |
| 77 | + } |
| 78 | + |
| 79 | + private void CheckGuess() |
| 80 | + { |
| 81 | + if (gameDone || !isGameStarted) |
| 82 | + return; |
| 83 | + |
| 84 | + if (userGuess < targetNumber) |
| 85 | + { |
| 86 | + guessCount++; |
| 87 | + message = "Too low, try again!"; |
| 88 | + } |
| 89 | + else if (userGuess > targetNumber) |
| 90 | + { |
| 91 | + guessCount++; |
| 92 | + message = "Too high, try again!"; |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + guessCount++; |
| 97 | + gameDone = true; |
| 98 | + message = "🎉 Congratulations! You guessed the number!"; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + private void ResetGame() |
| 103 | + { |
| 104 | + isGameStarted = false; |
| 105 | + gameDone = false; |
| 106 | + message = string.Empty; |
| 107 | + userGuess = 0; |
| 108 | + guessCount = 0; |
| 109 | + } |
| 110 | + |
| 111 | + private void HandleKeyPress(KeyboardEventArgs e) |
| 112 | + { |
| 113 | + if (e.Key == "Enter" && !gameDone && isGameStarted) |
| 114 | + { |
| 115 | + CheckGuess(); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
0 commit comments