-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
106 lines (95 loc) · 2.13 KB
/
Copy pathBoard.cpp
File metadata and controls
106 lines (95 loc) · 2.13 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
#include <iostream>
#include <assert.h>
#include "Board.h"
void Board::delete_grid() {
for (int c = 0; c < num_cols; c++) {
delete[] grid[c];
}
delete[] grid;
}
Board::Board(int cols, int rows) :
num_cols(cols), num_rows(rows) {
grid = new char*[num_cols];
for (int c = 0; c < num_cols; c++) {
grid[c] = new char[num_rows];
for (int r = 0; r < num_rows; r++) {
grid[c][r] = EMPTY;
}
}
}
Board::Board(const Board& other) {
num_cols = other.num_cols;
num_rows = other.num_rows;
grid = new char*[num_cols];
for (int c = 0; c < num_cols; c++) {
grid[c] = new char[num_rows];
for (int r = 0; r < num_rows; r++) {
grid[c][r] = other.grid[c][r];
}
}
}
Board& Board::operator=(const Board& rhs) {
if (this == &rhs) {
return *this;
} else {
num_cols = rhs.num_cols;
num_rows = rhs.num_rows;
if (grid != NULL) {
delete_grid();
}
grid = new char*[num_cols];
for (int c = 0; c < num_cols; c++) {
grid[c] = new char[num_rows];
for (int r = 0; r < num_rows; r++) {
grid[c][r] = rhs.grid[c][r];
}
}
return *this;
}
}
Board::~Board() {
delete_grid();
}
char Board::get_cell(int col, int row) const {
assert((col >= 0) && (col < num_cols));
assert((row >= 0) && (row < num_rows));
return grid[col][row];
}
void Board::set_cell(int col, int row, char val) {
assert((col >= 0) && (col < num_cols));
assert((row >= 0) && (row < num_rows));
grid[col][row] = val;
}
bool Board::is_cell_empty(int col, int row) const {
if (grid[col][row] == EMPTY) {
return true;
} else {
return false;
}
}
bool Board::is_in_bounds(int col, int row) const {
if ((col >= 0) && (col < num_cols) && (row >= 0) && (row < num_rows)) {
return true;
} else {
return false;
}
}
void Board::display() const {
for (int r = get_num_rows() - 1; r >= 0; r--) {
std::cout << r << ":| ";
for (int c = 0; c < get_num_cols(); c++) {
std::cout << get_cell(c, r) << " ";
}
std::cout << std::endl;
}
std::cout << " -";
for (int c = 0; c < get_num_cols(); c++) {
std::cout << "--";
}
std::cout << "\n";
std::cout << " ";
for (int c = 0; c < get_num_cols(); c++) {
std::cout << c << " ";
}
std::cout << "\n\n";
}