-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainRules.cs
374 lines (350 loc) · 13 KB
/
MainRules.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
using System;
using System.Collections.Generic;
using System.Text;
using KingsTableConsoleEdition.Interfaces;
namespace KingsTableConsoleEdition
{
public class MainRules : IRules
{
Board board;
IPlayer attacker, defender;
bool gameOver;
char emptyChar, goalChar, throneChar, attackerChar, defenderChar, kingChar, highlightChar; //TODO: make enum
List<int[]> highlighted;
int[] kingPosition;
public MainRules()
{
emptyChar = '_';
goalChar = 'X';
throneChar = '+';
attackerChar = 'A';
defenderChar = 'D';
kingChar = 'K';
highlightChar = '0';
highlighted = new List<int[]>();
gameOver = false;
}
public string GetIntro()
{
StringBuilder builder = new StringBuilder();
builder.AppendLine("");
builder.AppendLine("-This is a custom King's Table ruleset-");
builder.AppendLine("-designed by c.s.tilstra and IVSugz-");
return builder.ToString();
}
public bool PrepareNewGame(Board newBoard, IPlayer[] players)
{
board = newBoard;
if (board.heightWidth == 11)
{
board.CreateEmptySpaces(emptyChar);
board.MarkCornersAsGoals(goalChar);
board.MarkCenterAsThrone(throneChar);
board.SetEmptyBoard(board.GetBoard());
PlaceKingOnThrone();
PlaceAttackers();
PlaceDefenders();
SetPlayers(players);
return true;
}else{
Console.WriteLine("");
Console.WriteLine("The current board is not compatible with this rule type");
Console.WriteLine("The rules require board height/width of 11");
Console.WriteLine("");
return false;
}
}
public bool GameContinues()
{
return !gameOver;
}
public bool MoveIsValid(int[][] move)
{
//TODO: check that the first position is not empty
//TODO: check that the character at first position belongs to current player
//check if there is a destination position and return true if not
if(move[1].Length == 0){
return true;
}
//check that character at first position can move to second position
List<int[]> moves = GetMovesForPieceAt(move[0]);
for (int i = 0; i < moves.Count; i++)
{
int[] position = { moves[i][0], moves[i][1] };
if(position[0] == move[1][0] & position[1] == move[1][1]){
return true;
}
}
Console.WriteLine("That piece cannot move there.");
return false;
}
public void ApplyMove(int[][] move)
{
if (MoveIsValid(move))
{
//clear display characters
RemoveHighlights();
//display moves for unit at first position
//TODO: make function
List<int[]> moves = GetMovesForPieceAt(move[0]);
for (int i = 0; i < moves.Count; i++)
{
int[] position = { moves[i][0], moves[i][1] };
board.SetPositionToValue(position, highlightChar);
}
highlighted = moves;
//try to move unit to second position, then clear display characters
char pieceMoving = board.GetValueAt(move[0]);
try
{
board.MovePiece(move[0], move[1]);
RemoveHighlights();
if(board.GetValueAt(move[1]) == kingChar){
kingPosition = move[1];
}
}
catch (IndexOutOfRangeException e)
{
//Console.WriteLine(e);
//set piece back to original position
board.SetPositionToValue(move[0], pieceMoving);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
CheckForCapture(move[1]);
CheckForWin();
}
}
public List<int[]> GetMovesForPieceAt(int[] position) //TODO: allow the King to move to the throne and the goals
{
char moving = board.GetValueAt(position);
int y = position[0];
int x = position[1];
//Console.Write("y = " + y);
//Console.WriteLine(", x = " + x);
List<int[]> moves = new List<int[]>();
// look up
for (int i = y - 1; i >= 0; i--){
int[] tempPosition = { i, x };
char occupant = board.GetValueAt(tempPosition);
if(moving == kingChar && board.PositionIsReservedForKing(tempPosition)){
moves.Add(tempPosition);
}else if(occupant == emptyChar || occupant == highlightChar){
moves.Add(tempPosition);
}else{
break;
}
}
// look right
for (int i = x + 1; i <= 10; i++)
{
//TODO: make function
int[] tempPosition = { y, i };
char occupant = board.GetValueAt(tempPosition);
if (moving == kingChar && board.PositionIsReservedForKing(tempPosition))
{
moves.Add(tempPosition);
}
else if (occupant == emptyChar || occupant == highlightChar)
{
moves.Add(tempPosition);
}
else
{
break;
}
}
// look down
for (int i = y + 1; i <= 10; i++)
{
int[] tempPosition = { i, x };
char occupant = board.GetValueAt(tempPosition);
if (moving == kingChar && board.PositionIsReservedForKing(tempPosition))
{
moves.Add(tempPosition);
}
else if (occupant == emptyChar || occupant == highlightChar)
{
moves.Add(tempPosition);
}
else
{
break;
}
}
// look left
for (int i = x - 1; i >= 0; i--)
{
int[] tempPosition = { y, i };
char occupant = board.GetValueAt(tempPosition);
if (moving == kingChar && board.PositionIsReservedForKing(tempPosition))
{
moves.Add(tempPosition);
}
else if (occupant == emptyChar || occupant == highlightChar)
{
moves.Add(tempPosition);
}
else
{
break;
}
}
return moves;
}
// Non Interface defined functions below
void PlaceKingOnThrone(){
board.SetPositionToValue(board.throne, kingChar);
kingPosition = board.throne;
}
void PlaceAttackers()
{
int[] position;
for (int i = 3; i < 8; i++)
{
position = new int[] { i, 0 };
board.SetPositionToValue(position, attackerChar);
position = new int[] { i, 10 };
board.SetPositionToValue(position, attackerChar);
position = new int[] { 0, i };
board.SetPositionToValue(position, attackerChar);
position = new int[] { 10, i };
board.SetPositionToValue(position, attackerChar);
}
position = new int[] { 5, 1 };
board.SetPositionToValue(position, attackerChar);
position = new int[] { 5, 9 };
board.SetPositionToValue(position, attackerChar);
position = new int[] { 1, 5 };
board.SetPositionToValue(position, attackerChar);
position = new int[] { 9, 5 };
board.SetPositionToValue(position, attackerChar);
}
void PlaceDefenders()
{
int[] position = new int[] { 3 , 5 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 4, 4 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 4, 5 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 4, 6 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 5, 3 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 5, 4 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 5, 6 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 5, 7 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 6, 4 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 6, 5 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 6, 6 };
board.SetPositionToValue(position, defenderChar);
position = new int[] { 7, 5 };
board.SetPositionToValue(position, defenderChar);
}
void SetPlayers(IPlayer[] players)
{
attacker = players[0];
defender = players[1];
try
{
attacker = players[0];
defender = players[1];
}
catch (System.Exception e)//TODO: catch null instance exception
{
Console.WriteLine("");
Console.WriteLine("Error encountered in MainRules.cs");
Console.WriteLine("SetPlayers()");
Console.WriteLine(e);
Console.WriteLine("");
}
}
void RemoveHighlights(){
for (int i = 0; i < highlighted.Count; i++)
{
if (board.GetValueAt(highlighted[i]) == highlightChar)
{
board.SetPositionToValue(highlighted[i], emptyChar);
}
}
}
void CheckForCapture(int [] position){
char pieceAt = board.GetValueAt(position);
if (pieceAt == attackerChar || pieceAt == defenderChar)
{
//TODO: create helper function
// look at each neighboring space checking for enemy
//up
int[] adjacentPosition = { position[0] - 1, position[1] };
char adjacentPiece = board.GetValueAt(adjacentPosition);
if((adjacentPiece == attackerChar || adjacentPiece == defenderChar) &&
(adjacentPiece != pieceAt)){
// look at the piece beyond
int[] beyondPosition = new int[]{ position[0] - 2, position[1] };
char nextPiece = board.GetValueAt(beyondPosition);
if(nextPiece == pieceAt){
board.RemovePieceAt(adjacentPosition);
Console.WriteLine("Piece removed");
}
}
//down
adjacentPosition = new int[]{ position[0] + 1, position[1] };
adjacentPiece = board.GetValueAt(adjacentPosition);
if ((adjacentPiece == attackerChar || adjacentPiece == defenderChar) &&
(adjacentPiece != pieceAt))
{
// look at the piece beyond
int[] beyondPosition = new int[] { position[0] + 2, position[1] };
char nextPiece = board.GetValueAt(beyondPosition);
if (nextPiece == pieceAt)
{
board.RemovePieceAt(adjacentPosition);
Console.WriteLine("Piece removed");
}
}
//left
adjacentPosition = new int[] { position[0], position[1] - 1 };
adjacentPiece = board.GetValueAt(adjacentPosition);
if ((adjacentPiece == attackerChar || adjacentPiece == defenderChar) &&
(adjacentPiece != pieceAt))
{
// look at the piece beyond
int[] beyondPosition = new int[] { position[0], position[1] -2 };
char nextPiece = board.GetValueAt(beyondPosition);
if (nextPiece == pieceAt)
{
board.RemovePieceAt(adjacentPosition);
Console.WriteLine("Piece removed");
}
}
//right
adjacentPosition = new int[] { position[0], position[1] + 1 };
adjacentPiece = board.GetValueAt(adjacentPosition);
if ((adjacentPiece == attackerChar || adjacentPiece == defenderChar) &&
(adjacentPiece != pieceAt))
{
// look at the piece beyond
int[] beyondPosition = new int[] { position[0], position[1] + 2 };
char nextPiece = board.GetValueAt(beyondPosition);
if (nextPiece == pieceAt)
{
board.RemovePieceAt(adjacentPosition);
Console.WriteLine("Piece removed");
}
}
}
}
void CheckForWin(){
//check if King is surrounded
//check if King is in corner
}
}
}