|
4 | 4 | <p>I really looked hard but I couldn't find the page you are looking for.</p>
|
5 | 5 | <p>Go back to <a href="/">safety</a></p>
|
6 | 6 | <hr />
|
7 |
| - <h3>Play a Number Guessing Game!</h3> |
| 7 | + <h3>Play a Number Guessing Game</h3> |
8 | 8 | <p>Since you are here, why not play a number guessing game?</p>
|
9 | 9 |
|
10 | 10 | @if (!isGameStarted)
|
|
19 | 19 | else
|
20 | 20 | {
|
21 | 21 | <p>I'm thinking of a number between 1 and @maxNumber. Can you guess it?</p>
|
| 22 | + <p>You have <strong>@remainingGuesses</strong> guesses left.</p> |
22 | 23 | <div class="input-group mb-3 w-25 mx-auto">
|
23 | 24 | <input type="number" class="form-control" @bind="userGuess" min="1" max="@maxNumber" @onkeyup="HandleKeyPress" />
|
24 | 25 | <button class="btn btn-primary" @onclick="CheckGuess">Guess</button>
|
25 | 26 | </div>
|
26 | 27 | <p>Total guesses: <strong>@guessCount</strong></p>
|
27 | 28 | @if (!string.IsNullOrEmpty(message))
|
28 | 29 | {
|
29 |
| - <p>@message</p> |
| 30 | + <div class="alert @alertClass mt-3" role="alert"> |
| 31 | + @message |
| 32 | + </div> |
30 | 33 | @if (gameDone)
|
31 | 34 | {
|
32 |
| - <button class="btn btn-success" @onclick="ResetGame">Play Again</button> |
| 35 | + <button class="btn btn-success mt-3" @onclick="ResetGame">Play Again</button> |
33 | 36 | }
|
34 | 37 | }
|
35 | 38 | }
|
|
40 | 43 | private int userGuess;
|
41 | 44 | private int guessCount;
|
42 | 45 | private int maxNumber;
|
| 46 | + private int maxGuesses; |
| 47 | + private int remainingGuesses; |
43 | 48 | private string message = string.Empty;
|
| 49 | + private string alertClass = ""; |
44 | 50 | private bool gameDone = false;
|
45 | 51 | private bool isGameStarted = false;
|
46 | 52 |
|
|
63 | 69 | {
|
64 | 70 | case DifficultyLevel.Easy:
|
65 | 71 | maxNumber = 10;
|
| 72 | + maxGuesses = 3; |
66 | 73 | break;
|
67 | 74 | case DifficultyLevel.Medium:
|
68 | 75 | maxNumber = 100;
|
| 76 | + maxGuesses = 7; |
69 | 77 | break;
|
70 | 78 | case DifficultyLevel.Hard:
|
71 | 79 | maxNumber = 1000;
|
| 80 | + maxGuesses = 10; |
72 | 81 | break;
|
| 82 | + default: |
| 83 | + throw new ArgumentOutOfRangeException(nameof(difficulty), difficulty, null); |
73 | 84 | }
|
74 | 85 |
|
75 |
| - var random = new Random(); |
76 |
| - targetNumber = random.Next(1, maxNumber + 1); |
| 86 | + remainingGuesses = maxGuesses; |
| 87 | + targetNumber = Random.Shared.Next(1, maxNumber + 1); |
77 | 88 | }
|
78 | 89 |
|
79 | 90 | private void CheckGuess()
|
80 | 91 | {
|
81 | 92 | if (gameDone || !isGameStarted)
|
82 | 93 | return;
|
83 | 94 |
|
84 |
| - if (userGuess < targetNumber) |
| 95 | + if (userGuess < 1 || userGuess > maxNumber) |
85 | 96 | {
|
86 |
| - guessCount++; |
87 |
| - message = "Too low, try again!"; |
| 97 | + message = $"Please enter a number between 1 and {maxNumber}."; |
| 98 | + alertClass = "alert-warning"; |
| 99 | + return; |
88 | 100 | }
|
89 |
| - else if (userGuess > targetNumber) |
| 101 | + |
| 102 | + guessCount++; |
| 103 | + remainingGuesses--; |
| 104 | + |
| 105 | + int difference = Math.Abs(userGuess - targetNumber); |
| 106 | + double proximity = (double)difference / maxNumber; |
| 107 | + |
| 108 | + if (userGuess == targetNumber) |
90 | 109 | {
|
91 |
| - guessCount++; |
92 |
| - message = "Too high, try again!"; |
| 110 | + gameDone = true; |
| 111 | + message = "🎉 Congratulations! You guessed the number!"; |
| 112 | + alertClass = "alert-success"; |
93 | 113 | }
|
94 |
| - else |
| 114 | + else if (remainingGuesses == 0) |
95 | 115 | {
|
96 |
| - guessCount++; |
97 | 116 | gameDone = true;
|
98 |
| - message = "🎉 Congratulations! You guessed the number!"; |
| 117 | + message = $"😞 Game over! You've run out of guesses. The number was {targetNumber}."; |
| 118 | + alertClass = "alert-danger"; |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + switch (proximity) |
| 123 | + { |
| 124 | + case < 0.05: |
| 125 | + message = "🔥 Scalding Hot!"; |
| 126 | + alertClass = "alert-danger"; |
| 127 | + break; |
| 128 | + case < 0.1: |
| 129 | + message = "🌡️ Very Hot!"; |
| 130 | + alertClass = "alert-warning"; |
| 131 | + break; |
| 132 | + case < 0.2: |
| 133 | + message = "🌞 Warm."; |
| 134 | + alertClass = "alert-info"; |
| 135 | + break; |
| 136 | + case < 0.3: |
| 137 | + message = "🌤️ Cool."; |
| 138 | + alertClass = "alert-secondary"; |
| 139 | + break; |
| 140 | + default: |
| 141 | + message = "❄️ Cold!"; |
| 142 | + alertClass = "alert-secondary"; |
| 143 | + break; |
| 144 | + } |
99 | 145 | }
|
100 | 146 | }
|
101 | 147 |
|
|
104 | 150 | isGameStarted = false;
|
105 | 151 | gameDone = false;
|
106 | 152 | message = string.Empty;
|
| 153 | + alertClass = ""; |
107 | 154 | userGuess = 0;
|
108 | 155 | guessCount = 0;
|
| 156 | + remainingGuesses = 0; |
109 | 157 | }
|
110 | 158 |
|
111 | 159 | private void HandleKeyPress(KeyboardEventArgs e)
|
|
0 commit comments