-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameLogic.cs
196 lines (146 loc) · 4.68 KB
/
GameLogic.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
using TicTacToe;
namespace TicTacToe
{
public class GameLogic
{
public Player[,] GameGrid { get; private set; }
public Player CurrentPlayer { get; private set; }
public int TurnPassed { get; private set; }
public bool GameOver { get; private set; }
public int CoRow { get; private set; }
public int CoColumn { get; private set; }
ComputerPlayer CoAlgorithm;
WinInfo winInfo;
GameResult gameResult;
public GameLogic()
{
CoAlgorithm = new ComputerPlayer();
GameGrid = new Player[3, 3];
winInfo = new WinInfo();
gameResult = new GameResult();
CurrentPlayer = Player.X;
TurnPassed = 0;
GameOver = false;
}
public GameResult GetGameResult()
{
return gameResult;
}
public void Reset()
{
CoAlgorithm = new ComputerPlayer();
GameGrid = new Player[3, 3];
winInfo = new WinInfo();
gameResult = new GameResult();
CurrentPlayer = Player.X;
TurnPassed = 0;
GameOver = false;
}
public void CoMakeMove()
{
(int, int) coordinates = CoAlgorithm.MakeBestMove(GameGrid, TurnPassed);
int row = coordinates.Item1;
int col = coordinates.Item2;
if (!(row == -1 && col == -1))
{
CoRow = row;
CoColumn = col;
GameGrid[row, col] = CurrentPlayer;
TurnPassed++;
}
if (DidMoveEndGame(row, col, ref gameResult))
{
GameOver = true;
}
else
{
SwitchPlayer();
}
}
public bool PlayerMakeMove(int r, int c)
{
if (!CanMakeMove(r, c))
{
return false;
}
GameGrid[r, c] = CurrentPlayer;
TurnPassed++;
if (DidMoveEndGame(r, c, ref gameResult))
{
GameOver = true;
}
else
{
SwitchPlayer();
}
return true;
}
private bool CanMakeMove(int r, int c)
{
return GameGrid[r, c] == Player.None && !GameOver;
}
private bool IsGridFull()
{
return TurnPassed == 9;
}
private void SwitchPlayer()
{
CurrentPlayer = (CurrentPlayer == Player.X ? CurrentPlayer = Player.O : CurrentPlayer = Player.X);
}
private bool AreSquaresMarked((int, int)[] WinningCombo, Player player)
{
foreach ((int r, int c) in WinningCombo)
{
if (GameGrid[r, c] != player)
{
return false;
}
}
return true;
}
private bool DidMoveWin(int r, int c, ref WinInfo winInfo)
{
(int, int)[] row = new[] { (r, 0), (r, 1), (r, 2) };
(int, int)[] col = new[] { (0, c), (1, c), (2, c) };
(int, int)[] mainDiag = new[] { (0, 0), (1, 1), (2, 2) };
(int, int)[] antiDiag = new[] { (0, 2), (1, 1), (2, 0) };
if (AreSquaresMarked(row, CurrentPlayer))
{
winInfo = new WinInfo { Winner = CurrentPlayer, WinTypeNum = r, WinTypeKey = WinInfo.WinType.row };
return true;
}
if (AreSquaresMarked(col, CurrentPlayer))
{
winInfo = new WinInfo { Winner = CurrentPlayer, WinTypeNum = c, WinTypeKey = WinInfo.WinType.col };
return true;
}
if (AreSquaresMarked(mainDiag, CurrentPlayer))
{
winInfo = new WinInfo { Winner = CurrentPlayer, WinTypeKey = WinInfo.WinType.mainDiag };
return true;
}
if (AreSquaresMarked(antiDiag, CurrentPlayer))
{
winInfo = new WinInfo { Winner = CurrentPlayer, WinTypeKey = WinInfo.WinType.antiDiag };
return true;
}
return false;
}
private bool DidMoveEndGame(int r, int c, ref GameResult gameResult)
{
if (DidMoveWin(r, c, ref winInfo))
{
gameResult.WinInfo = winInfo;
gameResult.GameWinner = CurrentPlayer;
return true;
}
if (IsGridFull())
{
gameResult.GameWinner = Player.None;
return true;
}
return false;
}
}
}