-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameManager.cs
233 lines (202 loc) · 7.18 KB
/
GameManager.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
using System.Collections.Generic;
using System.Drawing;
namespace othello
{
public class GameManager
{
/*Fields*/
public enum GameOption
{
UserVsUser,
UserVsComputer,
}
private readonly GameOption m_GameType;
private readonly User m_BlackPlayer;
private readonly User m_WhitePlayer;
private List<Point> m_AvailableMoves = new List<Point>();
private Board m_Board;
private eCoinColor m_ColorPlaying = eCoinColor.White;
public List<Point> AvailableMoves
{
get { return m_AvailableMoves; }
set { m_AvailableMoves = value; }
}
public eCoinColor ColorPlaying
{
get { return m_ColorPlaying; }
set { m_ColorPlaying = value; }
}
public Board Board
{
get { return m_Board; }
set { m_Board = value; }
}
public GameManager()
{
User.UserOption whitePlayerType;
UserInterface.Start(out int o_BoardSize, out GameOption o_GameType, out string o_NameOfBlackPlayer, out string o_NameOfWhitePlayer);
Board = new Board(o_BoardSize);
m_GameType = o_GameType;
UserInterface.DrawBoard(Board);
if (o_GameType == GameOption.UserVsComputer)
{
whitePlayerType = User.UserOption.Computer;
}
else
{
whitePlayerType = User.UserOption.Human;
}
m_BlackPlayer = new User(User.UserOption.Human, eCoinColor.Black, o_NameOfBlackPlayer);
m_WhitePlayer = new User(whitePlayerType, eCoinColor.White, o_NameOfWhitePlayer);
}
public GameManager(GameOption i_GameType, User i_BlackPlayer, User i_WhitePlayer)
{
m_GameType = i_GameType;
m_BlackPlayer = i_BlackPlayer;
m_WhitePlayer = i_WhitePlayer;
}
public GameManager Clone()
{
GameManager clonedGame = new GameManager(m_GameType, m_BlackPlayer, m_WhitePlayer);
clonedGame.m_ColorPlaying = m_ColorPlaying;
clonedGame.Board = Board.Clone();
m_AvailableMoves = CloneAvailableMoves();
return clonedGame;
}
public List<Point> CloneAvailableMoves()
{
List<Point> ClonedAvailableMoves = new List<Point>();
foreach (Point PointToMove in m_AvailableMoves)
{
ClonedAvailableMoves.Add(PointToMove);
}
return ClonedAvailableMoves;
}
public void StartGame()
{
swapTurns();
gameOver(this);
}
private void swapTurns()
{
int countOfSwapTurnWithOutMoves = 0;
while (countOfSwapTurnWithOutMoves != 2)
{
swapCurrentColorPlaying();
GetAvailableMoves();
if (!IsThereAvailableMove())
{
++countOfSwapTurnWithOutMoves;
UserInterface.PrintAnyValidMoves(m_ColorPlaying);
}
else
{
countOfSwapTurnWithOutMoves = 0;
playTurn();
}
}
}
private void playTurn()
{
if (m_GameType == GameOption.UserVsComputer && m_ColorPlaying == m_WhitePlayer.CoinColor)
{
Point cell = Ai.FindMaxMinPoint(this);
Board.MakeMove(cell, ColorPlaying);
UserInterface.DrawBoard(Board);
}
else
{
bool isLegalMove = true;
UserInterface.PrintFormatForTheUser();
UserInterface.PrintUserPlayTurn(m_ColorPlaying, m_BlackPlayer, m_WhitePlayer);
while (isLegalMove)
{
Point cell = UserInterface.GetUserMove(Board, m_ColorPlaying);
if (IsPointInAvailableMoveList(cell))
{
Board.MakeMove(cell, ColorPlaying);
UserInterface.DrawBoard(Board);
isLegalMove = false;
}
else
{
UserInterface.PrintNotBlockCoins();
}
}
}
}
private void gameOver(GameManager i_Game)
{
Board.UpdateScoreOfUser(out int o_SumOfBlackPoints, out int o_SumOfWhitePoints);
if (o_SumOfBlackPoints > o_SumOfWhitePoints)
{
UserInterface.PrintEndOfTheGame(o_SumOfBlackPoints, o_SumOfWhitePoints, m_BlackPlayer, i_Game);
}
else
{
UserInterface.PrintEndOfTheGame(o_SumOfBlackPoints, o_SumOfWhitePoints, m_WhitePlayer, i_Game);
}
startNewRound();
}
private void swapCurrentColorPlaying()
{
m_ColorPlaying = Board.SwapColor(m_ColorPlaying);
}
private void startNewRound()
{
if (UserInterface.IfTheUserWantNewGame())
{
Board.InitBoard();
UserInterface.DrawBoard(Board);
Board.SwapColor(eCoinColor.Black);
StartGame();
}
else
{
System.Environment.Exit(1);
}
}
public void GetScoreOfUsers(out int o_SumOfBlackPoints, out int o_SumOfWhitePoints)
{
Board.UpdateScoreOfUser(out o_SumOfBlackPoints, out o_SumOfWhitePoints);
}
public void GetAvailableMoves()
{
m_AvailableMoves.Clear();
Point cell = new Point(0, 0);
for (cell.X = 0; cell.X < Board.SizeOfBoard; ++cell.X)
{
for (cell.Y = 0; cell.Y < Board.SizeOfBoard; ++cell.Y)
{
if (IsPointIsAvailableMove(cell))
{
m_AvailableMoves.Add(cell);
}
}
}
}
public bool IsThereAvailableMove()
{
return m_AvailableMoves.Count != 0;
}
public bool IsPointInAvailableMoveList(Point i_PointToCheck)
{
return m_AvailableMoves.Contains(i_PointToCheck);
}
public bool IsPointIsAvailableMove(Point i_PointToCheck)
{
bool isFoundAvailableMove = false;
if (Board.IsEmptyCell(i_PointToCheck))
{
for (int x = -1; x < 2 && !isFoundAvailableMove; ++x)
{
for (int y = -1; y < 2 && !isFoundAvailableMove; ++y)
{
isFoundAvailableMove = Board.IsLegalDirection(m_ColorPlaying, i_PointToCheck, x, y);
}
}
}
return isFoundAvailableMove;
}
}
}