-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVirtualGameBoard.cs
108 lines (89 loc) · 3.46 KB
/
VirtualGameBoard.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
namespace Renju.Core
{
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using Debugging;
using Infrastructure;
using Infrastructure.Events;
using Infrastructure.Model;
using Infrastructure.Model.Extensions;
[Serializable]
[DebuggerVisualizer(typeof(RenjuBoardVisualizer))]
public class VirtualGameBoard<TPoint> : ModelBase, IReadBoardState<TPoint>
where TPoint : IReadOnlyBoardPoint
{
private readonly ObservableCollection<PieceLine> _lines = new ObservableCollection<PieceLine>();
private readonly LinkedList<TPoint> _droppedPoints = new LinkedList<TPoint>();
private readonly List<TPoint> _points;
public VirtualGameBoard(int size, Func<int, TPoint> createPoint)
{
Debug.Assert(size > 0, nameof(size) + " must be positive.");
Debug.Assert(createPoint != null);
Size = size;
_points = new List<TPoint>(Enumerable.Range(0, size * size).Select(createPoint));
}
[field: NonSerialized]
public event EventHandler<GenericEventArgs<NewGameOptions>> Begin;
[field: NonSerialized]
public virtual event EventHandler<PieceDropEventArgs> PieceDropped;
[field: NonSerialized]
public virtual event EventHandler<BoardPosition> Taken;
public virtual IEnumerable<TPoint> DroppedPoints
{
get { return _droppedPoints; }
}
public virtual int DropsCount
{
get { return _droppedPoints.Count; }
}
public virtual IEnumerable<TPoint> Points
{
get { return _points; }
}
public virtual IEnumerable<PieceLine> Lines
{
get { return _lines; }
}
public string VisualBoard
{
get { return this.GetLiternalPresentation(); }
}
public IGameRuleEngine RuleEngine { get; protected set; }
public int Size { get; protected set; }
public TPoint this[BoardPosition position]
{
get { return GetPoint(position); }
}
protected internal virtual void UpdateLines(IEnumerable<PieceLine> lines)
{
_lines.Clear();
_lines.AddRange(lines);
}
protected virtual void RaiseGameBeginEvent(NewGameOptions options)
{
RaiseEvent(Begin, new GenericEventArgs<NewGameOptions>(options));
}
protected virtual void RaisePieceTakenEvent(BoardPosition position)
{
var point = GetPoint(position);
_droppedPoints.RemoveLast();
UpdateLines(((IReadBoardState<IReadOnlyBoardPoint>)this).FindAllLinesOnBoardWithoutPoint(point).ToList());
RaiseEvent(Taken, position);
}
protected virtual void RaisePeiceDroppedEvent(PieceDrop drop, OperatorType operatorType)
{
var point = GetPoint(drop);
_droppedPoints.AddLast(point);
((IReadBoardState<IReadOnlyBoardPoint>)this).InvalidateNearbyPointsOf(point);
UpdateLines(((IReadBoardState<IReadOnlyBoardPoint>)this).FindAllLinesOnBoardWithNewPoint(point).ToList());
RaiseEvent(PieceDropped, new PieceDropEventArgs(drop, operatorType));
}
protected virtual TPoint GetPoint(BoardPosition position)
{
return _points[position.Y * Size + position.X];
}
}
}