From 51ff3b37d859b25f1cb72ae80bb309bf38b648e4 Mon Sep 17 00:00:00 2001 From: Odweta Date: Wed, 18 Feb 2026 18:16:56 +0100 Subject: [PATCH] first draft (HAS BUGS, DOES NOT WORK) --- src/chess.c | 18 +++++++++--------- src/chess.h | 8 ++++---- src/main.c | 6 +++++- src/render.c | 28 ++++++++++++++++++++++++++++ src/render.h | 8 ++++++++ src/util.c | 2 +- 6 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 src/render.c create mode 100644 src/render.h diff --git a/src/chess.c b/src/chess.c index 58de3ae..c9bf3d4 100644 --- a/src/chess.c +++ b/src/chess.c @@ -1,12 +1,11 @@ #include -#include #include "chess.h" #include "util.h" -void init_board(board_t *board) { +void init_board(board_t board) { for (int i = 0; i < BOARD_VERTICAL; i++) { for (int j = 0; j < BOARD_HORIZONTAL; j++) { - *board[i][j] = BOARD_DEFAULT_CONFIG[i][j]; + board[j][i] = BOARD_DEFAULT_CONFIG[i][j]; } } } @@ -52,11 +51,10 @@ bool is_move_in_bounds(move_t move) { } bool get_move(move_t *move) { - clear_stdin(); - char _x_from, _x_to; int _y_from, _y_to; - scanf("%c%d %c%d", + printf("move >> "); + scanf(" %c%d %c%d", &_x_from, &_y_from, &_x_to, @@ -202,11 +200,11 @@ bool check_captures(board_t *board, player_e player, captured_t *captured, move_ if (is_dest_opponent(*board, player, move)) { switch (player) { case WHITE: - captured->white[captured->white_n++] = abs(*board[move.to.y][move.to.x]); + captured->white[captured->white_n++] = *board[move.to.y][move.to.x]; *board[move.to.y][move.to.x] = NONE; break; case BLACK: - captured->white[captured->white_n++] = abs(*board[move.to.y][move.to.x]); + captured->white[captured->white_n++] = *board[move.to.y][move.to.x]; *board[move.to.y][move.to.x] = NONE; break; } @@ -378,7 +376,9 @@ end_condition_e check_end(board_t board, player_e player, move_t move) { } end_condition_e perform_move(board_t *board, player_e player, captured_t *captured, move_t move) { - if (validate_move(*board, player, move)) { + bool valid = validate_move(*board, player, move); + printf("move valid? %d", valid); + if (valid) { if ( *board[move.to.y][move.to.x] != WKING || // accounting for both players *board[move.to.y][move.to.x] != BKING // you cannot capture a king diff --git a/src/chess.h b/src/chess.h index 7ff1454..7dd59ca 100644 --- a/src/chess.h +++ b/src/chess.h @@ -21,8 +21,6 @@ // type abstractions typedef enum { WHITE, BLACK } player_e; -typedef int board_t[BOARD_VERTICAL][BOARD_HORIZONTAL]; - #define PIECE_COUNT 13 // how many unique pieces are there #define MAX_PIECE_MOVE_COUNT 27 // the max possible num of options where to move in one moment typedef enum { @@ -43,6 +41,8 @@ typedef enum { BKING } piece_e; +typedef piece_e board_t[BOARD_VERTICAL][BOARD_HORIZONTAL]; + typedef struct { int x; int y; @@ -95,7 +95,7 @@ typedef struct { * absolute x,y values -> e.g. the rook can move * up, down, left, right * n -> and then bounds & edge cases are checked */ -#define DEFAULT_VALID_MOVES (valid_move_t[PIECE_COUNT]){\ +#define DEFAULT_VALID_MOVES {\ {NONE, {}, 0},\ \ {WPAWN, {\ @@ -169,7 +169,7 @@ typedef struct { } captured_t; // initializes the board with the default configuration of pieces -void init_board(board_t *board); +void init_board(board_t board); // selects the player to currently play // if it is the first round, it selects white diff --git a/src/main.c b/src/main.c index 135e893..33a2379 100644 --- a/src/main.c +++ b/src/main.c @@ -1,7 +1,9 @@ // a chess implementation in pure C :) #include "chess.h" +#include "render.h" #include +#include /** * steps to victory: @@ -27,7 +29,7 @@ int main() { // algo begin - init_board(&board); + init_board(board); show_help(); @@ -36,6 +38,8 @@ int main() { select_player(&player, round_count); + show_board(board, player); + get_move(&move); end_condition = perform_move(&board, player, &captured, move); diff --git a/src/render.c b/src/render.c new file mode 100644 index 0000000..91ef2c8 --- /dev/null +++ b/src/render.c @@ -0,0 +1,28 @@ +#include "render.h" +#include "chess.h" +#include + +void show_board(board_t board, player_e player) { + printf("\n "); + for (int i = 0; i < BOARD_HORIZONTAL; i++) { + printf("%-2c ", i+97); + } + printf("\n\n"); + for (int i = 0; i < BOARD_VERTICAL; i++) { + printf("%-2d [", i+1); + for (int j = 0; j < BOARD_HORIZONTAL-1; j++) { + printf("%-2d ", board[j][i]); + } + printf("%d]\n", board[BOARD_HORIZONTAL-1][i]); + } + + printf("\ncurrently plays: "); + switch (player) { + case WHITE: + printf("WHITE\n"); + break; + case BLACK: + printf("BLACK\n"); + break; + } +} \ No newline at end of file diff --git a/src/render.h b/src/render.h new file mode 100644 index 0000000..70ed91a --- /dev/null +++ b/src/render.h @@ -0,0 +1,8 @@ +#ifndef RENDER_H +#define RENDER_H + +#include "chess.h" + +void show_board(board_t board, player_e player); + +#endif // RENDER_H \ No newline at end of file diff --git a/src/util.c b/src/util.c index 974c325..64a5c1e 100644 --- a/src/util.c +++ b/src/util.c @@ -2,7 +2,7 @@ #include void clear_stdin() { - while (getchar() != '\n'); + while ((getchar()) != '\n'); } bool is_int_in_range(int x, int start, int end) {