-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMineSweeperGrid.cpp
More file actions
245 lines (235 loc) · 6.11 KB
/
MineSweeperGrid.cpp
File metadata and controls
245 lines (235 loc) · 6.11 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#include <iostream>
#include <time.h>
#include "Color.h"
#include "MineSweeperGirdHeader.h"
void DeleteCharFromArray(char* arr, unsigned int& Length, char Target)
{
bool bFound = false;
unsigned int x = 0;
while (!bFound && x < Length)
{
bFound = arr[x++] == Target;
}
x--;
if (bFound)
{
if (x == Length)
arr[Length-1] = 0;
else
for (; x < Length; x++)
arr[x] = arr[x + 1];
Length--;
}
}
MineGrid::MineGrid(unsigned int RowCounter = 0, unsigned int ColumCounter = 0) : ColCount(ColumCounter), RowCount(RowCounter)
{
MeSize = ColumCounter * RowCounter;
GridMemoryDisplay = new char[MeSize];
GridMemoryValue = new unsigned char[MeSize];
for (unsigned int x = 0; x < MeSize; x++)
{
GridMemoryDisplay[x] = '#';
GridMemoryValue[x] = 0;
}
}
MineGrid::~MineGrid()
{
delete[] GridMemoryValue;
delete[] GridMemoryDisplay;
}
void MineGrid::MakeTitle()
{
std::cout << "__";
for (unsigned int x = 0; x < ColCount; x++)
printf("|%c|",'A'+x);
std::cout << std::endl;
}
void MineGrid::PrintRules()
{
printf("In this game + is a bomb, # is a covered square.\nYou can select the square to be uncovered by entering the colum and row ID\nPlease enter the size of the grid you would like to unmine.\n");
}
void MineGrid::GetInput()
{
printf("Colums and Rows:");
}
void MineGrid::Setup()
{
PrintRules();
GetInput();
}
void MineGrid::GenerateBombs()
{
#define DebugFix 1;
#if DebugFix
//This seeds the bombs to a 1/8 Density of the grid size
unsigned int BombCount = (unsigned int)(MeSize * BombDensity);
//Bomblist is used to prevent bombs generating on the same spot
unsigned int* BombList = new unsigned int[BombCount];
unsigned int BLIndex = 0;
//List of effected Cells
#else
unsigned int BombCount = 1;
#endif // 0
//Seeds Random
srand(time(NULL));
//The list of cells effect acording to active cell placement
char TopArr[] = "123", BotArr[] = "876", LefArr[] = "146",RigArr[] = "358";
unsigned int ActiveCell;
//Iterator to loop through the bomblist array
unsigned int iIter;
bool Found;
for (unsigned int x = 0; x < BombCount; x++)
{
#if DebugFix
//Yes I use GoTo and no I don't care This is used in a correct and simple way
//This keeps generating bomb locations till it finds a spot that has not been used
char ArrEffected[] = "12354678";
unsigned int ArrEffLength = 8;
GenerateRandomAgain:
ActiveCell = rand() % MeSize;
Found = false;
iIter = 0;
while (!Found && iIter < BLIndex)//Check each Value for duplication
Found = BombList[iIter++] == ActiveCell;
if (Found)//Checks if the value is a dup
goto GenerateRandomAgain;
else//Adds new bomb Location
BombList[BLIndex++] = ActiveCell;
#else
//Debug used to create static bomb spawns
unsigned int TestArray[] = {89,87,55,1 };
unsigned int ActiveCell = TestArray[x];
#endif
//Assign the value 10 which indicates that there is a bomb placed
GridMemoryValue[ActiveCell] = 10;
ActiveCell++;
//Checks if the effected cell is in the top row
if (ActiveCell <= ColCount)
{
for (unsigned int x = 0; x < 3; x++)
DeleteCharFromArray(ArrEffected, ArrEffLength,TopArr[x]);
}
//Checks if effected cell is in the bottom row
else if (MeSize - ColCount < ActiveCell)
{
for (unsigned int x = 0; x < 3; x++)
DeleteCharFromArray(ArrEffected, ArrEffLength, BotArr[x]);
}
//Check if Cell is in left and right most colum
if (ActiveCell % ColCount == 0)
{
for (unsigned int x = 0; x < 3; x++)
DeleteCharFromArray(ArrEffected, ArrEffLength, RigArr[x]);
}
else if (ActiveCell % ColCount == 1)
{
for (unsigned int x = 0; x < 3; x++)
DeleteCharFromArray(ArrEffected, ArrEffLength, LefArr[x]);
}
ActiveCell--;
for (unsigned int x = 0;x < ArrEffLength; x++)
{//Executes for all cells that need to be effected
switch (ArrEffected[x])
{
case '1': GridMemoryValue[ActiveCell - ColCount - 1]++;
break;
case '2': GridMemoryValue[ActiveCell - ColCount]++;
break;
case '3': GridMemoryValue[ActiveCell - ColCount + 1]++;
break;
case '4': GridMemoryValue[ActiveCell - 1]++;
break;
case '5': GridMemoryValue[ActiveCell + 1]++;
break;
case '6': GridMemoryValue[ActiveCell + ColCount - 1]++;
break;
case '7': GridMemoryValue[ActiveCell + ColCount]++;
break;
case '8': GridMemoryValue[ActiveCell + ColCount + 1]++;
break;
default:
break;
}
}
}
#if 0 //Debug used to check for the Overwriting bug
for (unsigned int x = 0; x < BombCount; x++)
std::cout << BombList[x] << std::endl;
#endif
}
void MineGrid::GameOver()
{
if (isGameOver)
printf("Game over you triggered a mine:\n");
else
printf("Well played you master of math you have done it\n");
DisplayValue();
}
void MineGrid::RevealCell(unsigned int uRow, char cCol)
{
unsigned int uCol = cCol - 'A';
char NewValue = (GridMemoryValue[uRow * ColCount + uCol] > 8) ? '+' : '0' + GridMemoryValue[uRow * ColCount + uCol];
if (NewValue == '+')
isGameOver = true;
else
GridMemoryDisplay[uRow * ColCount + uCol] =NewValue ;
CompleteDisplay();
}
void MineGrid::CompleteDisplay()
{
printf("\033c");
PrintRules();
MakeTitle();
DisplayGrid();
}
void MineGrid::DisplayGrid()
{
//Loops through rows and then colums
for (unsigned int x = 0; x < RowCount; x++)
{
printf("%-2d",x);
for (unsigned int y = 0; y < ColCount; y++)
PriorityConverter(GridMemoryDisplay[y + ColCount*x]);
std::cout << std::endl;
}
}
void MineGrid::PriorityConverter(char Value)
{
const char* Color;
switch (Value)
{//Color to value Corrispondents
case '1': Color = GREEN;
break;
case '2': Color = BLUE;
break;
case '3': Color = CYAN;
break;
case '4': Color = MAGENTA;
break;
case '5': Color = YELLOW;
break;
case '6': Color = RED;
break;
case '7': Color = RED;
break;
case '8': Color = RED;
break;
case '+': Color = RED;
break;
default: Color = WHITE;
break;
}
printf("|%s%c%s|", Color, Value, RESET);
}
void MineGrid::DisplayValue()
{
MakeTitle();
for (unsigned int x = 0; x < RowCount; x++)
{
//GitTest
printf("%-2d",x);
for (unsigned int y = 0; y < ColCount; y++)
PriorityConverter( ((GridMemoryValue[y+x*ColCount] > 9)? '+' :'0'+ GridMemoryValue[y + x * ColCount]));
std::cout << std::endl;
}
}