66 lines
1.5 KiB
C
66 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdbool.h>
|
|
#include "game.h"
|
|
#include "world.h"
|
|
|
|
typedef struct {
|
|
GameState game;
|
|
} AppState;
|
|
|
|
void* init_game_wrapper() {
|
|
AppState* state = malloc(sizeof(AppState));
|
|
init_game(&state->game);
|
|
return state;
|
|
}
|
|
|
|
int world_event(struct event* ev, void* data) {
|
|
AppState* app = (AppState*)data;
|
|
|
|
if (ev->type == EVENT_KEY) {
|
|
int key = ev->key;
|
|
|
|
if (app->game.game_over) {
|
|
if (key == '\n') {
|
|
init_game(&app->game);
|
|
} else if (key == 'q' || key == 'Q') {
|
|
return 1;
|
|
}
|
|
} else {
|
|
if (key == 'q' || key == 'Q') {
|
|
return 1;
|
|
}
|
|
|
|
handle_input(&app->game, key);
|
|
|
|
int winner = check_winner(&app->game);
|
|
if (winner != 0) {
|
|
app->game.winner = winner;
|
|
app->game.game_over = 1;
|
|
} else {
|
|
bool draw = true;
|
|
for (int y = 0; y < 3; y++)
|
|
for (int x = 0; x < 3; x++)
|
|
if (app->game.board[y][x] == EMPTY)
|
|
draw = false;
|
|
if (draw) {
|
|
app->game.winner = 0;
|
|
app->game.game_over = 1;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
clear_screen();
|
|
draw_game(&app->game);
|
|
return 0;
|
|
}
|
|
|
|
void destroy_game_wrapper(void* data) {
|
|
free(data);
|
|
}
|
|
|
|
int main() {
|
|
return start_world(init_game_wrapper, world_event, destroy_game_wrapper);
|
|
}
|