c_winter_2019/battleships.c

180 lines
3.6 KiB
C
Raw Normal View History

2020-01-31 14:49:37 +01:00
#include <stdio.h>
#include <stdlib.h>
2020-01-31 14:55:34 +01:00
#include <string.h>
#include <ctype.h>
2020-01-31 14:49:37 +01:00
enum {
2020-01-31 14:55:34 +01:00
BOARD_SIZE = 10,
};
struct state {
char board[BOARD_SIZE][BOARD_SIZE];
int game_over;
2020-01-31 14:49:37 +01:00
};
struct position {
int x;
int y;
};
2020-01-31 14:55:34 +01:00
enum orientation {
O_VERTICAL,
O_HORIZONTAL
2020-01-31 14:49:37 +01:00
};
2020-01-31 14:55:34 +01:00
struct boat {
enum orientation orientation;
/** left upper corner */
struct position position;
int length;
2020-01-31 14:49:37 +01:00
};
2020-01-31 14:55:34 +01:00
struct position read_position(void);
struct boat generate_boat(int length);
struct state generate_state(void);
2020-01-31 14:49:37 +01:00
2020-01-31 14:55:34 +01:00
void display_state(struct state state);
2020-01-31 14:49:37 +01:00
2020-01-31 14:55:34 +01:00
int valid_position(struct position p);
2020-01-31 14:49:37 +01:00
2020-01-31 14:55:34 +01:00
/**
* Simulates one step of battleship.
*
* Performs the following steps:
*
* - apply hit to board
* - check sunken ships
* - check game over
*/
void update_state(struct state * state, struct position hit);
enum target {
T_WATER,
T_BOAT,
};
enum target check_hit(struct state state, struct position hit);
2020-01-31 14:49:37 +01:00
int main(void) {
2020-01-31 14:55:34 +01:00
struct state state = generate_state();
while (!state.game_over) {
display_state(state);
struct position hit = read_position();
/* game logic */
update_state(&state, hit);
}
}
struct position read_position(void) {
printf("enter coordinate: ");
while (1) {
char* line = NULL;
size_t size = 0;
getline(&line, &size, stdin);
fflush(stdout);
struct position position;
char col;
sscanf("%c%d", &col, &position.y);
free(line);
position.x = tolower(col) - 'a';
if (valid_position(position)) {
return position;
2020-01-31 14:49:37 +01:00
}
2020-01-31 14:55:34 +01:00
puts("invalid input!");
2020-01-31 14:49:37 +01:00
}
}
2020-01-31 14:55:34 +01:00
struct position random_position(void) {
struct position p;
p.x = rand() % BOARD_SIZE;
p.y = rand() % BOARD_SIZE;
return p;
}
enum orientation random_orientation(void) {
return (rand() % 2);
}
struct boat generate_boat(int length) {
struct boat b;
b.length = length;
b.position = random_position();
b.orientation = random_orientation();
return b;
2020-01-31 14:49:37 +01:00
}
2020-01-31 14:55:34 +01:00
int valid_position(struct position p) {
return (p.x >= 0) && (p.x < BOARD_SIZE) && (p.y >= 0) && (p.y < BOARD_SIZE);
2020-01-31 14:49:37 +01:00
}
2020-01-31 14:55:34 +01:00
int place_boat(struct state * state, struct boat boat, char c) {
int dx = (boat.orientation == O_VERTICAL) ? 1 : 0;
int dy = (boat.orientation == O_HORIZONTAL) ? 1 : 0;
struct position curpos = boat.position;
for (int i = 0; i < boat.length; ++i) {
if (!valid_position(curpos)) return 0;
if (state->board[curpos.y][curpos.x] != 0) return 0;
if (c) {
state->board[curpos.y][curpos.x] = c;
}
2020-01-31 14:49:37 +01:00
2020-01-31 14:55:34 +01:00
curpos.x += dx;
curpos.y += dy;
2020-01-31 14:49:37 +01:00
}
2020-01-31 14:55:34 +01:00
return 1;
2020-01-31 14:49:37 +01:00
}
2020-01-31 14:55:34 +01:00
const int BOAT_LENGTHS[] = { 5, 4, 4, 3, 3, 3, 2, 2, 2, 2 };
struct state generate_state(void) {
struct state state;
memset(state.board, 0, BOARD_SIZE*BOARD_SIZE);
state.game_over = 0;
// generate some boats
char boat_char = 'A';
for (size_t i = 0; i < sizeof(BOAT_LENGTHS) / sizeof(BOAT_LENGTHS[0]); ++i) {
int length = BOAT_LENGTHS[i];
struct boat boat;
do {
boat = generate_boat(length);
} while(!place_boat(&state, boat, '\0'));
place_boat(&state, boat, boat_char);
boat_char++;
}
return state;
}
void display_state(struct state state) {
for (int y = 0; y < BOARD_SIZE; ++y) {
for (int x = 0; x < BOARD_SIZE; ++x) {
if (state.board[y][x]) {
printf("%c", state.board[y][x]);
} else {
printf("~");
}
}
printf("\n");
2020-01-31 14:49:37 +01:00
}
2020-01-31 14:55:34 +01:00
puts("---");
2020-01-31 14:49:37 +01:00
}
2020-01-31 14:55:34 +01:00
void update_state(struct state * state, struct position hit) {
2020-01-31 14:49:37 +01:00
2020-01-31 14:55:34 +01:00
}