/
Board.cpp
166 lines (150 loc) · 3.01 KB
/
Board.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
/**************************************************************
* Author: Keyul Patel
* Date Created: 10/12/15
* Last Modification Date: 10/20/15
* Lab Number: CST 211 Lab 1
* Filename: Board.cpp
*
*
* Overview: A 2D Array of cells
*
* Input: The size and number of mines
*
* Output: none
****************************************************************/
#include "Board.h"
Board::Board(Point loc, int mines) : m_board(loc.x(), loc.y()), m_numBombs(mines)
{
add_Bombs();
add_nearBombs();
m_safeSpaces = ( ( loc.x() * loc.y() ) - m_numBombs );
}
void Board::flagSpace(Point loc)
{
if (!m_board[loc.x() - 1][loc.y() - 1].turned())
{
m_board[loc.x() - 1][loc.y() - 1].flag();
}
}
void Board::turnSpace(Point loc)
{
int newX = loc.x() - 1;
int newY = loc.y() - 1;
Point newLoc(newX, newY);
if (!m_board[newX][newY].turned() && !m_board[newX][newY].flagged()){
m_board[newX][newY].turn();
if (!m_board[newX][newY].bomb() && !m_board[newX][newY].bombsNear())
{
reveal_around(newLoc);
}
}
}
void Board::displayBoard()
{
cout << "[" << m_board.getRow() << "x" << m_board.getColumn() << " with " << m_numBombs << " mines]" << endl;
cout << endl;
for (int i = 0; i < m_board.getRow(); i++)
{
cout << " ";
for (int id = 0; id < m_board.getColumn(); id++)
{
cout << m_board[i][id];
}
cout << endl;
}
cout << endl;
}
bool Board::loseCheck()
{
for (int i = 0; i < m_board.getRow(); i++)
{
for (int id = 0; id < m_board.getColumn(); id++)
{
if (m_board[i][id].bomb() && m_board[i][id].turned())
{
return true;
}
}
}
return false;
}
bool Board::winCheck()
{
int spaces = 0;
for (int i = 0; i < m_board.getRow(); i++)
{
for (int id = 0; id < m_board.getColumn(); id++)
{
if (m_board[i][id].turned() && !m_board[i][id].bomb())
{
spaces++;
}
}
}
return m_safeSpaces == spaces;
}
bool Board::inBounds(Point loc)
{
if (0 <= loc.x()-1 && loc.x()-1 < m_board.getRow() && 0 <= loc.y()-1 && loc.y()-1 < m_board.getColumn())
{
return true;
}
return false;
}
void Board::add_Bombs()
{
srand(time(NULL));
int bombsLeft = m_numBombs;
unsigned int rNum;
do{
for (int i = 0; i < m_board.getRow(); i++)
{
for (int id = 0; id < m_board.getColumn(); id++)
{
rNum = rand() % 10;
if (!rNum && bombsLeft && !m_board[i][id].bomb())
{
m_board[i][id].placeBomb();
bombsLeft--;
}
}
}
} while (bombsLeft);
}
void Board::add_nearBombs()
{
for (int i = 0; i < m_board.getRow(); i++)
{
for (int id = 0; id < m_board.getColumn(); id++)
{
for (int idx = 0; idx < 3; idx++)
{
for (int indx = 0; indx < 3; indx++)
{
Point place(i+idx, id+indx);
if (inBounds(place))
{
if (m_board[i+idx-1][id+indx-1].bomb())
{
m_board[i][id].addNearBomb();
}
}
}
}
}
}
}
void Board::reveal_around(Point loc)
{
for (int idx = 0; idx < 3; idx++)
{
for (int indx = 0; indx < 3; indx++)
{
Point place(loc.x() + idx, loc.y() + indx);
if (inBounds(place))
{
turnSpace(place);
}
}
}
}