-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathToriverse.cpp
206 lines (172 loc) · 4.98 KB
/
Toriverse.cpp
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
#include "Toriverse.h"
#include "Harvester.h"
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <istream>
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
#include <fstream>
#include <vector>
using namespace std;
// Constructor: Defaults NULL toriverse
Toriverse::Toriverse(){
xDim = 1;
yDim = 1;
energyDensity = 0;
lifeTime = 1;
status = 1;
nSLocks = 0;
}
// Constructor: Default to NULL Toriverse with given momentum
Toriverse::Toriverse(char* filename){
nSLocks = 0;
ifstream toriSpec(filename);
toriSpec.seekg(0); //set position to 0
string line;
string val;
getline(toriSpec, line); //read line in file
istringstream lineStr(line);
vector<int> valsDim;
while(getline(lineStr, val, 'x')){
valsDim.push_back(atoi(val.c_str()));
}
xDim = valsDim[0];
yDim = valsDim[1];
Map.resize(xDim);
for (int i =0; i < xDim; i++){
Map[i].resize(yDim);
}
WHoles.resize(26);
for(int i=0;i<yDim;i++){
getline(toriSpec, line);//loop to read map
for (int j = 0; j < line.length(); j++){
Map[j][yDim-i-1] = line[j];
if (line[j] == '\\'){
nSLocks++;
}
if (96 < (int(line[j])) && (int(line[j])) < 123){
vector<int> tmpVec(0);
tmpVec.push_back(j);
tmpVec.push_back(yDim-i-1);
WHoles[int(line[j])-97].push_back(tmpVec);
}
else if (64 < (int(line[j])) && (int(line[j])) < 91){
nSLocks++;
}
}
}
getline(toriSpec, line); //read line in file
istringstream lineStrB(line);
vector<int> valsLife;
while(getline(lineStrB, val, ' ')){
valsLife.push_back(atoi(val.c_str()));
}
lifeTime = valsLife[1];
getline(toriSpec, line); //read line in file
istringstream lineStrC(line);
vector<int> valsEngD;
while(getline(lineStrC, val, ' ')){
valsEngD.push_back(atoi(val.c_str()));
}
energyDensity = valsEngD[2];
if (lifeTime > 0){
status = 1;
}
else{
status = 0;
}
}
Toriverse::~Toriverse(){}
void Toriverse::Interaction(Harvester& Harvey){
int tmpVecPosX = Harvey.getXPos();
int tmpVecPosY = Harvey.getYPos();
//cout << "INT Val of WHole is: " << int((getObject(tmpVecPosX,tmpVecPosY)).at(0)) << endl;
if (getObject(tmpVecPosX,tmpVecPosY)=="."){}
else if (getObject(tmpVecPosX,tmpVecPosY)=="*"){
setObject(".",tmpVecPosX,tmpVecPosY);
Harvey.setEnergy(energyDensity);
}
else if (getObject(tmpVecPosX,tmpVecPosY)=="@"){
Harvey.setScore(0);
Harvey.setStatus(0);
Harvey.setFate("DESTOYED BY BLACK HOLE!");
}
else if (getObject(tmpVecPosX,tmpVecPosY)=="\\"){
setObject(".",tmpVecPosX,tmpVecPosY);
nSLocks--;
cout << "SSLock Removed!" << endl;
}
else if ((96 < int((getObject(tmpVecPosX,tmpVecPosY)).at(0)) &&
int((getObject(tmpVecPosX,tmpVecPosY)).at(0)) < 123))
{
int lTmp = int((getObject(tmpVecPosX,tmpVecPosY)).at(0)) - 97;
if (tmpVecPosX == getWHolePos(lTmp,0,0) &&
tmpVecPosY == getWHolePos(lTmp,0,1)){
Harvey.setXPos(getWHolePos(lTmp,1,0));
Harvey.setYPos(getWHolePos(lTmp,1,1));
}
else if (tmpVecPosX == getWHolePos(lTmp,1,0) &&
tmpVecPosY == getWHolePos(lTmp,1,1)){
Harvey.setXPos(getWHolePos(lTmp,0,0));
Harvey.setYPos(getWHolePos(lTmp,0,1));
}
}
else if ((64 < int((getObject(tmpVecPosX,tmpVecPosY)).at(0)) &&
int((getObject(tmpVecPosX,tmpVecPosY)).at(0)) < 92))
{
int lTmp = int((getObject(tmpVecPosX,tmpVecPosY)).at(0)) - 65;
if (MLockStore.empty()){
vector<int> tmpVecML;
tmpVecML.push_back(tmpVecPosX);
tmpVecML.push_back(tmpVecPosY);
MLockStore.push_back(tmpVecML);
lastMLock = lTmp;
cout << "MLock FILLED WITH LETTER: " << getObject(tmpVecPosX,tmpVecPosY) << endl;
}
else if (!MLockStore.empty() && lastMLock == lTmp){
if (((MLockStore[0][0] != tmpVecPosX) ||
(MLockStore[0][1] != tmpVecPosY)) &&
((MLockStore[0][1] != tmpVecPosX) ||
(MLockStore[0][0] != tmpVecPosY))){
cout << "FOUND MATCHING LETTER: " << getObject(tmpVecPosX,tmpVecPosY) << endl;
vector<int> tmpVecML;
tmpVecML.push_back(tmpVecPosX);
tmpVecML.push_back(tmpVecPosY);
MLockStore.push_back(tmpVecML);
setObject(".",MLockStore[0][0],MLockStore[0][1]);
setObject(".",MLockStore[1][0],MLockStore[1][1]);
nSLocks--;
nSLocks--;
cout << "MSLock Removed!" << endl;
while(!MLockStore.empty()){
MLockStore.pop_back();
}
}
}
else if (!MLockStore.empty() && lastMLock != lTmp){
while(!MLockStore.empty()){
MLockStore.pop_back();
}
vector<int> tmpVecML;
tmpVecML.push_back(tmpVecPosX);
tmpVecML.push_back(tmpVecPosY);
MLockStore.push_back(tmpVecML);
lastMLock = lTmp;
cout << "MLOCK WAS OCCUPIED, EMPTIED AND NOW FILLED WITH LETTER: " << getObject(tmpVecPosX,tmpVecPosY) << endl;
}
}
//timeStep();
}
void Toriverse::printMap(){
cout << "Current Map:" <<endl;
for (int i = getYDim()-1; i >= 0; i--){
for (int j = 0; j < getXDim(); j++){
cout << getObject(j,i);
}
cout << endl;
}
cout << endl;
}