-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBoard.cpp
More file actions
87 lines (72 loc) · 1.59 KB
/
Copy pathBoard.cpp
File metadata and controls
87 lines (72 loc) · 1.59 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
#include "Board.hpp"
using std::vector;
using std::ostream;
using std::endl;
using std::cout;
#include <string>
using std::string;
Board::Board(){
for(int i = 0; i < _boardSize; i++){
_squares.push_back(vector<int>());
for(int k = 0; k < _boardSize; k++){
_squares[i].push_back(0);
}
}
}
ostream& operator<<(ostream& os, Board& board){
cout << endl;
int counter = 1;
for(auto rowVector : board._squares){
cout << counter << " ";
for(auto item : rowVector){
os << " " << item;
}
os << " " << endl;
counter++;
}
cout << endl << " ";
for(int i = 1; i < 6; ++i){
cout << " " << i;
}
return os;
}
void Board::placePieceHorizontal(int x, int y, int shipSize){
if ((shipSize > 3) or (x > _boardSize) or (x < 1) or (y > 5) or (y < 1)) {
throw "Error: Invalid coordinates.";
}
else {
for (int i = 0; i < shipSize; ++i) {
++x;
_squares[y - 1][x - 2] = 1;
}
}
}
void Board::placePieceVertical(int x, int y, int shipSize){
if ((shipSize > 3) or (x > _boardSize) or (x < 1) or (y > 5) or (y < 1)) {
throw "Error: Invalid coordinates.";
}
else {
for (int i = 0; i < shipSize; ++i) {
++y;
_squares[y - 2][x - 1] = 1;
}
}
}
bool Board::checkForHit(int x, int y){
return (_squares[y-1][x-1] == 1);
}
bool Board::shipsRemaining(){
for(auto rowVector : _squares){
for(auto item : rowVector){
if(item == 1)
return false;
}
}
return true;
}
void Board::processHit(int x, int y){
_squares[y-1][x-1] = 2;
}
int Board::getBoardSize(){
return _boardSize;
}