-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoardManager.cpp
105 lines (104 loc) · 2.79 KB
/
BoardManager.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
#include "BoardManager.h"
BoardManager::BoardManager(unsigned int width, unsigned int height,
unsigned int mineCount, unsigned int tileCount)
{
_width = width;
_height = height;
_mineCount = mineCount;
_tileCount = tileCount;
}
void BoardManager::StoreValues(string fileName)
{
ifstream boardInfo(fileName);
if (boardInfo.is_open())
{
string lineFromFile;
getline(boardInfo, lineFromFile);
_width = stoi(lineFromFile) * 32;
getline(boardInfo, lineFromFile);
_height = stoi(lineFromFile) * 32 + 100;
getline(boardInfo, lineFromFile);
_mineCount = stoi(lineFromFile);
_tileCount = (_width / 32) * ((_height - 100)/ 32);
}
else
cout << "File couldn't open! Default Board Loaded." << endl;
}
void BoardManager::StorePremadeValues(string fileName, Tiles**& array2D)
{
ifstream boardInfo(fileName);
if (boardInfo.is_open())
{
unsigned int totalHeight = 0;
string lineFromFile;
unsigned int totalMine = 0;
while (getline(boardInfo, lineFromFile))
{
totalHeight += 1;
for (unsigned int i = 0; i < lineFromFile.length(); i++)
{
if (lineFromFile[i] == '1')
{
array2D[totalHeight - 1][i].ChangeTileValue(9);
totalMine += 1;
}
}
}
_height = totalHeight * 32 + 100;
_width = lineFromFile.length() * 32;
_mineCount = totalMine;
_tileCount = (_width / 32) * ((_height - 100) / 32);
for (unsigned int i = 0; i < (_height - 100) / 32; i++)
{
for (unsigned int j = 0; j < _width / 32; j++)
{
if (array2D[i][j].ReturnTileValue() == 9)
continue;
unsigned int totalMines = 0;
if (i != 0 && j != 0)
if (array2D[i - 1][j - 1].ReturnTileValue() == 9)
totalMines += 1;
if (i != 0)
if (array2D[i - 1][j].ReturnTileValue() == 9)
totalMines += 1;
if (i != 0 && j != _width / 32 - 1)
if (array2D[i - 1][j + 1].ReturnTileValue() == 9)
totalMines += 1;
if (j != 0)
if (array2D[i][j - 1].ReturnTileValue() == 9)
totalMines += 1;
if (j != _width / 32 - 1)
if (array2D[i][j + 1].ReturnTileValue() == 9)
totalMines += 1;
if (i != (_height - 100) / 32 - 1 && j != 0)
if (array2D[i + 1][j - 1].ReturnTileValue() == 9)
totalMines += 1;
if (i != (_height - 100) / 32 - 1)
if (array2D[i + 1][j].ReturnTileValue() == 9)
totalMines += 1;
if (i != (_height - 100) / 32 - 1 && j != _width / 32 - 1)
if (array2D[i + 1][j + 1].ReturnTileValue() == 9)
totalMines += 1;
array2D[i][j].ChangeTileValue(totalMines);
}
}
}
else
cout << "File couldn't open! Default Board Loaded." << endl;
}
unsigned int BoardManager::ReturnWidth()
{
return (_width/32);
}
unsigned int BoardManager::ReturnHeight()
{
return (_height - 100)/32;
}
unsigned int BoardManager::ReturnMineCount()
{
return _mineCount;
}
unsigned int BoardManager::ReturnTileCount()
{
return _tileCount;
}