81 lines
2.0 KiB
C
81 lines
2.0 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#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;
|
|
}
|
|
|