c_winter_2019/battleships.c
2020-01-31 14:49:37 +01:00

81 lines
1.8 KiB
C

#include <stdio.h>
#include <stdlib.h>
enum {
N_SHIPS,
WIDTH = 8,
HEIGHT = 8,
};
struct position {
int x;
int y;
};
struct ship {
int length;
struct position position;
char sunken;
char alignment;
char *hits;
};
struct game_state {
char running;
char board[WIDTH][HEIGHT];
struct ship ships[N_SHIPS];
};
struct ship create_ship(int length, struct position position);
void delete_ship(struct ship ship);
void hit(struct game_state *game_state, struct position position);
struct game_state init_game_state();
void deinit_game_state(struct game_state *game_state);
char check_win_condition(struct game_state *game_state);
struct position read_position();
int main(void) {
struct game_state game_state = init_game_state();
while (game_state.running) {
struct position hit_position = read_position();
hit(&game_state, hit_position);
if (check_win_condition(&game_state)) {
game_state.running = 0;
printf("You win!");
}
}
deinit_game_state(&game_state);
return 0;
}
struct ship create_ship(int length, struct position position) {
struct ship ship = {.length = length, .position = position, .sunken = 0};
ship.hits = calloc(length, sizeof *ship.hits);
return ship;
}
void delete_ship(struct ship ship) {
free(ship.hits);
}
void hit(struct game_state *game_state, struct position position);
struct game_state init_game_state() {
struct game_state game_state;
for (int i = 0; i < N_SHIPS; ++i) {
/* generate ship */
}
}
void deinit_game_state(struct game_state *game_state) {
for (int i = 0; i < N_SHIPS; ++i) {
delete_ship(game_state->ships[i]);
}
}
char check_win_condition(struct game_state *game_state);
struct position read_position();