forked from itsjohncs/GrassyKnight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GrassStats.cs
49 lines (41 loc) · 1.48 KB
/
GrassStats.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
using System;
namespace GrassyKnight
{
class GrassStats {
// Maps from GrassState (ex: Cut) to number of grass in that state. I'm
// curious if there's a way to create a mutable-tuple-of-sorts with the
// correct size (the number of enum values in GrassState)... but I
// don't think there is.
private int[] GrassInState;
public GrassStats() {
GrassInState = new int[Enum.GetNames(typeof(GrassState)).Length];
}
public int Total() {
int sum = 0;
foreach (int numGrass in GrassInState) {
sum += numGrass;
}
return sum;
}
public int GetNumGrassInState(GrassState state) {
return GrassInState[(int)state];
}
public int this[GrassState state] {
get => GetNumGrassInState(state);
}
public void HandleUpdate(GrassState? oldState, GrassState newState) {
if (oldState is GrassState oldStateValue) {
GrassInState[(int)oldStateValue] -= 1;
}
GrassInState[(int)newState] += 1;
}
public override string ToString() {
string result = "GrassStats(";
foreach (GrassState state in Enum.GetValues(typeof(GrassState))) {
result += $"{Enum.GetName(typeof(GrassState), state)}=" +
$"{GrassInState[(int)state]}, ";
}
return result + ")";
}
}
}