-
Notifications
You must be signed in to change notification settings - Fork 5k
/
MainWindow.xaml.cs
573 lines (493 loc) · 21.8 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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Media3D;
namespace Raytracer.Wpf
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private const double CYLINDER_HEIGHT = 3.0;
private const double CYLINDER_WIDTH = 5.0;
private readonly Color _darkColor = Color.FromRgb(30, 30, 30);
private readonly Color _lightColor = Colors.White;
private readonly ReversiGame _gameEngine;
private readonly MinimaxSpot[,] _guiBoard;
private Task _aiUiTask;
private CancellationTokenSource _aiUiCts;
private readonly TaskScheduler _uiScheduler;
private bool _isGameOver, _isAiMoving, _isAiParallel, _isAuto;
private Duration _progressBarDuration;
private DoubleAnimation _progressBarAnimation;
private bool _useAnimation;
public MainWindow()
{
InitializeComponent();
_gamePieces = new Dictionary<Point, Cylinder>();
_ghostPieces = new Dictionary<Cylinder, Point>();
NameScope.SetNameScope(mainViewport, new NameScope());
ui_dopSlider.Minimum = 1.0;
ui_dopSlider.Maximum = Environment.ProcessorCount * 2;
ui_dopSlider.Value = Environment.ProcessorCount;
ui_depthSlider.Minimum = 8.0;
ui_depthSlider.Maximum = 24.0;
ui_depthSlider.Value = 18.0;
ui_timeoutSlider.Minimum = 1.0;
ui_timeoutSlider.Maximum = 60.0;
ui_timeoutSlider.Value = 8.0;
_useAnimation = true;
// Set the UI scheduler
_uiScheduler = TaskScheduler.FromCurrentSynchronizationContext();
// Initialize the game
_gameEngine = new ReversiGame(8, 8);
_isGameOver = false;
_isAiMoving = false;
_isAiParallel = true;
_isAuto = false;
// Initialize the GUI's board
_guiBoard = new MinimaxSpot[8, 8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
_guiBoard[i, j] = MinimaxSpot.Empty;
}
}
UpdateBoard();
}
private void GoAI()
{
if (_isGameOver)
{
return;
}
_isAiMoving = true;
_progressBarAnimation = new DoubleAnimation(0.0, 100.0, _progressBarDuration);
if (_isAiParallel)
{
ui_parProgBar.Visibility = Visibility.Visible;
ui_parProgBar.BeginAnimation(ProgressBar.ValueProperty, _progressBarAnimation, HandoffBehavior.SnapshotAndReplace);
}
else
{
ui_seqProgBar.Visibility = Visibility.Visible;
ui_seqProgBar.BeginAnimation(ProgressBar.ValueProperty, _progressBarAnimation, HandoffBehavior.SnapshotAndReplace);
}
_aiUiCts = new CancellationTokenSource();
_aiUiTask = Task.Factory.StartNew(() =>
{
return _gameEngine.GetAIMove(_isAiParallel);
}, _aiUiCts.Token, TaskCreationOptions.None, TaskScheduler.Default)
.ContinueWith(completedTask =>
{
var aiMove = completedTask.Result;
if (aiMove.Row != -1)
{
_gameEngine.MakeMove(aiMove.Row, aiMove.Col);
}
else
{
_gameEngine.PassMove();
}
string s;
if (_isAiParallel)
{
s = $"{_gameEngine.MovesConsidered:N}";
s = s.Substring(0, s.Length - 3);
ui_parLabel.Content = s;
ui_parProgBar.Visibility = Visibility.Hidden;
}
else
{
s = $"{_gameEngine.MovesConsidered:N}";
s = s.Substring(0, s.Length - 3);
ui_seqLabel.Content = s;
ui_seqProgBar.Visibility = Visibility.Hidden;
}
UpdateBoard();
_isAiMoving = false;
if (_isAuto)
{
_isAiParallel = !_isAiParallel;
GoAI();
}
}, _aiUiCts.Token, TaskContinuationOptions.None, _uiScheduler);
}
private bool UpdateBoard()
{
if (_isGameOver)
{
return false;
}
MinimaxSpot[,] game = _gameEngine.Board;
MinimaxSpot[,] gui = _guiBoard;
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
if (gui[i, j] == MinimaxSpot.Empty && game[i, j] != MinimaxSpot.Empty)
{
AddPiece(i, j, game[i, j] == MinimaxSpot.Light ? true : false);
}
else if (gui[i, j] == MinimaxSpot.Light && game[i, j] == MinimaxSpot.Dark ||
gui[i, j] == MinimaxSpot.Dark && game[i, j] == MinimaxSpot.Light)
{
FlipPiece(i, j);
}
}
}
// Remove old ghost pieces.
foreach (var c in _ghostPieces)
{
mainViewport.Children.Remove(c.Key);
}
_ghostPieces.Clear();
// Generate new ghost pieces.
var moves = _gameEngine.GetValidMoves();
foreach (var m in moves)
{
ShowGhost(m.Row, m.Col, _gameEngine.IsLightMove);
}
var gameResult = _gameEngine.GetGameResult();
var (isGameOver, message, result) = HandleGameState(gameResult);
if (!result)
{
_isGameOver = isGameOver;
MessageBox.Show(message, "GAME OVER");
}
return result;
static (bool isGameOver, string message, bool result) HandleGameState(
ReversiGameResult result) =>
result.GameState switch
{
ReversiGameState.LightWon => (true, $"Light Won! {result.NumLightPieces}-{result.NumDarkPieces}", false),
ReversiGameState.DarkWon => (true, $"Dark Won! {result.NumLightPieces}-{result.NumDarkPieces}", false),
ReversiGameState.Draw => (true, $"Draw! {result.NumLightPieces}-{result.NumDarkPieces}", false),
_ => (false, null, true)
};
}
private void OnMainWindowKeyDown(object sender, KeyEventArgs e)
{
var cameraPos = mainCamera.Position;
if (e.Key == Key.Up)
{
cameraPos.Z += 1;
}
if (e.Key == Key.Down)
{
cameraPos.Z -= 1;
}
if (e.Key == Key.T)
{
mainCamera.FieldOfView += 10;
}
if (e.Key == Key.G)
{
mainCamera.FieldOfView -= 10;
}
if (e.Key == Key.D)
{
_gameEngine.Dump("");
MessageBox.Show("Dumped.");
}
mainCamera.Position = cameraPos;
}
/// <summary>
/// Collection of pieces on the board.
/// </summary>
private readonly Dictionary<Point, Cylinder> _gamePieces;
private readonly Dictionary<Cylinder, Point> _ghostPieces;
public void AddPiece(int row, int col, bool isPlayerLight)
{
if (_useAnimation)
{
AddPieceAnimated(row, col, isPlayerLight);
}
else
{
AddPieceUnanimated(row, col, isPlayerLight);
}
}
const int CYLINDER_RESOLUTION = 50;
/// <summary>
/// Places a piece on the gameboard by visually "dropping the piece."
/// </summary>
/// <param name="row">The row of the piece to place.</param>
/// <param name="col">The column of the piece to place.</param>
/// <param name="isBlack">If true, the piece color is black; otherwise, it's white.</param>
public void AddPieceAnimated(int row, int col, bool isPlayerLight)
{
// Update the GUI board
_guiBoard[row, col] = isPlayerLight ? MinimaxSpot.Light : MinimaxSpot.Dark;
// Hack to match danny's orientation
row = 7 - row;
// create the piece and place it above the destination square
var newPiece = new Cylinder(CYLINDER_WIDTH, CYLINDER_HEIGHT, CYLINDER_RESOLUTION, isPlayerLight
? new DiffuseMaterial(new SolidColorBrush(_lightColor))
: new DiffuseMaterial(new SolidColorBrush(_darkColor)));
GetViewCoordinates(col, row, out double centerX, out double centerY);
newPiece.MoveTo(new Point3D(centerX, centerY, 20.0));
mainViewport.Children.Add(newPiece);
// animate it's descent
var animationStoryboard = new Storyboard();
var translateTransform = new TranslateTransform3D(
centerX, centerY, 20.0);
var moveDownAnimation = new DoubleAnimation(0, new Duration(TimeSpan.FromMilliseconds(500)));
mainViewport.RegisterName("GamePieceDrop", translateTransform);
Storyboard.SetTargetName(moveDownAnimation, "GamePieceDrop");
Storyboard.SetTargetProperty(moveDownAnimation, new PropertyPath(TranslateTransform3D.OffsetZProperty));
animationStoryboard.Children.Add(moveDownAnimation);
newPiece.Transform = translateTransform;
animationStoryboard.Begin(mainViewport);
mainViewport.UnregisterName("GamePieceDrop");
_gamePieces[new Point(row, col)] = newPiece;
}
/// <summary>
/// Adds a piece to the game board without animation. This method is called when the board is
/// initialized.
/// </summary>
/// <param name="col">The column of the piece to add.</param>
/// <param name="row">The row of the piece to add.</param>
/// <param name="isBlack">If true, the piece color is black; otherwise, it's white.</param>
public void AddPieceUnanimated(int row, int col, bool isPlayerLight)
{
// Update the GUI board
_guiBoard[row, col] = isPlayerLight ? MinimaxSpot.Light : MinimaxSpot.Dark;
// Hack to match danny's orientation
row = 7 - row;
// create the piece and place it above the destination square
var newPiece = new Cylinder(CYLINDER_WIDTH, CYLINDER_HEIGHT, CYLINDER_RESOLUTION, isPlayerLight
? new DiffuseMaterial(new SolidColorBrush(_lightColor))
: new DiffuseMaterial(new SolidColorBrush(_darkColor)));
MovePiece(newPiece, col, row);
_gamePieces[new Point(row, col)] = newPiece;
mainViewport.Children.Add(newPiece);
}
public void FlipPiece(int row, int col)
{
if (_useAnimation)
{
FlipPieceAnimated(row, col);
}
else
{
FlipPieceUnanimated(row, col);
}
}
public void FlipPieceUnanimated(int row, int col)
{
// Update the GUI board
_guiBoard[row, col] = _guiBoard[row, col] == MinimaxSpot.Light ? MinimaxSpot.Dark : MinimaxSpot.Light;
// Hack to match danny's orientation
row = 7 - row;
// get the piece's world coordinates
GetViewCoordinates(col, row, out double centerX, out double centerY);
var gamePiece = _gamePieces[new Point(row, col)];
// get the diffuse material
var diffMaterialBrush = (gamePiece.Material as DiffuseMaterial).Brush as SolidColorBrush;
diffMaterialBrush.Color = diffMaterialBrush.Color.Equals(_lightColor) ? _darkColor : _lightColor;
}
/// <summary>
/// "Flips" the game piece by lifting it, rotating it, and fading it's color from black to white, or vice versa.
/// </summary>
/// <param name="col">The column of the piece to flip.</param>
/// <param name="row">The row of the piece to flip.</param>
public void FlipPieceAnimated(int row, int col)
{
// Update the GUI board
_guiBoard[row, col] = _guiBoard[row, col] == MinimaxSpot.Light ? MinimaxSpot.Dark : MinimaxSpot.Light;
// Hack to match danny's orientation
row = 7 - row;
// get the piece's world coordinates
GetViewCoordinates(col, row, out double centerX, out double centerY);
var gamePiece = _gamePieces[new Point(row, col)];
// define an animation storyboard and a transform group
var animationsStoryboard = new Storyboard();
var transformGroup = new Transform3DGroup();
// setup the lift animation
var translateTransform = new TranslateTransform3D(
centerX, centerY, CYLINDER_HEIGHT / 2.0);
var moveUpAnimation = new DoubleAnimation(10, new Duration(TimeSpan.FromMilliseconds(500)))
{
AutoReverse = true
};
mainViewport.RegisterName("GamePieceMoveUp", translateTransform);
Storyboard.SetTargetName(moveUpAnimation, "GamePieceMoveUp");
Storyboard.SetTargetProperty(moveUpAnimation, new PropertyPath(TranslateTransform3D.OffsetZProperty));
animationsStoryboard.Children.Add(moveUpAnimation);
// setup the rotate animation
var axisAngleRotation = new AxisAngleRotation3D(new Vector3D(0, 1, 0), 0);
var rotateTransform = new RotateTransform3D(axisAngleRotation, new Point3D(-0, 0, CYLINDER_HEIGHT / 2.0))
{
Rotation = axisAngleRotation
};
var flipOverAnimation = new DoubleAnimation(0, 180, new Duration(TimeSpan.FromMilliseconds(1000)));
mainViewport.RegisterName("GamePieceFlipOver", axisAngleRotation);
Storyboard.SetTargetName(flipOverAnimation, "GamePieceFlipOver");
Storyboard.SetTargetProperty(flipOverAnimation, new PropertyPath(AxisAngleRotation3D.AngleProperty));
animationsStoryboard.Children.Add(flipOverAnimation);
// setup the recolor animation
// get the diffuse material
var diffMaterialBrush = (gamePiece.Material as DiffuseMaterial).Brush as SolidColorBrush;
var changeColorAnimation = new ColorAnimation
{
To = diffMaterialBrush.Color.Equals(_lightColor) ? _darkColor : _lightColor,
Duration = TimeSpan.FromMilliseconds(1000)
};
mainViewport.RegisterName("GamePieceChangeColor", diffMaterialBrush);
Storyboard.SetTargetName(changeColorAnimation, "GamePieceChangeColor");
Storyboard.SetTargetProperty(changeColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty));
animationsStoryboard.Children.Add(changeColorAnimation);
// start the animation
// note the transforms are applied in this specific order so that the piece ends up in the right location
transformGroup.Children.Add(rotateTransform);
transformGroup.Children.Add(translateTransform);
gamePiece.Transform = transformGroup;
animationsStoryboard.Begin(mainViewport);
// unregister all animations so they can be performed again
mainViewport.UnregisterName("GamePieceMoveUp");
mainViewport.UnregisterName("GamePieceFlipOver");
mainViewport.UnregisterName("GamePieceChangeColor");
}
public void ShowGhost(int row, int col, bool isPlayerLight)
{
// Hack to match danny's orientation
row = 7 - row;
// create the piece and place it above the destination square
var newPiece = new Cylinder(CYLINDER_WIDTH, CYLINDER_HEIGHT, CYLINDER_RESOLUTION, isPlayerLight
? new DiffuseMaterial(new SolidColorBrush(_lightColor))
: new DiffuseMaterial(new SolidColorBrush(_darkColor)));
GetViewCoordinates(col, row, out double centerX, out double centerY);
// make the piece transparent
var pieceColor = (newPiece.Material as DiffuseMaterial).Color;
pieceColor.A = 70;
(newPiece.Material as DiffuseMaterial).Color = pieceColor;
newPiece.MoveTo(new Point3D(centerX, centerY, 0.0));
mainViewport.Children.Add(newPiece);
_ghostPieces.Add(newPiece, new Point(col, row));
}
// moves a cylinder piece to a place on the gameboard
private void MovePiece(Cylinder piece, int col, int row)
{
GetViewCoordinates(col, row, out double centerX, out double centerY);
piece.MoveTo(new Point3D(centerX, centerY, CYLINDER_HEIGHT / 2.0));
}
private static void GetViewCoordinates(int col, int row, out double centerX, out double centerY)
{
// width of cell * number of row/col - center point of first col/row
centerY = -(row * 10.0 - 35.0);
centerX = col * 10.0 - 35.0;
}
private void OnMainViewPortMouseUp(object sender, MouseButtonEventArgs e)
{
if (_isGameOver)
{
MessageBox.Show("Game is over.", "GAME OVER");
return;
}
if (_isAiMoving)
{
MessageBox.Show("AI is moving. Wait for your opponent.", "INVALID");
return;
}
var res = GetGhostPiecePosition(e.GetPosition(mainViewport), out Point ghostPos);
if (res)
{
_gameEngine.SetMinimaxKnobs((int)ui_depthSlider.Value, TimeSpan.FromSeconds((int)ui_timeoutSlider.Value), (int)ui_dopSlider.Value);
_progressBarDuration = new Duration(_gameEngine.TimeLimit);
int row = 7 - (int)ghostPos.Y;
int col = (int)ghostPos.X;
if (_gameEngine.MakeMove(row, col))
{
if (UpdateBoard())
{
GoAI();
}
}
else
{
MessageBox.Show("Can't move there. Shouldn't be seeing this message.", "ERROR");
}
}
else
{
MessageBox.Show("You chose to pass.", "USER MOVE");
_gameEngine.PassMove();
if (UpdateBoard())
{
GoAI();
}
}
}
private bool GetGhostPiecePosition(Point mousePosition, out Point ghostPosition)
{
ghostPosition = new Point();
var hitTestResult = VisualTreeHelper.HitTest(mainViewport, mousePosition);
if (hitTestResult.VisualHit is Cylinder)
{
var cylinder = hitTestResult.VisualHit as Cylinder;
if (!_ghostPieces.ContainsKey(cylinder))
{
return false;
}
ghostPosition = _ghostPieces[cylinder];
return true;
}
else
{
return false;
}
}
private void OnStartStopButtonClick(object sender, RoutedEventArgs e)
{
if (!_isAuto)
{
ui_dopSlider.IsEnabled = false;
ui_depthSlider.IsEnabled = false;
ui_timeoutSlider.IsEnabled = false;
ui_startStopButton.Content = "Stop Sequential vs. Parallel";
_gameEngine.SetMinimaxKnobs((int)ui_depthSlider.Value, TimeSpan.FromSeconds((int)ui_timeoutSlider.Value), (int)ui_dopSlider.Value);
_progressBarDuration = new Duration(_gameEngine.TimeLimit);
_isAuto = true;
_isAiParallel = false;
ui_seqPlayerLabel.Content = "Sequential Player";
GoAI();
}
else
{
_gameEngine.Cancel();
_aiUiCts.Cancel();
ui_seqProgBar.Visibility = Visibility.Hidden;
ui_parProgBar.Visibility = Visibility.Hidden;
ui_dopSlider.IsEnabled = true;
ui_depthSlider.IsEnabled = true;
ui_timeoutSlider.IsEnabled = true;
ui_startStopButton.Content = "Start Sequential vs. Parallel";
_isAuto = false;
_isAiMoving = false;
_isAiParallel = true;
ui_seqPlayerLabel.Content = "You";
}
ui_settings.Focus(); // stop the button from blinking due to it having focus
}
private void OnAnimationToggler(object sender, RoutedEventArgs e) => _useAnimation = !_useAnimation;
private void OnMainWindowLoaded(object sender, RoutedEventArgs e)
{
// add piece icons to the player badges
seqPlayerViewport.Children.Add(new Cylinder(CYLINDER_WIDTH, CYLINDER_HEIGHT, CYLINDER_RESOLUTION, new DiffuseMaterial(new SolidColorBrush(_darkColor))));
parPlayerViewport.Children.Add(new Cylinder(CYLINDER_WIDTH, CYLINDER_HEIGHT, CYLINDER_RESOLUTION, new DiffuseMaterial(new SolidColorBrush(_lightColor))));
}
}
}