-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameState.cpp
More file actions
195 lines (168 loc) · 6.75 KB
/
GameState.cpp
File metadata and controls
195 lines (168 loc) · 6.75 KB
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
#include "GameState.h"
#include "fstream"
GameState::GameState(sf::Vector2i _dimensions, int _numberOfMines) // default constructor
: dimensions(_dimensions), numberOfMines(_numberOfMines), flagCount(_numberOfMines), playStatus(PlayStatus::PLAYING) {
// Initialize the state based on the dimensions and number of mines
initializeRandomState(); // calls a private function that sets up the default constructor
}
GameState::GameState(const char* filepath) : flagCount(0), mineCount(0), playStatus(PlayStatus::PLAYING) { // file based constructor
// Initialize the state based on the file
initializeFromFile(filepath); // calls private function that sets up the file constructor
}
GameState::~GameState() { // private destructor for gamestate
// Release memory for tiles
for (int i = 0; i < dimensions.x; ++i) {
for (int j = 0; j < dimensions.y; ++j) {
delete grid[i][j];
}
}
}
int GameState::getFlagCount() const { // returns the flag count
return flagCount;
}
int GameState::getMineCount() const { // returns the number of mines
return numberOfMines;
}
Tile* GameState::getTile(int x, int y) const { // returns the tile at a spesific part in the grid
// Check if coordinates are within bounds
if (x >= 0 && x < dimensions.x && y >= 0 && y < dimensions.y) {
return grid[x][y];
}
return nullptr;
}
GameState::PlayStatus GameState::getPlayStatus() const { // returns the games playstatus
return playStatus;
}
void GameState::setPlayStatus(PlayStatus _status) { // sets the playstatus
playStatus = _status;
}
void GameState::initializeRandomState() { // initializes the random gamstate
// Makes a grid of Tiles
sf::Vector2f tilePosition(0, 0);
for(int i = 0; i < dimensions.x; i++){ // loops through the grid and makes a new tile for the dimentions
for(int j = 0; j < dimensions.y; j++){
grid[i][j] = (new Tile(tilePosition));
tilePosition.y += 32;
if(tilePosition.y >= 32*16){
tilePosition.y = 0;
tilePosition.x += 32;
}
}
}
// Sets Mines
mineCount = 0;
while(mineCount < numberOfMines){ // loops through and sets the mines
for (int i = 0; i < dimensions.x; i++) {
for (int j = 0; j < dimensions.y; j++) {
if (rand() % 400 == 0 && mineCount < numberOfMines && !grid[i][j]->getMine()) {
mineCount++;
grid[i][j]->setMine();
}
}
}
}
// Gets Neighbors
int counter = 0;
for (int x = 0; x < dimensions.x; ++x) { // loops through the grid and sets the neighbors making sure that there is no neighbor then it is a nullptr
for (int y = 0; y < dimensions.y; ++y) {
// Makes an array of neighbors for the current tile
std::array<Tile*, 8> neighbors;
// Check and set neighbors for the current tile
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
// Skip the current tile itself
if (i == 0 && j == 0) {
continue;
}
int neighborX = x + i;
int neighborY = y + j;
// Check if the neighbor coordinates are within bounds
if (neighborX >= 0 && neighborX < dimensions.x &&
neighborY >= 0 && neighborY < dimensions.y) {
// Set the neighbor for the current tile
neighbors[counter] = grid[neighborX][neighborY];
}else{
neighbors[counter] = nullptr;
}
counter++;
}
}
counter = 0;
grid[x][y]->setNeighbors(neighbors);
}
}
}
void GameState::initializeFromFile(const char* filepath) { // initializes from the file
std::ifstream fileBoard; // Makes an ifstream
fileBoard.open(filepath);
std::string myString; // makes a string to hold what is in the file
if(fileBoard.is_open()){
// makes a board
sf::Vector2f tilePosition(0, 0);
int y = 0;
int x = 0;
//Gets dimentions
while (std::getline(fileBoard, myString) && myString != "") {
y++;
x = myString.length();
}
fileBoard.close();
fileBoard.open(filepath);
sf::Vector2i fileDimensions(x, y);
dimensions = fileDimensions;
for(int i = 0; i < dimensions.x; i++){ // makes the new grid the size of the file
for(int j = 0; j < dimensions.y; j++){
grid[i][j] = (new Tile(tilePosition));
tilePosition.y += 32;
if(tilePosition.y >= 32*16){
tilePosition.y = 0;
tilePosition.x += 32;
}
}
}
flagCount = 0;
mineCount = 0;
//sets mines
char ch; // sets a mine on where there is a one
for(int i = 0; i < dimensions.y; i++){
for(int j = 0; j < dimensions.x; j++){
fileBoard >> ch;
if (ch - 48 == 1){
mineCount++;
grid[j][i]->setMine();
flagCount++;
mineCount++;
}
}
}
int counter = 0; // keeps track of which neighbor she is in
for (int x = 0; x < dimensions.x; ++x) {
for (int y = 0; y < dimensions.y; ++y) {
// Makes an array of neighbors for the current tile
std::array<Tile*, 8> neighbors;
// Check and set neighbors for the current tile
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
// Skip the current tile itself
if (i == 0 && j == 0) {
continue;
}
int neighborX = x + i;
int neighborY = y + j;
// Check if the neighbor coordinates are within bounds
if (neighborX >= 0 && neighborX < dimensions.x &&
neighborY >= 0 && neighborY < dimensions.y) {
// Set the neighbor for the current tile
neighbors[counter] = grid[neighborX][neighborY];
}else{
neighbors[counter] = nullptr;
}
counter++;
}
}
counter = 0;
grid[x][y]->setNeighbors(neighbors); // sets the neighbors
}
}
}
}