forked from oreillymedia/building_maintainable_software
/
BoardFactory.cs
52 lines (40 loc) · 1.32 KB
/
BoardFactory.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
using System.Diagnostics;
namespace eu.sig.training.ch02
{
public class BoardFactory {
// tag::createBoard[]
public Board CreateBoard(Square[,] grid) {
Debug.Assert(grid != null);
Board board = new Board(grid);
int width = board.Width;
int height = board.Height;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
Square square = grid[x,y];
foreach (Direction dir in Direction.Values) {
int dirX = (width + x + dir.DeltaX) % width;
int dirY = (height + y + dir.DeltaY) % height;
Square neighbour = grid[dirX,dirY];
square.Link(neighbour, dir);
}
}
}
return board;
}
// end::createBoard[]
}
public class Board {
public Board(Square[,] grid) {}
public int Width { get; set; }
public int Height { get; set; }
}
public class Square {
public void Link(Square neighbour, Direction dir) {}
}
public class Direction
{
public static Direction[] Values { get; set; }
public int DeltaY { get; set; }
public int DeltaX { get; set; }
}
}