-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cs
115 lines (100 loc) · 3.8 KB
/
Board.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
using Microsoft.Xna.Framework;
using Newtonsoft.Json;
namespace ShogiClient
{
/// <summary>
/// The state and data used for DrawableBoard.
/// </summary>
public class Board
{
public Grid<PieceData> Data { get; private set; }
public PieceData HeldPiece { get; set; }
public Vector2 HeldPiecePosition { get; set; } = new Vector2(0, 0);
// If null, that means it's taken from the hand
public Point? HeldPiecePickUpPosition { get; set; } = null;
[JsonIgnore]
public GameplayScreenState GameplayState { get; set; }
/// <summary>
/// Removes a piece from the board and puts it in the HeldPiece property.
/// </summary>
public bool PickUpPiece(Point point, bool isPlayerOne)
{
var piece = Data.GetAt(point).Data;
if (piece == null || piece.IsPlayerOne != isPlayerOne)
return false;
Data.SetAt(point, null);
HeldPiece = piece;
return true;
}
/// <summary>
/// Removes a piece from the HeldPiece property and puts it on the board.
/// </summary>
public bool PlacePiece(Point from, Point target, out PieceData captured)
{
captured = null;
if (!Utils.ValidMovesForPiece(new GridRef<PieceData> { Data = HeldPiece, Position = from }, Data, GameplayState.CurrentPlayer, GameplayState.OpponentPlayer).Contains(target))
{
return false;
}
var piece = Data.GetAt(target).Data;
if (piece != null)
{
captured = piece;
}
PlacePiece(target);
return true;
}
/// <summary>
/// Removes a piece from the hand and puts it on the board.
/// </summary>
public bool PlacePieceFromHand(Point target)
{
if (!Utils.ValidPositionsForPieceDrop(HeldPiece, Data, GameplayState.CurrentPlayer, GameplayState.OpponentPlayer).Contains(target))
{
return false;
}
var piece = Data.GetAt(target);
PlacePiece(target);
return true;
}
public void PlacePiece(Point point)
{
Data.SetAt(point, HeldPiece);
HeldPiece = null;
}
public Board(int width, int height)
{
Data = new Grid<PieceData>(width, height);
// The variable is ordered from bottom-up but arrays are accessed from top-to-bottom so it needs to be read in reverse
// it makes more sense to this for readability purposes
var setup = new string[] {
"ppppppppp",
" b r ",
"lnsgkgsnl"
};
for (int y = 0; y < setup.Length; y++)
{
for (int x = 0; x < setup[0].Length; x++)
{
// Read in reverse, bottom up
var c = setup[setup.Length - y - 1][x];
if (Utils.PieceNotationToPieceType(c) is (PieceType, bool) type)
{
Data.SetAt(Data.Width - x - 1, y, new PieceData()
{
Type = type.type,
Promoted = type.promoted && Utils.CanPromotePieceType(type.type),
IsPlayerOne = false,
});
Data.SetAt(x, Data.Height - y - 1, new PieceData()
{
Type = type.type,
Promoted = type.promoted && Utils.CanPromotePieceType(type.type),
IsPlayerOne = true,
});
}
}
}
}
}
}