Refactor
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
chess
|
bin/
|
||||||
|
obj/
|
||||||
docs/
|
docs/
|
||||||
|
|||||||
29
Makefile
29
Makefile
@@ -1,16 +1,29 @@
|
|||||||
|
|
||||||
CC = clang
|
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
|
all: bin/chess
|
||||||
|
|
||||||
|
obj:
|
||||||
|
mkdir -p $@
|
||||||
|
|
||||||
|
bin:
|
||||||
|
mkdir -p $@
|
||||||
|
|
||||||
docs:
|
docs:
|
||||||
doxygen doxygen-config
|
doxygen doxygen-config
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm bin/*
|
rm bin/* obj/*.o
|
||||||
|
|
||||||
gdb: bin/chess
|
gdb: bin/chess
|
||||||
gdb $<
|
gdb $<
|
||||||
@@ -18,10 +31,10 @@ gdb: bin/chess
|
|||||||
run: bin/chess
|
run: bin/chess
|
||||||
./$<
|
./$<
|
||||||
|
|
||||||
bin/chess: chess.c bin
|
obj/%.o: src/%.c
|
||||||
$(CC) $(CFLAGS) $< -o $@
|
$(CC) -o $@ $(CFLAGS) -c $<
|
||||||
|
|
||||||
|
bin/chess: $(OBJ)
|
||||||
|
$(CC) -o $@ $(CFLAGS) $(LDFLAGS) $^
|
||||||
|
|
||||||
bin:
|
.PHONY: all run gdb clean docs
|
||||||
mkdir -p bin
|
|
||||||
|
|
||||||
|
|||||||
4
compile_flags.txt
Normal file
4
compile_flags.txt
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
-Iinclude
|
||||||
|
-Wall
|
||||||
|
-Wextra
|
||||||
|
-Werror
|
||||||
1500
doxygen-config
1500
doxygen-config
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
#include <stdint.h> /* int32_t */
|
|
||||||
#include <stddef.h> /* ptrdiff_t */
|
|
||||||
#include <stdbool.h> /* true, false, bool */
|
#include <stdbool.h> /* true, false, bool */
|
||||||
|
#include <stddef.h> /* ptrdiff_t */
|
||||||
|
#include <stdint.h> /* int32_t */
|
||||||
|
|
||||||
/** Type representing piece/tile on a chessboard */
|
/** Type representing piece/tile on a chessboard */
|
||||||
typedef int32_t tile_t;
|
typedef int32_t tile_t;
|
||||||
@@ -7,7 +7,6 @@
|
|||||||
bool move_ok(const tile_t board[BOARD_SIZE],
|
bool move_ok(const tile_t board[BOARD_SIZE],
|
||||||
index_t from,
|
index_t from,
|
||||||
index_t to,
|
index_t to,
|
||||||
const int player);
|
int player);
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,10 +1,9 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
|
||||||
bool tile_empty(tile_t t);
|
bool tile_empty(tile_t t);
|
||||||
index_t abs_pos(index_t p);
|
index_t abs_pos(index_t p);
|
||||||
index_t column(index_t i);
|
index_t column(index_t i);
|
||||||
index_t row(index_t i);
|
index_t row(index_t i);
|
||||||
tile_t abs_tile(tile_t t);
|
tile_t abs_tile(tile_t t);
|
||||||
bool same_color(tile_t a, tile_t b);
|
bool same_color(tile_t a, tile_t b);
|
||||||
bool opposite_color(tile_t a, tile_t b);
|
bool opposite_color(tile_t a, tile_t b);
|
||||||
|
|
||||||
@@ -1,14 +1,14 @@
|
|||||||
|
|
||||||
#include <ctype.h> /* isalpha, isdigit ... */
|
|
||||||
#include <locale.h> /* setlocale */
|
|
||||||
#include <stdio.h> /* printf, scanf */
|
|
||||||
#include <string.h> /* memcpy */
|
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "graphics.h"
|
#include "graphics.h"
|
||||||
#include "pieces.h"
|
#include "pieces.h"
|
||||||
|
|
||||||
void do_turn(int turn_no, tile_t board[BOARD_SIZE]);
|
#include <ctype.h> /* isalpha, isdigit ... */
|
||||||
|
#include <locale.h> /* setlocale */
|
||||||
|
#include <stdio.h> /* printf, scanf */
|
||||||
|
#include <string.h> /* memcpy */
|
||||||
|
|
||||||
|
int do_turn(int turn_no, tile_t board[BOARD_SIZE]);
|
||||||
void init_board(tile_t board[BOARD_SIZE]);
|
void init_board(tile_t board[BOARD_SIZE]);
|
||||||
index_t input_to_index(char input[2]);
|
index_t input_to_index(char input[2]);
|
||||||
|
|
||||||
@@ -25,8 +25,10 @@ int main()
|
|||||||
while (true) {
|
while (true) {
|
||||||
printf("\033[2J"); // clear screen
|
printf("\033[2J"); // clear screen
|
||||||
print_board(board);
|
print_board(board);
|
||||||
do_turn(turn, board);
|
|
||||||
++turn;
|
// turn increments on valid inputs,
|
||||||
|
// otherwise refreshes screen
|
||||||
|
turn = do_turn(turn, board);
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@@ -54,49 +56,43 @@ void init_board(tile_t board[BOARD_SIZE])
|
|||||||
// TODO: Implement algebaric notation
|
// TODO: Implement algebaric notation
|
||||||
/** Asks for move, validates move and does move if valid
|
/** 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
|
* \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 };
|
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);
|
printf("\nPlayer %i, your turn to move", 1 + turn_no % 2);
|
||||||
|
printf("\nMove piece\nfrom: ");
|
||||||
|
|
||||||
/* temporary and ugly solution - read from and to */
|
scanf(" %2s", input);
|
||||||
while (from == -1 || to == -1) {
|
|
||||||
from = -1;
|
|
||||||
to = -1;
|
|
||||||
|
|
||||||
printf("\nMove piece\nfrom: ");
|
from = input_to_index(input);
|
||||||
scanf(" %2s", input);
|
|
||||||
|
|
||||||
tmp = input_to_index(input);
|
if (from == -1)
|
||||||
|
return turn_no;
|
||||||
|
|
||||||
if (tmp == -1)
|
printf("\nto: ");
|
||||||
continue;
|
scanf(" %2s", input);
|
||||||
|
|
||||||
from = tmp;
|
to = input_to_index(input);
|
||||||
|
|
||||||
printf("\nto: ");
|
if (to == -1)
|
||||||
scanf(" %2s", input);
|
return turn_no;
|
||||||
tmp = input_to_index(input);
|
|
||||||
|
|
||||||
if (tmp == -1)
|
if (! move_ok(board, from, to, turn_no % 2 ? BLACK : WHITE))
|
||||||
continue;
|
return turn_no;
|
||||||
|
|
||||||
to = tmp;
|
|
||||||
|
|
||||||
if (! move_ok(board, from, to, turn_no % 2 ? BLACK : WHITE)) {
|
|
||||||
from = -1;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
board[to] = board[from];
|
board[to] = board[from];
|
||||||
board[from] = E;
|
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
|
else
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,26 +1,28 @@
|
|||||||
|
|
||||||
#include "graphics.h"
|
#include "graphics.h"
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
/** Set background to dark blue */
|
/** Set background to dark blue */
|
||||||
#define BG_DARKBLUE() setcolor(0, 100, 100, 150)
|
#define BG_DARKBLUE() setcolor(0, 100, 100, 150)
|
||||||
|
|
||||||
/** Set background to light blue */
|
/** Set background to light blue */
|
||||||
#define BG_LIGHTBLUE() setcolor(0, 150, 150, 200)
|
#define BG_LIGHTBLUE() setcolor(0, 150, 150, 200)
|
||||||
|
|
||||||
/** Set foreground to black */
|
/** Set foreground to black */
|
||||||
#define FG_BLACK() setcolor(1, 0, 0, 0)
|
#define FG_BLACK() setcolor(1, 0, 0, 0)
|
||||||
|
|
||||||
/** Set foreground to white */
|
/** Set foreground to white */
|
||||||
#define FG_WHITE() setcolor(1, 0xff, 0xff, 0xff)
|
#define FG_WHITE() setcolor(1, 0xff, 0xff, 0xff)
|
||||||
|
|
||||||
/** 0x2659 == ♙ */
|
/** 0x2659 == ♙ */
|
||||||
#define UNICODE_CHESS_SYMBOL 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.
|
* 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 b amount of blue (0 to 255)
|
||||||
* \param g amount of green (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)
|
if (mode == 2)
|
||||||
printf("\033[0m");
|
printf("\033[0m");
|
||||||
@@ -2,19 +2,23 @@
|
|||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
|
|
||||||
|
bool
|
||||||
static bool bishop_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
|
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);
|
bool
|
||||||
static bool diagonal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
|
cardinal_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);
|
bool
|
||||||
static bool knight_move_ok(index_t from, index_t to);
|
diagonal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
|
||||||
static bool pawn_move_ok(const tile_t board[BOARD_SIZE],
|
bool
|
||||||
index_t from,
|
king_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
|
||||||
index_t to,
|
bool knight_move_ok(index_t from, index_t to);
|
||||||
int player);
|
bool pawn_move_ok(const tile_t board[BOARD_SIZE],
|
||||||
static bool queen_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
|
index_t from,
|
||||||
static bool rook_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to);
|
index_t to,
|
||||||
|
int player);
|
||||||
|
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
|
* 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))
|
if (abs_pos(col_diff) != abs_pos(row_diff))
|
||||||
return false;
|
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]))
|
if (! tile_empty(board[p]))
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -87,18 +90,16 @@ bool cardinal_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to)
|
|||||||
const index_t col_diff = column(to) - column(from);
|
const index_t col_diff = column(to) - column(from);
|
||||||
const index_t row_diff = row(to) - row(from);
|
const index_t row_diff = row(to) - row(from);
|
||||||
|
|
||||||
/* cardinal moves means one direction has to be zero */
|
/* cardinal moves means one direction has to be zero */
|
||||||
if (row_diff > 0 && col_diff > 0)
|
if (row_diff > 0 && col_diff > 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
index_t step = row_diff ?
|
index_t step = row_diff ? ROW * row_diff / abs_pos(row_diff) :
|
||||||
ROW * row_diff / abs_pos(row_diff)
|
col_diff / abs_pos(col_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]))
|
if (! tile_empty(board[p]))
|
||||||
return false;
|
return false;
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
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 c = abs_pos(column(to) - column(from));
|
||||||
const index_t r = abs_pos(row(to - from));
|
const index_t r = abs_pos(row(to - from));
|
||||||
|
|
||||||
return (c == 1 && r == 2)
|
return (c == 1 && r == 2) || (c == 2 && r == 1);
|
||||||
|| (c == 2 && r == 1);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -183,7 +183,7 @@ bool queen_move_ok(const tile_t board[BOARD_SIZE], index_t from, index_t to)
|
|||||||
bool move_ok(const tile_t board[BOARD_SIZE],
|
bool move_ok(const tile_t board[BOARD_SIZE],
|
||||||
index_t from,
|
index_t from,
|
||||||
index_t to,
|
index_t to,
|
||||||
int player)
|
int player)
|
||||||
{
|
{
|
||||||
/* player must own piece it moves */
|
/* player must own piece it moves */
|
||||||
if (board[from] * player < 0)
|
if (board[from] * player < 0)
|
||||||
@@ -78,5 +78,3 @@ bool same_color(tile_t a, tile_t b)
|
|||||||
{
|
{
|
||||||
return a * b > 0;
|
return a * b > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user