summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile4
-rw-r--r--src/config.h2
-rw-r--r--src/main.c92
-rw-r--r--src/pieces.h24
4 files changed, 114 insertions, 8 deletions
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 <ncurses.h>
+#include <locale.h>
+#include <stdio.h>
+#include <string.h>
+
+#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[] = {
+ ".", "#",
+ "♟︎", "♙",
+ "♞", "♘",
+ "♝", "♗",
+ "♜", "♖",
+ "♛", "♕",
+ "♚", "♔",
+};
+