-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomGame.xaml.cs
302 lines (276 loc) · 11.4 KB
/
CustomGame.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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Collections.Generic;
namespace Shogi
{
/// <summary>
/// Interaction logic for CustomGame.xaml
/// </summary>
public partial class CustomGame : Window
{
public Pieces.Piece?[,] Board { get; private set; }
public ShogiGame? GeneratedGame { get; private set; }
public bool SenteIsComputer { get; private set; }
public bool GoteIsComputer { get; private set; }
private readonly Settings config;
private readonly Dictionary<Type, int> sentePieceDrops = new()
{
{ typeof(Pieces.GoldGeneral), 0 },
{ typeof(Pieces.SilverGeneral), 0 },
{ typeof(Pieces.Rook), 0 },
{ typeof(Pieces.Bishop), 0 },
{ typeof(Pieces.Knight), 0 },
{ typeof(Pieces.Lance), 0 },
{ typeof(Pieces.Pawn), 0 },
};
private readonly Dictionary<Type, int> gotePieceDrops = new()
{
{ typeof(Pieces.GoldGeneral), 0 },
{ typeof(Pieces.SilverGeneral), 0 },
{ typeof(Pieces.Rook), 0 },
{ typeof(Pieces.Bishop), 0 },
{ typeof(Pieces.Knight), 0 },
{ typeof(Pieces.Lance), 0 },
{ typeof(Pieces.Pawn), 0 },
};
private Pieces.King? senteKing = null;
private Pieces.King? goteKing = null;
private double tileWidth;
private double tileHeight;
public CustomGame(Settings config, bool minishogi)
{
GeneratedGame = null;
this.config = config;
InitializeComponent();
if (minishogi)
{
Board = new Pieces.Piece?[5, 5];
shogiBoardBackground.Visibility = Visibility.Collapsed;
}
else
{
Board = new Pieces.Piece?[9, 9];
miniShogiBoardBackground.Visibility = Visibility.Collapsed;
}
}
public void UpdateBoard()
{
tileWidth = shogiGameCanvas.ActualWidth / Board.GetLength(0);
tileHeight = shogiGameCanvas.ActualHeight / Board.GetLength(1);
shogiGameCanvas.Children.Clear();
for (int x = 0; x < Board.GetLength(0); x++)
{
for (int y = 0; y < Board.GetLength(1); y++)
{
Pieces.Piece? piece = Board[x, y];
if (piece is not null)
{
Image newPiece = new()
{
Source = new BitmapImage(
new Uri($"pack://application:,,,/Pieces/{config.PieceSet}/{(piece.IsSente ? "Sente" : "Gote")}/{piece.Name}.png")),
Width = tileWidth,
Height = tileHeight
};
RenderOptions.SetBitmapScalingMode(newPiece, BitmapScalingMode.HighQuality);
_ = shogiGameCanvas.Children.Add(newPiece);
Canvas.SetBottom(newPiece, y * tileHeight);
Canvas.SetLeft(newPiece, x * tileWidth);
}
}
}
foreach (Grid dropItem in senteDropsPanel.Children)
{
Type pieceType = (Type)dropItem.Tag;
int heldCount = sentePieceDrops[pieceType];
dropItem.Opacity = heldCount == 0 ? 0.55 : 1;
((Label)dropItem.Children[1]).Content = heldCount;
((Image)dropItem.Children[0]).Source = new BitmapImage(new Uri(
$"pack://application:,,,/Pieces/{config.PieceSet}/Sente/{((Image)dropItem.Children[0]).Tag}.png"));
}
foreach (Grid dropItem in goteDropsPanel.Children)
{
Type pieceType = (Type)dropItem.Tag;
int heldCount = gotePieceDrops[pieceType];
dropItem.Opacity = heldCount == 0 ? 0.55 : 1;
((Label)dropItem.Children[1]).Content = heldCount;
((Image)dropItem.Children[0]).Source = new BitmapImage(new Uri(
$"pack://application:,,,/Pieces/{config.PieceSet}/Gote/{((Image)dropItem.Children[0]).Tag}.png"));
}
startButton.IsEnabled = senteKing is not null && goteKing is not null;
}
private void startButton_Click(object sender, RoutedEventArgs e)
{
SenteIsComputer = computerSelectSente.IsChecked ?? false;
GoteIsComputer = computerSelectGote.IsChecked ?? false;
bool currentTurnSente = turnSelectSente.IsChecked ?? false;
GeneratedGame = new ShogiGame(Board, currentTurnSente,
ShogiGame.EndingStates.Contains(BoardAnalysis.DetermineGameState(Board, currentTurnSente)),
new(), new(), new(), sentePieceDrops, gotePieceDrops, new(), null, null);
Close();
}
private void importButton_Click(object sender, RoutedEventArgs e)
{
importOverlay.Visibility = Visibility.Visible;
}
private void Window_MouseUp(object sender, MouseButtonEventArgs e)
{
Point mousePoint = Mouse.GetPosition(shogiGameCanvas);
if (mousePoint.X < 0 || mousePoint.Y < 0
|| mousePoint.X > shogiGameCanvas.ActualWidth || mousePoint.Y > shogiGameCanvas.ActualHeight)
{
return;
}
// Canvas coordinates are relative to top-left, whereas shogi's are from bottom-left, so y is inverted
System.Drawing.Point coord = new((int)(mousePoint.X / tileWidth),
(int)((shogiGameCanvas.ActualHeight - mousePoint.Y) / tileHeight));
if (coord.X < 0 || coord.Y < 0 || coord.X >= Board.GetLength(0) || coord.Y >= Board.GetLength(1))
{
return;
}
if (Board[coord.X, coord.Y] is null)
{
bool sente = e.ChangedButton == MouseButton.Left;
if (pieceSelectKing.IsChecked ?? false)
{
// Only allow one king of each colour
if (sente && senteKing is null)
{
senteKing = new Pieces.King(coord, true);
Board[coord.X, coord.Y] = senteKing;
}
else if (!sente && goteKing is null)
{
goteKing = new Pieces.King(coord, false);
Board[coord.X, coord.Y] = goteKing;
}
}
else if (pieceSelectGoldGeneral.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.GoldGeneral(coord, sente);
}
else if (pieceSelectSilverGeneral.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.SilverGeneral(coord, sente);
}
else if (pieceSelectPromotedSilverGeneral.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.PromotedSilverGeneral(coord, sente);
}
else if (pieceSelectRook.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.Rook(coord, sente);
}
else if (pieceSelectPromotedRook.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.PromotedRook(coord, sente);
}
else if (pieceSelectBishop.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.Bishop(coord, sente);
}
else if (pieceSelectPromotedBishop.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.PromotedBishop(coord, sente);
}
else if (pieceSelectKnight.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.Knight(coord, sente);
}
else if (pieceSelectPromotedKnight.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.PromotedKnight(coord, sente);
}
else if (pieceSelectLance.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.Lance(coord, sente);
}
else if (pieceSelectPromotedLance.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.PromotedLance(coord, sente);
}
else if (pieceSelectPawn.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.Pawn(coord, sente);
}
else if (pieceSelectPromotedPawn.IsChecked ?? false)
{
Board[coord.X, coord.Y] = new Pieces.PromotedPawn(coord, sente);
}
}
else
{
if (Board[coord.X, coord.Y] is Pieces.King king)
{
if (king.IsSente)
{
senteKing = null;
}
else
{
goteKing = null;
}
}
Board[coord.X, coord.Y] = null;
}
UpdateBoard();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
UpdateBoard();
}
private void submitFenButton_Click(object sender, RoutedEventArgs e)
{
SenteIsComputer = computerSelectSente.IsChecked ?? false;
GoteIsComputer = computerSelectGote.IsChecked ?? false;
try
{
GeneratedGame = ShogiGame.FromShogiForsythEdwards(sfenInput.Text);
Close();
}
catch (Exception ex)
{
_ = MessageBox.Show(ex.Message, "Shogi Forsyth–Edwards Notation Error",
MessageBoxButton.OK, MessageBoxImage.Error);
}
}
private void cancelFenButton_Click(object sender, RoutedEventArgs e)
{
importOverlay.Visibility = Visibility.Hidden;
}
private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
{
UpdateBoard();
}
private void GoteDrop_MouseUp(object sender, MouseButtonEventArgs e)
{
Type clickedType = (Type)((Grid)sender).Tag;
if (e.ChangedButton == MouseButton.Left)
{
gotePieceDrops[clickedType]++;
}
else if (gotePieceDrops[clickedType] != 0)
{
gotePieceDrops[clickedType]--;
}
UpdateBoard();
}
private void SenteDrop_MouseUp(object sender, MouseButtonEventArgs e)
{
Type clickedType = (Type)((Grid)sender).Tag;
if (e.ChangedButton == MouseButton.Left)
{
sentePieceDrops[clickedType]++;
}
else if (sentePieceDrops[clickedType] != 0)
{
sentePieceDrops[clickedType]--;
}
UpdateBoard();
}
}
}