-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcells.cpp
201 lines (168 loc) · 3.96 KB
/
cells.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
#include "cells.hpp"
size_t Cell::mCellNewCount = 0;
size_t Cell::mCellDelCount = 0;
size_t Cell::mECellNewCount = 0;
size_t Cell::mECellDelCount = 0;
size_t Cell::mBCellNewCount = 0;
size_t Cell::mBCellDelCount = 0;
size_t Cell::mNCellNewCount = 0;
size_t Cell::mNCellDelCount = 0;
size_t Cell::mLCellNewCount = 0;
size_t Cell::mLCellDelCount = 0;
void *Cell::operator new(size_t size)
{
void *ptr = std::malloc(size);
++mCellNewCount;
return ptr;
}
void Cell::operator delete(void *ptr)
{
++mCellDelCount;
std::free(ptr);
}
size_t Cell::sizeOfSelf() const
{
return sizeof(mView) + sizeof(mColor) + sizeof(mIsActivated) + sizeof(mAlterView) + sizeof(mBombsAround) + sizeof(mPoints) + sizeof(mUniqueView);
}
uint8_t Cell::mark()
{
return mAlterView = mAlterView == MarkedCellView ? mView : MarkedCellView;
}
void Cell::save(FILE *file)
{
fwrite(&mView, sizeof(mView), 1, file);
fwrite(&mColor, sizeof(mColor), 1, file);
fwrite(&mIsActivated, sizeof(mIsActivated), 1, file);
fwrite(&mAlterView, sizeof(mIsActivated), 1, file);
fwrite(&mBombsAround, sizeof(mBombsAround), 1, file);
fwrite(&mUniqueView, sizeof(mUniqueView), 1, file);
fwrite(&mPoints, sizeof(mPoints), 1, file);
}
void Cell::load(FILE *file)
{
fread(&mView, sizeof(mView), 1, file);
fread(&mColor, sizeof(mColor), 1, file);
fread(&mIsActivated, sizeof(mIsActivated), 1, file);
fread(&mAlterView, sizeof(mIsActivated), 1, file);
fread(&mBombsAround, sizeof(mBombsAround), 1, file);
fread(&mUniqueView, sizeof(mUniqueView), 1, file);
fread(&mPoints, sizeof(mPoints), 1, file);
}
uint8_t Cell::view() const
{
return mIsActivated ? mView : mAlterView;
}
int Cell::uniqueIntView() const
{
return mUniqueView;
}
stf::ColorTable Cell::color() const
{
return mIsActivated ? mColor : stf::ColorTable::Default;
}
int Cell::pickUpPoints()
{
if(mPoints)
return mPoints--;
return mPoints;
}
int Cell::bombsAround() const
{
return mBombsAround;
}
void Cell::bombsAround(int value)
{
mBombsAround = value;
switch (mBombsAround) {
case 1: mColor = stf::ColorTable::Cyan; break;
case 2: mColor = stf::ColorTable::Green; break;
case 3: mColor = stf::ColorTable::Red; break;
case 4: mColor = stf::ColorTable::Blue; break;
case 5: mColor = stf::ColorTable::Yellow; break;
case 6: mColor = stf::ColorTable::Magenta; break;
case 7: mColor = stf::ColorTable::Default; break;
case 8: mColor = stf::ColorTable::White; break;
}
}
Cell *Cell::activate()
{
mIsActivated = true;
return this;
}
bool Cell::isActivated() const
{
return mIsActivated;
}
EmptyCell::EmptyCell()
{
mView = '-';
mUniqueView = 1;
}
void *EmptyCell::operator new(size_t size)
{
void *ptr = std::malloc(size);
++mECellNewCount;
return ptr;
}
void EmptyCell::operator delete(void *ptr)
{
++mECellDelCount;
std::free(ptr);
}
BombCell::BombCell()
{
mView = 'o';
mUniqueView = 2;
mPoints = 1;
}
stf::ColorTable BombCell::color() const
{
return mIsActivated ? stf::ColorTable::Magenta : mColor;
}
void *BombCell::operator new(size_t size)
{
void *ptr = std::malloc(size);
++mBCellNewCount;
return ptr;
}
void BombCell::operator delete(void *ptr)
{
++mBCellDelCount;
std::free(ptr);
}
BombsNeighborCell::BombsNeighborCell()
{
mUniqueView = 3;
}
uint8_t BombsNeighborCell::view() const
{
return mIsActivated ? '0' + mBombsAround : mAlterView;
}
void *BombsNeighborCell::operator new(size_t size)
{
void *ptr = std::malloc(size);
++mNCellNewCount;
return ptr;
}
void BombsNeighborCell::operator delete(void *ptr)
{
++mNCellDelCount;
std::free(ptr);
}
LifeCell::LifeCell()
{
mUniqueView = 4;
mView = 'L';
mColor = stf::ColorTable::Red;
}
void *LifeCell::operator new(size_t size)
{
void *ptr = std::malloc(size);
++mLCellNewCount;
return ptr;
}
void LifeCell::operator delete(void *ptr)
{
++mLCellDelCount;
std::free(ptr);
}