-
Notifications
You must be signed in to change notification settings - Fork 1
/
othergameinit.cpp
157 lines (137 loc) · 3.75 KB
/
othergameinit.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
#include "othergameinit.h"
#include "abstractpixmapwidget.h"
#include "classicgamewidget.h"
#include "endlessgamewidget.h"
#include "timinggamewidget.h"
#include "gamerecord.h"
#include "statistic.h"
#include "ball.h"
extern GameRecord gameRecord;
extern Statistic statistic;
// The names of the files
static const char* fileNames[] = {
"SwapClassic",
"RotateClassic",
"SwapEndless",
"RotateEndless"
};
// Convert a ball to an integer
int ballToInt(Ball *ball)
{
if (ball == NULL)
return 0xFFFF;
return ((int) ball->getColor()) +
(ball->getLocked() ? 0x8000 : 0x0000);
}
// Convert an integer to a ball
Ball *intToBall(int value)
{
if (value == 0xFFFF)
return NULL;
Ball::Color color = (Ball::Color)(value & 0x7FFF);
bool locked = (value & 0x8000) == 0x8000;
Ball *result = new Ball(color);
result->setLocked(locked);
return result;
}
AbstractPixmapWidget *OtherGameInit::initOtherGame
(AbstractRule::Gesture gesture, int type)
{
// Create the correct game
if (type == 0)
return new ClassicGameWidget(gesture);
else if (type == 1)
return new EndlessGameWidget(gesture);
else if (type == 2)
return new TimingGameWidget(gesture);
return 0;
}
int OtherGameInit::getHighest(int index)
{
return statistic.getStatistic(
(Statistic::StatisticType)((int)Statistic::SwapClassicPoint + index));
}
void OtherGameInit::setHighest(int index, int score)
{
statistic.changeStatistic(
(Statistic::StatisticType)((int)Statistic::SwapClassicPoint + index),
score);
}
void OtherGameInit::testHighest(int index, int score)
{
// Record the score if neccessary
if (getHighest(index) < score)
setHighest(index, score);
}
OtherGameRecord *OtherGameInit::loadOtherGame(int index)
{
if (index > 3)
return NULL;
// The record
OtherGameRecord *result = new OtherGameRecord();
result->highestScore = getHighest(index);
// Set the values to default if neccessary
if (!gameRecord.exists(fileNames[index]))
{
result->currentLevel = 1;
result->minScore = 0;
result->currentScore = 0;
result->maxScore = 30;
result->flame = 0;
result->star = 0;
result->balls = NULL;
}
// Set the values to the values in the file
else
{
int *data;
int size;
gameRecord.readDataArr(fileNames[index], data, size);
int count = 0;
result->currentLevel = data[count++];
result->minScore = data[count++];
result->currentScore = data[count++];
result->maxScore = data[count++];
result->flame = data[count++];
result->star = data[count++];
result->balls = new Ball *[size - count];
int ballCount = count;
for (int i = 0;i < size - ballCount;++i)
result->balls[i] = intToBall(data[count++]);
}
return result;
}
void OtherGameInit::saveOtherGame(OtherGameRecord *record,
int index,
int ballCount)
{
if (index > 3)
return;
// Test the highest score
if (getHighest(index) < record->currentScore)
setHighest(index, record->currentScore);
// Make the data array
int *data = new int[6 + ballCount];
int count = 0;
data[count++] = record->currentLevel;
data[count++] = record->minScore;
data[count++] = record->currentScore;
data[count++] = record->maxScore;
data[count++] = record->flame;
data[count++] = record->star;
if (record->balls)
for (int i = 0;i < ballCount;++i)
data[count++] = ballToInt(record->balls[i]);
else
for (int i = 0;i < ballCount;++i)
data[count++] = ballToInt(NULL);
// Write the data to the file
gameRecord.writeDataArr(fileNames[index], data, 6 + ballCount);
// Release space
delete [] data;
}
void OtherGameInit::clearGame(int index)
{
// Remove the file of the game
gameRecord.remove(fileNames[index]);
}