-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevelthree.cc
152 lines (142 loc) · 3.4 KB
/
levelthree.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
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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include "block.h"
#include "levels.h"
#include "levelthree.h"
#include <random>
#include "jblock.h"
#include "oblock.h"
#include "lblock.h"
#include "sblock.h"
#include "tblock.h"
#include "zblock.h"
using namespace std;
// Refer to levels.h for function explanations
shared_ptr<Block> levelthree::createBlock()
{
// Read from file if norandom option is enabled
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 number
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> levelthree::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 levelthree::is_file_exist(const string file)
{
ifstream infile{file};
return (infile.good());
}
void levelthree::blocksFromFile(string filename)
{
// controller will pass these to level
if (this->norandom)
{
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 levelthree::setRandom(bool val) {
this->norandom = val;
}
bool levelthree::getRandom() {
return this->norandom;
}
void levelthree::clearVector() {
block_file.clear();
this->curseq = 0;
}
bool levelthree::isHeavy() { return true; }
levelthree::~levelthree() {}