forked from nenslen/othello
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cpp
177 lines (150 loc) · 4.85 KB
/
MainForm.cpp
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "MainForm.h"
#include "oGame.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TfrmMain *frmMain;
TShape *bgTiles[8][8];
//---------------------------------------------------------------------------
__fastcall TfrmMain::TfrmMain(TComponent* Owner) : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormCreate(TObject *Sender)
{
// Creates an 8x8 grid of background tiles
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
bgTiles[i][j] = new TShape(pnlGame);
bgTiles[i][j]->Parent = pnlGame;
bgTiles[i][j]->Height = 60;
bgTiles[i][j]->Width = 60;
bgTiles[i][j]->Brush->Color = clGreen;
bgTiles[i][j]->Top = i * 60;
bgTiles[i][j]->Left = j * 60;
}
}
// Creates the game object
game = new oGame(pnlGame);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::btnResetClick(TObject *Sender)
{
// Resets the game
game->reset();
shCurrentPlayer->Brush->Color = game->getP1Color();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::fileBoardClick(TObject *Sender)
{
// Changes the color of the background tiles
if(clrDialog->Execute())
{
TColor clBoard = clrDialog->Color;
game->setBoardColor(clBoard);
game->refreshBoard();
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
bgTiles[i][j]->Brush->Color = clBoard;
}
}
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::fileP1Click(TObject *Sender)
{
if(clrDialog->Execute())
{
// Sets player color in game object
game->setP1Color(clrDialog->Color);
game->refreshBoard();
// Changes UI colors
shP1Points->Brush->Color = clrDialog->Color;
if(game->getCurrentPlayer() == 1) { shCurrentPlayer->Brush->Color = clrDialog->Color; }
else { shCurrentPlayer->Brush->Color = game->getP2Color(); }
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::fileP2Click(TObject *Sender)
{
if(clrDialog->Execute())
{
// Sets player color in game object
game->setP2Color(clrDialog->Color);
game->refreshBoard();
// Changes UI colors
shP2Points->Brush->Color = clrDialog->Color;
if(game->getCurrentPlayer() == 2) { shCurrentPlayer->Brush->Color = clrDialog->Color; }
else { shCurrentPlayer->Brush->Color = game->getP1Color(); }
}
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::fileDefaultsClick(TObject *Sender)
{
// Changes background tile colors
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
bgTiles[i][j]->Brush->Color = clGreen;
}
}
// Resets game colors
game->setBoardColor(clGreen);
game->setP1Color(clWhite);
game->setP2Color(clBlack);
game->refreshBoard();
// Resets UI colors
if(game->getCurrentPlayer() == 2) { shCurrentPlayer->Brush->Color = game->getP2Color(); }
else { shCurrentPlayer->Brush->Color = game->getP1Color(); }
shP1Points->Brush->Color = game->getP1Color();
shP2Points->Brush->Color = game->getP2Color();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::cbHintsClick(TObject *Sender)
{
// Turns on/off display of hints
game->setShowHints(cbHints->Checked);
game->refreshBoard();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::menuResetClick(TObject *Sender)
{
// Resets the game
game->reset();
shCurrentPlayer->Brush->Color = game->getP1Color();
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::menuExitClick(TObject *Sender)
{
// Exits the game
exit(0);
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::fileAboutClick(TObject *Sender)
{
// Shows a message about the game
ShowMessage("During your turn, put a piece onto the grid so that it aligns with another piece of yours on a straight line. Opponent pieces between the new tile and your other tiles will become yours.\n\nYou cannot move without capturing opponent pieces.\n\nWhen both players cannot move, the game ends. The player with the most pieces wins!");
}
//---------------------------------------------------------------------------
void __fastcall TfrmMain::FormClose(TObject *Sender, TCloseAction &Action)
{
// Deletes all of the dynamically created objects
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
delete board[i][j];
delete bgTiles[i][j];
}
}
delete game;
}
//---------------------------------------------------------------------------