Just a quickie

This commit is contained in:
cdricms
2023-12-29 20:20:37 +01:00
parent b6fb001f98
commit 32b71b40d7
5 changed files with 79 additions and 34 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
*.dSYM
main
build
gmon.out
perf.data

View File

@@ -1,5 +1,5 @@
CC = gcc
CFLAGS = -Wall -g
CFLAGS = -Wall -pg
SRCDIR = src
BUILDDIR = build
TARGET = main

View File

@@ -21,8 +21,8 @@
#define BLACK_QUEEN "\u265B"
#define BLACK_KING "\u265A"
PieceColors piece_getColor(const Pieces piece) { return Black & piece; }
Piece piece_getColorlessPiece(const Pieces piece) {
PieceColors piece_getColor(const Pieces piece) { return BlackPiece & piece; }
Pieces piece_getColorlessPiece(const Pieces piece) {
return piece_getColor(piece) ^ piece;
}
@@ -81,7 +81,7 @@ void piece_getLinearMoves(const Coordinates relative_coords, Moves *moves,
}
Moves *piece_getMoves(const Pieces piece, const Coordinates piece_coords) {
Piece colorless_piece = piece_getColorlessPiece(piece);
Pieces colorless_piece = piece_getColorlessPiece(piece);
PieceColors piece_color = piece_getColor(piece);
int board_index = board_getIndex(piece_coords);
if (board_index == -1)
@@ -93,16 +93,16 @@ Moves *piece_getMoves(const Pieces piece, const Coordinates piece_coords) {
moves->max_capacity = PAWN_MAXMOVES;
uint moves_i = 0;
Coordinates new_coordinates = {.column = piece_coords.column,
.row = piece_color == Black
.row = piece_color == BlackPiece
? piece_coords.row - 1
: piece_coords.row + 1};
moves->coords[moves_i++] = new_coordinates;
bool is_starting_pos =
piece_color == Black ? piece_coords.row == 7 : piece_coords.row == 2;
bool is_starting_pos = piece_color == BlackPiece ? piece_coords.row == 7
: piece_coords.row == 2;
if (is_starting_pos) {
Coordinates leap_coords = {.column = piece_coords.column,
.row = piece_color == Black
.row = piece_color == BlackPiece
? piece_coords.row - 2
: piece_coords.row + 2};
moves->coords[moves_i++] = leap_coords;
@@ -176,13 +176,15 @@ Moves *piece_getMoves(const Pieces piece, const Coordinates piece_coords) {
moves->length = moves_i;
return moves;
}
default:
break;
}
return NULL;
}
bool board_getLegalMoves(const Board board, const Coordinates piece_coords,
const Coordinates *moves) {
Moves *board_getLegalMoves(const Board board, const Coordinates piece_coords,
const Moves *moves) {
const uint piece_index = board_getIndex(piece_coords);
if (piece_index == -1) {
return false;
@@ -191,9 +193,38 @@ bool board_getLegalMoves(const Board board, const Coordinates piece_coords,
if (piece == -1)
return false;
const PieceColors piece_color = piece_getColor(piece);
const Piece bare_piece = piece_getColorlessPiece(piece);
const Pieces bare_piece = piece_getColorlessPiece(piece);
// General rule of thumb when squares need to be dismissed:
// - If a square is occupied by an ally.
// Specific cases (Bishop, Rook, Queen):
// - The squares behind another piece.
//
// King:
// - Can not move to a square if said square is threatened.
// - Can not take a piece, if said piece is protected.
//
// Absolute pinning:
// - A piece can not move if it covers the King from being in check.
Moves *legal_moves = malloc(sizeof(Moves));
legal_moves->max_capacity = moves->length;
// Remove squares that are occupied by allied forces.
uint l_index = 0;
for (uint i = 0; i < moves->length; i++) {
const uint other_index = board_getIndex(moves->coords[i]);
if (other_index == -1)
continue;
const Pieces other_piece = board.squares[other_index].piece;
if (other_piece == -1) {
legal_moves->coords[l_index++] = moves->coords[i];
legal_moves->length++;
continue;
}
const PieceColors other_color = piece_getColor(other_piece);
if (other_color == piece_color)
continue;
}
return true;
}
@@ -220,22 +251,24 @@ void board_print(const Board board) {
char *unicode;
switch (bare_piece) {
case Pawn:
unicode = piece_color == White ? WHITE_PAWN : BLACK_PAWN;
unicode = piece_color == WhitePiece ? WHITE_PAWN : BLACK_PAWN;
break;
case Knight:
unicode = piece_color == White ? WHITE_KNIGHT : BLACK_KNIGHT;
unicode = piece_color == WhitePiece ? WHITE_KNIGHT : BLACK_KNIGHT;
break;
case Bishop:
unicode = piece_color == White ? WHITE_BISHOP : BLACK_BISHOP;
unicode = piece_color == WhitePiece ? WHITE_BISHOP : BLACK_BISHOP;
break;
case Rook:
unicode = piece_color == White ? WHITE_ROOK : BLACK_ROOK;
unicode = piece_color == WhitePiece ? WHITE_ROOK : BLACK_ROOK;
break;
case Queen:
unicode = piece_color == White ? WHITE_QUEEN : BLACK_QUEEN;
unicode = piece_color == WhitePiece ? WHITE_QUEEN : BLACK_QUEEN;
break;
case King:
unicode = piece_color == White ? WHITE_KING : BLACK_KING;
unicode = piece_color == WhitePiece ? WHITE_KING : BLACK_KING;
break;
default:
break;
}

View File

@@ -3,9 +3,9 @@
#include "../datastructures/LinkedList.h"
#include "common.h"
typedef int Piece;
typedef enum { Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces;
typedef enum { White = 0, Black = 8 } PieceColors;
typedef enum { None = -1, Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces;
typedef enum { WhitePiece = 0, BlackPiece = 8 } PieceColors;
typedef enum { WhiteSquare = 0, BlackSquare } SquareColors;
typedef struct {
uint row;
@@ -19,8 +19,9 @@ typedef struct {
} Moves;
typedef struct {
Piece piece;
Pieces piece;
Coordinates coords;
SquareColors color;
} Square;
typedef LinkedList FEN_History;
@@ -31,7 +32,7 @@ typedef struct {
} Board;
PieceColors piece_getColor(const Pieces piece);
Piece piece_getColorlessPiece(const Pieces piece);
Pieces piece_getColorlessPiece(const Pieces piece);
bool board_movePiece(Board board, const Coordinates from, const Coordinates to);
int board_getIndex(const Coordinates coords);

View File

@@ -18,34 +18,38 @@ void fen_toBoard(const FEN fen, Board *board) {
if ((is_upper_case = fen_char > 'A' && fen_char < 'Z') ||
(fen_char > 'a' && fen_char < 'z')) {
char lowered_char = chrToLower(fen_char);
Pieces p;
Pieces _piece;
switch (lowered_char) {
case 'p':
p = Pawn;
_piece = Pawn;
break;
case 'n':
p = Knight;
_piece = Knight;
break;
case 'b':
p = Bishop;
_piece = Bishop;
break;
case 'r':
p = Rook;
_piece = Rook;
break;
case 'q':
p = Queen;
_piece = Queen;
break;
case 'k':
p = King;
_piece = King;
break;
default:
printf("[ERROR] %c is not an authorized character\n", fen_char);
return;
}
PieceColors color = is_upper_case ? White : Black;
Piece piece = p ^ color;
PieceColors color = is_upper_case ? WhitePiece : BlackPiece;
Pieces piece = _piece ^ color;
Coordinates coords = {.row = row, .column = ++column};
Square square = {.piece = piece, .coords = coords};
SquareColors square_color =
(column + 1) % 2 == 0 ? BlackSquare : WhiteSquare;
if ((row + 1) % 2 == 1)
square_color = !square_color;
Square square = {.piece = piece, .coords = coords, .color = square_color};
board->squares[board_i++] = square;
} else if (fen_char > '0' && fen_char < '9') {
// if the char is a number bet. 1 and 8
@@ -54,7 +58,12 @@ void fen_toBoard(const FEN fen, Board *board) {
if (offset < 9) {
for (int j = 0; j < offset; j++) {
Coordinates coords = {.row = row, .column = ++column};
Square square = {.piece = -1, .coords = coords};
SquareColors square_color =
(column + 1) % 2 == 0 ? BlackSquare : WhiteSquare;
if ((row + 1) % 2 == 1)
square_color = !square_color;
Square square = {
.piece = None, .coords = coords, .color = square_color};
board->squares[board_i++] = square;
}
} else {
@@ -115,7 +124,7 @@ void fen_fromBoard(const Board board, FEN fen) {
printf("[ERROR]");
return;
}
if (piece_getColor(square.piece) == White)
if (piece_getColor(square.piece) == WhitePiece)
fen_char = chrToUpper(fen_char);
fen[fen_i++] = fen_char;
}