forked from jhauberg/ComponentKit-Example-RockPaperScissors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Decision.cs
228 lines (177 loc) · 6.68 KB
/
Decision.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
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
namespace ComponentKit.Examples.RockPaperScissors {
internal struct Decision {
public static readonly Decision Undecided =
new Decision();
public int InfluenceTowardsRock {
get;
private set;
}
public int InfluenceTowardsPaper {
get;
private set;
}
public int InfluenceTowardsScissors {
get;
private set;
}
public Hand MostInfluencedHand {
get {
if (InfluenceTowardsRock == 0 &&
InfluenceTowardsPaper == 0 &&
InfluenceTowardsScissors == 0) {
return Hand.NotSpecified;
}
Tuple<Hand, int> hand =
InfluenceTowardsRock > InfluenceTowardsPaper ?
Tuple.Create(Hand.Rock, InfluenceTowardsRock) :
Tuple.Create(Hand.Paper, InfluenceTowardsPaper);
Hand mostInfluencedHand = hand.Item1;
if (InfluenceTowardsScissors > hand.Item2) {
mostInfluencedHand = Hand.Scissors;
}
return mostInfluencedHand;
}
}
public Outcome DetermineOutcome(Decision otherDecision) {
Outcome outcome = Outcome.Unknown;
Hand ha = MostInfluencedHand;
Hand hb = otherDecision.MostInfluencedHand;
if (ha == Hand.NotSpecified || hb == Hand.NotSpecified) {
return outcome;
}
if (ha != hb) {
switch (ha) {
default: break;
case Hand.Rock: {
if (hb == Hand.Scissors) {
outcome = Outcome.Win;
} else if (hb == Hand.Paper) {
outcome = Outcome.Loss;
}
} break;
case Hand.Paper: {
if (hb == Hand.Rock) {
outcome = Outcome.Win;
} else if (hb == Hand.Scissors) {
outcome = Outcome.Loss;
}
} break;
case Hand.Scissors: {
if (hb == Hand.Paper) {
outcome = Outcome.Win;
} else if (hb == Hand.Rock) {
outcome = Outcome.Loss;
}
} break;
}
} else {
outcome = Outcome.Tie;
}
return outcome;
}
public static Decision Win(Decision decision, int influence) {
Hand hand = Hand.NotSpecified;
switch (decision.MostInfluencedHand) {
default: break;
case Hand.Rock: {
hand = Hand.Paper;
} break;
case Hand.Paper: {
hand = Hand.Scissors;
} break;
case Hand.Scissors: {
hand = Hand.Rock;
} break;
}
return Decision.Distribute(
hand, influence);
}
public static Decision Loss(Decision decision, int influence) {
Hand hand = Hand.NotSpecified;
switch (decision.MostInfluencedHand) {
default: break;
case Hand.Rock: {
hand = Hand.Scissors;
} break;
case Hand.Paper: {
hand = Hand.Rock;
} break;
case Hand.Scissors: {
hand = Hand.Paper;
} break;
}
if (hand != Hand.NotSpecified) {
return Decision.Distribute(
hand, influence);
}
return Decision.Undecided;
}
public static Decision Distribute(int influenceTowardsRock, int influenceTowardsPaper, int influenceTowardsScissors) {
Decision weights = new Decision() {
InfluenceTowardsRock = influenceTowardsRock,
InfluenceTowardsPaper = influenceTowardsPaper,
InfluenceTowardsScissors = influenceTowardsScissors
};
return weights;
}
public static Decision Distribute(Hand hands, int influence) {
if (hands == Hand.NotSpecified) {
return Decision.Undecided;
}
Decision decision = new Decision();
int modes = (int)hands;
if ((modes & (int)Hand.Rock) == (int)Hand.Rock) {
decision.InfluenceTowardsRock += influence;
}
if ((modes & (int)Hand.Paper) == (int)Hand.Paper) {
decision.InfluenceTowardsPaper += influence;
}
if ((modes & (int)Hand.Scissors) == (int)Hand.Scissors) {
decision.InfluenceTowardsScissors += influence;
}
return decision;
}
public static Decision Next(int influence) {
Hand hands =
Hand.Rock |
Hand.Paper |
Hand.Scissors;
return Decision.Next(
hands, influence);
}
public static Decision Next(Hand hands, int influence) {
if (hands == Hand.NotSpecified) {
return Decision.Undecided;
}
int modes = (int)hands;
IList<Hand> picks = new List<Hand>();
if ((modes & (int)Hand.Rock) == (int)Hand.Rock) {
picks.Add(Hand.Rock);
}
if ((modes & (int)Hand.Paper) == (int)Hand.Paper) {
picks.Add(Hand.Paper);
}
if ((modes & (int)Hand.Scissors) == (int)Hand.Scissors) {
picks.Add(Hand.Scissors);
}
Hand hand = picks[
Roll.Next(0, picks.Count)];
return Decision.Distribute(
hand, influence);
}
public static Decision operator +(Decision left, Decision right) {
return Decision.Distribute(
left.InfluenceTowardsRock + right.InfluenceTowardsRock,
left.InfluenceTowardsPaper + right.InfluenceTowardsPaper,
left.InfluenceTowardsScissors + right.InfluenceTowardsScissors);
}
public override string ToString() {
return String.Format("{{ {0}, {1}, {2} }}",
InfluenceTowardsRock,
InfluenceTowardsPaper,
InfluenceTowardsScissors);
}
}
}