-
Notifications
You must be signed in to change notification settings - Fork 2
/
BattleField.cs
118 lines (107 loc) · 3.76 KB
/
BattleField.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
using System;
using System.Text;
using SnakeBattleNet.Core.Contract;
namespace SnakeBattleNet.Core
{
public class BattleField
{
public int SideLength { get; private set; }
private readonly Content[,] field;
public Content this[int x, int y]
{
get
{
try
{
return field[x, y];
}
catch
{
return Content.Empty;
}
}
set { field[x, y] = value; }
}
public BattleField()
{
SideLength = 27;
field = new Content[SideLength, SideLength];
CreateEmpty();
CreateWalls();
}
public Cell<Content> RelativeCell(Direction direction, Position fieldHead, Position chipHead, Position chipCell)
{
switch (direction)
{
case Direction.North:
return NorthView(fieldHead, chipHead, chipCell);
case Direction.West:
return WestView(fieldHead, chipHead, chipCell);
case Direction.East:
return EastView(fieldHead, chipHead, chipCell);
case Direction.South:
return SouthView(fieldHead, chipHead, chipCell);
default:
throw new ArgumentOutOfRangeException("direction");
}
}
private Cell<Content> NorthView(Position fieldHead, Position chipHead, Position chipCell)
{
var x = fieldHead.X + chipCell.X - chipHead.X;
var y = fieldHead.Y + chipCell.Y - chipHead.Y;
return new Cell<Content> { X = x, Y = y, Content = this[x, y] };
}
private Cell<Content> WestView(Position fieldHead, Position chipHead, Position chipCell)
{
var x = fieldHead.X - chipCell.Y + chipHead.Y;
var y = fieldHead.Y + chipCell.X - chipHead.X;
return new Cell<Content> { X = x, Y = y, Content = this[x, y] };
}
private Cell<Content> EastView(Position fieldHead, Position chipHead, Position chipCell)
{
var x = fieldHead.X + chipCell.Y - chipHead.Y;
var y = fieldHead.Y - chipCell.X + chipHead.X;
return new Cell<Content> { X = x, Y = y, Content = this[x, y] };
}
private Cell<Content> SouthView(Position fieldHead, Position chipHead, Position chipCell)
{
var x = fieldHead.X - chipCell.X + chipHead.X;
var y = fieldHead.Y - chipCell.Y + chipHead.Y;
return new Cell<Content> { X = x, Y = y, Content = this[x, y] };
}
private void CreateEmpty()
{
for (var x = 1; x < SideLength - 1; x++)
for (var y = 1; y < SideLength - 1; y++)
{
field[x, y] = Content.Empty;
}
}
private void CreateWalls()
{
for (var x = 0; x < SideLength; x++)
{
field[x, 0] = Content.Wall;
field[x, SideLength - 1] = Content.Wall;
}
for (var y = 0; y < SideLength; y++)
{
field[0, y] = Content.Wall;
field[SideLength - 1, y] = Content.Wall;
}
}
public override string ToString()
{
var builder = new StringBuilder();
for (var y = SideLength - 1; y >= 0; y--)
{
for (var x = 0; x < SideLength; x++)
{
builder.Append(field[x, y].ToString().Substring(0, 1));
}
builder.AppendLine();
}
return builder.ToString();
}
}
}