From 3d356b409005ffeadfc8493dadfec5dc8cd4f4d4 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 10 Jun 2021 13:17:55 +0300 Subject: feat: print chess board to stdout, remove ncurses --- Makefile | 4 +-- src/config.h | 2 ++ src/main.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- src/pieces.h | 24 ++++++++++++++++ 4 files changed, 114 insertions(+), 8 deletions(-) create mode 100644 src/config.h create mode 100644 src/pieces.h diff --git a/Makefile b/Makefile index b682fe2..94e43d6 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ CC=tcc -CFLAGS=-lncurses +CFLAGS=-g SRC=src OBJ=obj SOURCES=$(wildcard $(SRC)/*.c) @@ -15,7 +15,7 @@ build: $(BIN) $(BIN): $(OBJECTS) $(CC) $(CFLAGS) $(OBJECTS) -o $(BIN) -$(OBJ)/%.o: $(SRC)/%.c +$(OBJ)/%.o: $(SRC)/%.c $(SRC)/*.h $(CC) -c $< -o $@ clean: diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..60d60c3 --- /dev/null +++ b/src/config.h @@ -0,0 +1,2 @@ +#define BOARD_SIZE 8 +#define DEFAULT_FEN "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" diff --git a/src/main.c b/src/main.c index 49db27a..62b4692 100644 --- a/src/main.c +++ b/src/main.c @@ -1,11 +1,91 @@ -#include +#include +#include +#include + +#include "config.h" +#include "pieces.h" + + +void print_board(int board[BOARD_SIZE][BOARD_SIZE]) { + for (int rank = BOARD_SIZE - 1; rank >= 0; rank--) { + printf("%i|", rank + 1); + for (int file = 0; file < BOARD_SIZE; file++) { + int piece = board[rank][file]; + printf("%s ", pieces[piece]); + } + printf("\n"); + } + printf(" a b c d e f g h\n"); +} + +void parse_FEN(char* FEN, int board[BOARD_SIZE][BOARD_SIZE]) { + // TODO: parse everything, not position only + int rank = 7; + int file = 0; + size_t lenght = strlen(FEN); + + for (int k = 0; k < lenght; k++) { + if (FEN[k] > '0' && FEN[k] <= '8') { + int last_file = file + FEN[k] - '0'; + for (file; file < last_file; file++) { + board[rank][file] = EMPTY; + } + } else { + switch (FEN[k]) { + case 'r': + board[rank][file] = BLACK | ROOK; + break; + case 'R': + board[rank][file] = ROOK; + break; + case 'n': + board[rank][file] = BLACK | KNIGHT; + break; + case 'N': + board[rank][file] = KNIGHT; + break; + case 'b': + board[rank][file] = BLACK | BISHOP; + break; + case 'B': + board[rank][file] = BISHOP; + break; + case 'q': + board[rank][file] = BLACK | QUEEN; + break; + case 'Q': + board[rank][file] = QUEEN; + break; + case 'k': + board[rank][file] = BLACK | KING; + break; + case 'K': + board[rank][file] = KING; + break; + case 'p': + board[rank][file] = BLACK | PAWN; + break; + case 'P': + board[rank][file] = PAWN; + break; + case '/': + rank--; + file = -1; // so that it becomes 0 + break; + case ' ': + return; + default: + board[rank][file] = KNIGHT; + } + file++; + } + } +} int main() { - initscr(); - printw("Hello, world!"); - refresh(); - getch(); - endwin(); + int board[BOARD_SIZE][BOARD_SIZE]; + parse_FEN(DEFAULT_FEN, board); + print_board(board); return 0; } diff --git a/src/pieces.h b/src/pieces.h new file mode 100644 index 0000000..10d4eec --- /dev/null +++ b/src/pieces.h @@ -0,0 +1,24 @@ +#define WHITE 0b0000 +#define BLACK 0b0001 + +#define EMPTY 0b0000 +#define VISUAL 0b0001 + +#define PAWN 0b0010 +#define KNIGHT 0b0100 +#define BISHOP 0b0110 +#define ROOK 0b1000 +#define QUEEN 0b1010 +#define KING 0b1100 + + +char* pieces[] = { + ".", "#", + "♟︎", "♙", + "♞", "♘", + "♝", "♗", + "♜", "♖", + "♛", "♕", + "♚", "♔", +}; + -- cgit v1.2.3