-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cs
More file actions
267 lines (233 loc) · 9 KB
/
Game.cs
File metadata and controls
267 lines (233 loc) · 9 KB
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tic_Tac_Toe
{
public partial class Game : Form
{
private bool PlayerTurn { get; set; } // if true, its the player's turn, else it is the computer's turn
private int Turn_count { get; set; }
private Button[] Buttons { get; set; }
private string[] Board { get; set; } // an array of all the different values in each button where each can either be "X", "O", or ""
private int WinStreak { get; set; }
private int Wins { get; set; }
private int Losses { get; set; }
private int Draws { get; set; }
private readonly Random random = new();
private int StarterBool { get; set; }
public Game()
{
InitializeComponent();
Buttons = new Button[] { A1, A2, A3, B1, B2, B3, C1, C2, C3 };
Board = new string[] { "", "", "", "", "", "", "", "", "" };
}
private void Game_Load(object sender, EventArgs e)
{
GameReset();
}
private void SetAllButtonsDisabled()
{
foreach (Button button in Buttons) button.Enabled = false;
}
private void GameReset()
{
foreach (Button button in Buttons) button.Text = "";
for (int i = 0; i < Board.Length; i++) Board[i] = "";
SetAllButtonsDisabled();
Turn_count = 0;
StarterBool = random.Next(0, 2);
btnPlayAgain.Enabled = false;
btnPlayAgain.BackColor = Color.FromArgb(71, 99, 74);
lblWinLose.Text = "";
if (StarterBool == 1)
{
PlayerTurn = true;
foreach (Button button in Buttons) button.Enabled = true;
}
else
{
PlayerTurn = false;
AIClick();
}
}
private void BtnGame_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
SetAllButtonsDisabled();
button.Text = StarterBool == 1 && PlayerTurn
? "X" : StarterBool == 0 && !PlayerTurn
? "X" : "O";
for (int i = 0; i < Buttons.Length; i++)
if (Buttons[i].Name == button.Name) Board[i] = button.Text;
Turn_count++;
PlayerTurn = !PlayerTurn;
string? winner = CheckWinner(button.Text);
if (winner != null) EndGame(winner);
else
{
if (!PlayerTurn) AIClick();
else foreach (Button buttonGame in Buttons) if (buttonGame.Text == "") buttonGame.Enabled = true;
}
}
private string? CheckWinner(string XorO)
{
string? winner = null;
// ---
if ((Board[0] == Board[1] && Board[1] == Board[2] && Board[1] != "") ||
(Board[3] == Board[4] && Board[4] == Board[5] && Board[4] != "") ||
(Board[6] == Board[7] && Board[7] == Board[8] && Board[7] != ""))
winner = XorO;
// |||
else if ((Board[0] == Board[3] && Board[3] == Board[6] && Board[3] != "") ||
(Board[1] == Board[4] && Board[4] == Board[7] && Board[4] != "") ||
(Board[2] == Board[5] && Board[5] == Board[8] && Board[5] != ""))
winner = XorO;
// X
else if ((Board[0] == Board[4] && Board[4] == Board[8] && Board[4] != "") ||
(Board[2] == Board[4] && Board[4] == Board[6] && Board[4] != ""))
winner = XorO;
if (winner == null && Turn_count == 9) return "tie";
int temp_turn_count = 0;
foreach (string text in Board) if (text != "") temp_turn_count++;
if (winner == null && temp_turn_count == 9) return "tie";
return winner;
}
private void EndGame(string? winner)
{
SetAllButtonsDisabled();
switch (StarterBool)
{
case 1 when winner == "X":
case 0 when winner == "O":
WinStreak++;
Wins++;
lblWins.Text = "Wins: " + Wins;
lblWinStreak.Text = "Win Streak: " + WinStreak;
lblWinLose.Text = "You Win!";
break;
case 1 when winner == "O":
case 0 when winner == "X":
Losses++;
WinStreak = 0;
lblLosses.Text = "Losses: " + Losses;
lblWinStreak.Text = "Win Streak: " + WinStreak;
lblWinLose.Text = "You Lose!";
break;
default:
Draws++;
WinStreak = 0;
lblDraws.Text = "Draws: " + Draws;
lblWinStreak.Text = "Win Streak: " + WinStreak;
lblWinLose.Text = "Draw!";
break;
}
PlayButtonShow();
}
private void PlayButtonShow()
{
btnPlayAgain.Enabled = true;
btnPlayAgain.BackColor = Color.FromArgb(192, 255, 192);
}
int nodeIndex = 0; // The number of nodes being traversed in the algorithm
private void AIClick()
{
nodeIndex = 0;
Stopwatch stopwatch = Stopwatch.StartNew();
int bestScore = StarterBool == 0 ? -1000 : 1000;
int MIN = -1000;
int MAX = 1000;
Button bestNextMove = new();
for (int i = 0; i < Board.Length; i++)
{
if (Board[i] == "")
{
nodeIndex++;
Board[i] = StarterBool == 0 ? "X" : "O";
int score = Minimax(Board, Board[i], 0, StarterBool == 1, MIN, MAX);
Board[i] = "";
if (StarterBool == 0 && score > bestScore)
{
bestScore = score;
bestNextMove = Buttons[i];
}
else if (StarterBool == 1 && score < bestScore)
{
bestScore = score;
bestNextMove = Buttons[i];
}
}
}
stopwatch.Stop();
Debug.WriteLine($"Number of nodes that have been traversed: {nodeIndex}, taking {stopwatch.ElapsedMilliseconds}ms to complete");
bestNextMove.Enabled = true;
bestNextMove.PerformClick();
}
private readonly Dictionary<string, int> scores = new()
{
{ "X", 1 },
{ "O", -1 },
{ "tie", 0 },
};
private int Minimax(string[] board, string latestXorO, int depth, bool isMaximising, int alpha, int beta)
{
string? result = CheckWinner(latestXorO);
if (result != null)
{
return scores[result];
}
if (isMaximising)
{
int bestScore = -1000; // Low number due to algorithm trying to find the highest possible number
for (int i = 0; i < board.Length; i++)
{
if (board[i] == "")
{
nodeIndex++;
board[i] = latestXorO == "X" ? "O" : "X";
int score = Minimax(board, board[i], depth + 1, false, alpha, beta);
board[i] = "";
bestScore = Math.Max(score, bestScore);
alpha = Math.Max(bestScore, alpha);
if (beta <= alpha) break;
}
}
return bestScore;
}
else
{
int bestScore = 1000; // High number due to algorithm trying to find the lowest possible number
for (int i = 0; i < board.Length; i++)
{
if (board[i] == "")
{
nodeIndex++;
board[i] = latestXorO == "X" ? "O" : "X";
int score = Minimax(board, board[i], depth + 1, true, alpha, beta);
board[i] = "";
bestScore = Math.Min(score, bestScore);
beta = Math.Min(bestScore, beta);
if (beta <= alpha) break;
}
}
return bestScore;
}
}
private void BtnClose_Click(object sender, EventArgs e)
{
Process.GetCurrentProcess().Kill();
}
private void BtnPlayAgain_Click(object sender, EventArgs e)
{
GameReset();
}
}
}