forked from harmattan/brainparty
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshufflepuzzler.cpp
245 lines (194 loc) · 7.17 KB
/
shufflepuzzler.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
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
// Brain Party
// Copyright (C) 2010 Paul Hudson (http://www.tuxradar.com/brainparty)
// Brain Party is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 3
// of the License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#include "shufflepuzzler.h"
#include "Minigame.h"
BPMiniGame_ShufflePuzzler::BPMiniGame_ShufflePuzzler(BPGame* game) : BPMiniGame(game) {
ShowResult = Loaded = false;
TimeStarted = TheGame->TickCount;
SuccessTime = -1;
GameTitle = "Shuffle Puzzler";
GameHelp = "Re-arrange the pieces to form the original picture by tapping on a piece to move. To see how the finished picture should look, tap the 'Shuffle Puzzler' title.";
GameHelp2 = "We recommend solving the top three first, because that's quite easy to do. Once they are solved, don't move them again: treat it as a 3x2 problem.";
MiniGameType = PUZZLE;
sfcBackground = TheGame->LoadBitmap("shufflepuzzler", 320, 416);
sfcPicture = TheGame->LoadBitmap("frog", 300, 300);
ImageBits.Add(TheGame->LoadBitmap("shufflepuzzler1", 100, 100));
ImageBits.Add(TheGame->LoadBitmap("shufflepuzzler2", 100, 100));
ImageBits.Add(TheGame->LoadBitmap("shufflepuzzler3", 100, 100));
ImageBits.Add(TheGame->LoadBitmap("shufflepuzzler4", 100, 100));
ImageBits.Add(TheGame->LoadBitmap("shufflepuzzler5", 100, 100));
ImageBits.Add(TheGame->LoadBitmap("shufflepuzzler6", 100, 100));
ImageBits.Add(TheGame->LoadBitmap("shufflepuzzler7", 100, 100));
ImageBits.Add(TheGame->LoadBitmap("shufflepuzzler8", 100, 100));
ImageBits.Add(TheGame->LoadBitmap("shufflepuzzler9", 100, 100));
sfcClock = NULL;
int count = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
BPMiniGame_ShufflePuzzler_Item* item = new BPMiniGame_ShufflePuzzler_Item();
item->ProperPosition = count;
item->ActualPosition = count;
item->Pic = ImageBits[count];
Items.Add(item);
++count;
}
}
}
BPMiniGame_ShufflePuzzler::~BPMiniGame_ShufflePuzzler() {
SAFE_DELETE(sfcBackground);
SAFE_DELETE(sfcPicture);
ImageBits.Clear();
Items.Clear();
SAFE_DELETE(sfcClock);
}
void BPMiniGame_ShufflePuzzler::OnMouseUp() {
}
void BPMiniGame_ShufflePuzzler::OnMouseMove() {
}
void BPMiniGame_ShufflePuzzler::OnMouseDown() {
if (TouchEvent.Y < 90) {
ShowResult = !ShowResult;
return;
}
// still here? User has clicked outside of the game logo - if they are showing the results, hide them now
if (ShowResult) {
ShowResult = false;
return;
}
int x = (TouchEvent.X - 10) / 100;
int y = (TouchEvent.Y - 106) / 100;
int pos = (y * 3) + x;
SwapPiece(pos);
bool success = true;
for (int i = 0; i < Items.Count; ++i) {
BPMiniGame_ShufflePuzzler_Item* item = Items[i];
if (item->ActualPosition != item->ProperPosition) {
success = false;
break;
}
}
if (success && SuccessTime == -1) {
SuccessTime = TheGame->TickCount;
}
}
void BPMiniGame_ShufflePuzzler::SwapPiece(int pos) {
// SwapPiece() is called many times at the beginning; only call it when it's a real player move
switch (pos) {
case 0: TrySwap(0, 1, 3, -1, -1); break;
case 1: TrySwap(1, 0, 2, 4, -1); break;
case 2: TrySwap(2, 1, 5, -1, -1); break;
case 3: TrySwap(3, 0, 4, 6, -1); break;
case 4: TrySwap(4, 1, 3, 5, 7); break;
case 5: TrySwap(5, 2, 4, 8, -1); break;
case 6: TrySwap(6, 3, 7, -1, -1); break;
case 7: TrySwap(7, 6, 4, 8, -1); break;
case 8: TrySwap(8, 5, 7, -1, -1); break;
}
}
void BPMiniGame_ShufflePuzzler::TrySwap(int clicked, int pos1, int pos2, int pos3, int pos4) {
BPMiniGame_ShufflePuzzler_Item* item_clicked = FindImageWithPos(clicked);
BPMiniGame_ShufflePuzzler_Item* item1 = FindImageWithPos(pos1);
BPMiniGame_ShufflePuzzler_Item* item2 = FindImageWithPos(pos2);
BPMiniGame_ShufflePuzzler_Item* item3 = FindImageWithPos(pos3);
BPMiniGame_ShufflePuzzler_Item* item4 = FindImageWithPos(pos4);
if (item1 != NULL && item1->ProperPosition == 8) {
if (Loaded) TheGame->PlaySound("slide");
int tmp = item1->ActualPosition;
item1->ActualPosition = item_clicked->ActualPosition;
item_clicked->ActualPosition = tmp;
return;
}
if (item2 != NULL && item2->ProperPosition == 8) {
if (Loaded) TheGame->PlaySound("slide");
int tmp = item2->ActualPosition;
item2->ActualPosition = item_clicked->ActualPosition;
item_clicked->ActualPosition = tmp;
return;
}
if (item3 != NULL && item3->ProperPosition == 8) {
if (Loaded) TheGame->PlaySound("slide");
int tmp = item3->ActualPosition;
item3->ActualPosition = item_clicked->ActualPosition;
item_clicked->ActualPosition = tmp;
return;
}
if (item4 != NULL && item4->ProperPosition == 8) {
if (Loaded) TheGame->PlaySound("slide");
int tmp = item4->ActualPosition;
item4->ActualPosition = item_clicked->ActualPosition;
item_clicked->ActualPosition = tmp;
return;
}
}
BPMiniGame_ShufflePuzzler_Item* BPMiniGame_ShufflePuzzler::FindImageWithPos(int pos) {
for (int i = 0; i < Items.Count; ++i) {
if (Items[i]->ActualPosition == pos) {
return Items[i];
}
}
return NULL;
}
void BPMiniGame_ShufflePuzzler::Start() {
Shuffle();
TimeStarted = TheGame->TickCount;
Loaded = true;
}
int BPMiniGame_ShufflePuzzler::GetWeight() {
float TimePassed = (TheGame->TickCount - TimeStarted) / 1000.0f;
return MinMax(500 - round(TimePassed * 1.25));
}
void BPMiniGame_ShufflePuzzler::Render() {
TheGame->DrawImage(sfcBackground, 0, 0);
if (ShowResult) {
TheGame->FillRectangle((*TheGame->Red), 8, 104, 304, 304);
TheGame->DrawImage(sfcPicture, 10, 106);
} else {
for (int i = 0; i <= 8; ++i) {
for (int j = 0; j < Items.Count; ++j) {
BPMiniGame_ShufflePuzzler_Item* item = Items[j];
if (item->ActualPosition == i) {
int x;
int y;
y = DivRem(i, 3, x);
TheGame->DrawImage(item->Pic, 10 + (x * 100), 106 + (y * 100));
}
}
}
if (SuccessTime == -1) {
TheGame->DrawLine(110, 106, 110, 406, TheGame->Black, 3);
TheGame->DrawLine(210, 106, 210, 406, TheGame->Black, 3);
TheGame->DrawLine(10, 206, 310, 206, TheGame->Black, 3);
TheGame->DrawLine(10, 306, 310, 306, TheGame->Black, 3);
}
}
if (SuccessTime == -1) {
int TimePassed = TheGame->TickCount - TimeStarted;
if (sfcClock == NULL || RedrawClock()) {
TheGame->AllocString(&sfcClock, TheGame->TicksToTime(TimePassed)->c_str(), XLARGE, 152, 49, RIGHT);
}
TheGame->DrawString(sfcClock, WHITE, 149, 30);
}
}
void BPMiniGame_ShufflePuzzler::Tick() {
// this delay is longer than normal to give them a moment to see the finished picture clearly
if (SuccessTime != -1 && SuccessTime + 750 < TheGame->TickCount) {
Success();
}
}
void BPMiniGame_ShufflePuzzler::Shuffle() {
// shuffle a la brute force
for (int i = 0; i < 1000; ++i) {
SwapPiece(TheGame->RandomRange(0, 8));
}
}