-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cc
161 lines (148 loc) · 3.05 KB
/
player.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
#include "player.h"
#include "queue.h"
#include <cmath>
#include <iostream>
#include <sstream>
using namespace std;
Player::Player(string filename, int score, int level, int effect)
: score { score }
, level { level }
, effect { effect }
, filename { filename }
{
BlockFactory* bf;
switch (level) {
case 0:
bf = new Level0Factory(filename);
break;
case 1:
bf = new Level1Factory();
break;
case 2:
bf = new Level2Factory();
break;
case 3:
bf = new Level3Factory();
break;
case 4:
bf = new Level4Factory();
break;
default:
throw "Invalid level";
break;
}
score = 0;
blockFactory = unique_ptr<BlockFactory>(bf);
q = make_unique<Queue>();
}
void Player::setBlind(bool blind)
{
if (blind && effect % 2)
effect *= 2;
else if (!blind && !(effect % 2))
effect /= 2;
}
void Player::setHeavy(bool heavy)
{
if (heavy && effect % 3)
effect *= 3;
else if (!heavy && !(effect % 3))
effect /= 3;
}
void Player::setForce(string block)
{
int prime;
switch (block[0]) {
case 'I':
prime = 5;
break;
case 'J':
prime = 7;
break;
case 'L':
prime = 11;
break;
case 'O':
prime = 13;
break;
case 'S':
prime = 17;
break;
case 'T':
prime = 19;
break;
case 'Z':
prime = 23;
break;
default:
prime = 1;
break;
}
if (effect % prime)
effect *= prime;
else
effect /= prime;
}
bool Player::setLevel(int level)
{
if (level < 0 || level > 4)
return false;
this->level = level;
gameBoard.level = level;
BlockFactory *bf;
switch (level) {
case 0:
bf = new Level0Factory(filename);
break;
case 1:
bf = new Level1Factory();
break;
case 2:
bf = new Level2Factory();
break;
case 3:
bf = new Level3Factory();
break;
case 4:
bf = new Level4Factory();
score5turnsago = score;
break;
default:
throw "Invalid level";
break;
}
blockFactory = unique_ptr<BlockFactory>(bf);
return true;
}
int Player::clearRow()
{
string board = gameBoard.toString(false);
vector<int> rows;
for (int i = 0; i < Board::ROWS; ++i) {
for (int j = 0; j < Board::COLS; ++j) {
if (board[i * Board::COLS + j] == ' ') {
break;
}
if (j == Board::COLS - 1) {
for (auto &block : gameBoard.blocks) {
block->deleteRow(i);
}
rows.push_back(i);
}
}
}
int size = (unsigned int) rows.size();
if (size > 0) {
score += pow<int, int>(size + level, 2);
}
for (int i : rows) {
for (auto &block : gameBoard.blocks) {
block->shiftDown(i);
}
}
score += gameBoard.gc();
return size;
}
Player::~Player()
{
}