summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2019-12-05 11:56:26 -0500
committerJakob Kaivo <jkk@ung.org>2019-12-05 11:56:26 -0500
commit36b58f73536a4046b6b985de84396bd46470b05a (patch)
tree319517dca3c10f82ac8ed86e37a6954143b973c5
parentca7b2ef105ca978862872c76d67514ea9fb24df3 (diff)
hide ships until fired upon
-rw-r--r--bs.c30
1 files changed, 16 insertions, 14 deletions
diff --git a/bs.c b/bs.c
index 21ef7be..8c590ce 100644
--- a/bs.c
+++ b/bs.c
@@ -8,6 +8,9 @@
#include <unistd.h>
#define CTRL_C 3
+#define BLANK_SPACE ' '
+#define MISSED_SPACE '.'
+#define HIT_SPACE '!'
#define COLUMNNAMES "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
@@ -50,7 +53,11 @@ static void updatescreen(const char *board, int width, int height, char *input)
for (int i = 0; i < height; i++) {
printw("%2d ", i + 1);
for (int j = 0; j < width; j++) {
- printw("%c", board[i * width + j]);
+ char c = board[i * width + j];
+ if (isalpha(c)) {
+ c = BLANK_SPACE;
+ }
+ printw("%c", c);
}
printw("\n");
}
@@ -71,7 +78,7 @@ static int place_ship(struct ship *s, char *board, int width, int height, int lo
}
for (int i = 0; i < s->len; i++) {
- if (board[loc + i * step] != '.') {
+ if (board[loc + i * step] != BLANK_SPACE) {
return 0;
}
}
@@ -93,7 +100,7 @@ static char *setup_board(int size)
return NULL;
}
- memset(board, '.', size);
+ memset(board, BLANK_SPACE, size);
for (size_t i = 0; i < sizeof(ships) / sizeof(ships[0]); i++) {
int loc = 0;
@@ -108,29 +115,27 @@ static char *setup_board(int size)
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 loc = row * size + col;
+ if (board[loc] == BLANK_SPACE) {
+ board[loc] = MISSED_SPACE;
+ } else {
+ board[loc] = HIT_SPACE;
+ }
}
int main(int argc, char *argv[])
@@ -202,7 +207,4 @@ int main(int argc, char *argv[])
updatescreen(board1, size, size, inbuf);
}
-
- endwin();
- return 0;
}