-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevelfive.cc
128 lines (116 loc) · 3.08 KB
/
levelfive.cc
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include "block.h"
#include "levels.h"
#include "levelfive.h"
#include <random>
using namespace std;
// Refer to levels.h for function explanations
// Returns a shared pointer to type Block
shared_ptr<Block> levelfive::createBlock() {
// Read from file norandom == true
if (this->norandom) {
char nowBlock = block_file.at(this->curseq);
this->curseq++;
// go through the same sequence if reached the end of sequence
if (this->curseq >= block_file.size()) {
this->curseq = 0;
}
return this->makeBlocks(nowBlock, this->isHeavy());
} else {
// randomly generate using the probabilities given
int generateLucky = rand()%9+1;
if (generateLucky == 1 || generateLucky == 2) {
return this->makeBlocks('S', this->isHeavy());
}
else if (generateLucky== 3 || generateLucky == 4) {
return this->makeBlocks('Z', this->isHeavy());
}
else if(generateLucky==5) {
return this->makeBlocks('I', this->isHeavy());
}
else if(generateLucky==6) {
return this->makeBlocks('J', this->isHeavy());
}
else if(generateLucky==7) {
return this->makeBlocks('L', this->isHeavy());
}
else if(generateLucky==8) {
return this->makeBlocks('O', this->isHeavy());
}
else {
return this->makeBlocks('T', this->isHeavy());
}
}
}
// Implementing methods
shared_ptr<Block> levelfive::makeBlocks(char type, bool isHeavy) {
shared_ptr<Block> p;
switch(type) {
case 'I':
p = make_shared<IBlock>(isHeavy);
break;
case 'J':
p = make_shared<JBlock>(isHeavy);
break;
case 'L':
p = make_shared<LBlock>(isHeavy);
break;
case 'O':
p = make_shared<OBlock>(isHeavy);
break;
case 'S':
p = make_shared<SBlock>(isHeavy);
break;
case 'Z':
p = make_shared<ZBlock>(isHeavy);
break;
default:
p = make_shared<TBlock>(isHeavy);
break;
}
return p;
}
// check if file exists
bool levelfive::is_file_exist(const string file) {
std::ifstream infile(file);
return infile.good();
}
void levelfive::blocksFromFile(string filename) {
if (this->norandom) {
// controller will pass these to level
if (is_file_exist(filename)) {
ifstream file{filename};
char t;
while(file>>t) {
block_file.emplace_back(t);
}
}
else {
string s;
if (filename!="") {
s = "File does not exist or is not readable";
}
else { s = "Empty filename"; }
throw(s);
}
}
// Do nothing otherwise;
}
void levelfive::setRandom(bool val) {
this->norandom = val;
}
bool levelfive::getRandom() {
return this->norandom;
}
void levelfive::clearVector() {
block_file.clear();
this->curseq = 0;
}
bool levelfive::isHeavy() {
return true;
}
levelfive::~levelfive() {}