-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap.cs
More file actions
131 lines (108 loc) · 4.42 KB
/
Map.cs
File metadata and controls
131 lines (108 loc) · 4.42 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
namespace Halite
{
public class Map
{
public void Update(string gameMapStr)
{
var gameMapValues = new Queue<string>(gameMapStr.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
ushort x = 0, y = 0;
while (y < Height)
{
ushort counter, owner;
if (!ushort.TryParse(gameMapValues.Dequeue(), out counter))
throw new ApplicationException("Could not get some counter from stdin");
if (!ushort.TryParse(gameMapValues.Dequeue(), out owner))
throw new ApplicationException("Could not get some owner from stdin");
while (counter > 0)
{
_sites[x, y].Owner = owner;
x++;
if (x == Width)
{
x = 0;
y++;
}
counter--;
}
}
var strengthValues = gameMapValues;
for (y = 0; y < Height; y++)
{
for (x = 0; x < Width; x++)
{
ushort strength;
if (!ushort.TryParse(strengthValues.Dequeue(), out strength))
throw new ApplicationException("Could not get some strength value from stdin");
_sites[x, y].Strength = strength;
}
}
}
public List<Site> GetSites(Func<Site, bool> filter)
{
return _sitesList.Where(filter).ToList();
}
public Site this[int x, int y] => _sites[x.Mod(Config.Get().MapWidth), y.Mod(Config.Get().MapHeight)];
public ushort Width => (ushort)_sites.GetLength(0);
public ushort Height => (ushort)_sites.GetLength(1);
public List<Site> GetMySites()
{
return _sitesList.Where(s => s.Owner == Config.Get().PlayerTag).ToList();
}
#region Implementation
private readonly Site[,] _sites;
private readonly List<Site> _sitesList;
private Map(int width, int height)
{
_sites = new Site[width, height];
_sitesList = new List<Site>();
for (ushort x = 0; x < width; x++)
{
for (ushort y = 0; y < height; y++)
{
_sites[x, y] = new Site(x, y);
_sitesList.Add(_sites[x, y]);
}
}
foreach (var site in _sitesList)
{
//populate neighbours
var top = _sites[site.X, (site.Y - 1).Mod(height)];
var bottom = _sites[site.X, (site.Y + 1).Mod(height)];
var left = _sites[(site.X - 1).Mod(width), site.Y];
var right = _sites[(site.X + 1).Mod(width), site.Y];
site.PopulateNeighbours(top, bottom, left, right);
}
}
private static void ParseMapSize(string mapSizeStr, out int width, out int height)
{
var parts = mapSizeStr.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
if (parts.Length != 2 || !int.TryParse(parts[0], out width) || !int.TryParse(parts[1], out height))
throw new ApplicationException("Could not get map size from stdin during init");
}
public static Map ParseMap(string mapSizeStr, string productionMapStr, string gameMapStr, int playerTag)
{
int width, height;
ParseMapSize(mapSizeStr, out width, out height);
var map = new Map(width, height);
var productionValues = new Queue<string>(productionMapStr.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
ushort x, y;
for (y = 0; y < map.Height; y++)
{
for (x = 0; x < map.Width; x++)
{
ushort production;
if (!ushort.TryParse(productionValues.Dequeue(), out production))
throw new ApplicationException("Could not get some production value from stdin");
map._sites[x, y].Production = production;
}
}
Config.Init(playerTag, width, height);
map.Update(gameMapStr);
return map;
}
#endregion
}
}