-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardRecorder.cs
92 lines (77 loc) · 2.61 KB
/
BoardRecorder.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
namespace Renju.Core
{
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Infrastructure;
using Infrastructure.Model;
using Prism.Commands;
public class BoardRecorder : ModelBase
{
private readonly IGameBoard<IReadOnlyBoardPoint> _board;
private readonly List<PieceDrop> _undoDrops = new List<PieceDrop>();
private readonly List<PieceDrop> _redoDrops = new List<PieceDrop>();
public BoardRecorder(IGameBoard<IReadOnlyBoardPoint> board)
{
_board = board;
_board.PieceDropped += OnBoardPieceDropped;
this.PropertyChanged += OnBoardRecorderPropertyChanged;
UndoCommand = new DelegateCommand(() => UndoDrop(), () => CanUndo);
RedoCommand = new DelegateCommand(() => RedoDrop(), () => CanRedo);
}
public DelegateCommand UndoCommand { get; private set; }
public DelegateCommand RedoCommand { get; private set; }
public IEnumerable<PieceDrop> Drops
{
get { return _undoDrops; }
}
public IEnumerable<PieceDrop> RedoDrops
{
get { return _redoDrops; }
}
public bool CanUndo
{
get { return _undoDrops.Count > 0; }
}
public bool CanRedo
{
get { return _redoDrops.Count > 0; }
}
public void ClearGameBoard()
{
while (CanUndo)
UndoDrop();
}
public void UndoDrop()
{
Debug.Assert(CanUndo, "There is no drop to undo.");
var drop = _undoDrops.Last();
_undoDrops.RemoveAt(_undoDrops.Count - 1);
_board.Take(drop);
_redoDrops.Add(drop);
OnPropertyChanged(() => CanUndo);
OnPropertyChanged(() => CanRedo);
}
public void RedoDrop()
{
Debug.Assert(CanRedo, "There is no drop to redo.");
var drop = _redoDrops.Last();
_redoDrops.RemoveAt(_redoDrops.Count - 1);
_board.Drop(drop, OperatorType.UndoOrRedo);
}
private void OnBoardPieceDropped(object sender, PieceDropEventArgs e)
{
_undoDrops.Add(e.Drop);
OnPropertyChanged(() => CanUndo);
}
private void OnBoardRecorderPropertyChanged(object sender, PropertyChangedEventArgs e)
{
RunInDispatcher(() =>
{
UndoCommand.RaiseCanExecuteChanged();
RedoCommand.RaiseCanExecuteChanged();
});
}
}
}