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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
#define _XOPEN_SOURCE 700
#include <ctype.h>
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#define CTRL_C 3
#define BLANK_SPACE ' '
#define MISSED_SPACE '.'
#define HIT_SPACE '!'
#define COLUMNNAMES "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define MINSIZE 6
#define DEFSIZE 10
#define MAXSIZE ((int)sizeof(COLUMNNAMES) - 1)
enum { VERTICAL, HORIZONTAL };
static struct ship {
char *name;
int len;
int sunk;
} ships[] = {
{ "Patrol Boat", 2, 0 },
{ "Submarine", 2, 0 },
{ "Cruiser", 3, 0 },
{ "Destroyer", 3, 0 },
{ "Battleship", 4, 0 },
{ "Aircraft Carrier", 5, 0 },
};
size_t nships = sizeof(ships) / sizeof(ships[0]);
static void usage(const char *progname, int status)
{
printf("usage: %s [-h] [SIZE]\n", progname);
exit(status);
}
static int sunk(const char *board, int size, char c)
{
int hits = 0;
struct ship *s = NULL;
char hit = toupper(c);
for (size_t i = 0; i < nships; i++) {
if (hit == toupper(ships[i].name[0])) {
if (ships[i].sunk) {
return 1;
}
s = ships + i;
break;
}
}
for (int i = 0; i < size * size; i++) {
hits += (board[i] == hit);
}
return s->sunk = (hits == s->len);
}
static void cheat(const char *board, int size)
{
clear();
move(0, 0);
printw(" ");
for (int i = 0; i < size; i++) {
printw("%c", COLUMNNAMES[i]);
}
printw("\n");
for (int i = 0; i < size; i++) {
printw("%2d ", i + 1);
for (int j = 0; j < size; j++) {
char c = board[i * size + j];
printw("%c", c);
}
printw("\n");
}
refresh();
sleep(1);
}
static void updatescreen(const char *board, int size, char *input)
{
clear();
move(0, 0);
printw(" ");
for (int i = 0; i < size; i++) {
printw("%c", COLUMNNAMES[i]);
}
printw("\n");
for (int i = 0; i < size; i++) {
printw("%2d ", i + 1);
for (int j = 0; j < size; j++) {
char c = board[i * size + j];
if (islower(c)) {
c = BLANK_SPACE;
} else if (isupper(c) && !sunk(board, size, c)) {
c = HIT_SPACE;
}
printw("%c", c);
}
printw("\n");
}
printw("\n");
for (size_t i = 0; i < nships; i++) {
printw("%d - %s\n", ships[i].len, ships[i].name);
}
printw("\nMove: %s", input);
refresh();
}
static int place_ship(struct ship *s, char *board, int size, int loc, int direction)
{
int step = direction == HORIZONTAL ? 1 : size;
if ((loc + (s->len * step)) > (size * size)) {
return 0;
}
if (direction == HORIZONTAL && ((loc + s->len) % size) < (loc % size)) {
return 0;
}
for (int i = 0; i < s->len; i++) {
if (board[loc + i * step] != BLANK_SPACE) {
return 0;
}
}
for (int i = 0; i < s->len; i++) {
board[loc + i * step] = tolower(s->name[0]);
}
return 1;
}
static char *setup_board(int size)
{
char *board = malloc(size * size);
if (!board) {
return NULL;
}
memset(board, BLANK_SPACE, size * size);
for (size_t i = 0; i < nships; i++) {
int loc = 0;
do {
loc = rand() % (size * size);
} while (place_ship(ships + i, board, size, loc, rand() & 1) != 1);
}
return board;
}
static void fire(char *board, int size, const char *input)
{
char cnames[] = COLUMNNAMES;
char *columnname = strchr(cnames, toupper(input[0]));
if (!columnname) {
return;
}
int col = columnname - cnames;
if (col < 0 || col > size) {
return;
}
int row = atoi(input + 1) - 1;
if (row < 0 || row > size) {
return;
}
int loc = row * size + col;
if (board[loc] == BLANK_SPACE) {
board[loc] = MISSED_SPACE;
} else {
board[loc] = toupper(board[loc]);
}
}
int main(int argc, char *argv[])
{
int size = DEFSIZE;
int c;
while ((c = getopt(argc, argv, "h")) != -1) {
switch (c) {
case 'h':
usage(argv[0], 0);
break;
default:
usage(argv[0], 1);
}
}
if (optind == argc - 1) {
size = atoi(argv[optind]);
} else if (optind < argc) {
usage(argv[0], 1);
}
if (size < MINSIZE || size > MAXSIZE) {
printf("size must be between %d and %d\n", MINSIZE, MAXSIZE);
return 1;
}
srand(time(NULL));
char *board1 = setup_board(size);
initscr();
keypad(stdscr, true);
noecho();
raw();
char inbuf[BUFSIZ] = {0};
char *input = inbuf;
updatescreen(board1, size, inbuf);
while ((c = getch()) != 0) {
switch (c) {
case '\n':
fire(board1, size, inbuf);
input = inbuf;
*input = '\0';
break;
case '\t':
cheat(board1, size);
break;
case KEY_BACKSPACE:
if (input != inbuf) {
input--;
*input = '\0';
}
break;
case KEY_BREAK:
case CTRL_C:
endwin();
exit(0);
break;
default:
*input++ = c;
*input = '\0';
}
updatescreen(board1, size, inbuf);
size_t nsunk = 0;
for (size_t i = 0; i < nships; i++) {
nsunk += ships[i].sunk;
}
if (nsunk == nships) {
printw("\rYou win! Play again [Y/N]? ");
refresh();
while ((c = getch()) != 0) {
if (tolower(c) == 'n') {
endwin();
exit(0);
} else if (tolower(c) == 'y') {
for (size_t i = 0; i < nships; i++) {
ships[i].sunk = 0;
}
free(board1);
board1 = setup_board(size);
inbuf[0] = '\0';
input = inbuf;
updatescreen(board1, size, input);
break;
}
}
}
}
}
|