Compare commits

...

2 Commits

Author SHA1 Message Date
cdricms
62a03b01c0 Bishop moves 2023-12-21 23:56:24 +01:00
cdricms
b05fb5c303 Started working on generating the moves 2023-12-21 12:04:19 +01:00
4 changed files with 140 additions and 13 deletions

View File

@@ -1,18 +1,144 @@
#include "board.h"
PieceColors piece_getColor(const Piece piece) { return Black & piece; }
Piece piece_getColorlessPiece(const Piece piece) {
PieceColors piece_getColor(const Pieces piece) { return Black & piece; }
Piece piece_getColorlessPiece(const Pieces piece) {
return piece_getColor(piece) ^ piece;
}
uint board_getIndex(const Coordinates coords) {
int board_getIndex(const Coordinates coords) {
if (coords.row < 1 || coords.row > 8) {
printf("[ERROR]: Row out of bounds.\n");
return -1;
}
if (coords.column < 1 || coords.column > 8) {
printf("[ERROR]: Column out of bounds.\n");
return -1;
}
return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8);
}
Coordinates *piece_getMoves(const Board board, const Pieces piece,
const Coordinates piece_coords) {
Piece colorless_piece = piece_getColorlessPiece(piece);
PieceColors piece_color = piece_getColor(piece);
int board_index = board_getIndex(piece_coords);
if (board_index == -1)
return NULL;
switch (colorless_piece) {
case Pawn: {
Coordinates *moves = malloc(3 * sizeof(Coordinates));
uint moves_i;
Coordinates new_coordinates = {.column = piece_coords.column,
.row = piece_color == Black
? piece_coords.row - 1
: piece_coords.row + 1};
moves[moves_i++] = new_coordinates;
bool is_starting_pos =
piece_color == Black ? piece_coords.row == 7 : piece_coords.row == 2;
if (is_starting_pos) {
Coordinates leap_coords = {.column = piece_coords.column,
.row = piece_color == Black
? piece_coords.row - 2
: piece_coords.row + 2};
moves[moves_i++] = leap_coords;
}
return moves;
}
case Knight: {
Coordinates *moves = malloc(8 * sizeof(Coordinates));
uint moves_i;
struct temp {
int x;
int y;
};
struct temp temps[8] = {{-1, 2}, {1, 2}, {2, 1}, {2, -1},
{1, -2}, {-1, -2}, {-2, -1}, {-2, 1}};
for (uint i = 0; i < 8; i++) {
Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y,
.column =
piece_coords.column + temps[i].x};
int index = board_getIndex(new_coordinates);
if (index != -1)
moves[moves_i++] = new_coordinates;
}
return moves;
};
case Bishop: {
Coordinates *moves = malloc(13 * sizeof(Coordinates));
uint moves_i = 0;
struct temp {
int x;
int y;
};
struct temp temps[4] = {{-1, 1}, {1, 1}, {1, -1}, {-1, -1}};
for (uint i = 0; i < 4; i++) {
Coordinates new_coordinates;
int index = 0;
uint mul = 1;
while (index != -1) {
new_coordinates.row = piece_coords.row + (temps[i].y * mul);
new_coordinates.column = piece_coords.column + (temps[i].x * mul);
mul++;
index = board_getIndex(new_coordinates);
if (index != -1)
moves[moves_i++] = new_coordinates;
}
}
return moves;
};
case Rook: {
uint max = 14;
Coordinates *moves = malloc(max * sizeof(Coordinates));
uint moves_i;
for (uint i = 0; i < max; i++) {
if (i + 1 != piece_coords.column) {
Coordinates horizontal_coordinates = {piece_coords.row, i + 1};
if (board_getIndex(horizontal_coordinates) != -1)
moves[moves_i++] = horizontal_coordinates;
}
if (i + 1 != piece_coords.row) {
Coordinates vertical_coordinates = {.row = i + 1,
.column = piece_coords.column};
if (board_getIndex(vertical_coordinates) != -1)
moves[moves_i++] = vertical_coordinates;
}
}
return moves;
};
case King: {
Coordinates *moves = malloc(8 * sizeof(Coordinates));
uint moves_i;
struct temp {
int x;
int y;
};
struct temp temps[8] = {{-1, 1}, {0, 1}, {1, 1}, {1, 0},
{1, -1}, {0, -1}, {-1, -1}, {-1, 0}};
for (uint i = 0; i < 8; i++) {
Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y,
.column =
piece_coords.column + temps[i].x};
if (board_getIndex(new_coordinates) != -1)
moves[moves_i++] = new_coordinates;
}
return moves;
} break;
case Queen: {
} break;
}
return NULL;
}
bool board_movePiece(Board board, const Coordinates from,
const Coordinates to) {
uint from_index = board_getIndex(from);
uint to_index = board_getIndex(to);
int from_index = board_getIndex(from);
int to_index = board_getIndex(to);
if (from_index == -1 || to_index == -1)
return false;
board[to_index].piece = board[from_index].piece;
board[from_index].piece = -1;
return true;

View File

@@ -1,7 +1,7 @@
#ifndef __BOARD_HEADER
#include "common.h"
typedef int Piece;
typedef uint Piece;
typedef enum { Pawn = 0, Knight, Bishop, Rook, Queen, King } Pieces;
typedef enum { White = 0, Black = 8 } PieceColors;
@@ -17,9 +17,12 @@ typedef struct {
typedef Square Board[64];
PieceColors piece_getColor(const Piece piece);
Piece piece_getColorlessPiece(const Piece piece);
PieceColors piece_getColor(const Pieces piece);
Piece piece_getColorlessPiece(const Pieces piece);
bool board_movePiece(Board board, const Coordinates from, const Coordinates to);
int board_getIndex(const Coordinates coords);
Coordinates *piece_getMoves(const Board board, const Pieces piece,
const Coordinates piece_coords);
#endif // !__BOARD_HEADER

View File

@@ -1,6 +1,7 @@
#ifndef __COMMON_HEADER
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
typedef unsigned int uint;

View File

@@ -4,10 +4,7 @@ int main() {
Board board;
FEN fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR";
fen_toBoard(fen, board);
Coordinates from = {.row = 8, .column = 3};
Coordinates to = {.row = 6, .column = 3};
board_movePiece(board, from, to);
fen_fromBoard(board, fen);
printf("%s", fen);
Coordinates *coords = piece_getMoves(board, board[2].piece, board[2].coords);
printf("%d,%d", coords->row, coords->column);
return 0;
}