This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameBoard.cs
148 lines (121 loc) · 4.19 KB
/
GameBoard.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
using FairyChess.Enums;
using FairyChess.Models;
using FairyChess.Models.GamePieces;
namespace FairyChess;
public partial class GameBoard : Form
{
public const int CELL_SIZE = 96;
public const int NUM_ROWS = 8;
#if !DEBUG_2
public const int NUM_COLUMNS = 10;
#endif
#if DEBUG_2
public const int NUM_COLUMNS = 8;
#endif
#region Variables
/// <summary>
/// Stores all cells of the grid
/// </summary>
private readonly GameCell[] _grid = new GameCell[NUM_COLUMNS * NUM_ROWS];
/// <summary>
/// Pieces currently in play
/// </summary>
public readonly List<GamePiece> Pieces = new();
#endregion
#region Properties
/// <summary>
/// Whose color is allowed to move
/// </summary>
public PieceColor Turn { get; set; }
/// <summary>
/// The currently selected cell, if there is one
/// </summary>
public GameCell? SelectedCell { get; set; }
/// <summary>
/// The currently used board Form
/// </summary>
public static GameBoard Main { get; }
#endregion
#region Constructors
private GameBoard()
{
InitializeComponent();
Turn = PieceColor.White;
}
static GameBoard()
{
Main = new GameBoard();
InitClassicBoard();
}
#endregion
#region Methods
private static void InitClassicBoard()
{
#if !DEBUG_2
for (var x = 0; x < NUM_COLUMNS; x++)
{
if (x is 4 or 5)
continue;
_ = new PiecePawn((x, 1), PieceColor.White);
_ = new PiecePawn((x, NUM_ROWS - 2), PieceColor.Black);
}
_ = new PieceGuard((4, 1), PieceColor.White);
_ = new PieceGuard((4, NUM_ROWS - 2), PieceColor.Black);
_ = new PieceGuard((5, 1), PieceColor.White);
_ = new PieceGuard((5, NUM_ROWS - 2), PieceColor.Black);
_ = new PieceRook((0, 0), PieceColor.White);
_ = new PieceRook((0, NUM_ROWS - 1), PieceColor.Black);
_ = new PieceRook((NUM_COLUMNS - 1, 0), PieceColor.White);
_ = new PieceRook((NUM_COLUMNS - 1, NUM_ROWS - 1), PieceColor.Black);
_ = new PieceKnight((1, 0), PieceColor.White);
_ = new PieceKnight((1, NUM_ROWS - 1), PieceColor.Black);
_ = new PieceKnight((NUM_COLUMNS - 2, 0), PieceColor.White);
_ = new PieceKnight((NUM_COLUMNS - 2, NUM_ROWS - 1), PieceColor.Black);
_ = new PieceBishop((2, 0), PieceColor.White);
_ = new PieceBishop((2, NUM_ROWS - 1), PieceColor.Black);
_ = new PieceBishop((NUM_COLUMNS - 3, 0), PieceColor.White);
_ = new PieceBishop((NUM_COLUMNS - 3, NUM_ROWS - 1), PieceColor.Black);
_ = new PieceChancellor((3, 0), PieceColor.White);
_ = new PieceChancellor((3, NUM_ROWS - 1), PieceColor.Black);
_ = new PieceCardinal((6, 0), PieceColor.White);
_ = new PieceCardinal((6, NUM_ROWS - 1), PieceColor.Black);
_ = new PieceQueen((4, 0), PieceColor.White);
_ = new PieceQueen((4, NUM_ROWS - 1), PieceColor.Black);
_ = new PieceKing((5, 0), PieceColor.White);
_ = new PieceKing((5, NUM_ROWS - 1), PieceColor.Black);
#endif
#if DEBUG_2
_ = new PieceDragon((0, 0), PieceColor.White);
_ = new PieceUnicorn((7, 7), PieceColor.White);
#endif
}
/// <summary>
/// Changes the turn to the opposite player
/// </summary>
public static void ChangeTurn()
{
Main.Turn = Main.Turn is PieceColor.White
? PieceColor.Black
: PieceColor.White;
}
/// <summary>
/// Flips the board's cells
/// </summary>
public static void FlipBoard()
{
var threads = new Thread[NUM_COLUMNS * NUM_ROWS];
for (var x = 0; x < NUM_COLUMNS; x++)
for (var y = 0; y < NUM_ROWS; y++)
{
GameCell cell = Main[x, y];
threads[x + NUM_COLUMNS * y] = new Thread(() => cell.Top = CELL_SIZE * NUM_ROWS - cell.Top);
}
for (var i = 0; i < threads.Length; i++)
threads[i].Start();
for (var i = 0; i < threads.Length; i++)
threads[i].Join();
}
public GameCell this[int x, int y]
=> _grid[x + NUM_COLUMNS * y];
#endregion
}