-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulator.cpp
172 lines (133 loc) · 3.53 KB
/
emulator.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
#include <iostream>
#include <vector>
#include <windows.h>
#include <stdlib.h>
using namespace std;
class Cell{
public:
string type;
string displayCharacter;
int x,y;
bool rendered = false;
Cell(string InitialType){
type = InitialType;
if(type == "Empty"){displayCharacter=" ";}
else if(type == "Sand"){displayCharacter = "#";}
else if(type == "Obstacle"){displayCharacter = "x";}
}
};
class Table{
public:
int tableSize;
vector<vector<Cell>> table;
void constructTable(){
for(int y = 0;y<tableSize;y++){
vector<Cell> row;
for(int x = 0;x<tableSize;x++){
Cell emptyCell = Cell("Empty");
emptyCell.x = x;
emptyCell.y = y;
row.push_back(emptyCell);
}
table.push_back(row);
}
}
void displayTable(){
for(int y = 0;y<tableSize;y++){
string row = "";
for(int x = 0;x<tableSize;x++){
row+=table[y][x].displayCharacter;
}
cout<<row<<endl;
}
}
bool cellExists(int x, int y){
if ((0<=x && x<tableSize)&&(0<=y &&y<tableSize)){
return true;
}
return false;
}
void moveCell(Cell cell, int x, int y){
int newX,newY;
newX = x;
newY = y;
Cell placeholder = Cell("Empty");
if (true){
table[cell.y][cell.x] = placeholder;
}
cell.y = newY;
cell.x = newX;
cell.rendered = true;
table[newY][newX] = cell;
}
void renderFrame(){
for(int i = 0; i<tableSize;i++){
for(int j = 0;j<tableSize;j++){
table[j][i].rendered = false;
}
}
}
void calculateNextSandCollision(Cell cell){
int x,y;
x = cell.x;
y = cell.y;
if(cellExists(x,y+1) && getCell(x,y+1).type == "Empty"){
moveCell(cell,x,y+1);
}
else if(cellExists(x+1,y+1) && getCell(x+1,y+1).type == "Empty"){
moveCell(cell,x+1,y+1);
}
else if(cellExists(x-1,y+1) && getCell(x-1,y+1).type == "Empty"){
moveCell(cell,x-1,y+1);
}
}
void randomizeCellSpread(Cell cell, int amount){
for (int i = 0;i<amount;i++){
int x,y;
x = rand() % tableSize;
y = rand() % tableSize;
moveCell(cell,x,y);
}
}
void spawnCell(Cell cell,int x,int y){
cell.x = 0;
cell.y = 0;
moveCell(cell,x,y);
}
Cell getCell(int x,int y){
return table[y][x];
}
Table(int tsize){
tableSize = tsize;
}
};
int main()
{
Table grid = Table(40);
grid.constructTable();
int iterations = 0;
grid.displayTable();
int renderStep = 8;
Cell obs = Cell("Obstacle");
grid.randomizeCellSpread(obs,60);
while (true){
iterations +=1;
if (iterations%3 == 0){
Cell cell = Cell("Sand");
grid.spawnCell(cell,3,1);
}
for (int y = 0;y<grid.tableSize;y++){
for(int x = 0;x<grid.tableSize;x++){
Cell currentCell = grid.getCell(x,y);
if (currentCell.type == "Sand" && currentCell.rendered == false){
grid.calculateNextSandCollision(currentCell);
}
}
}
grid.displayTable();
Sleep(1000/renderStep);
system("cls");
grid.renderFrame();
}
return 0;
}