Bishop moves

This commit is contained in:
cdricms
2023-12-21 23:56:24 +01:00
parent b05fb5c303
commit 62a03b01c0
4 changed files with 84 additions and 19 deletions

View File

@@ -1,7 +1,7 @@
#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;
}
@@ -17,17 +17,17 @@ int board_getIndex(const Coordinates coords) {
return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8);
}
void piece_getMoves(const Board board, const Piece piece,
const Coordinates piece_coords) {
Pieces colorless_piece = piece_getColorlessPiece(piece);
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;
Coordinates moves[27];
uint moves_i = 0;
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
@@ -43,15 +43,18 @@ void piece_getMoves(const Board board, const Piece piece,
: piece_coords.row + 2};
moves[moves_i++] = leap_coords;
}
} break;
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 (int i = 0; i < 8; i++) {
for (uint i = 0; i < 8; i++) {
Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y,
.column =
piece_coords.column + temps[i].x};
@@ -59,12 +62,74 @@ void piece_getMoves(const Board board, const Piece piece,
if (index != -1)
moves[moves_i++] = new_coordinates;
}
} break;
return moves;
};
case Bishop: {
uint max = 13;
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,

View File

@@ -17,10 +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;
}