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 *.dSYM
main main
build build
gmon.out
perf.data

View File

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

View File

@@ -21,8 +21,8 @@
#define BLACK_QUEEN "\u265B" #define BLACK_QUEEN "\u265B"
#define BLACK_KING "\u265A" #define BLACK_KING "\u265A"
PieceColors piece_getColor(const Pieces piece) { return Black & piece; } PieceColors piece_getColor(const Pieces piece) { return BlackPiece & piece; }
Piece piece_getColorlessPiece(const Pieces piece) { Pieces piece_getColorlessPiece(const Pieces piece) {
return piece_getColor(piece) ^ 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) { 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); PieceColors piece_color = piece_getColor(piece);
int board_index = board_getIndex(piece_coords); int board_index = board_getIndex(piece_coords);
if (board_index == -1) if (board_index == -1)
@@ -93,16 +93,16 @@ Moves *piece_getMoves(const Pieces piece, const Coordinates piece_coords) {
moves->max_capacity = PAWN_MAXMOVES; moves->max_capacity = PAWN_MAXMOVES;
uint moves_i = 0; uint moves_i = 0;
Coordinates new_coordinates = {.column = piece_coords.column, Coordinates new_coordinates = {.column = piece_coords.column,
.row = piece_color == Black .row = piece_color == BlackPiece
? piece_coords.row - 1 ? piece_coords.row - 1
: piece_coords.row + 1}; : piece_coords.row + 1};
moves->coords[moves_i++] = new_coordinates; moves->coords[moves_i++] = new_coordinates;
bool is_starting_pos = bool is_starting_pos = piece_color == BlackPiece ? piece_coords.row == 7
piece_color == Black ? piece_coords.row == 7 : piece_coords.row == 2; : piece_coords.row == 2;
if (is_starting_pos) { if (is_starting_pos) {
Coordinates leap_coords = {.column = piece_coords.column, Coordinates leap_coords = {.column = piece_coords.column,
.row = piece_color == Black .row = piece_color == BlackPiece
? piece_coords.row - 2 ? piece_coords.row - 2
: piece_coords.row + 2}; : piece_coords.row + 2};
moves->coords[moves_i++] = leap_coords; moves->coords[moves_i++] = leap_coords;
@@ -176,13 +176,15 @@ Moves *piece_getMoves(const Pieces piece, const Coordinates piece_coords) {
moves->length = moves_i; moves->length = moves_i;
return moves; return moves;
} }
default:
break;
} }
return NULL; return NULL;
} }
bool board_getLegalMoves(const Board board, const Coordinates piece_coords, Moves *board_getLegalMoves(const Board board, const Coordinates piece_coords,
const Coordinates *moves) { const Moves *moves) {
const uint piece_index = board_getIndex(piece_coords); const uint piece_index = board_getIndex(piece_coords);
if (piece_index == -1) { if (piece_index == -1) {
return false; return false;
@@ -191,9 +193,38 @@ bool board_getLegalMoves(const Board board, const Coordinates piece_coords,
if (piece == -1) if (piece == -1)
return false; return false;
const PieceColors piece_color = piece_getColor(piece); 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. // 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; return true;
} }
@@ -220,22 +251,24 @@ void board_print(const Board board) {
char *unicode; char *unicode;
switch (bare_piece) { switch (bare_piece) {
case Pawn: case Pawn:
unicode = piece_color == White ? WHITE_PAWN : BLACK_PAWN; unicode = piece_color == WhitePiece ? WHITE_PAWN : BLACK_PAWN;
break; break;
case Knight: case Knight:
unicode = piece_color == White ? WHITE_KNIGHT : BLACK_KNIGHT; unicode = piece_color == WhitePiece ? WHITE_KNIGHT : BLACK_KNIGHT;
break; break;
case Bishop: case Bishop:
unicode = piece_color == White ? WHITE_BISHOP : BLACK_BISHOP; unicode = piece_color == WhitePiece ? WHITE_BISHOP : BLACK_BISHOP;
break; break;
case Rook: case Rook:
unicode = piece_color == White ? WHITE_ROOK : BLACK_ROOK; unicode = piece_color == WhitePiece ? WHITE_ROOK : BLACK_ROOK;
break; break;
case Queen: case Queen:
unicode = piece_color == White ? WHITE_QUEEN : BLACK_QUEEN; unicode = piece_color == WhitePiece ? WHITE_QUEEN : BLACK_QUEEN;
break; break;
case King: case King:
unicode = piece_color == White ? WHITE_KING : BLACK_KING; unicode = piece_color == WhitePiece ? WHITE_KING : BLACK_KING;
break;
default:
break; break;
} }

View File

@@ -3,9 +3,9 @@
#include "../datastructures/LinkedList.h" #include "../datastructures/LinkedList.h"
#include "common.h" #include "common.h"
typedef int Piece; typedef enum { None = -1, Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces;
typedef enum { Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces; typedef enum { WhitePiece = 0, BlackPiece = 8 } PieceColors;
typedef enum { White = 0, Black = 8 } PieceColors; typedef enum { WhiteSquare = 0, BlackSquare } SquareColors;
typedef struct { typedef struct {
uint row; uint row;
@@ -19,8 +19,9 @@ typedef struct {
} Moves; } Moves;
typedef struct { typedef struct {
Piece piece; Pieces piece;
Coordinates coords; Coordinates coords;
SquareColors color;
} Square; } Square;
typedef LinkedList FEN_History; typedef LinkedList FEN_History;
@@ -31,7 +32,7 @@ typedef struct {
} Board; } Board;
PieceColors piece_getColor(const Pieces piece); 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); bool board_movePiece(Board board, const Coordinates from, const Coordinates to);
int board_getIndex(const Coordinates coords); 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') || if ((is_upper_case = fen_char > 'A' && fen_char < 'Z') ||
(fen_char > 'a' && fen_char < 'z')) { (fen_char > 'a' && fen_char < 'z')) {
char lowered_char = chrToLower(fen_char); char lowered_char = chrToLower(fen_char);
Pieces p; Pieces _piece;
switch (lowered_char) { switch (lowered_char) {
case 'p': case 'p':
p = Pawn; _piece = Pawn;
break; break;
case 'n': case 'n':
p = Knight; _piece = Knight;
break; break;
case 'b': case 'b':
p = Bishop; _piece = Bishop;
break; break;
case 'r': case 'r':
p = Rook; _piece = Rook;
break; break;
case 'q': case 'q':
p = Queen; _piece = Queen;
break; break;
case 'k': case 'k':
p = King; _piece = King;
break; break;
default: default:
printf("[ERROR] %c is not an authorized character\n", fen_char); printf("[ERROR] %c is not an authorized character\n", fen_char);
return; return;
} }
PieceColors color = is_upper_case ? White : Black; PieceColors color = is_upper_case ? WhitePiece : BlackPiece;
Piece piece = p ^ color; Pieces piece = _piece ^ color;
Coordinates coords = {.row = row, .column = ++column}; 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; board->squares[board_i++] = square;
} else if (fen_char > '0' && fen_char < '9') { } else if (fen_char > '0' && fen_char < '9') {
// if the char is a number bet. 1 and 8 // 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) { if (offset < 9) {
for (int j = 0; j < offset; j++) { for (int j = 0; j < offset; j++) {
Coordinates coords = {.row = row, .column = ++column}; 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; board->squares[board_i++] = square;
} }
} else { } else {
@@ -115,7 +124,7 @@ void fen_fromBoard(const Board board, FEN fen) {
printf("[ERROR]"); printf("[ERROR]");
return; return;
} }
if (piece_getColor(square.piece) == White) if (piece_getColor(square.piece) == WhitePiece)
fen_char = chrToUpper(fen_char); fen_char = chrToUpper(fen_char);
fen[fen_i++] = fen_char; fen[fen_i++] = fen_char;
} }