c_winter_2019/battleships.c

242 lines
5 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-02-06 16:53:29 +01:00
#include <time.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,
2020-02-06 17:39:40 +01:00
T_COMPLETE_BOAT,
2020-01-31 14:55:34 +01:00
};
enum target check_hit(struct state state, struct position hit);
2020-01-31 14:49:37 +01:00
int main(void) {
2020-02-06 16:53:29 +01:00
srand(time(NULL));
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;
2020-02-06 17:03:26 +01:00
sscanf(line, "%c%d", &col, &position.y);
2020-01-31 14:55:34 +01:00
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-02-06 17:39:40 +01:00
/* const int BOAT_LENGTHS[] = { 5, 4, 4, 3, 3, 3, 2, 2, 2, 2 }; */
const int BOAT_LENGTHS[] = {1};
2020-01-31 14:55:34 +01:00
2020-02-06 17:39:40 +01:00
struct state
generate_state(void) {
2020-01-31 14:55:34 +01:00
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-02-06 17:39:40 +01:00
enum target check_hit(struct state state, struct position pos) {
char ship = state.board[pos.y][pos.x];
// check if we hit anything
if (ship == 0) return T_WATER;
ship = toupper(ship);
int count = 0;
for (int y = 0; y < BOARD_SIZE; ++y) {
for (int x = 0; x < BOARD_SIZE; ++x) {
if (state.board[y][x] == ship) {
count += 1;
}
}
}
return (count > 1) ? T_BOAT : T_COMPLETE_BOAT;
}
void sink_ship(struct state * state, struct position pos) {
char ship = state->board[pos.y][pos.x];
for (int x = 0; x < BOARD_SIZE; ++x) {
for (int y = 0; y < BOARD_SIZE; ++y) {
if (ship == toupper(state->board[y][x])) {
state->board[y][x] = '#';
}
}
}
}
void update_state(struct state * state, struct position pos) {
enum target target = check_hit(*state, pos);
// update board
switch (target) {
case T_BOAT:
state->board[pos.y][pos.x] = tolower(state->board[pos.y][pos.x]);
break;
case T_COMPLETE_BOAT: {
sink_ship(state, pos);
break;
}
default:
state->board[pos.y][pos.x] = '*';
break;
}
// check for game over
int letter_count = 0;
for (int x = 0; x < BOARD_SIZE; ++x) {
for (int y = 0; y < BOARD_SIZE; ++y) {
2020-02-06 17:46:40 +01:00
if (isalpha(state->board[y][x])) {
2020-02-06 17:39:40 +01:00
letter_count += 1;
}
}
}
state->game_over = (letter_count == 0);
2020-01-31 14:55:34 +01:00
}