clang-format chess.c

This commit is contained in:
Ole Morud
2023-03-19 21:18:54 +01:00
parent c25b983674
commit 4e4a28b910

111
chess.c
View File

@@ -1,10 +1,10 @@
#include <ctype.h> /* isalpha, isdigit ... */
#include <locale.h> /* setlocale */
#include <string.h> /* memcpy */
#include <stdio.h> /* printf, scanf */
#include <stdint.h> /* int32_t */
#include <stdbool.h> /* true, false */
#include <stdint.h> /* int32_t */
#include <stdio.h> /* printf, scanf */
#include <string.h> /* memcpy */
typedef int32_t tile_t;
typedef ssize_t pos_t;
@@ -32,15 +32,15 @@ typedef ssize_t pos_t;
#define UNICODE_CHESS_SYMBOL 0x2659
bool bishop_move_ok(tile_t const* board, pos_t from, pos_t to);
bool cardinal_move_ok(tile_t const *board, pos_t from, pos_t to);
bool diagonal_move_ok(tile_t const *board, pos_t from, pos_t to);
bool king_move_ok(tile_t const *board, pos_t from, pos_t to);
bool bishop_move_ok(const tile_t *board, pos_t from, pos_t to);
bool cardinal_move_ok(const tile_t *board, pos_t from, pos_t to);
bool diagonal_move_ok(const tile_t *board, pos_t from, pos_t to);
bool king_move_ok(const tile_t *board, pos_t from, pos_t to);
bool knight_move_ok(pos_t from, pos_t to);
bool move_ok(tile_t *board, pos_t from, pos_t to, int player);
bool pawn_move_ok(tile_t const* board, pos_t from, pos_t to, int direction);
bool queen_move_ok(tile_t const *board, pos_t from, pos_t to);
bool rook_move_ok(tile_t const *board, pos_t from, pos_t to);
bool pawn_move_ok(const tile_t *board, pos_t from, pos_t to, int direction);
bool queen_move_ok(const tile_t *board, pos_t from, pos_t to);
bool rook_move_ok(const tile_t *board, pos_t from, pos_t to);
bool tile_empty(tile_t t);
int get_piece(char *input);
pos_t abs_pos(pos_t p);
@@ -52,7 +52,8 @@ void init_board(tile_t* board);
void print_board(tile_t *board);
void setcolor(int mode, int r, int g, int b);
int main(){
int main()
{
int turn = 0;
setlocale(LC_ALL, "C.UTF-8");
@@ -76,7 +77,8 @@ int main(){
1: change foreground
2: reset colors
*/
void setcolor(const int mode, const int r, const int g, const int b){
void setcolor(const int mode, const int r, const int g, const int b)
{
if (mode == 2)
printf("\033[0m");
else
@@ -84,7 +86,8 @@ void setcolor(const int mode, const int r, const int g, const int b){
};
/* Prints the board */
void print_board(tile_t* board){
void print_board(tile_t *board)
{
/* https://en.wikipedia.org/wiki/Chess_symbols_in_Unicode
The unicode symbol is calculated from adding 0x2653 with the
@@ -127,38 +130,28 @@ void print_board(tile_t* board){
printf(" %c", 'a' + i);
}
/*
* Resets/inits the board
*/
void init_board(tile_t *board)
{
// black pieces are prefixed by a minus (-)
const tile_t start[] = {
-R,-N,-B,-Q,-K,-B,-N,-R,
-P,-P,-P,-P,-P,-P,-P,-P,
E, E, E, E, E, E, E, E,
E, E, E, E, E, E, E, E,
E, E, E, E, E, E, E, E,
E, E, E, E, E, E, E, E,
P, P, P, P, P, P, P, P,
R, N, B, Q, K, B, N, R
};
const tile_t start[]
= { -R, -N, -B, -Q, -K, -B, -N, -R, -P, -P, -P, -P, -P, -P, -P, -P,
E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
E, E, E, E, E, E, E, E, E, E, E, E, E, E, E, E,
P, P, P, P, P, P, P, P, R, N, B, Q, K, B, N, R };
memcpy(board, start, sizeof(start));
}
// TODO: Implement algebaric notation
/* Get move, check move and log move for turn <turn_no> */
void do_turn(int turn_no, tile_t *board)
{
char input[3] = { 0 };
int from = -1,
to = -1,
tmp;
int from = -1, to = -1, tmp;
printf("\nPlayer %i, your turn to move", 1 + turn_no % 2);
@@ -196,13 +189,10 @@ void do_turn(int turn_no, tile_t *board)
board[from] = E;
}
/* Translates A1, 3B etc. to the 1D index of the board */
int get_piece(char *input)
{
int x = -1,
y = -1,
c;
int x = -1, y = -1, c;
for (int i = 0; i < 2; i++) {
c = input[i];
@@ -269,7 +259,7 @@ bool same_color(tile_t a, tile_t b)
}
/* Returns true if a move is valid, false otherwise */
bool move_ok(tile_t* board, pos_t from, pos_t to, int const player)
bool move_ok(tile_t *board, pos_t from, pos_t to, const int player)
{
/* player must own piece it moves */
if (board[from] * player < 0) {
@@ -285,7 +275,6 @@ bool move_ok(tile_t* board, pos_t from, pos_t to, int const player)
/* check piece specific moves */
switch (abs_tile(board[from])) {
/* PAWNS */
case P:
return pawn_move_ok(board, from, to, player);
@@ -317,9 +306,9 @@ bool move_ok(tile_t* board, pos_t from, pos_t to, int const player)
from - index of board piece starts at
to - index of board piece wants to move to
direction - pawns movement direction */
bool pawn_move_ok(tile_t const* board, pos_t from, pos_t to, int direction)
bool pawn_move_ok(const tile_t *board, pos_t from, pos_t to, int direction)
{
pos_t const diff = (to - from) * -direction;
const pos_t diff = (to - from) * -direction;
switch (diff) {
default:
@@ -328,7 +317,8 @@ bool pawn_move_ok(tile_t const* board, pos_t from, pos_t to, int direction)
case ROW: /* single move */
return tile_empty(board[to]);
case ROW - 1: case ROW+1: /* diagonal attack */
case ROW - 1:
case ROW + 1: /* diagonal attack */
return opposite_color(board[to], board[from]);
case 2 * ROW: /* double move */
@@ -342,14 +332,14 @@ bool pawn_move_ok(tile_t const* board, pos_t from, pos_t to, int direction)
board - array of tiles representing chess board state
from - index of board piece is at
to - index of board piece tries to move to */
bool diagonal_move_ok(tile_t const *board, pos_t from, pos_t to)
bool diagonal_move_ok(const tile_t *board, pos_t from, pos_t to)
{
pos_t const col_diff = column(to) - column(from);
pos_t const row_diff = row(to) - row(from);
const pos_t col_diff = column(to) - column(from);
const pos_t row_diff = row(to) - row(from);
pos_t const x_step = col_diff / abs_pos(col_diff);
pos_t const y_step = ROW * row_diff / abs_pos(row_diff);
pos_t const step = x_step + y_step;
const pos_t x_step = col_diff / abs_pos(col_diff);
const pos_t y_step = ROW * row_diff / abs_pos(row_diff);
const pos_t step = x_step + y_step;
if (abs_pos(col_diff) != abs_pos(row_diff)) {
printf("\nnot a diagonal move");
@@ -370,22 +360,20 @@ bool diagonal_move_ok(tile_t const *board, pos_t from, pos_t to)
board - array of tiles representing chess board state
from - index of board piece is at
to - index of board piece tries to move to */
bool cardinal_move_ok(tile_t const *board, pos_t from, pos_t to)
bool cardinal_move_ok(const tile_t *board, pos_t from, pos_t to)
{
pos_t const col_diff = column(to) - column(from);
pos_t const row_diff = row(to) - row(from);
const pos_t col_diff = column(to) - column(from);
const pos_t row_diff = row(to) - row(from);
if (row_diff > 0 && col_diff > 0) {
if (row_diff > 0 && col_diff > 0)
printf("Must move in a straight line");
}
pos_t step;
if (row_diff) {
if (row_diff)
step = ROW * row_diff / abs_pos(row_diff);
} else {
else
step = col_diff / abs_pos(col_diff);
}
for (pos_t p = from + step; p != to; p += step) {
if (! tile_empty(board[p])) {
@@ -401,7 +389,7 @@ bool cardinal_move_ok(tile_t const *board, pos_t from, pos_t to)
board - array of tiles representing chess board state
from - index of board bishop is at
to - index of board bishop wants to move to */
bool bishop_move_ok(tile_t const* board, pos_t from, pos_t to)
bool bishop_move_ok(const tile_t *board, pos_t from, pos_t to)
{
return diagonal_move_ok(board, from, to);
}
@@ -410,7 +398,7 @@ bool bishop_move_ok(tile_t const* board, pos_t from, pos_t to)
board - array of tiles representing chess board state
from - index of board rook is at
to - index of board rook wants to move to */
bool rook_move_ok(tile_t const *board, pos_t from, pos_t to)
bool rook_move_ok(const tile_t *board, pos_t from, pos_t to)
{
return cardinal_move_ok(board, from, to);
}
@@ -421,8 +409,8 @@ bool rook_move_ok(tile_t const *board, pos_t from, pos_t to)
to - index of board knight wants to move to */
bool knight_move_ok(pos_t from, pos_t to)
{
pos_t const abs_col_diff = abs_pos(column(to) - column(from));
pos_t const abs_row_diff = abs_pos(row(to) - row(from));
const pos_t abs_col_diff = abs_pos(column(to) - column(from));
const pos_t abs_row_diff = abs_pos(row(to) - row(from));
return (abs_col_diff == 1 && abs_row_diff == 2)
|| (abs_col_diff == 2 && abs_row_diff == 1);
@@ -432,22 +420,21 @@ bool knight_move_ok(pos_t from, pos_t to)
board - array of tiles representing chess board state
from - index of board king is at
to - index of board king wants to move to */
bool king_move_ok(tile_t const *board, pos_t from, pos_t to)
bool king_move_ok(const tile_t *board, pos_t from, pos_t to)
{
pos_t const abs_col_diff = abs_pos(column(to) - column(from));
pos_t const abs_row_diff = abs_pos(row(to) - row(from));
const pos_t abs_col_diff = abs_pos(column(to) - column(from));
const pos_t abs_row_diff = abs_pos(row(to) - row(from));
(void)board;
return abs_col_diff <= 1
&& abs_row_diff <= 1;
return abs_col_diff <= 1 && abs_row_diff <= 1;
}
/* Returns true if move is a valid queen move
board - array of tiles representing chess board state
from - index of board queen is at
to - index of board queen wants to move to */
bool queen_move_ok(tile_t const *board, pos_t from, pos_t to)
bool queen_move_ok(const tile_t *board, pos_t from, pos_t to)
{
return diagonal_move_ok(board, from, to)
|| cardinal_move_ok(board, from, to);