-
Notifications
You must be signed in to change notification settings - Fork 2
/
SimonGame.cs
88 lines (76 loc) · 1.93 KB
/
SimonGame.cs
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
using System;
namespace Simon;
public enum GameState
{
Start,
NextLevel,
Win,
GameOver,
}
public class SimonEventArgs : EventArgs
{
public GameState GameState { get; set; }
public SimonEventArgs(GameState state)
{
GameState = state;
}
}
public class SimonGame
{
static int MAX_LEVELS = 25;
static int NUM_BUTTONS = 4;
public delegate void GameStateChangedDelegate(object sender, SimonEventArgs e);
public event GameStateChangedDelegate OnGameStateChanged = delegate { };
public int Level { get; set; }
int currentStep;
int[] Steps = new int[MAX_LEVELS];
Random rand = new Random((int)DateTime.Now.Ticks);
public void Reset()
{
OnGameStateChanged(this, new SimonEventArgs(GameState.Start));
Level = 1;
currentStep = 0;
NextLevel();
}
public int[] GetStepsForLevel()
{
var steps = new int[Level];
for (int i = 0; i < Level; i++)
steps[i] = Steps[i];
return steps;
}
public void EnterStep(int step)
{
if (Steps[currentStep] == step)
{
currentStep++;
}
else
{
OnGameStateChanged(this, new SimonEventArgs(GameState.GameOver));
Reset();
}
if (currentStep == Level)
{
NextLevel();
}
}
void NextLevel()
{
currentStep = 0;
Level++;
if (Level >= MAX_LEVELS)
{
OnGameStateChanged(this, new SimonEventArgs(GameState.Win));
Reset();
return;
}
var level = string.Empty;
for (int i = 0; i < Level; i++)
{
Steps[i] = rand.Next(NUM_BUTTONS);
level += Steps[i] + ", ";
}
OnGameStateChanged(this, new SimonEventArgs(GameState.NextLevel));
}
}