-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
235 lines (188 loc) · 7.12 KB
/
MainWindow.xaml.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Animation;
using System.Numerics;
using System.Runtime;
namespace TicTacToe
{
public partial class MainWindow : Window
{
private readonly Dictionary<Player, ImageSource> imageSources = new Dictionary<Player, ImageSource>
{
{ Player.X , new BitmapImage(new Uri("pack://application:,,,/Assets/X15.png")) },
{ Player.O , new BitmapImage(new Uri("pack://application:,,,/Assets/O15.png")) },
{ Player.None, null }
};
private readonly Dictionary<Player, ObjectAnimationUsingKeyFrames> animations = new Dictionary<Player, ObjectAnimationUsingKeyFrames>
{
{ Player.X , new ObjectAnimationUsingKeyFrames() },
{ Player.O , new ObjectAnimationUsingKeyFrames() },
};
private readonly Image[,] imageControlsArry = new Image[3, 3];
private readonly GameLogic gameLogic = new GameLogic();
public MainWindow()
{
InitializeComponent();
SetUpGameGrid();
SetUpAnimations();
OnGameRestart();
}
private void SetUpAnimations()
{
animations[Player.X].Duration = TimeSpan.FromSeconds(.25);
animations[Player.O].Duration = TimeSpan.FromSeconds(.25);
for (int i = 0; i < 16; i++)
{
Uri xUri = new Uri($"pack://application:,,,/Assets/X{i}.png");
BitmapImage xImg = new BitmapImage(xUri);
DiscreteObjectKeyFrame xKeyFrame = new DiscreteObjectKeyFrame(xImg);
animations[Player.X].KeyFrames.Add(xKeyFrame);
Uri oUri = new Uri($"pack://application:,,,/Assets/O{i}.png");
BitmapImage oImg = new BitmapImage(oUri);
DiscreteObjectKeyFrame oKeyFrame = new DiscreteObjectKeyFrame(oImg);
animations[Player.O].KeyFrames.Add(oKeyFrame);
}
}
private async void GameFlow(int r, int c)
{
if (gameLogic.PlayerMakeMove(r, c) && !gameLogic.GameOver)
{
OnMoveMade(r, c);
await Task.Delay(400);
gameLogic.CoMakeMove();
if (!gameLogic.GameOver)
{
OnMoveMade(gameLogic.CoRow, gameLogic.CoColumn);
}
else
{
OnMoveMade(gameLogic.CoRow, gameLogic.CoColumn);
OnGameEnded(gameLogic.GetGameResult());
}
}
else if (gameLogic.GameOver)
{
OnMoveMade(r, c);
OnGameEnded(gameLogic.GetGameResult());
}
}
private void GameGrid_Click(object sender, MouseButtonEventArgs e)
{
if (gameLogic.CurrentPlayer != Player.O) { return; }
double squareSize = GameGrid.Width / 3;
Point clickPosition = e.GetPosition(GameGrid);
int row = (int)(clickPosition.Y / squareSize);
int col = (int)(clickPosition.X / squareSize);
GameFlow(row, col);
}
private (Point, Point) FindLinePoints(WinInfo winInfo)
{
double squareSize = GameGrid.Width / 3;
double margin = squareSize / 2;
if (winInfo.WinTypeKey == WinInfo.WinType.row)
{
double y = winInfo.WinTypeNum * squareSize + margin;
return (new Point(0, y), new Point(GameGrid.Width, y));
}
if (winInfo.WinTypeKey == WinInfo.WinType.col)
{
double x = winInfo.WinTypeNum * squareSize + margin;
return (new Point(x, 0), new Point(x, GameGrid.Height));
}
if (winInfo.WinTypeKey == WinInfo.WinType.mainDiag)
{
return (new Point(0, 0), new Point(GameGrid.Width, GameGrid.Height));
}
return (new Point(GameGrid.Width, 0), new Point(0, GameGrid.Height));
}
private void ShowLine(WinInfo winInfo)
{
(Point start, Point end) = FindLinePoints(winInfo);
Line.X1 = start.X;
Line.Y1 = start.Y;
Line.X2 = end.X;
Line.Y2 = end.Y;
Line.Visibility = Visibility.Visible;
}
private void OnMoveMade(int r, int c)
{
Player player = gameLogic.GameGrid[r, c];
imageControlsArry[r, c].BeginAnimation(Image.SourceProperty, animations[player]);
PlayerImage.Source = imageSources[gameLogic.CurrentPlayer];
}
private async void OnGameEnded(GameResult gameResult)
{
await Task.Delay(800);
if (gameResult.GameWinner == Player.None)
{
TransitionToEndScreen("it's a draw", null);
}
else
{
ShowLine(gameResult.WinInfo);
await Task.Delay(1000);
TransitionToEndScreen("The Winner: ", imageSources[gameResult.GameWinner]);
}
}
private async void OnGameRestart()
{
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
imageControlsArry[r, c].BeginAnimation(Image.SourceProperty, null);
imageControlsArry[r, c].Source = null;
}
}
PlayerImage.Source = imageSources[gameLogic.CurrentPlayer];
TransitionToGameScreen();
gameLogic.CoMakeMove();
await Task.Delay(80);
OnMoveMade(gameLogic.CoRow, gameLogic.CoColumn);
}
private void SetUpGameGrid()
{
for (int r = 0; r < 3; r++)
{
for (int c = 0; c < 3; c++)
{
Image imageControl = new Image();
GameGrid.Children.Add(imageControl);
imageControlsArry[r, c] = imageControl;
}
}
}
private void TransitionToEndScreen(string text, ImageSource winnerImage)
{
TurnPanel.Visibility = Visibility.Hidden;
GameCanvas.Visibility = Visibility.Hidden;
ResultText.Text = text;
WinnerImage.Source = winnerImage;
EndScreen.Visibility = Visibility.Visible;
}
private void TransitionToGameScreen()
{
Line.Visibility = Visibility.Hidden;
EndScreen.Visibility = Visibility.Hidden;
TurnPanel.Visibility = Visibility.Visible;
GameCanvas.Visibility = Visibility.Visible;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
gameLogic.Reset();
OnGameRestart();
}
}
}