-
Notifications
You must be signed in to change notification settings - Fork 10
/
Simon.razor
168 lines (155 loc) · 4.1 KB
/
Simon.razor
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
168
@page "/simon"
<h3>Simon</h3>
<div class="source-code-badge">
<a href="https://github.com/ZacharyPatten/dotnet-blazor-games/blob/main/dotnet-blazor-games/Pages/Simon.razor" alt="Go To Source Code">
<img title="Go To Source Code" alt="Go To Source Code" src="https://img.shields.io/badge/source-code-black?logo=github&style=flat">
</a>
</div>
<div class="row">
<div class="controls">
<button class="btn simon red @(IsLit[(int)Button.Red] ? "lit" : "")" @onclick="() => { TryPress(Button.Red); }"></button>
<button class="btn simon green @(IsLit[(int)Button.Green] ? "lit" : "")" @onclick="() => { TryPress(Button.Green); }"></button>
<button class="btn simon blue @(IsLit[(int)Button.Blue] ? "lit" : "")" @onclick="() => { TryPress(Button.Blue); }"></button>
<button class="btn simon yellow @(IsLit[(int)Button.Yellow] ? "lit" : "")" @onclick="() => { TryPress(Button.Yellow); }"></button>
</div>
</div>
<div class="row">
<button class="btn btn-primary" @onclick="StartNewGame">Start New Game</button>
</div>
<div class="row" style="display:@(GameOver ? "block" : "none")">
Game Over! Press "Start New Game" to play again.
</div>
<div class="row">
High Score: @HighScore
</div>
<div class="alert alert-dismissible alert-secondary">
<strong>Rules:</strong>
Simon is a memory game. The computer will genernerate a random sequence of button presses. Repeat the pattern back the the computer. Every time you successfully
repeat the pattern another random button is added to it: 1 button, 2 buttons, 3 buttons, etc. Mess up the pattern and you must start from the beginning. Press "Start New Game" to begin.
</div>
<style>
.controls {
margin-top: 1rem;
}
.controls button {
margin-right: .5rem;
margin-bottom: .5rem;
}
.simon { width: 4rem; height: 4rem; }
.simon.red { background-color: darkred; }
.simon.green { background-color: darkgreen; }
.simon.blue { background-color: darkblue; }
.simon.yellow { background-color: darkgoldenrod; }
.simon.red.lit { background-color: lightcoral; }
.simon.green.lit { background-color: lightgreen; }
.simon.blue.lit { background-color: lightblue; }
.simon.yellow.lit { background-color: lightgoldenrodyellow; }
</style>
@code
{
Random random = new Random();
List<Button> pattern = new List<Button>();
bool[] IsLit = new bool[4];
int Index;
int HighScore;
bool Busy;
bool GameOver;
double ButtonTime = 500; // milliseconds
bool Started = false;
void StartNewGame()
{
if (!Busy)
{
Started = true;
GameOver = false;
IsLit[(int)Button.Red] = false;
IsLit[(int)Button.Green] = false;
IsLit[(int)Button.Blue] = false;
IsLit[(int)Button.Yellow] = false;
Index = 0;
Busy = false;
pattern.Clear();
pattern.Add((Button)random.Next(4));
PlayPattern();
}
}
void TryPress(Button button)
{
if (Busy || GameOver || !Started)
{
return;
}
Busy = true;
IsLit[(int)button] = true;
if (pattern[Index] != button)
{
GameOver = true;
}
else
{
HighScore = Math.Max(Index + 1, HighScore);
}
Index++;
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = ButtonTime;
timer.Elapsed += (_, _) =>
{
Busy = false;
IsLit[(int)button] = false;
timer.Enabled = false;
StateHasChanged();
if (!GameOver && Index >= pattern.Count)
{
Index = 0;
pattern.Add((Button)random.Next(4));
PlayPattern();
}
};
timer.Enabled = true;
StateHasChanged();
}
void PlayPattern()
{
Busy = true;
int index = 0;
bool first = true;
System.Timers.Timer timer = new System.Timers.Timer();
timer.Interval = ButtonTime;
timer.Elapsed += (_, _) =>
{
if (first)
{
IsLit[(int)pattern[index]] = true;
StateHasChanged();
first = false;
return;
}
if (IsLit[(int)pattern[index]])
{
IsLit[(int)pattern[index]] = false;
StateHasChanged();
return;
}
index++;
if (index >= pattern.Count)
{
timer.Enabled = false;
Busy = false;
}
else
{
IsLit[(int)pattern[index]] = true;
}
StateHasChanged();
};
timer.Enabled = true;
StateHasChanged();
}
enum Button
{
Red = 0,
Green = 1,
Blue = 2,
Yellow = 3,
}
}