-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameForm.cpp
More file actions
254 lines (241 loc) · 9.81 KB
/
Copy pathGameForm.cpp
File metadata and controls
254 lines (241 loc) · 9.81 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
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
#include "GameForm.h"
#include "GameUser.h"
#include "GameLogic.h"
#include "GameStats.h"
#include <iostream>
#include <string>
#include <algorithm>
namespace szablon {
GameForm::GameForm(GameUser* gameuser, std::string difficulty, int difficultyRemoveDigits, GameStats* gameStats)
{
InitializeComponent();
this->gameUser = gameuser;
this->gameStats = gameStats;
this->difficulty = difficultyRemoveDigits;
this->createGame();
}
void GameForm::createGame() {
this->entries = 0;
this->entries_wrong = 0;
this->hint = 0;
this->gameLogic = new GameLogic();
this->gameLogic->fillArrays();
this->gameLogic->GenerateSudoku();
this->gameLogic->removeDigits(this->difficulty);
this->CreateTextBoxes();
this->unFocusBox();
this->UpdateUserName();
this->UpdatePoints();
this->UpdateDifficulty();
this->UpdateHighScore();
}
void GameForm::UpdateDifficulty() {
this->label_difficulty->Text = "Trudność: " + gcnew System::String(this->gameUser->getDifficulty().c_str());
}
void GameForm::UpdateUserName() {
this->label_username->Text = "Nazwa: " + gcnew System::String(this->gameUser->getName().c_str());
}
void GameForm::UpdatePoints() {
this->label_points->Text = "Punkty: " + this->gameUser->getPoints();
this->label_total->Text = "Total: " + this->gameUser->getTotal();
}
void GameForm::UpdateHighScore() {
this->high_score_name->Text = "Nazwa: "+gcnew System::String(this->gameStats->getHighScoreUser()->getName().c_str());
this->high_score_points->Text = "Punkty: " + this->gameStats->getHighScore();
}
void GameForm::CreateTextBoxes() {
for (int blockRow = 0; blockRow < 3; blockRow++)
for (int blockCol = 0; blockCol < 3; blockCol++) {
auto tb = dynamic_cast<TableLayoutPanel^>(this->table->GetControlFromPosition(blockCol, blockRow));
if (!tb) continue;
for (int rowOffset = 0; rowOffset < 3; rowOffset++)
for (int colOffset = 0; colOffset < 3; colOffset++) {
int globalRow = blockRow * 3 + rowOffset;
int globalCol = blockCol * 3 + colOffset;
CreateTextBox(tb, globalRow, globalCol);
}
}
label1->Text += this->gameLogic->getGenerateTime() + "ms";
label2->Text += this->gameLogic->getSamples();
}
void GameForm::CreateTextBox(TableLayoutPanel^ parentTable, int row, int col) {
TextBox^ text = gcnew TextBox();
text->BackColor = System::Drawing::Color::FromArgb(13, 50, 50);
text->Padding = System::Windows::Forms::Padding(0);
text->Margin = System::Windows::Forms::Padding(0);
text->Width = 57;
text->Height = 57;
text->AutoSize = false;
text->MaxLength = 1;
text->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 30, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(238)));
text->ForeColor = ForeColor.White;
text->TextAlign = HorizontalAlignment::Center;
text->KeyPress += gcnew KeyPressEventHandler(this, &GameForm::TextBox_KeyPress);
int value = gameLogic->getBoard(row, col);
text->Name = L"text_" + row + "_" + col;
text->Tag = gcnew array<int>{ row, col };
if (value != 0) {
text->Text = value.ToString();
text->ReadOnly = true;
text->BackColor = System::Drawing::Color::FromArgb(20, 170, 20);
text->Click += gcnew EventHandler(this, &GameForm::LockedTextBox_Click);
}
parentTable->Controls->Add(text, col % 3, row % 3);
}
void GameForm::TextBox_KeyPress(Object^ sender, KeyPressEventArgs^ e)
{
if ((!Char::IsDigit(e->KeyChar) || e->KeyChar == '0') && e->KeyChar != (char)Keys::Back) {
e->Handled = true;
return;
}
TextBox^ textBox = dynamic_cast<TextBox^>(sender);
if (!textBox || textBox->ReadOnly) {
this->unFocusBox();
return;
}
if (e->KeyChar == (char)Keys::Back) {
return;
}
int value = e->KeyChar - '0';
auto coordinates = dynamic_cast<array<int>^>(textBox->Tag);
if (!coordinates) return;
int row = coordinates[0];
int col = coordinates[1];
gameLogic->setBoard(row, col, value);
if (value == gameLogic->getCopyBoard(row, col)) {
this->unFocusBox();
textBox->BackColor = System::Drawing::Color::FromArgb(255, 170, 20);
textBox->Text = value.ToString();
textBox->ReadOnly = true;
textBox->Click += gcnew EventHandler(this, &GameForm::LockedTextBox_Click);
this->gameLogic->setCopyBoard(row, col, 0);
this->entries++;
this->gameUser->addPoints(5);
}
else {
this->entries_wrong++;
this->gameUser->removePoints(1);
}
this->UpdatePoints();
this->label_wrong_guess->Text = "Błędne: " + this->entries_wrong;
this->label_right_guess->Text = "Poprawne: " + this->entries;
}
void GameForm::LockedTextBox_Click(Object^ sender, EventArgs^ e) {
this->unFocusBox();
}
void GameForm::unFocusBox()
{
this->table->Focus();
this->table->Select();
}
void GameForm::verify_button_Click(System::Object^ sender, System::EventArgs^ e) {
if (this->gameLogic->checkSudoku()) {
System::String^ userName = gcnew System::String(this->gameUser->getName().c_str());
System::String^ message = "Gratulacje " + userName + "!\nUdało Ci się poprawnie rozwiązać sudoku!\n\nUzyskując " +
this->gameUser->getPoints().ToString() + " Punktów \n";
MessageBox::Show(
message,
"Wygrana",
MessageBoxButtons::OK,
MessageBoxIcon::Information
);
this->gameStats->saveUser(this->gameUser);
this->Close();
return;
}
MessageBox::Show(
"Niestety, rozwiązanie jest błędne. Spróbuj jeszcze raz!",
"Błąd",
MessageBoxButtons::OK,
MessageBoxIcon::Error
);
}
void GameForm::help_button_Click(System::Object^ sender, System::EventArgs^ e)
{
for (int blockRow = 0; blockRow < 3; blockRow++) {
for (int blockCol = 0; blockCol < 3; blockCol++) {
TableLayoutPanel^ tb = dynamic_cast<TableLayoutPanel^>(this->table->GetControlFromPosition(blockCol, blockRow));
if (tb != nullptr && ProcessBlock(tb, blockRow, blockCol)) {
return;
}
}
}
}
void GameForm::reset_button_Click(System::Object^ sender, System::EventArgs^ e)
{
for (int blockRow = 0; blockRow < 3; blockRow++)
for (int blockCol = 0; blockCol < 3; blockCol++) {
auto tb = dynamic_cast<TableLayoutPanel^>(this->table->GetControlFromPosition(blockCol, blockRow));
if (tb == nullptr) continue;
for (int rowOffset = 0; rowOffset < 3; rowOffset++)
for (int colOffset = 0; colOffset < 3; colOffset++) {
auto textBox = dynamic_cast<TextBox^>(tb->GetControlFromPosition(colOffset, rowOffset));
if (textBox)tb->Controls->Remove(textBox);
}
}
this->label_wrong_guess->Text = "Błędne: 0";
this->label_right_guess->Text = "Poprawne: 0";
this->label_hint->Text = "Wskazówki: 0";
this->label1->Text = "Generowanie: ";
this->label2->Text = "Kroki: ";
this->entries = 0;
this->entries_wrong = 0;
this->hint = 0;
this->gameUser->setPoints(0);
this->createGame();
this->unFocusBox();
}
bool GameForm::ProcessBlock(TableLayoutPanel^ tb, int blockRow, int blockCol) {
for (int rowOffset = 0; rowOffset < 3; rowOffset++) {
for (int colOffset = 0; colOffset < 3; colOffset++) {
int globalRow = blockRow * 3 + rowOffset;
int globalCol = blockCol * 3 + colOffset;
int value = this->gameLogic->getCopyBoard(globalRow, globalCol);
if (value != 0) {
UpdateTextBox(tb, rowOffset, colOffset, value);
this->gameLogic->setBoard(globalRow, globalCol, value);
this->gameLogic->setCopyBoard(globalRow, globalCol, 0);
this->gameUser->removePoints(5);
this->UpdatePoints();
return true;
}
}
}
return false;
}
void GameForm::UpdateTextBox(TableLayoutPanel^ tb, int rowOffset, int colOffset, int value) {
TextBox^ textBox = dynamic_cast<TextBox^>(tb->GetControlFromPosition(colOffset, rowOffset));
if (textBox != nullptr) {
//HELP ACTION
textBox->BackColor = System::Drawing::Color::FromArgb(170, 20, 20);
textBox->Text = value.ToString();
textBox->ReadOnly = true;
textBox->Click += gcnew EventHandler(this, &GameForm::LockedTextBox_Click);
this->label_hint->Text = "Wskazówki: " + (++this->hint);
}
}
GameUser* GameForm::getGameUser()
{
return this->gameUser;
}
GameStats* GameForm::getGameStats() {
return this->gameStats;
}
int GameForm::getEntriesRight()
{
return this->entries;
}
int GameForm::getEntriesWrong()
{
return this->entries_wrong;
}
int GameForm::getHint()
{
return this->hint;
}
void GameForm::setGameUser(GameUser* gameuser)
{
this->gameUser = gameuser;
}
}