-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
62 lines (48 loc) · 1.34 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
#include "Board.h"
Board::Board(int w, int h) {
initscr();
cbreak();
noecho();
int maxx = getmaxx(stdscr);
if (w > maxx || w < 0){
width = maxx;
} else {
width = w;
}
int maxy = getmaxy(stdscr);
if (h > maxy || h < 0){
height = maxy;
} else {
height = h;
}
}
void Board::print() {
int n = objects.size();
for (int i = 0; i < n; i++){
std::vector<std::string> v = objects[i]->print();
int y = objects[i]->posY();
int x = objects[i]->posX();
for (std::string s : v) {
move(y, x);
addstr(s.c_str());
y++;
}
}
}
std::ostream& operator<<(std::ostream& os, const Board& board) {
std::string boardString;
for (int i = 0; i < board.height; i++){
boardString += std::string(board.width, ' ') += "\n" ;
}
int n = board.objects.size();
for (int i = 0; i < n; i++){
std::vector<std::string> v = board.objects[i]->print();
int y = board.objects[i]->posY();
int x = board.objects[i]->posX();
for (int j = 0; j < v.size() && (j + y) < board.height; j++){
boardString.replace((y + j) * (board.width + 1) + x, std::min((int)v[j].length(), board.width - x), v[j], 0, board.width - x);
}
}
os << boardString;
return os;
}