Přidat du8/main.c
This commit is contained in:
parent
c99d8a1c2b
commit
d9dc92b65a
65
du8/main.c
Normal file
65
du8/main.c
Normal file
@ -0,0 +1,65 @@
|
||||
#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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user