-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cs
205 lines (192 loc) · 6.36 KB
/
Game.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
namespace TicTac
{
// $G$ CSS-999 (-7) coding standarts not kept as required
using System;
public class Game
{
private readonly eGameType m_GameType;
private ePlayerSign m_NextPlayer;
private readonly BoardObject[,] m_Board;
private readonly int m_Size;
private int m_GameCounter = 0;
public Game(int i_BoardSize, eGameType i_GameType)
{
m_NextPlayer = ePlayerSign.Player1;
m_Board = new BoardObject[i_BoardSize, i_BoardSize];
m_Size = i_BoardSize;
m_GameType = i_GameType;
}
public void initBoard()
{
foreach (BoardObject current in m_Board)
{
current.Sign = ePlayerSign.None;
}
m_GameCounter = 0;
}
public BoardObject GetBoardObj(int i_Row, int i_Col)
{
var boardObj = m_Board[i_Row, i_Col];
if(boardObj == null)
{
boardObj = new BoardObject(i_Row, i_Col);
m_Board[i_Row, i_Col] = boardObj;
}
return boardObj;
}
public void playMove(int i_Row, int i_Col)
{
m_Board[i_Row, i_Col].Sign = m_NextPlayer;
m_Board[i_Row, i_Col].OnCellUpdate(EventArgs.Empty);
m_GameCounter++;
}
public void SetNextPlayer()
{
if(m_NextPlayer == ePlayerSign.Player1)
{
m_NextPlayer = ePlayerSign.Player2;
}
else
{
m_NextPlayer = ePlayerSign.Player1;
}
}
public void computerNextMove()
{
Random rnd = new Random();
int row = rnd.Next(m_Size);
int col = rnd.Next(m_Size);
while (!isSpotAvialable(row, col))
{
row = rnd.Next(m_Size);
col = rnd.Next(m_Size);
}
m_Board[row, col].Sign = ePlayerSign.Player2;
m_Board[row, col].OnCellUpdate(EventArgs.Empty);
m_GameCounter++;
}
public bool isSpotAvialable(int i_Row, int i_Col)
{
bool isAvailable = false;
if (m_Board[i_Row, i_Col].Sign == ePlayerSign.None)
{
isAvailable = true;
}
return isAvailable;
}
public bool CheckWinner(out ePlayerSign o_LastPlayed, out bool o_TieFlag)
{
o_LastPlayed = getLastPlayer();
bool therIsAWin = false;
o_TieFlag = false;
if (CheckWinnerCols(o_LastPlayed) || CheckWinnerRow(o_LastPlayed)
|| CheckWinnerDiagDec(o_LastPlayed) || CheckWinnerDiagInc(o_LastPlayed))
{
therIsAWin = true;
}
if (m_GameCounter == m_Size * m_Size && !therIsAWin)
{
o_TieFlag = true;
}
return therIsAWin;
}
private bool CheckWinnerCols(ePlayerSign i_PlayerSign)
{
int counter;
bool res = false;
for (int i = 0; i < m_Size; i++)
{
counter = 0;
for (int j = 0; j < m_Size; j++)
{
if (m_Board[i, j].Sign == i_PlayerSign)
{
counter++;
}
else
{ counter = 0; }
}
if (counter == m_Size)
{
res = true;
}
}
return res;
}
private bool CheckWinnerRow(ePlayerSign i_PlayerSign)
{
int counter = 0;
bool res = false;
for (int i = 0; i < m_Size; i++)
{
for (int j = 0; j < m_Size; j++)
{
if (m_Board[j, i].Sign == i_PlayerSign)
{
counter++;
}
else
{ counter = 0; }
}
if (counter == m_Size)
{
res = true;
}
}
return res;
}
private bool CheckWinnerDiagDec(ePlayerSign i_PlayerSign)
{
int counter = 0;
int index = 0;
bool res = false;
for (; index < m_Size; ++index)
{
if (m_Board[index, index].Sign == i_PlayerSign)
{
counter++;
}
}
if (counter == m_Size)
{
res = true;
}
return res;
}
private bool CheckWinnerDiagInc(ePlayerSign i_PlayerSign)
{
int rowC = 0;
int colC = m_Size-1;
int counter = 0;
bool res = false;
for (; rowC < m_Size; rowC++, colC--)
{
if (m_Board[rowC, colC].Sign == i_PlayerSign)
{
counter++;
}
}
if (counter == m_Size)
{
res = true;
}
return res;
}
private ePlayerSign getLastPlayer()
{
if(m_NextPlayer == ePlayerSign.Player1)
{
return ePlayerSign.Player2;
}
else
{
return ePlayerSign.Player1;
}
}
public eGameType GameType
{
get { return m_GameType; }
}
}
}