From 06634a793f76f50ff12d0dbf4f19dd8594b1ef46 Mon Sep 17 00:00:00 2001 From: st529yr Date: Tue, 14 May 2024 21:47:44 +0200 Subject: [PATCH] funguje --- final/final.c | 80 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 final/final.c diff --git a/final/final.c b/final/final.c new file mode 100644 index 0000000..a343b75 --- /dev/null +++ b/final/final.c @@ -0,0 +1,80 @@ +#include +#include +#include "world.h" + +#define WIDTH 11 +#define HEIGHT 11 + +typedef enum {EMPTY, CROSS, CIRCLE} Cell; + +Cell board[HEIGHT][WIDTH] = {EMPTY}; + +void draw_board() { + clear_world(); + for (int y = 0; y < HEIGHT; ++y) { + for (int x = 0; x < WIDTH; ++x) { + int world_x = x * 2 + 1; + int world_y = y * 2 + 1; + if (board[y][x] == CROSS) { + draw_character('X', world_x, world_y); + } else if (board[y][x] == CIRCLE) { + draw_character('O', world_x, world_y); + } + } + } + refresh_world(); +} + +int check_win(Cell player) { + // Check rows + for (int y = 0; y < HEIGHT; ++y) { + int count = 0; + for (int x = 0; x < WIDTH; ++x) { + if (board[y][x] == player) { + count++; + } else { + count = 0; + } + if (count == 5) return 1; + } + } + // Check columns + for (int x = 0; x < WIDTH; ++x) { + int count = 0; + for (int y = 0; y < HEIGHT; ++y) { + if (board[y][x] == player) { + count++; + } else { + count = 0; + } + if (count == 5) return 1; + } + } + // TODO: Implement diagonal checks + return 0; +} + +int main() { + init_world(2 * WIDTH + 1, 2 * HEIGHT + 1); + draw_board(); + int turn = 0; + while (1) { + int x, y; + printf("Player %d's turn. Enter coordinates (x y): ", turn % 2 + 1); + scanf("%d %d", &x, &y); + if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT && board[y][x] == EMPTY) { + board[y][x] = (turn % 2 == 0) ? CROSS : CIRCLE; + draw_board(); + if (check_win(board[y][x])) { + printf("Player %d wins!\n", turn % 2 + 1); + break; + } + turn++; + } else { + printf("Invalid move. Try again.\n"); + } + } + close_world(); + return 0; +} +