-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cc
executable file
·247 lines (219 loc) · 6.05 KB
/
Game.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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include "Game.h"
#include "Interpretter.h"
#include "Block.h"
#include "Level.h"
#include "TextView.h"
#include "Score.h"
#include "Board.h"
#include "Coords.h"
#include "Level0.h"
#include "Level1.h"
#include "Level2.h"
#include "Level3.h"
#include "Level4.h"
#include "SpecialBlock.h"
#include "SBlock.h"
#include "OBlock.h"
#include "ZBlock.h"
#include "TBlock.h"
#include "JBlock.h"
#include "LBlock.h"
#include "iblock.h"
#include "CBlock.h"
#include "NBlock.h"
#include "Level5.h"
#include "GraphicView.h"
#include "View.h"
#include <string>
#include <ctime>
#include <stdexcept>
#include <iostream>
using namespace std;
int MAXLEVEL = 5;
Game::Game(): textonly{false}, loss{false}{
theInterpreter = new Interpretter{this};
displays.emplace_back(new TextView{rows, columns});
theBoard = new Board
{rows, columns, Coords{startXCoordinate, startYCoordinate}, displays};
currScore = new Score;
scriptFile = "sequence.txt"; // setting default file to read from
currLevel = nullptr;
}
Game::~Game() {
delete theInterpreter;
delete theBoard;
delete currBlock;
delete currScore;
delete currLevel;
int len = displays.size();
for(int i = 0; i < len; ++i){delete displays[i];}
displays.clear();
}
void Game::left(int n){
int level = currLevel->getLevel();
for(int i = 0; i < n; ++i){
currBlock->moveLeft();
}
if(level == 3 || level == 4) currBlock->moveDown();
updateDisplay();
}
void Game::right(int n){
int level = currLevel->getLevel();
for(int i =0; i < n; ++i){
currBlock->moveRight();
}
if(level == 3 || level == 4) currBlock->moveDown();
updateDisplay();
}
void Game::down(int n){
int level = currLevel->getLevel();
for(int i = 0; i < n; ++i){
currBlock->moveDown();
}
if(level == 3 || level ==4) currBlock->moveDown();
updateDisplay();
}
void Game::drop(int n){
for(int i = 0; i < n; ++i){
currBlock->drop();
int scoreInc = theBoard->updateScore(currLevel->getLevel());
currScore->incScore(scoreInc);
if(theBoard->getTotalRowsCleared() > totalRowsCleared){
totalRowsCleared = theBoard->getTotalRowsCleared();
trySinceLastClear = 0;
}else{
++trySinceLastClear;
}
if(trySinceLastClear >= 5 && currLevel->getLevel() == 4){
nextBlockType = '*';
trySinceLastClear = 0;
}
updateDisplay();
generateNextBlock();
if(loss) return;
}
}
void Game::clockwise(int n){
int level = currLevel->getLevel();
for(int i = 0; i < n; ++i){
currBlock->clockwise();
}
if(level == 3 || level ==4) currBlock->moveDown();
updateDisplay();
}
void Game::anticlockwise(int n){
int level = currLevel->getLevel();
for(int i = 0; i < n; ++i){
currBlock->anticlockwise();
}
if(level == 3 || level ==4) currBlock->moveDown();
updateDisplay();
}
void Game::levelUp(int n){
for(int i = 0; i < n; ++i){
int currLevelNum = currLevel->getLevel();
//updateDisplay();
setLevel(currLevelNum+1);
//updateDisplay();
}
updateDisplay();
}
void Game::levelDown(int n){
for(int i = 0; i < n; ++i){
int currLevelNum = currLevel->getLevel();
// delete currLevel;
setLevel(currLevelNum-1);
}
updateDisplay();
}
void Game::setLevel(int levelNum){
Level* tmp = currLevel;
if(levelNum < 0) levelNum = 0; // checking if level is in the correct range
if(levelNum > MAXLEVEL) levelNum = MAXLEVEL; // checking if level is in the correct range
if(levelNum == 0){ currLevel = new Level0{scriptFile}; }
else if(levelNum == 1) { currLevel = new Level1{scriptFile}; }
else if(levelNum == 2) { currLevel = new Level2{scriptFile}; }
else if(levelNum == 3) { currLevel = new Level3{scriptFile}; }
else if(levelNum == 4) { currLevel = new Level4{scriptFile}; }
else if(levelNum == 5) { currLevel = new Level5{scriptFile}; }
else {}
delete tmp;
}
void Game::setScriptFile(string fileName){ scriptFile = fileName; }
void Game::generateNextBlock(){
int levelNum = currLevel->getLevel();
// if else statements to set it to its new value
try{
if(nextBlockType=='I'){currBlock = new IBlock{theBoard, levelNum};}
else if(nextBlockType=='S'){currBlock = new SBlock{theBoard, levelNum};}
else if(nextBlockType=='Z'){currBlock = new ZBlock{theBoard, levelNum};}
else if(nextBlockType=='T'){currBlock = new TBlock{theBoard, levelNum};}
else if(nextBlockType=='O'){currBlock = new OBlock{theBoard, levelNum};}
else if(nextBlockType=='J'){currBlock = new JBlock{theBoard, levelNum};}
else if(nextBlockType=='L'){currBlock = new LBlock{theBoard, levelNum};}
else if(nextBlockType=='C'){currBlock = new CBlock{theBoard, levelNum};}
else if(nextBlockType=='N'){currBlock = new NBlock{theBoard, levelNum};}
else if(nextBlockType=='*'){
currBlock = new SpecialBlock{theBoard, levelNum};
nextBlockType = currLevel->getCharForBlockType();
drop(1);
}
else{}
}catch(out_of_range){
currBlock = nullptr;
loss = true;
lost();
}
// update nextBlockType
nextBlockType = currLevel->getCharForBlockType();
updateDisplay();
}
void Game::start(){
if(!textonly){
GraphicView* gv = new GraphicView;
displays.emplace_back(gv);
theBoard->addView(gv);
}
if(!currLevel) currLevel = new Level0{scriptFile};
nextBlockType = currLevel->getCharForBlockType();
generateNextBlock();
theInterpreter->start();
}
void Game::lost(){
theInterpreter->gameLost();
}
void Game::restart(){
theBoard->clearBoard();
currScore->resetScore();
delete currBlock;
nextBlockType = currLevel->getCharForBlockType();
generateNextBlock();
theInterpreter->start();
}
void Game::updateDisplay(){
int level = currLevel->getLevel();
int cscore = currScore->getCurrScore();
int hscore = currScore->getHighScore();
int len = displays.size();
for(int i = 0; i < len; ++i){
displays[i]->update(level, cscore, hscore, nextBlockType);
}
}
void Game::setCurrBlock(char c) {
nextBlockType = c;
currBlock->removeItFromBoard();
delete currBlock;
generateNextBlock();
}
void Game::setLevelRandom(){
int lev = currLevel->getLevel();
if(lev == 3 || lev == 4) currLevel->setRandom();
}
void Game::setLevelNoRandom(string fileName){
int lev = currLevel->getLevel();
if(lev == 3 || lev == 4){
currLevel->setFile(fileName);
currLevel->removeRandom();
}
}
void Game::setTextOnly(){ textonly = true; }