-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cs
631 lines (536 loc) · 17.8 KB
/
Game.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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
using Godot;
using System;
using System.Collections.Generic;
using CustomConstants;
public partial class Game : Control
{
private bool _gameStarted;
private bool _blackPlayer; // False = Human, True = AI
private bool _whitePlayer; // False = Human, True = AI
private PackedScene _tileScene;
private GridContainer _playgroundBoardTilesNode;
private PackedScene _pieceScene;
private Control _playgroundBoardNode;
private List<Tile> _playgroundBoardTiles;
private List<Tile> _whiteBoardTiles;
private List<Tile> _blackBoardTiles;
private PackedScene _whiteBoardScene;
private PackedScene _blackBoardScene;
private VBoxContainer _whiteBoardTilesNode;
private VBoxContainer _blackBoardTilesNode;
private Control _whiteBoardNode;
private Control _blackBoardNode;
private readonly Vector2 _iconOffset = new(80, 80);
private Piece _pieceSelected;
private int _turn;
private List<int> _extraLegitimateTiles;
public bool GameStarted
{
get
{
return _gameStarted;
}
}
public int Turn
{
set
{
_turn = value;
}
}
public bool BlackPlayer
{
set
{
_blackPlayer = value;
}
}
public bool WhitePlayer
{
set
{
_whitePlayer = value;
}
}
// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
_gameStarted = false;
_blackPlayer = false; // False = Human, True = AI
_whitePlayer = false; // False = Human, True = AI
_tileScene = GD.Load<PackedScene>("res://scenes/tile.tscn");
_playgroundBoardTilesNode = GetNode<GridContainer>("Board/BoardGrid");
_pieceScene = GD.Load<PackedScene>("res://scenes/piece.tscn");
_playgroundBoardNode = GetNode<Control>("Board");
_playgroundBoardTiles = new List<Tile>();
_pieceSelected = null;
_turn = 0;
_extraLegitimateTiles = new List<int>();
HidePossibleMoves();
for (int i = 0; i < 16; i++)
{
CreateTile(i);
}
_whiteBoardTilesNode = GetNode<VBoxContainer>("WhiteBoard/Board/BoardVBox");
_blackBoardTilesNode = GetNode<VBoxContainer>("BlackBoard/Board/BoardVBox");
_whiteBoardNode = GetNode<Control>("WhiteBoard/Board");
_blackBoardNode = GetNode<Control>("BlackBoard/Board");
_whiteBoardTiles = new List<Tile>();
_blackBoardTiles = new List<Tile>();
for (int i = 0; i < 3; i++)
{
CreateWhiteTile(i);
CreateBlackTile(i);
}
}
// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta)
{
}
private void CreateTile(int id)
{
Tile tile = (Tile)_tileScene.Instantiate();
_playgroundBoardTiles.Add(tile);
tile.TileID = id;
tile.Type = TileTypes.PLAYGROUND;
_playgroundBoardTilesNode.AddChild(tile);
tile.TileClicked += OnTileClicked;
}
private void CreateWhiteTile(int id)
{
Tile tile = (Tile)_tileScene.Instantiate();
_whiteBoardTiles.Add(tile);
tile.TileID = id;
tile.Type = TileTypes.WHITE;
_whiteBoardTilesNode.AddChild(tile);
tile.TileClicked += OnTileClicked;
}
private void CreateBlackTile(int id)
{
Tile tile = (Tile)_tileScene.Instantiate();
_blackBoardTiles.Add(tile);
tile.TileID = id;
tile.Type = TileTypes.BLACK;
_blackBoardTilesNode.AddChild(tile);
tile.TileClicked += OnTileClicked;
}
private void InitWhiteBoardPieces()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
Piece piece = (Piece)_pieceScene.Instantiate();
piece.TileType = TileTypes.WHITE;
int type = 0;
switch (i)
{
case 0: type = 1; piece.SetTexture(GD.Load<Texture2D>("res://assets/white_25px.svg")); break;
case 1: type = 2; piece.SetTexture(GD.Load<Texture2D>("res://assets/white_50px.svg")); break;
case 2: type = 3; piece.SetTexture(GD.Load<Texture2D>("res://assets/white_75px.svg")); break;
case 3: type = 4; piece.SetTexture(GD.Load<Texture2D>("res://assets/white_100px.svg")); break;
default: break;
}
piece.Type = type;
_whiteBoardTiles[j].Pieces.Push(piece);
}
}
}
private void InitBlackBoardPieces()
{
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 3; j++)
{
Piece piece = (Piece)_pieceScene.Instantiate();
piece.TileType = TileTypes.BLACK;
int type = 0;
switch (i)
{
case 0: type = -1; piece.SetTexture(GD.Load<Texture2D>("res://assets/black_25px.svg")); break;
case 1: type = -2; piece.SetTexture(GD.Load<Texture2D>("res://assets/black_50px.svg")); break;
case 2: type = -3; piece.SetTexture(GD.Load<Texture2D>("res://assets/black_75px.svg")); break;
case 3: type = -4; piece.SetTexture(GD.Load<Texture2D>("res://assets/black_100px.svg")); break;
default: break;
}
piece.Type = type;
_blackBoardTiles[j].Pieces.Push(piece);
}
}
}
public void InitGame()
{
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < _playgroundBoardTiles[i].Pieces.Count; j++)
{
_playgroundBoardNode.RemoveChild(_playgroundBoardTiles[i].Pieces.Pop()); // .QueueFree()
}
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < _whiteBoardTiles[i].Pieces.Count; j++)
{
_whiteBoardNode.RemoveChild(_whiteBoardTiles[i].Pieces.Pop()); // .QueueFree()
}
for (int j = 0; j < _blackBoardTiles[i].Pieces.Count; j++)
{
_blackBoardNode.RemoveChild(_blackBoardTiles[i].Pieces.Pop()); // .QueueFree()
}
}
InitWhiteBoardPieces();
InitBlackBoardPieces();
for (int i = 0; i < 3; i++)
{
Piece whitePiece = _whiteBoardTiles[i].Pieces.Peek();
Piece blackPiece = _blackBoardTiles[i].Pieces.Peek();
_whiteBoardNode.AddChild(whitePiece);
_blackBoardNode.AddChild(blackPiece);
whitePiece.GlobalPosition = _whiteBoardTiles[i].GlobalPosition + _iconOffset;
blackPiece.GlobalPosition = _blackBoardTiles[i].GlobalPosition + _iconOffset;
whitePiece.TileID = i;
blackPiece.TileID = i;
whitePiece.PieceSelected += OnPieceSelected;
blackPiece.PieceSelected += OnPieceSelected;
}
_turn = -1; // Black starts the game.
}
private void OnTileClicked(Tile tile)
{
// If I pressed an empty tile.
if (_pieceSelected == null)
{
return;
}
// If the destination tile is not playground.
if (tile.Type != TileTypes.PLAYGROUND)
{
_pieceSelected = null;
HidePossibleMoves();
return;
}
MovePiece(_pieceSelected, tile.TileID);
_pieceSelected = null;
_turn *= -1; // Change the turn.
HidePossibleMoves();
}
public void MovePiece(Piece piece, int location)
{
if (!_gameStarted)
{
_gameStarted = true;
}
// piece is the source piece that we need to move.
// If we will add a new piece to the ground.
if (piece.TileType != TileTypes.PLAYGROUND)
{
switch (piece.TileType)
{
case TileTypes.WHITE:
{
_whiteBoardNode.RemoveChild(_whiteBoardTiles[piece.TileID].Pieces.Pop());
if (_whiteBoardTiles[piece.TileID].Pieces.Count != 0)
{
Piece whitePiece = _whiteBoardTiles[piece.TileID].Pieces.Peek();
_whiteBoardNode.AddChild(whitePiece);
whitePiece.GlobalPosition = _whiteBoardTiles[piece.TileID].GlobalPosition + _iconOffset;
whitePiece.TileID = piece.TileID;
whitePiece.PieceSelected += OnPieceSelected;
}
break;
}
case TileTypes.BLACK:
{
_blackBoardNode.RemoveChild(_blackBoardTiles[piece.TileID].Pieces.Pop());
if (_blackBoardTiles[piece.TileID].Pieces.Count != 0)
{
Piece blackPiece = _blackBoardTiles[piece.TileID].Pieces.Peek();
_blackBoardNode.AddChild(blackPiece);
blackPiece.GlobalPosition = _blackBoardTiles[piece.TileID].GlobalPosition + _iconOffset;
blackPiece.TileID = piece.TileID;
blackPiece.PieceSelected += OnPieceSelected;
}
break;
}
default: break;
}
_playgroundBoardNode.AddChild(piece);
piece.GlobalPosition = _playgroundBoardTiles[location].GlobalPosition + _iconOffset;
_playgroundBoardTiles[location].Pieces.Push(piece);
piece.TileType = TileTypes.PLAYGROUND;
}
// If we will move a piece that is on the ground.
else
{
_playgroundBoardNode.MoveChild(piece, -1); // Move the child to the top on the tree.
_playgroundBoardTiles[piece.TileID].Pieces.Pop();
_playgroundBoardTiles[location].Pieces.Push(piece);
var tween = GetTree().CreateTween();
tween.TweenProperty(piece, "global_position", _playgroundBoardTiles[location].GlobalPosition + _iconOffset, 0.5);
}
piece.TileID = location;
}
private void OnPieceSelected(Piece piece)
{
bool extraLegitimateTiles = false;
// If I am not selecting any piece.
if (_pieceSelected == null)
{
HideIllegitimateMoves();
// If it is not my turn.
if (_turn < 0 && piece.Type > 0 || _turn > 0 && piece.Type < 0)
{
ShowIllegitimateMoves(piece, piece);
return;
}
_pieceSelected = piece;
// TODO: Check if there are any 3 lined pieces to give the player the option to stack on them before showing the possible moves.
extraLegitimateTiles = CheckExtraLegitimateTiles();
ShowPossibleMoves(extraLegitimateTiles);
return;
}
// If the source and destination pieces are the same.
if (_pieceSelected.TileID == piece.TileID && _pieceSelected.TileType == piece.TileType)
{
_pieceSelected = null;
HidePossibleMoves();
return;
}
// If the destination tile is not in the playground.
if (piece.TileType != TileTypes.PLAYGROUND)
{
ShowIllegitimateMoves(_pieceSelected, piece);
_pieceSelected = null;
HidePossibleMoves();
return;
}
// If the place is not empty and has bigger piece.
if (_playgroundBoardTiles[piece.TileID].Pieces.Count != 0 &&
Math.Abs(_pieceSelected.Type) <= Math.Abs(_playgroundBoardTiles[piece.TileID].Pieces.Peek().Type))
{
ShowIllegitimateMoves(_pieceSelected, piece);
_pieceSelected = null;
HidePossibleMoves();
return;
}
// If the place is not empty and has smaller piece and there are no extra legitimate tiles.
if (_playgroundBoardTiles[piece.TileID].Pieces.Count != 0 &&
Math.Abs(_pieceSelected.Type) > Math.Abs(_playgroundBoardTiles[piece.TileID].Pieces.Peek().Type) &&
!extraLegitimateTiles)
{
ShowIllegitimateMoves(_pieceSelected, piece);
_pieceSelected = null;
HidePossibleMoves();
return;
}
OnTileClicked(_playgroundBoardTiles[piece.TileID]);
}
private void ShowPossibleMoves(bool extraLegitimateTiles = false)
{
for (int i = 0; i < _playgroundBoardTiles.Count; i++)
{
// If I am in the same location in the playground board.
if (_pieceSelected.TileType == TileTypes.PLAYGROUND && _pieceSelected.TileID == i) continue;
// If the place is not empty.
if (_playgroundBoardTiles[i].Pieces.Count != 0) continue;
_playgroundBoardTiles[i].SetFilter(TileStates.LEGITIMATE);
}
// If there are no extra legitimate tiles.
if (!extraLegitimateTiles) return;
for (int i = 0; i < _extraLegitimateTiles.Count; i++)
{
_playgroundBoardTiles[_extraLegitimateTiles[i]].SetFilter(TileStates.LEGITIMATE);
}
}
private void HidePossibleMoves()
{
for (int i = 0; i < _playgroundBoardTiles.Count; i++)
{
if (_playgroundBoardTiles[i].State == TileStates.ILLEGITIMATE) continue;
_playgroundBoardTiles[i].SetFilter(TileStates.NONE);
}
_extraLegitimateTiles.Clear();
}
private void ShowIllegitimateMoves(Piece source, Piece destination)
{
switch (source.TileType)
{
case TileTypes.BLACK: _blackBoardTiles[source.TileID].SetFilter(TileStates.ILLEGITIMATE); break;
case TileTypes.PLAYGROUND: _playgroundBoardTiles[source.TileID].SetFilter(TileStates.ILLEGITIMATE); break;
case TileTypes.WHITE: _whiteBoardTiles[source.TileID].SetFilter(TileStates.ILLEGITIMATE); break;
default: break;
}
switch (destination.TileType)
{
case TileTypes.BLACK: _blackBoardTiles[destination.TileID].SetFilter(TileStates.ILLEGITIMATE); break;
case TileTypes.PLAYGROUND: _playgroundBoardTiles[destination.TileID].SetFilter(TileStates.ILLEGITIMATE); break;
case TileTypes.WHITE: _whiteBoardTiles[destination.TileID].SetFilter(TileStates.ILLEGITIMATE); break;
default: break;
}
}
private void HideIllegitimateMoves()
{
for (int i = 0; i < _blackBoardTiles.Count; i++)
{
if (_blackBoardTiles[i].State != TileStates.ILLEGITIMATE) continue;
_blackBoardTiles[i].SetFilter(TileStates.NONE);
}
for (int i = 0; i < _playgroundBoardTiles.Count; i++)
{
if (_playgroundBoardTiles[i].State != TileStates.ILLEGITIMATE) continue;
_playgroundBoardTiles[i].SetFilter(TileStates.NONE);
}
for (int i = 0; i < _whiteBoardTiles.Count; i++)
{
if (_whiteBoardTiles[i].State != TileStates.ILLEGITIMATE) continue;
_whiteBoardTiles[i].SetFilter(TileStates.NONE);
}
}
// TODO: Fix me.
private bool CheckExtraLegitimateTiles()
{
bool extraLegitimateTiles = false;
for (int i = 0; i < 4; i++)
{
int counter = 0;
// Check for rows.
for (int j = 0; j < 4; j++)
{
// If the place is empty.
if (_playgroundBoardTiles[i * 4 + j].Pieces.Count == 0) continue;
// If the place has bigger piece.
if (Math.Abs(_pieceSelected.Type) <= Math.Abs(_playgroundBoardTiles[i * 4 + j].Pieces.Peek().Type)) continue;
// If the piece is not mine.
if (_turn < 0 && _playgroundBoardTiles[i * 4 + j].Pieces.Peek().Type > 0 ||
_turn > 0 && _playgroundBoardTiles[i * 4 + j].Pieces.Peek().Type < 0)
{
counter++;
}
}
// The counter must not be >= 4 beecause in this case the game will be over.
if (counter >= 3)
{
extraLegitimateTiles = true;
for (int j = 0; j < 4; j++)
{
// If the place is empty.
if (_playgroundBoardTiles[i * 4 + j].Pieces.Count == 0) continue;
// If the place has bigger piece.
if (Math.Abs(_pieceSelected.Type) <= Math.Abs(_playgroundBoardTiles[i * 4 + j].Pieces.Peek().Type)) continue;
// If the piece is not mine.
if (_turn < 0 && _playgroundBoardTiles[i * 4 + j].Pieces.Peek().Type > 0 ||
_turn > 0 && _playgroundBoardTiles[i * 4 + j].Pieces.Peek().Type < 0)
{
_extraLegitimateTiles.Add(i * 4 + j);
}
}
counter = 0;
}
// Check for columns.
for (int j = 0; j < 4; j++)
{
// If the place is empty for columns.
if (_playgroundBoardTiles[j * 4 + i].Pieces.Count == 0) continue;
// If the place has bigger piece for columns.
if (Math.Abs(_pieceSelected.Type) <= Math.Abs(_playgroundBoardTiles[j * 4 + i].Pieces.Peek().Type)) continue;
// If the piece is not mine.
if (_turn < 0 && _playgroundBoardTiles[j * 4 + i].Pieces.Peek().Type > 0 ||
_turn > 0 && _playgroundBoardTiles[j * 4 + i].Pieces.Peek().Type < 0)
{
counter++;
}
}
// The counter must not be >= 4 beecause in this case the game will be over.
if (counter >= 3)
{
extraLegitimateTiles = true;
for (int j = 0; j < 4; j++)
{
// If the place is empty for columns.
if (_playgroundBoardTiles[j * 4 + i].Pieces.Count == 0) continue;
// If the place has bigger piece for columns.
if (Math.Abs(_pieceSelected.Type) <= Math.Abs(_playgroundBoardTiles[j * 4 + i].Pieces.Peek().Type)) continue;
// If the piece is not mine.
if (_turn < 0 && _playgroundBoardTiles[j * 4 + i].Pieces.Peek().Type > 0 ||
_turn > 0 && _playgroundBoardTiles[j * 4 + i].Pieces.Peek().Type < 0)
{
_extraLegitimateTiles.Add(j * 4 + i);
}
}
}
}
int temp_counter = 0;
// Check -45 slope diagonal.
for (int j = 0; j < 4; j++)
{
// If the place is empty for diagonals.
if (_playgroundBoardTiles[j * 4 + j].Pieces.Count == 0) continue;
// If the place has bigger piece for diagonals.
if (Math.Abs(_pieceSelected.Type) <= Math.Abs(_playgroundBoardTiles[j * 4 + j].Pieces.Peek().Type)) continue;
// If the piece is not mine.
if (_turn < 0 && _playgroundBoardTiles[j * 4 + j].Pieces.Peek().Type > 0 ||
_turn > 0 && _playgroundBoardTiles[j * 4 + j].Pieces.Peek().Type < 0)
{
temp_counter++;
}
}
// The counter must not be >= 4 beecause in this case the game will be over.
if (temp_counter >= 3)
{
extraLegitimateTiles = true;
for (int j = 0; j < 4; j++)
{
// If the place is empty for diagonals.
if (_playgroundBoardTiles[j * 4 + j].Pieces.Count == 0) continue;
// If the place has bigger piece for diagonals.
if (Math.Abs(_pieceSelected.Type) <= Math.Abs(_playgroundBoardTiles[j * 4 + j].Pieces.Peek().Type)) continue;
// If the piece is not mine.
if (_turn < 0 && _playgroundBoardTiles[j * 4 + j].Pieces.Peek().Type > 0 ||
_turn > 0 && _playgroundBoardTiles[j * 4 + j].Pieces.Peek().Type < 0)
{
_extraLegitimateTiles.Add(j * 4 + j);
}
}
temp_counter = 0;
}
// Check +45 slope diagonal.
for (int j = 0; j < 4; j++)
{
// If the place is empty for diagonals.
if (_playgroundBoardTiles[j * 4 - j + 3].Pieces.Count == 0) continue;
// If the place has bigger piece for diagonals.
if (Math.Abs(_pieceSelected.Type) <= Math.Abs(_playgroundBoardTiles[j * 4 - j + 3].Pieces.Peek().Type)) continue;
// If the piece is not mine.
if (_turn < 0 && _playgroundBoardTiles[j * 4 - j + 3].Pieces.Peek().Type > 0 ||
_turn > 0 && _playgroundBoardTiles[j * 4 - j + 3].Pieces.Peek().Type < 0)
{
temp_counter++;
}
}
// The counter must not be >= 4 beecause in this case the game will be over.
if (temp_counter >= 3)
{
extraLegitimateTiles = true;
for (int j = 0; j < 4; j++)
{
// If the place is empty for diagonals.
if (_playgroundBoardTiles[j * 4 - j + 3].Pieces.Count == 0) continue;
// If the place has bigger piece for diagonals.
if (Math.Abs(_pieceSelected.Type) <= Math.Abs(_playgroundBoardTiles[j * 4 - j + 3].Pieces.Peek().Type)) continue;
// If the piece is not mine.
if (_turn < 0 && _playgroundBoardTiles[j * 4 - j + 3].Pieces.Peek().Type > 0 ||
_turn > 0 && _playgroundBoardTiles[j * 4 - j + 3].Pieces.Peek().Type < 0)
{
_extraLegitimateTiles.Add(j * 4 - j + 3);
}
}
}
// GD.Print("Extra Legitimate Tiles:");
// for (int i = 0; i < _extraLegitimateTiles.Count; i++)
// {
// GD.Print(_extraLegitimateTiles[i]);
// }
// GD.Print("End of Extra Legitimate Tiles.");
return extraLegitimateTiles;
}
}