-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel.cc
More file actions
140 lines (120 loc) · 2.76 KB
/
Level.cc
File metadata and controls
140 lines (120 loc) · 2.76 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
#include "Level.h"
#include "abstractblock.h"
#include <iostream>
#include "sblock.h"
#include "oblock.h"
#include "jblock.h"
#include "lblock.h"
#include "iblock.h"
#include "tblock.h"
#include "zblock.h"
Level::Level() { }
Level::~Level() { } //delete block_file;}
Level::Level(std::map<char, int> weights) : curr_weights{ weights }, block_count{ 0 }, is_random{ true }, weight_sum{ 0 } {
for (auto iter = weights.begin(); iter != weights.end(); iter++) {
weight_sum += iter->second;
}
}
void Level::updateBlockCount(bool cleared_row) {
if (cleared_row) {
block_count = 0;
}
else {
block_count++;
}
}
void Level::setFile(std::unique_ptr<std::ifstream> new_stream) {
block_file = std::move(new_stream);
}
char Level::getTypeFromFile() const {
char next_block;
*block_file >> next_block;
if (block_file->fail()) {
// throw an exception
}
if (block_file->eof()) {
block_file->clear();
block_file->seekg(0, std::ios::beg);
return getTypeFromFile();
}
return next_block;
}
void Level::move(AbstractBlock* currentBlock, std::string command) {
if (!currentBlock) {
return;
}
currentBlock->move(command);
//currentBlock->move("down");
}
void Level::heavyMove(AbstractBlock* currentBlock, std::string command) {
if (!currentBlock) {
return;
}
currentBlock->move(command);
if (command == "left" || command == "right") {
currentBlock->move("down");
currentBlock->move("down");
}
}
std::unique_ptr<Level> Level::getNextLevel() const {
return std::make_unique<Level>();
}
std::unique_ptr<Level> Level::getPrevLevel() const {
return std::make_unique<Level>();
}
int Level::getLevel() const {
return 0;
}
int Level::getRandom() const {
return (std::rand() % (weight_sum + 1));
}
char Level::sampleType() const {
auto r = getRandom();
char type = ' ';
for (auto iter = curr_weights.begin(); iter != curr_weights.end(); iter++) {
auto curr_weight = iter->second;
auto curr_type = iter->first;
if (r <= curr_weight) {
type = curr_type;
break;
}
r -= curr_weight;
}
return type;
}
std::unique_ptr<AbstractBlock> Level::createBlock(char type) const {
switch (type) {
case 'S':
return std::make_unique<SBlock>();
break;
case 'Z':
return std::make_unique<ZBlock>();
break;
case 'L':
return std::make_unique<LBlock>();
break;
case 'I':
return std::make_unique<IBlock>();
break;
case 'J':
return std::make_unique<JBlock>();
break;
case 'O':
return std::make_unique<OBlock>();
break;
case 'T':
return std::make_unique<TBlock>();
break;
default:
// throw exception
return nullptr;
}
}
std::unique_ptr<AbstractBlock> Level::getNextBlock() const {
const auto type = is_random ? sampleType() : getTypeFromFile();
auto block = createBlock(type);
return block;
}
void Level::setRandom(bool random) {
is_random = random;
}