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
|
#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 COLUMNNAMES "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define MINSIZE 6
#define DEFSIZE 20
#define MAXSIZE ((int)sizeof(COLUMNNAMES) - 1)
enum { VERTICAL, HORIZONTAL };
static struct ship {
char *name;
int len;
int nhits;
} ships[] = {
{ "Patrol Boat", 2, 0 },
{ "Submarine", 2, 0 },
{ "Cruiser", 3, 0 },
{ "Destroyer", 3, 0 },
{ "Battleship", 4, 0 },
{ "Aircraft Carrier", 5, 0 },
};
static void usage(const char *progname, int status)
{
printf("usage: %s [-h] [SIZE]\n", progname);
exit(status);
}
static void updatescreen(const char *board, int width, int height, char *input)
{
clear();
move(0, 0);
printw(" ");
for (int i = 0; i < width; i++) {
printw("%c", COLUMNNAMES[i]);
}
printw("\n");
for (int i = 0; i < height; i++) {
printw("%2d ", i + 1);
for (int j = 0; j < width; j++) {
printw("%c", board[i * width + j]);
}
printw("\n");
}
printw("\nMove: %s", input);
refresh();
}
static int place_ship(struct ship *s, char *board, int width, int height, int loc, int direction)
{
int step = direction == HORIZONTAL ? 1 : width;
if (loc + s->len * step > width * height) {
return 0;
}
if (direction == HORIZONTAL && (loc + s->len) % width < loc % width) {
return 0;
}
for (int i = 0; i < s->len; i++) {
if (board[loc + i * step] != '.') {
return 0;
}
}
for (int i = 0; i < s->len; i++) {
board[loc + i * step] = s->name[0];
}
return 1;
}
static char *setup_board(int size)
{
int width = size;
int height = size;
size *= size;
char *board = malloc(size);
if (!board) {
return NULL;
}
memset(board, '.', size);
for (size_t i = 0; i < sizeof(ships) / sizeof(ships[0]); i++) {
int loc = 0;
do {
loc = rand() % (size);
} while (place_ship(ships + i, board, width, height, loc, rand() & 1) != 1);
}
return board;
}
static void fire(char *board, int size, const char *input)
{
clear();
move(0, 0);
printw("firing at %s\n", input);
char cnames[] = COLUMNNAMES;
char *columnname = strchr(cnames, toupper(input[0]));
if (!columnname) {
printw("invalid column\n");
return;
}
int col = columnname - cnames;
if (col < 0 || col > size) {
printw("column %d out of range\n", col);
return;
}
int row = atoi(input + 1) - 1;
if (row < 0 || row > size) {
printw("row %d out of range\n", row);
return;
}
printw("row %d, column %d\n", row, col);
board[row * size + col] = '!';
}
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, size, inbuf);
while ((c = getch()) != 0) {
switch (c) {
case '\n':
fire(board1, size, inbuf);
refresh();
input = inbuf;
*input = '\0';
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, size, inbuf);
}
endwin();
return 0;
}
|