-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.cs
292 lines (258 loc) · 8.25 KB
/
Map.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
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
namespace GoGame
{
public enum ColorTaken
{
Black,
White,
Liberty
}
public class Map : Component
{
#region Fields
private Texture2D _mapTexture;
private float _gridSize;
private Vector2 _rectSize;
private Vector2 _mapSize;
private Square[,] _grid;
private Dictionary<int, Chain> _chains;
private int _chainId;
public event EventHandler<KilledEventArgs> SquareKilled;
private bool _visible = true;
private bool _enabled = true;
#endregion
#region Constructor
public Map(Texture2D mapTexture, float gridSize)
{
_chains = new Dictionary<int, Chain>();
_mapTexture = mapTexture;
_gridSize = gridSize;
_mapSize = CalculateMapSize();
_rectSize = CalculateRectSize(_gridSize, _mapSize.X, _mapSize.Y);
_grid = CreateGrid(gridSize, _rectSize);
}
#endregion
#region Properties
public Dictionary<int, Chain> Chains
{
get { return _chains; }
}
public Texture2D Texture
{
get { return _mapTexture; }
}
public Square[,] Grid
{
get { return _grid; }
}
public float GridSize
{
get { return _gridSize; }
}
public Vector2 Size
{
get { return _mapSize; }
}
public Vector2 RectSize
{
get { return _rectSize; }
}
public float Width
{
get { return _mapSize.X; }
}
public float Height
{
get { return _mapSize.Y; }
}
public override int UpdateOrder => 1;
public override int DrawOrder => 0;
public override bool Visible
{
get { return _visible; }
set { _visible = value; }
}
public override bool Enabled
{
get { return _enabled; }
set { _enabled = value; }
}
#endregion
#region Methods
public Square GetRectangle(int[] coordinates)
{
return _grid[coordinates[0], coordinates[1]];
}
public List<Square> GetSurroundingRectangles(int[] coordinates)
{
List<Square> surroundingRects = new List<Square>();
if (coordinates[0] > 0)
{
surroundingRects.Add(Grid[coordinates[0] - 1, coordinates[1]]);
}
if (coordinates[0] + 1 < GridSize)
{
surroundingRects.Add(Grid[coordinates[0] + 1, coordinates[1]]);
}
if (coordinates[1] > 0)
{
surroundingRects.Add(Grid[coordinates[0], coordinates[1] - 1]);
}
if (coordinates[1] + 1 < GridSize)
{
surroundingRects.Add(Grid[coordinates[0], coordinates[1] + 1]);
}
return surroundingRects;
}
public List<Chain> GetSurroundingChains(int[] chosenCoordinates)
{
List<Square> surroundingRects = GetSurroundingRectangles(chosenCoordinates);
List<Chain> surroundingChains = new List<Chain>();
foreach (Square rect in surroundingRects)
{
if (rect.Chain != null)
{
surroundingChains.Add(rect.Chain);
}
}
return surroundingChains;
}
public int CheckForLiberties(int[] coordinates)
{
int liberties = 0;
List<Square> surroundingSquares = GetSurroundingRectangles(coordinates);
foreach (Square sqr in surroundingSquares)
{
if (sqr.Color == ColorTaken.Liberty)
{
liberties++;
}
}
return liberties;
}
public void CreateNewChain(Square rect)
{
Chain chain = new Chain(rect, rect.Liberties, _chainId, rect.Color);
_chains.Add(_chainId, chain);
rect.Chain = chain;
_chainId++;
}
public void RemoveChain(int chainId)
{
if (_chains.TryGetValue(chainId, out Chain chain))
{
chain.Dispose();
}
_chains.Remove(chainId);
}
public void RemoveKilledChains(Chain currentChain)
{
List<int> toBeDisposed = GetChainKeysToBeDisposed(currentChain);
SquareKilledInList(toBeDisposed);
DisposeOfChain(toBeDisposed);
}
private List<int> GetChainKeysToBeDisposed(Chain currentChain)
{
List<int> toBeDisposed = new List<int>();
foreach (KeyValuePair<int, Chain> entry in Chains)
{
if (entry.Value.Liberties == 0 && entry.Value != currentChain)
{
toBeDisposed.Add(entry.Key);
}
}
return toBeDisposed;
}
private void SquareKilledInList(List<int> toBeDisposed)
{
foreach (int i in toBeDisposed)
{
foreach (Square rect in Chains[i].Rectangles)
{
SquareKilled?.Invoke(this, new KilledEventArgs(rect.TokenID));
}
}
}
private void DisposeOfChain(List<int> toBeDisposed)
{
foreach (int i in toBeDisposed)
{
Chains[i].Dispose();
Chains.Remove(i);
}
}
public void AddToChain(Square rectangle, Chain chain)
{
chain.AddRectangle(rectangle);
rectangle.Chain = chain;
chain.Liberties += rectangle.Liberties;
}
private Vector2 CalculateMapSize()
{
Vector2 mapSize = new Vector2(_mapTexture.Width, _mapTexture.Height);
return mapSize;
}
private Vector2 CalculateRectSize(float gridSize, float mapWidth, float mapHeight)
{
Vector2 rectSize = new Vector2((float)mapWidth / (float)gridSize, (float)mapHeight / (float)gridSize);
return rectSize;
}
public Square[,] CreateGrid(float gridSize, Vector2 rectSize)
{
Vector2 rectPos = new Vector2(0, 0);
float nextRectStepX = rectSize.X;
float nextRectStepY = rectSize.Y;
Square[,] grid = new Square[(int)gridSize, (int)gridSize];
int rectID = 0;
for (int i = 0; i < gridSize; i++)
{
for (int j = 0; j < gridSize; j++)
{
int[] coordinates = { j, i };
Square rect = new Square(rectPos, rectSize, coordinates, rectID);
grid[j, i] = rect;
rectPos = new Vector2(rectPos.X + nextRectStepX, rectPos.Y);
rectID++;
}
rectPos = new Vector2(0, rectPos.Y + nextRectStepY);
}
return grid;
}
public int CountTerritory()
{
int territoryCount = 0;
foreach(Square rect in Grid)
{
int[] currentCoordinates = rect.Coordinates;
}
return territoryCount;
}
public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
spriteBatch.Draw(
Texture,
position: Vector2.Zero,
color: Color.White);
}
public override void Update(GameTime gameTime)
{
//
}
public ColorTaken[,] CreateSquareColorMap()
{
ColorTaken[,] squareColors = new ColorTaken[(int)GridSize, (int)GridSize]; ;
for (int i = 0; i < GridSize; i++)
{
for (int j = 0; j < GridSize; j++)
{
squareColors[j, i] = Grid[j, i].Color;
}
}
return squareColors;
}
#endregion
}
}