-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cs
166 lines (144 loc) · 5.22 KB
/
Board.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
using System.Drawing;
namespace othello
{
public enum eCoinColor
{
Black,
White,
Empty,
}
public class Board
{
private eCoinColor[,] m_BoardMatrix;
private int m_SizeOfBoard;
public eCoinColor[,] BoardMatrix
{
get { return m_BoardMatrix; }
set { m_BoardMatrix = value; }
}
public int SizeOfBoard
{
get { return m_SizeOfBoard; }
set { m_SizeOfBoard = value; }
}
public Board(int i_SizeOfBoard)
{
SizeOfBoard = i_SizeOfBoard;
BoardMatrix = new eCoinColor[SizeOfBoard, SizeOfBoard];
this.InitBoard();
}
public static eCoinColor SwapColor(eCoinColor i_ColorToSwap)
{
if (i_ColorToSwap == eCoinColor.Black)
{
return eCoinColor.White;
}
else if (i_ColorToSwap == eCoinColor.White)
{
return eCoinColor.Black;
}
else
{
return eCoinColor.Empty;
}
}
public Board Clone()
{
Board clonedBoard = new Board(SizeOfBoard);
for (int y = 0; y < clonedBoard.BoardMatrix.GetLength(0); ++y)
{
for (int x = 0; x < clonedBoard.BoardMatrix.GetLength(1); ++x)
{
clonedBoard.BoardMatrix[x, y] = BoardMatrix[x, y];
}
}
return clonedBoard;
}
public void InitBoard()
{
int middleOfHeight = BoardMatrix.GetLength(0) / 2;
int middleOfWidth = BoardMatrix.GetLength(1) / 2;
for (int x = 0; x < BoardMatrix.GetLength(0); ++x)
{
for (int y = 0; y < BoardMatrix.GetLength(1); ++y)
{
BoardMatrix[x, y] = eCoinColor.Empty;
}
}
// Init the middle 4 coins
BoardMatrix[middleOfHeight, middleOfWidth - 1] = eCoinColor.Black;
BoardMatrix[middleOfHeight - 1, middleOfWidth - 1] = eCoinColor.White;
BoardMatrix[middleOfHeight - 1, middleOfWidth] = eCoinColor.Black;
BoardMatrix[middleOfHeight, middleOfWidth] = eCoinColor.White;
}
public void MakeMove(Point i_PointToPlay, eCoinColor i_ColorPlaying)
{
setCellColor(i_PointToPlay, i_ColorPlaying);
for (int x = -1; x < 2; ++x)
{
for (int y = -1; y < 2; ++y)
{
if (IsLegalDirection(i_ColorPlaying, i_PointToPlay, x, y))
{
setColorInGivenDirection(i_PointToPlay, i_ColorPlaying, x, y);
}
}
}
}
private void setCellColor(Point i_Cell, eCoinColor i_ColorToSet)
{
BoardMatrix[i_Cell.X, i_Cell.Y] = i_ColorToSet;
}
private void setColorInGivenDirection(Point i_StartCell, eCoinColor i_CoinColor, int i_XDirection, int i_YDirection)
{
i_StartCell.X += i_XDirection;
i_StartCell.Y += i_YDirection;
while (IsInBorders(i_StartCell) && BoardMatrix[i_StartCell.X, i_StartCell.Y] != i_CoinColor)
{
setCellColor(i_StartCell, i_CoinColor);
i_StartCell.X += i_XDirection;
i_StartCell.Y += i_YDirection;
}
}
public void UpdateScoreOfUser(out int o_SumOfBlackPoints, out int o_SumOfWhitePoints)
{
o_SumOfBlackPoints = 0;
o_SumOfWhitePoints = 0;
foreach (eCoinColor Cell in BoardMatrix)
{
if (Cell == eCoinColor.Black)
{
++o_SumOfBlackPoints;
}
else if (Cell == eCoinColor.White)
{
++o_SumOfWhitePoints;
}
}
}
public bool IsEmptyCell(Point i_Point)
{
return BoardMatrix[i_Point.X, i_Point.Y] == eCoinColor.Empty;
}
public bool IsInBorders(Point i_Cell)
{
return i_Cell.X >= 0 && i_Cell.X < BoardMatrix.GetLength(0) && i_Cell.Y >= 0 && i_Cell.Y < BoardMatrix.GetLength(1);
}
public bool IsLegalDirection(eCoinColor i_CoinColor, Point i_StartCell, int i_XDirection, int i_YDirection)
{
Point Cell = new Point(i_StartCell.X + i_XDirection, i_StartCell.Y + i_YDirection);
if (IsInBorders(Cell) && BoardMatrix[Cell.X, Cell.Y] == SwapColor(i_CoinColor))
{
Cell.X += i_XDirection;
Cell.Y += i_YDirection;
while (IsInBorders(Cell) && BoardMatrix[Cell.X, Cell.Y] == SwapColor(i_CoinColor))
{
Cell.X += i_XDirection;
Cell.Y += i_YDirection;
}
return IsInBorders(Cell) && BoardMatrix[Cell.X, Cell.Y] == i_CoinColor;
}
return false;
}
}
}