-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
116 lines (97 loc) · 3.19 KB
/
Game.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
using System.Reflection.PortableExecutable;
namespace Advance;
/// <summary>
/// Represents the Game which includes two players, a board, and the wall pieces.
/// </summary>
public class Game
{
/// <summary>
/// Gets the white player in the game.
/// </summary>
public Player White { get; }
/// <summary>
/// Gets the black player in the game.
/// </summary>
public Player Black { get; }
/// <summary>
/// Gets the game board.
/// </summary>
public Board Board { get; }
public List<Wall> walls = new List<Wall>();
/// <summary>
/// Initializes a new instance of the Game class.
/// </summary>
public Game()
{
Board = new Board();
White = new Player(Colour.White, this);
Black = new Player(Colour.Black, this);
}
/// <summary>
/// Returns a string that represents the current game state.
/// </summary>
/// <returns>A string that represents the current game state.</returns>
public override string ToString()
{
StringWriter writer = new StringWriter();
Write(writer);
return writer.ToString();
}
/// <summary>
/// Clears the board of all pieces.
/// </summary>
public void Clear()
{
Black.Army.RemoveAllPieces();
White.Army.RemoveAllPieces();
}
/// <summary>
/// Reads the game state from a text reader.
/// </summary>
/// <param name="reader">The text reader to read the game state from.</param>
public void Read(TextReader? reader)
{
Clear();
for (int row = 0; row < Board.Size; row++)
{
string? currentRow = reader.ReadLine();
if (currentRow == null)
throw new Exception("Ran out of data before reading full board");
if (currentRow.Length != Board.Size)
{
Console.WriteLine($"row length {currentRow.Length}");
throw new Exception($"Row {row} is not the right length");
}
for (int col = 0; col < Board.Size; col++)
{
Square? currentSquare = Board.Get(row, col);
char icon = currentRow[col];
if (icon != '.')
{
Player currentPlayer = Char.IsLower(icon) ? Black : White;
currentPlayer.Army.Recruit(icon, currentSquare);
}
}
}
}
/// <summary>
/// Writes the game state to a text writer.
/// </summary>
/// <param name="writer">The text writer to write the game state to.</param>
public void Write(TextWriter writer)
{
for (int row = 0; row < Board.Size; row++)
{
for (int col = 0; col < Board.Size; col++)
{
Square currentSquare = Board.Get(row, col);
Piece? currentPiece = currentSquare.Occupant;
if (currentPiece == null)
writer.Write('.');
else
writer.Write(currentPiece.Icon);
}
writer.WriteLine();
}
}
}