This commit is contained in:
Ole Morud
2023-03-22 21:45:29 +01:00
parent f18c9215e5
commit 60942dcfd2
13 changed files with 99 additions and 1581 deletions

3
.gitignore vendored
View File

@@ -1,2 +1,3 @@
chess
bin/
obj/
docs/

View File

@@ -1,16 +1,29 @@
CC = clang
CFLAGS = -Ofast -fsanitize=address -static-libsan -ggdb3 -Wall -Wextra -Werror
LD = ld.lld
CFLAGS = -Iinclude
CFLAGS += -Ofast -ggdb3
CFLAGS += -Wall -Wextra -Werror
CFLAGS += -fsanitize=address
LDFLAGS = -fuse-ld=lld
LDFLAGS = -fsanitize=address
.PHONY: all run gdb clean docs
_OBJ = chess.o common.o graphics.o pieces.o util.o
OBJ = $(addprefix obj/, $(_OBJ))
all: bin/chess
obj:
mkdir -p $@
bin:
mkdir -p $@
docs:
doxygen doxygen-config
clean:
rm bin/*
rm bin/* obj/*.o
gdb: bin/chess
gdb $<
@@ -18,10 +31,10 @@ gdb: bin/chess
run: bin/chess
./$<
bin/chess: chess.c bin
$(CC) $(CFLAGS) $< -o $@
obj/%.o: src/%.c
$(CC) -o $@ $(CFLAGS) -c $<
bin/chess: $(OBJ)
$(CC) -o $@ $(CFLAGS) $(LDFLAGS) $^
bin:
mkdir -p bin
.PHONY: all run gdb clean docs

4
compile_flags.txt Normal file
View File

@@ -0,0 +1,4 @@
-Iinclude
-Wall
-Wextra
-Werror

File diff suppressed because it is too large Load Diff

View File

@@ -1,7 +1,7 @@
#include <stdint.h> /* int32_t */
#include <stddef.h> /* ptrdiff_t */
#include <stdbool.h> /* true, false, bool */
#include <stddef.h> /* ptrdiff_t */
#include <stdint.h> /* int32_t */
/** Type representing piece/tile on a chessboard */
typedef int32_t tile_t;

View File

@@ -7,7 +7,6 @@
bool move_ok(const tile_t board[BOARD_SIZE],
index_t from,
index_t to,
const int player);
int player);
#endif

View File

@@ -7,4 +7,3 @@ index_t row(index_t i);
tile_t abs_tile(tile_t t);
bool same_color(tile_t a, tile_t b);
bool opposite_color(tile_t a, tile_t b);

View File

@@ -1,14 +1,14 @@
#include "common.h"
#include "graphics.h"
#include "pieces.h"
#include <ctype.h> /* isalpha, isdigit ... */
#include <locale.h> /* setlocale */
#include <stdio.h> /* printf, scanf */
#include <string.h> /* memcpy */
#include "common.h"
#include "graphics.h"
#include "pieces.h"
void do_turn(int turn_no, tile_t board[BOARD_SIZE]);
int do_turn(int turn_no, tile_t board[BOARD_SIZE]);
void init_board(tile_t board[BOARD_SIZE]);
index_t input_to_index(char input[2]);
@@ -25,8 +25,10 @@ int main()
while (true) {
printf("\033[2J"); // clear screen
print_board(board);
do_turn(turn, board);
++turn;
// turn increments on valid inputs,
// otherwise refreshes screen
turn = do_turn(turn, board);
}
return 0;
@@ -54,49 +56,43 @@ void init_board(tile_t board[BOARD_SIZE])
// TODO: Implement algebaric notation
/** Asks for move, validates move and does move if valid
*
* \param turn_no Turn number, is increased between every `do_turn` elsewhere
* \param turn_no Turn number
* \param board Pointer to list of tiles representing board state
*
* \return turn+1 if successful, turn+0 if invalid input from player
* */
void do_turn(int turn_no, tile_t board[BOARD_SIZE])
int do_turn(int turn_no, tile_t board[BOARD_SIZE])
{
char input[3] = { 0 };
int from = -1, to = -1, tmp;
int from = -1, to = -1;
printf("\nPlayer %i, your turn to move", 1 + turn_no % 2);
/* temporary and ugly solution - read from and to */
while (from == -1 || to == -1) {
from = -1;
to = -1;
printf("\nMove piece\nfrom: ");
scanf(" %2s", input);
tmp = input_to_index(input);
from = input_to_index(input);
if (tmp == -1)
continue;
from = tmp;
if (from == -1)
return turn_no;
printf("\nto: ");
scanf(" %2s", input);
tmp = input_to_index(input);
if (tmp == -1)
continue;
to = input_to_index(input);
to = tmp;
if (to == -1)
return turn_no;
if (! move_ok(board, from, to, turn_no % 2 ? BLACK : WHITE)) {
from = -1;
continue;
}
}
if (! move_ok(board, from, to, turn_no % 2 ? BLACK : WHITE))
return turn_no;
board[to] = board[from];
board[from] = E;
/* increment to make it next turn */
return turn_no+1;
}
/**
@@ -126,4 +122,3 @@ index_t input_to_index(char input[2])
else
return -1;
}

View File

@@ -1,5 +1,6 @@
#include "graphics.h"
#include "common.h"
#include "util.h"
@@ -20,7 +21,8 @@
/** 0x2659 == ♙ */
#define UNICODE_CHESS_SYMBOL 0x2659
static inline void setcolor(const int mode, const int r, const int g, const int b);
static inline void
setcolor(const int mode, const int r, const int g, const int b);
/**
* Sets the foreground or background color for subsequent writes.
@@ -33,7 +35,8 @@ static inline void setcolor(const int mode, const int r, const int g, const int
* \param b amount of blue (0 to 255)
* \param g amount of green (0 to 255)
*/
static inline void setcolor(const int mode, const int r, const int g, const int b)
static inline void
setcolor(const int mode, const int r, const int g, const int b)
{
if (mode == 2)
printf("\033[0m");

View File

@@ -2,19 +2,23 @@
#include "common.h"
#include "util.h"
static bool bishop_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
static bool cardinal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
static bool diagonal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
static bool king_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
static bool knight_move_ok(index_t from, index_t to);
static bool pawn_move_ok(const tile_t board[BOARD_SIZE],
bool
bishop_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
bool
cardinal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
bool
diagonal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
bool
king_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
bool knight_move_ok(index_t from, index_t to);
bool pawn_move_ok(const tile_t board[BOARD_SIZE],
index_t from,
index_t to,
int player);
static bool queen_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
static bool rook_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
bool
queen_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
bool
rook_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
/**
* Returns true if move is a valid pawn move
@@ -67,10 +71,9 @@ bool diagonal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to)
if (abs_pos(col_diff) != abs_pos(row_diff))
return false;
for (index_t p = from + step; p != to; p += step) {
for (index_t p = from + step; p != to; p += step)
if (! tile_empty(board[p]))
return false;
}
return true;
}
@@ -91,14 +94,12 @@ bool cardinal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to)
if (row_diff > 0 && col_diff > 0)
return false;
index_t step = row_diff ?
ROW * row_diff / abs_pos(row_diff)
: col_diff / abs_pos(col_diff);
index_t step = row_diff ? ROW * row_diff / abs_pos(row_diff) :
col_diff / abs_pos(col_diff);
for (index_t p = from + step; p != to; p += step) {
for (index_t p = from + step; p != to; p += step)
if (! tile_empty(board[p]))
return false;
}
return true;
}
@@ -138,8 +139,7 @@ bool knight_move_ok(index_t from, index_t to)
const index_t c = abs_pos(column(to) - column(from));
const index_t r = abs_pos(row(to - from));
return (c == 1 && r == 2)
|| (c == 2 && r == 1);
return (c == 1 && r == 2) || (c == 2 && r == 1);
}
/**

View File

@@ -78,5 +78,3 @@ bool same_color(tile_t a, tile_t b)
{
return a * b > 0;
}