-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMain.cs
More file actions
195 lines (176 loc) · 6.43 KB
/
Main.cs
File metadata and controls
195 lines (176 loc) · 6.43 KB
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
namespace ChineseChess
{
public partial class Main : Form
{
#region Fields
private Board? board;
private Game? game;
private readonly NewGameDialog newGameDialog;
#endregion
#region Properties
public Game? Game
{
get { return game; }
}
public Label CurrentPlayerLabel
{
get { return currentPlayerLabel; }
}
public Button UndoButton
{
get { return undoButton; }
}
public Label StatusLabel
{
get { return statusLabel; }
}
public Button StartButton
{
get { return startButton; }
}
#endregion
#region Constructor
public Main()
{
InitializeComponent();
newGameDialog = new NewGameDialog();
}
#endregion
#region Methods
private void Main_Load(object sender, EventArgs e)
{
NewGameButton_Click(newGameButton, EventArgs.Empty);
}
private void NewGameButton_Click(object sender, EventArgs e)
{
if (newGameDialog.ShowDialog(this) == DialogResult.OK)
{
if (newGameDialog.Player1 != null && newGameDialog.Player2 != null)
{
board = new Board(boardPanel);
game = new Game(board, newGameDialog.GameType, newGameDialog.Player1, newGameDialog.Player2, newGameDialog.AIDifficulty);
game?.Start();
}
}
}
private void UndoButton_Click(object sender, EventArgs e)
{
if (board == null || game == null || game.Moves.Count == 0)
return;
if (game.Type == Game.GameType.TwoPlayers || game.Type == Game.GameType.TwoPlayersHandicap)
{
if (game.Status != Game.GameStatus.Ended)
{
board.UndoMove(game.Moves.Last());
game.Moves.Remove(game.Moves.Last());
game.SwitchTurn();
}
else
{
board.UndoMove(game.Moves.Last());
game.Moves.Remove(game.Moves.Last());
board.UndoMove(game.Moves.Last());
game.Moves.Remove(game.Moves.Last());
game.SwitchTurn();
}
if (game.Moves.Count == 0)
{
undoButton.Enabled = false;
}
}
else
{
if (game.Status != Game.GameStatus.Ended)
{
board.UndoMove(game.Moves.Last());
game.Moves.Remove(game.Moves.Last());
board.UndoMove(game.Moves.Last());
game.Moves.Remove(game.Moves.Last());
}
else
{
if (game.CurrentPlayer.Side == game.AIPlayerSide)
{
board.UndoMove(game.Moves.Last());
game.Moves.Remove(game.Moves.Last());
board.UndoMove(game.Moves.Last());
game.Moves.Remove(game.Moves.Last());
game.SwitchTurn();
}
else
{
board.UndoMove(game.Moves.Last());
game.Moves.Remove(game.Moves.Last());
}
}
if ((game.Moves.Count == 0) || (game.Moves.Count == 1 && game.Player1.IsAI))
{
undoButton.Enabled = false;
}
}
game.Status = Game.GameStatus.Started;
//Enable all the pieces of the current player and disable all the pieces of the opposite side
foreach (Cell cell in board.Cells)
{
if (cell.Piece != null)
{
if (cell.Piece.Side == game.CurrentPlayer.Side)
cell.Piece.Image.Enabled = true;
else
cell.Piece.Image.Enabled = false;
}
}
}
private void StartButton_Click(object sender, EventArgs e)
{
if (board == null || game == null)
return;
Program.ChessBoard.CurrentPlayerLabel.Text = game.Player1.Name;
Program.ChessBoard.CurrentPlayerLabel.ForeColor = Color.Red;
statusLabel.Text = "";
startButton.Visible = false;
game.Status = Game.GameStatus.Started;
switch (game.Type)
{
case Game.GameType.TwoPlayersHandicap:
//Enable all the pieces of the current player and disable all the pieces of the opposite side
foreach (Cell cell in board.Cells)
{
if (cell.Piece != null)
{
if (cell.Piece.Side == game.CurrentPlayer.Side)
cell.Piece.Image.Enabled = true;
else
cell.Piece.Image.Enabled = false;
}
}
break;
case Game.GameType.VsAIHandicap:
if (game.Player1.IsAI)
{
game.AIPlayerSide = game.Player1.Side;
game.HumanPlayerSide = game.Player2.Side;
game.AIPlayerMove();
}
else
{
game.AIPlayerSide = game.Player2.Side;
game.HumanPlayerSide = game.Player1.Side;
}
//Enable all the pieces of the current player and disable all the pieces of the opposite side
foreach (Cell cell in board.Cells)
{
if (cell.Piece != null)
{
if (cell.Piece.Side == game.CurrentPlayer.Side)
cell.Piece.Image.Enabled = true;
else
cell.Piece.Image.Enabled = false;
}
}
break;
}
}
#endregion
}
}