-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardForm.cs
238 lines (215 loc) · 8.22 KB
/
BoardForm.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
234
235
236
237
238
using System;
using System.Windows.Forms;
using System.Drawing;
namespace TicTac
{
public class BoardForm : Form
{
private MyButton[,] m_ButtonsMatrix;
private Label m_Player1Name;
private Label m_Player1Score;
private Label m_Player2Name;
private Label m_Player2Score;
private Game m_Game;
public BoardForm()
{
this.CenterToScreen();
this.Text = "TicTacToeMisere";
this.AutoSize = true;
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
}
protected override void OnLoad(EventArgs i_E)
{
SettingForm settingsForm = new SettingForm();
settingsForm.ShowDialog();
initializeComponents(settingsForm);
base.OnLoad(i_E);
}
private void initializeComponents(SettingForm i_SettingsForm)
{
int size = i_SettingsForm.BoardSize;
m_ButtonsMatrix = new MyButton[size, size];
eGameType gameType = getGameType(i_SettingsForm);
m_Game = new Game(size, gameType);
for (int row = 0; row < size; row++)
{
for (int col = 0; col < size; col++)
{
m_ButtonsMatrix[row, col] = new MyButton();
{
m_ButtonsMatrix[row, col].Size = new Size(70, 70);
m_Game.GetBoardObj(row, col).UpdateCell += BoardForm_UpdateCell;
m_ButtonsMatrix[row, col].TabStop = false;
m_ButtonsMatrix[row, col].Row = row;
m_ButtonsMatrix[row, col].Col = col;
m_ButtonsMatrix[row, col].Left = col * 70;
m_ButtonsMatrix[row, col].Top = row * 70;
m_ButtonsMatrix[row, col].Click += NewButton_Click;
this.Controls.Add(m_ButtonsMatrix[row, col]);
}
}
}
m_Player1Name = new Label();
{
m_Player1Name.Font = new Font(Label.DefaultFont, FontStyle.Bold);
m_Player1Name.AutoSize = true;
m_Player1Name.Text = i_SettingsForm.player1Name;
m_Player1Name.Location = m_ButtonsMatrix[size - 1, size - 1].Location;
m_Player1Name.Left = this.Width / 2 - 60;
m_Player1Name.Top += 70;
this.Controls.Add(m_Player1Name);
}
m_Player1Score = new Label();
{
m_Player1Score.AutoSize = true;
m_Player1Score.Text = "0";
m_Player1Score.Location = m_Player1Name.Location;
m_Player1Score.Left = m_Player1Name.Right;
this.Controls.Add(m_Player1Score);
}
m_Player2Name = new Label();
{
m_Player2Name.Font = new Font(Label.DefaultFont, FontStyle.Bold);
m_Player2Name.AutoSize = true;
m_Player2Name.Location = m_ButtonsMatrix[size - 1, size - 1].Location;
m_Player2Name.Left = m_Player1Score.Right + 5;
m_Player2Name.Top = m_Player1Name.Top;
setPlayer2Name(i_SettingsForm);
this.Controls.Add(m_Player2Name);
}
m_Player2Score = new Label();
{
m_Player2Score.AutoSize = true;
m_Player2Score.Text = "0";
m_Player2Score.Location = m_Player2Name.Location;
m_Player2Score.Left = m_Player2Name.Right;
this.Controls.Add(m_Player2Score);
}
}
private void BoardForm_UpdateCell(object i_Sender, EventArgs e)
{
BoardObject cell = i_Sender as BoardObject;
int row = cell.Row;
int col = cell.Col;
ePlayerSign sign = cell.Sign;
if (sign == ePlayerSign.Player1)
{
m_ButtonsMatrix[row, col].Text = "X";
}
else if (sign == ePlayerSign.Player2)
{
m_ButtonsMatrix[row, col].Text = "O";
}
m_ButtonsMatrix[row, col].Enabled = false;
}
private eGameType getGameType(SettingForm i_SettingsForm)
{
eGameType gameType;
if (i_SettingsForm.player2Name == "[Computer]")
{
gameType = eGameType.AgainstComputer;
}
else
{
gameType = eGameType.TwoPlayersGame;
}
return gameType;
}
private void setPlayer2Name(SettingForm i_SettingsForm)
{
if (i_SettingsForm.player2Name == "[Computer]")
{
m_Player2Name.Text = "Computer";
}
else
{
m_Player2Name.Text = i_SettingsForm.player2Name;
}
}
public void RestBoardForm()
{
foreach (MyButton current in m_ButtonsMatrix)
{
current.Text = " ";
current.Enabled = true;
}
}
private void NewButton_Click(object i_Sender, EventArgs e)
{
MyButton theSender = i_Sender as MyButton;
int buttonRow = theSender.Row;
int buttonCol = theSender.Col;
m_Game.playMove(buttonRow, buttonCol);
m_Game.SetNextPlayer();
CheckGameConditions();
if (m_Game.GameType == eGameType.AgainstComputer)
{
m_Game.computerNextMove();
m_Game.SetNextPlayer();
}
}
private void CheckGameConditions()
{
ePlayerSign lastPlayed;
bool tieFlag;
bool finishGame = m_Game.CheckWinner(out lastPlayed, out tieFlag);
if (finishGame && !tieFlag)
{
displayWinner(lastPlayed);
}
if (tieFlag)
{
MessageBox.Show("Its a tie !");
}
if (finishGame)
{
DialogResult result = MessageBox.Show($" Would you like to play another round ?", "Game Message:", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
restGame();
}
else
{
endSessions();
}
}
}
private void displayWinner(ePlayerSign i_LastPlayed)
{
if (i_LastPlayed == ePlayerSign.Player1)
{
MessageBox.Show(m_Player1Name.Text + " has Won !!");
m_Player1Score.Text = (int.Parse(m_Player1Score.Text) + 1).ToString();
}
else if (i_LastPlayed == ePlayerSign.Player2)
{
MessageBox.Show(m_Player2Name.Text + " has Won !!");
m_Player2Score.Text = (int.Parse(m_Player2Score.Text) + 1).ToString();
}
}
// $G$ CSS-999 (-3) missing blank lines
private void restGame()
{
RestBoardForm();
m_Game.initBoard();
}
private void endSessions()
{
int player1Score = int.Parse(m_Player1Score.Text);
int player2Score = int.Parse(m_Player2Score.Text);
if (player1Score > player2Score)
{
MessageBox.Show(m_Player1Name.Text + " is the winner of the session !!", "GoodBye Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (player1Score == player2Score)
{
MessageBox.Show("Its a tie !", "GoodBye Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show(m_Player2Name.Text + " is the winner of the session !!", "GoodBye Message:", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
this.Close();
}
}
}