-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
184 lines (117 loc) · 2.87 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "Board.h"
#include <string>
#include <fstream>
#include <sstream>
#include "Exceptions.h"
Board::Board(std::string board_file_namey){
initializedWithTiles = false;
std::ifstream boardFileStream(board_file_namey);
if(boardFileStream.fail()){
throw FileException("BOARD");
}
std::string line;
getline(boardFileStream, line);
std::stringstream ss1(line);
ss1 >> height >> width; //read in the dimensions
getline(boardFileStream, line);
std::stringstream ss2(line);
ss2 >> starty >> startx; //read in the starting square
//dynamically allocating the board
board = new Square**[height];
for(unsigned int x = 0; x < height; x++){
board[x] = new Square*[width];
}
unsigned int index = 0;
char ch;
while(getline(boardFileStream, line)){ //reading in board layout
std::stringstream ss(line);
for(unsigned int x = 0; x < height; x++){ //goes through each line
bool start = false; //if it's the starting square
ss >> ch; //read in potential multiplier
if(x == starty-1 && index == startx-1){
start = true;
}
if(ch == 't'){
board[x][index] = new Square(1, 3, start);
}
else if(ch == 'd'){
board[x][index] = new Square(1, 2, start);
}
else if(ch == '2'){
board[x][index] = new Square(2, 1, start);
}
else if(ch == '3'){
board[x][index] = new Square(3, 1, start);
}
else{
board[x][index] = new Square(1, 1, start);
}
}
index++;
}
}
Board::~Board(){
//deconstructing the board
for(size_t x = 0; x < height; x++){
for(size_t y = 0; y < width; y++){
if(board[x][y]){
delete[] board[x][y];
}
}
}
for(unsigned int x = 0; x < height; x++){
delete[] board[x];
}
delete[] board;
}
Square * Board::getSquare (size_t x, size_t y) const{
return board[x-1][y-1];
}
size_t Board::getRows() const{
return height;
}
size_t Board::getColumns() const{
return width;
}
bool Board::isEmpty(){ //returns if the board is empty
for(size_t x = 0; x < height; x++){
for(size_t y = 0; y < width; y++){
if(board[x][y] != nullptr){
return false;
}
}
}
return true;
}
void Board::initialize(std::string file){
if(file == ""){
return;
}
std::ifstream filestream(file);
if(filestream.fail()){
throw FileException("BOARD");
}
else{
initializedWithTiles = true;
std::string line;
for(size_t x = 0; x < height; x++){
std::getline(filestream,line);
char letter1, letter2, letter3;
//int points;
stringstream ss(line);
for(size_t y = 0; y < width; y++){
ss >> letter1 >> letter2 >> letter3;
if(letter1 != '.'){
if(letter1 < 91){ //handles uppercase
letter1 = letter1 + 32;
}
initializedWithTiles = true;
int points = ((letter2-48)*10) + (letter3 - 48);
Tile* added = new Tile(letter1, points);
getSquare(y+1, x+1)->placeTile(added);
//MAKE SURE THE DIMENSIONS ARE CORRECT
}
}
}
}
}