-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
250 lines (202 loc) · 4.47 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
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
246
247
248
249
250
#ifndef BOARD_CPP
#define BOARD_CPP
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include "board.h"
//#include "cell.cpp"
using namespace std;
template<class T>
Board<T>::Board(const T &initVal, int padWidth, int bs)
{
//number of columns
boardSize = bs;
//allocate first dimension
grid = new T*[boardSize];
//allocate second dimension
for (int i = 0; i < boardSize; i++)
{
grid[i] = new T[boardSize];
}
initialValue = initVal;
paddingWidth = padWidth;
reset(); // set all positions with initial value passed
}
//Copy
template<class T>
Board<T>::Board(const Board<T> &other) {
//copy non-dynamic members
boardSize = other.boardSize;
//allocate 1st dimension
grid = new T*[boardSize];
//copy 1st dimension
for (int i = 0; i < boardSize; i++)
{
grid[i] = new T[boardSize];
}
//allocate and copy 2nd dimension
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
grid[i][j] = other.grid[i][j];
}
}
}
//Assignment
template<class T>
Board<T> &Board<T>::operator=(const Board<T> &other) {
//check for self-assignment
if (this != &other) {
//copy non-dynamic
boardSize = other.boardSize;
//deallocate old
for (int i = 0; i < boardSize; i++)
{
//deallocate 2nd (inside)
delete[] grid[i];
}
//deallocate 1st (outside)
delete[] grid;
//allocate new array for copy
//allocate 1st dimension
grid = new T*[boardSize];
//copy 1st dimension
for (int i = 0; i < boardSize; i++)
{
grid[i] = new T[boardSize];
}
//allocate and copy 2nd dimension
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
grid[i][j] = other.grid[i][j];
}
}
}
//return self
return *this;
}
//Destructor
template<class T>
Board<T>::~Board() {
//deallocate
for (int i = 0; i < boardSize; i++)
{
delete[] grid[i];
}
delete[] grid;
//reset member variables
boardSize = 0;
}
//Get board size
template<class T>
int Board<T>::getSize() const
{
return boardSize;
}
//Display board
template<class T>
void Board<T>::display() const
{
int i, j;
//Give col numbers
cout << " ";
for (j = 0; j < boardSize; j++)
cout << "|" << setw(paddingWidth) << (j + 1);
cout << endl;
for (i = 0; i < boardSize; i++)
{
cout << setw(2) << setfill('0') << (i + 1) << " "; // display row number
for (j = 0; j < boardSize; j++)
{
if ((i > 0 && i < 10) && (j > 0 && j < 10)) {
cout << "|" << setw(paddingWidth) << " ";
}
else
{
// Display value at position.
cout << "|" << setw(paddingWidth) << grid[i][j];
}
}
cout << endl; // each row on own line
}
}
//Get position
template<class T>
T Board<T>::getPos(int row, int col) const
{
//check for array out of bounds
return (row > 0) ? grid[row - 1][col - 1] : grid[0][col - 1];
}
//Set position
template<class T>
void Board<T>::setPos(int row, int col, const T &val)
{
// Place value at specified position on the board.
grid[row - 1][col - 1] = val;
}
//Reset board
template<class T>
void Board<T>::reset()
{
// Set all positions to the initial value.
for (int i = 0; i < boardSize; i++)
for (int j = 0; j < boardSize; j++)
grid[i][j] = initialValue;
}
//Player information
template<class T>
void Board<T>::playerInfo(int player) {
cout << endl;
cout << "Player " << player + 1 << " info: " << endl;
}
//Set the cell values
template<class T>
void Board<T>::setCellValue() {
//allocate
cells = new Cell*[boardSize];
//allocate second dimension
for (int i = 0; i < boardSize; i++)
{
cells[i] = new Cell[boardSize];
}
//open file
ifstream inFile;
string line;
inFile.open("CellValues.txt");
//Check for Error
if (inFile.fail()) {
cerr << "File does not exist!";
exit(1);
}
int index = 0, max_num = 0;
string type, name;
istringstream inStream;
while (getline(inFile, line)) {
inStream.clear();
inStream.str(line);
inStream >> type >> index >> max_num >> name;
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
if (cells[i, j] != NULL) {
//cells[i, j] = new Cell();
cells[i, j] = new Cell(index, max_num);
}
}
}
}
for (int i = 0; i < boardSize; i++)
{
for (int j = 0; j < boardSize; j++)
{
//cout << cells[i, j].getId();
}
}
}
#endif