-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
76 lines (65 loc) · 1.81 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
/*************************************************************
*
* Copyright (C) 2010 John O'Laughlin
*
* All rights reserved.
*
*************************************************************
*/
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "board.h"
using namespace std;
Board::Board(int width, int height, const Config *config) {
_width = width;
_height = height;
_config = config;
clear();
}
inline void Board::clear() {
for (int row = 0; row < _height; ++row)
for (int col = 0; col < _width; ++col)
_letters[row][col] = EMPTY_SQUARE;
_empty = true;
}
void Board::writeColumnHeaders(ostream &o) {
o << " ";
for (int col = 0; col < _width; ++col) o << " " << (char)('a' + col);
o << endl;
}
void Board::writeHorizontalLine(ostream &o) {
o << " ";
for (int i = 0; i < _width*2 - 1; ++i) o << "-";
o << endl;
}
void Board::writeRows(ostream &o) {
for (int row = 0; row < _height; ++row) writeRow(o, row);
}
void Board::writeRow(ostream &o, int row) {
int displayRow = row + 1;
if (displayRow < 10) o << " ";
o << displayRow << "|";
for (int col = 0; col < _width; ++col) {
if (col) o << " ";
writeSquare(o, row, col);
}
o << "|" << endl;
}
// doesn't yet handle blanks
void Board::writeLetter(ostream &o, int row, int col) {
char displayLetter = (char)(_letters[row][col] - 1 + 'A');
o << displayLetter;
}
void Board::writeSquare(ostream &o, int row, int col) {
if (_letters[row][col]) writeLetter(o, row, col);
else _config->writeSquare(o, row, col);
}
ostream &operator<<(ostream &o, Board &board) {
board.writeColumnHeaders(o);
board.writeHorizontalLine(o);
board.writeRows(o);
board.writeHorizontalLine(o);
board.writeColumnHeaders(o);
}