-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cs
260 lines (236 loc) · 5.87 KB
/
Board.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Runtime.Serialization;
namespace Mancala
{
public delegate void TurnOverDelegate(Player currentPlayer);
public delegate void GameOverDelegate(Player winner);
[Serializable()]
public class Board : ISerializable
{
private Player _player1;
private Player _player2;
private Player _currentPlayer;
private AbstractDish[] _dishes = new AbstractDish[14];
private EndDish _player1End;
private EndDish _player2End;
public event TurnOverDelegate turnOver;
public event DishUpdateDelegate dishUpdate;
public event GameOverDelegate gameOver;
public Board(Player one, Player two)
{
_player1 = one;
_player2 = two;
_currentPlayer = _player1;
// config the board
for (int i = 0; i < 6; i++)
{
_dishes[i] = new MiddleDish(_player1, i);
_dishes[i].dishUpdate += new DishUpdateDelegate(DishUpdate);
}
_dishes[6] = new EndDish(_player1, 6);
_dishes[6].dishUpdate += new DishUpdateDelegate(DishUpdate);
_player1End = (EndDish)_dishes[6];
for (int i = 7; i<13; i++)
{
_dishes[i] = new MiddleDish(_player2, i );
_dishes[i].dishUpdate += new DishUpdateDelegate(DishUpdate);
}
_dishes[13] = new EndDish(_player2, 13);
_dishes[13].dishUpdate += new DishUpdateDelegate(DishUpdate);
_player2End = (EndDish)_dishes[13];
}
#region ISerializable Members
public Board(SerializationInfo info, StreamingContext context)
{
// TODO: Implement Serialization
}
public void GetObjectData(SerializationInfo info, StreamingContext context)
{
// TODO: Add Board.GetObjectData implementation
}
#endregion
public Player CurrentPlayer
{
get { return _currentPlayer; }
}
public void TakeTurn(int iDish)
{
AbstractDish dish = _dishes[iDish];
if (dish is MiddleDish)
{
// check if dish clicked in belongs to current player
if (dish.Owner != _currentPlayer)
{
throw new Exception("Cannot move opponents tokens!");
}
if (dish.HasTokens)
{
int tokens = dish.removeTokens();
for (int i = 1; i <= tokens; i++)
{
iDish = ++iDish % 14;
dish = _dishes[iDish];
if (dish is EndDish && dish.Owner != _currentPlayer)
{
// don't drop
iDish = ++iDish % 14;
dish = _dishes[iDish];
}
dish.dropToken();
}
// check if dish has only one token and stopped on yours.
if (dish.Tokens == 1 && dish.Owner == _currentPlayer && !(dish is EndDish))
{
// claim all oppositions (and yours)
int oppDish = (6 + (6 - iDish));
AbstractDish oppositeDish = _dishes[oppDish];
if (_currentPlayer == _player1)
{
_player1End.dropToken(oppositeDish.removeTokens());
_player1End.dropToken(dish.removeTokens());
}
else
{
_player2End.dropToken(oppositeDish.removeTokens());
_player2End.dropToken(dish.removeTokens());
}
}
// check for win-state
Player winner = null;
if (this.IsGameOver(ref winner))
{
// current player must have won
// move all other player's tokens to their win bin
Player loser;
EndDish endDish;
//if (winner == _player1)
//{
// loser = _player2;
// endDish = _player2End;
//}
//else
//{
// loser = _player1;
// endDish = _player1End;
//}
//foreach (AbstractDish d in _dishes)
//{
// if (d is MiddleDish && d.Owner == loser)
// {
// endDish.dropToken(d.removeTokens());
// }
//}
// determine winner by size of mancala's
if (_player1End.Tokens > _player2End.Tokens)
OnGameOver(_player1);
else if (_player2End.Tokens > _player1End.Tokens)
OnGameOver(_player2);
else
OnGameOver(null);
}
else
{
// player gets another go if they drop their last token
// in their end dish.
if (!(dish is EndDish && dish.Owner == _currentPlayer))
{
_currentPlayer = this.NextPlayer();
}
OnTurnOver(_currentPlayer);
}
}
else
{
throw new Exception("This dish has no tokens!");
}
}
else
{
throw new Exception("Cannot take from end dish!");
}
}
protected virtual void OnTurnOver(Player current)
{
if (turnOver != null)
{
turnOver(current);
}
}
private Player NextPlayer()
{
if (_currentPlayer == _player1)
return _player2;
else
return _player1;
}
private void DishUpdate(object sender, DishUpdateArgs args)
{
OnDishUpdate(this, args);
}
protected virtual void OnDishUpdate(object sender, DishUpdateArgs args)
{
if (this.dishUpdate != null)
{
dishUpdate(sender, args);
}
}
protected virtual void OnGameOver(Player winner)
{
if (gameOver != null)
{
gameOver(winner);
}
}
private bool IsGameOver(ref Player winner)
{
bool player1Empty = true;
bool player2Empty = true;
// check player 1
for (int i = 0; i < 6; i++)
{
if (_dishes[i].HasTokens)
{
player1Empty = false;
break;
}
}
// check player 2
for (int i = 7; i < 13; i++)
{
if (_dishes[i].HasTokens)
{
player2Empty = false;
break;
}
}
return player1Empty && player2Empty;
//if (player1Empty && player2Empty)
//{
// winner = null;
// return true;
//}
//else if (player1Empty)
//{
// winner = _player1;
// return true;
//}
//else if (player2Empty)
//{
// winner = _player2;
// return true;
//}
//else
//{
// return false;
//}
}
public void Refresh()
{
foreach (AbstractDish dish in _dishes)
{
dish.Refresh();
}
OnTurnOver(_currentPlayer); //doesn't actually mean end of turn
}
}
}