Newer
Older
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
136
137
#include "board.h"
#include "colors.h"
std::string ship_names[] = { "Aircraft Carrier", "Battleship", "Cruiser", "Submarine", "Destroyer" };
int ship_sizes[] = { 5, 4, 3, 3, 2 };
board::board(int side) : side(side) {
int i, j, k;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
guesses[i][j] = 0;
}
}
for (i = 0; i < 5; i++) {
while (true) {
int dir = randrange(0, 3);
int x = randrange(0, 9);
int y = randrange(0, 9);
int dx = 0;
int dy = 0;
if (dir == UP) {
dy = -1;
} else if (dir == DOWN) {
dy = 1;
} else if (dir == LEFT) {
dx = -1;
} else if (dir == RIGHT) {
dx = 1;
}
bool valid = true;
for (j = 0; j < ship_sizes[i]; j++) {
int tmpx = x + (j * dx);
int tmpy = y + (j * dy);
if (tmpx < 0 || tmpx > 9 || tmpy < 0 || tmpy > 9) {
valid = false;
break;
}
bool collides = false;
for (k = 0; k < (int)ships.size(); k++) {
if (ships[k]->contains(tmpx, tmpy)) collides = true;
}
if (collides) {
valid = false;
break;
}
}
if (!valid) continue;
ships.push_back(new ship(x, y, dir, ship_sizes[i], ship_names[i]));
break;
}
}
cursor_x = 0;
cursor_y = 0;
show_cursor = false;
}
board::board(int side, std::string &board_description) : side(side) {
int i, j;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
guesses[i][j] = 0;
}
}
for (i = 0; i < 5; i++) {
int y = board_description[(i * 3) + 0] - 'A';
int x = board_description[(i * 3) + 1] - '0';
int dir = board_description[(i * 3) + 2] - '0';
ships.push_back(new ship(x, y, dir, ship_sizes[i], ship_names[i]));
}
cursor_x = 0;
cursor_y = 0;
show_cursor = false;
}
std::string board::get_description() {
std::string result;
for (int i = 0; i < 5; i++) {
result.push_back('A' + ships[i]->get_y());
result.push_back('0' + ships[i]->get_x());
result.push_back('0' + ships[i]->get_dir());
}
return result;
}
board::~board() {
for (int i = 0; i < (int)ships.size(); i++) {
delete ships[i];
}
ships.clear();
}
bool board::has_lost() {
for (int i = 0; i < (int)ships.size(); i++) {
if (!ships[i]->is_sunk()) return false;
}
return true;
}
void board::make_move(std::string position) {
int y = position[0] - 'A';
int x = position[1] - '1';
for (int i = 0; i < (int)ships.size(); i++) {
if (ships[i]->contains(x, y)) ships[i]->hit();
}
guesses[y][x] = true;
}
int board::get_position_color(int x, int y) {
if (side == OPPONENT) {
if (show_cursor && x == cursor_x && y == cursor_y) {
return BG_GREEN;
}
if (guesses[y][x]) {
for (int i = 0; i < (int)ships.size(); i++) {
if (ships[i]->contains(x, y)) {
return BG_RED;
}
}
return BG_WHITE;
return BG_BLACK;
} else {
if (guesses[y][x]) {
for (int i = 0; i < (int)ships.size(); i++) {
if (ships[i]->contains(x, y)) {
return BG_RED;
}
}
return BG_WHITE;
} else {
for (int i = 0; i < (int)ships.size(); i++) {
if (ships[i]->contains(x, y)) {
return BG_BLACK;
}
}
return BG_CYAN;
}
}
}
void board::draw() {
int x_offset = 0;
if (side == PLAYER) {
}
attron(COLOR_PAIR(TEXT_WHITE));
mvprintw(0, 4, "Opponent's Ships");
mvprintw(0, 64, "Your Ships");
mvprintw(1, x_offset, " 1 2 3 4 5 6 7 8 9 10 ");
mvaddch(3 + i, x_offset, 'A' + i);
mvaddch(3 + i, x_offset+1, '|');
mvaddch(3 + i, x_offset+22, '|');
}
for (int i = 0; i < 22; i++) {
mvaddch(2, x_offset+i+1, '-');
mvaddch(13, x_offset+i+1, '-');
attroff(COLOR_PAIR(TEXT_WHITE));
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
int color = get_position_color(j, i);
attron(COLOR_PAIR(color));
attroff(COLOR_PAIR(color));
}
}
for (int i = 0; i < 5; i++) {
ships[i]->print_ship(x_offset, 14+i);
}
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
refresh();
}
std::string board::get_move() {
show_cursor = true;
draw();
while(true) {
int key = getch();
if (key == 'f') {
break;
}
else if (key == KEY_UP) cursor_y--;
else if (key == KEY_DOWN) cursor_y++;
else if (key == KEY_LEFT) cursor_x--;
else if (key == KEY_RIGHT) cursor_x++;
else continue;
cursor_x = std::min(9, cursor_x);
cursor_x = std::max(0, cursor_x);
cursor_y = std::min(9, cursor_y);
cursor_y = std::max(0, cursor_y);
draw();
}
std::string move;
move.push_back('A' + cursor_y);
move.push_back('1' + cursor_x);
show_cursor = false;
return move;
}