-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStudentBoard.cpp
135 lines (110 loc) · 3.5 KB
/
StudentBoard.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
#include "Board.hpp"
#include "BoardValues.hpp"
#include <algorithm>
#include <iostream>
/**
* Construct a new Board:: Board object
*/
Board::Board(){
// create new grid and fill it with empties
grid = new int[WIDTH * HEIGHT];
std::fill(grid, grid + (WIDTH * HEIGHT), EMPTY);
}
/**
* Construct a new Board:: Board object
*
* Copy the values from board other
*/
Board::Board(const Board& other){
// create new grid and copy other's grid into it
grid = new int[WIDTH * HEIGHT];
std::copy(other.grid, other.grid + (WIDTH * HEIGHT), grid);
// copy other's visible property
visible = other.visible;
}
Board& Board::operator=(const Board& other){
// create new grid in memory and copy vals into it
int* temp = new int[WIDTH * HEIGHT];
std::copy(other.grid, other.grid + (WIDTH * HEIGHT), temp);
// delete the grid reference and replace it with the new grid
delete grid;
grid = temp;
// copy other's visible property
visible = other.visible;
// return the board reference
return *this;
}
Board::~Board(){
delete(grid);
}
/**
* Sets the visibility status of the board
* If the board is set to not visible, only hits and misses will print
* If the board is set to visible, ships will print as well
*/
void Board::setVisible(bool v){
visible = v;
}
int& Board::Internal::operator[](int index) {
// shift by width offset so the index is 0, 1, etc instead of 1, 2, etc
index -= WIDTH_OFFSET;
if (index >= WIDTH || index < 0) {
// if the index is out of bounds, throw an out-of-range exception
throw std::out_of_range(std::to_string(index) + " is out of the range of allowable column indices");
}
return _grid[index];
}
Board::Internal Board::operator[](int index) {
// shift by height offset so the index is 0, 1, etc instead of 'A', 'B', etc
index -= HEIGHT_OFFSET;
if (index >= HEIGHT || index < 0) {
// if the index is out of bounds, throw an out-of-range exception
throw std::out_of_range(std::to_string(index) + " is out of the range of allowable row indices");
}
return Board::Internal(grid + (index * WIDTH));
}
/**
* Overload the stream operator for ease of printing board
*/
std::ostream& operator<<(std::ostream& os, Board& b) {
// print empty tab for header
os << "\t";
// print column headers
for (int i = 1; i <= WIDTH; i++) {
os << i << "\t";
}
// print header new line
os << std::endl;
// print header divider line
os << "----------------------------------------------------------------------------------------" << std::endl;
// loop through rows
for (int i = HEIGHT_OFFSET; i < HEIGHT_OFFSET + HEIGHT; i++) {
// print row header
os << (char)i << "\t";
// loop through cols
for (int j = WIDTH_OFFSET; j < WIDTH_OFFSET + WIDTH; j++) {
// print char at index [row][col]
if (b.visible) {
os << (char)b[i][j] << "\t";
} else {
if (b[i][j] == EMPTY || b[i][j] == HIT || b[i][j] == MISS) {
os << (char)b[i][j] << "\t";
} else {
os << " \t";
}
}
}
// print newline
os << std::endl;
}
// return the stream
return os;
}
// count function placeholder -- not used
int Board::count() const {
return -1;
}
// operater< function placeholder -- not used
bool Board::operator< (const Board& other){
return false;
}