-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoard.cpp
63 lines (57 loc) · 1.54 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
//
// Created by joaof on 21/04/2020.
//
#include "Board.h"
#include <iostream>
using namespace std;
Board::Board(unsigned size){
this->size = size;
this->board.resize(size, vector<char>(size, '\0'));
}
char Board::getLetter(const int &x, const int &y) const{
if(!validPos(x,y))
return '\0';
return this->board[x][y];
}
void Board::setLetter(const int &x, const int &y, char letter) {
this->board[x][y] = letter;
}
unsigned Board::getSize() const {
return this->size;
}
/**
* Determine if a given (x,y) pair is inside the board.
* @param x The x coordinate.
* @param y The y coordinate.
* @return True if the (x,y) pair is inside the board.
*/
bool Board::validPos(const int &x, const int &y) const {
return (x >= 0) && (x < this->size) && (y >= 0) && (y < this->size);
}
/**
* Show a 2D representation of the board through ostream out.
* @param out The ostream through which the board will be shown (usually cout or an ofstream).
*/
void Board::show(ostream &out) const {
out << " ";
for(int x = 0; x < size; x++){ //x indices
out << " " << (char) (x + 'a');
}
out << endl << " " << "/";
for(int x = 0; x < 2*size; x++){
out << "-";
}
out << "\\" << endl;
for(int y = 0; y < size; y++){
out << (char) (y + 'A') << " " << "|";
for(int x = 0; x < size; x++){
out << " " << this->board[x][y];
}
out << "|" << endl;
}
out << " " << "\\";
for (int x = 0; x < 2*size; x++) {
out << "-";
}
out << "/" << endl;
}