-
Notifications
You must be signed in to change notification settings - Fork 0
/
puyo.h
165 lines (159 loc) · 5.3 KB
/
puyo.h
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
#pragma once
class WELLRNG512Generator;
// enum class PuyoKind : char { BLANK, AKARI, KYOKO, YUI, CHINATSU, AYANO, CHITOSE, SAKURAKO, HIMAWARI, OBSTACLE, EXPLOSION };
// NOTHING, RED, YELLOW, BLACK, PINK, PURPLE, WHITE, BRIGHT BROWN, BLUE, TRANSPARENT, GRAY
enum class PuyoKind : char { BLANK, RED, YELLOW, GREEN, BLUE, OBSTACLE, EXPLOSION };
// a b c d e f g h i j k
class Puyo {
PuyoKind puyo;
public:
Puyo() {
puyo = PuyoKind::BLANK;
}
Puyo(PuyoKind puyo) {
this->puyo = puyo;
}
Puyo(const Puyo& puyo) {
this->puyo = puyo.puyo;
}
void Draw() {
switch (puyo) {
case PuyoKind::BLANK:
std::cout << Console::black << " ";
break;
case PuyoKind::RED:
std::cout << Console::red << "●";
break;
case PuyoKind::YELLOW:
std::cout << Console::yellow << "●";
break;
case PuyoKind::GREEN:
std::cout << Console::green << "●";
break;
case PuyoKind::BLUE:
std::cout << Console::blue << "●";
break;
case PuyoKind::OBSTACLE:
std::cout << Console::white << "○";
break;
case PuyoKind::EXPLOSION:
std::cout << Console::gray << "※";
break;
default: break;
}
}
bool IsSameColor(const Puyo& other) {
return puyo == other.puyo;
}
int ToInt() {
return static_cast<int>(puyo);
}
char Serialize() {
return 'a' + static_cast<char>(puyo);
}
void Deserialize(char c) {
c -= 'a';
puyo = static_cast<PuyoKind>(c);
}
bool IsBlank() {
return puyo == PuyoKind::BLANK;
}
bool IsObstacle() {
return puyo == PuyoKind::OBSTACLE;
}
};
class BiPuyo {
enum class Direction { UP, RIGHT, DOWN, LEFT };
Puyo main;
Puyo sub;
Direction direction = Direction::UP;
BiPuyo() { }
public:
BiPuyo(PuyoKind main, PuyoKind sub) : main(main), sub(sub) { }
Puyo GetMain() {
return main;
}
Puyo GetSub() {
return sub;
}
void GetDirection(int &x, int &y) {
switch (direction) {
case Direction::UP: { x = 0; y = -1; } break;
case Direction::RIGHT: { x = 1; y = 0; } break;
case Direction::DOWN: { x = 0; y = 1; } break;
case Direction::LEFT: { x = -1; y = 0; } break;
default: { x = 0; y = -1; } break;
}
}
void Draw(int offset_x, int offset_y) {
int x, y;
GetDirection(x, y);
std::cout << Console::GotoXY(X(offset_x), Y(offset_y));
main.Draw();
std::cout << Console::GotoXY(X(offset_x + x), Y(offset_y + y));
sub.Draw();
}
void Draw(int x, int y, int offset_x, int offset_y) {
int sub_x, sub_y;
GetDirection(sub_x, sub_y);
if (y > 0) {
std::cout << Console::GotoXY(X(x + offset_x), Y(y + offset_y));
main.Draw();
}
if (y + sub_y > 0) {
std::cout << Console::GotoXY(X(x + offset_x + sub_x), Y(y + offset_y + sub_y));
sub.Draw();
}
}
void RotateCW() {
// Rotate ClockWise
switch (direction) {
case Direction::UP: { direction = Direction::RIGHT; } break;
case Direction::RIGHT: { direction = Direction::DOWN; } break;
case Direction::DOWN: { direction = Direction::LEFT; } break;
case Direction::LEFT: { direction = Direction::UP; } break;
default: direction = Direction::UP; break;
}
}
void RotateCCW() {
// Rotate CounterClockWise
switch (direction) {
case Direction::UP: { direction = Direction::LEFT; } break;
case Direction::RIGHT: { direction = Direction::UP; } break;
case Direction::DOWN: { direction = Direction::RIGHT; } break;
case Direction::LEFT: { direction = Direction::DOWN; } break;
default: direction = Direction::UP; break;
}
}
std::string Serialize() {
std::ostringstream data;
data << main.Serialize();
data << sub.Serialize();
data << static_cast<int>(direction);
return data.str();
}
void Deserialize(std::string data) {
main.Deserialize(data.at(0));
sub.Deserialize(data.at(1));
direction = static_cast<Direction>(data.at(2) - '0');
}
};
class BiPuyoGenerator {
std::shared_ptr<WELLRNG512Generator> puyo_color_random;
public:
BiPuyoGenerator() {
puyo_color_random = std::make_shared<WELLRNG512Generator>();
}
BiPuyoGenerator(const int &seed) {
puyo_color_random = std::make_shared<WELLRNG512Generator>(seed);
}
std::shared_ptr<BiPuyo> GenerateBipuyo() {
return std::make_shared<BiPuyo>(
static_cast<PuyoKind>(puyo_color_random->Generate() % 4 + 1),
static_cast<PuyoKind>(puyo_color_random->Generate() % 4 + 1)
);
}
static std::shared_ptr<BiPuyo> GenerateEmptyBipuyo() {
return std::make_shared<BiPuyo>(PuyoKind::BLANK, PuyoKind::BLANK);
}
};