Queen moves

This commit is contained in:
cdricms
2023-12-26 23:03:50 +01:00
parent d204340d29
commit 5d830528cd
2 changed files with 69 additions and 47 deletions

View File

@@ -1,4 +1,12 @@
#include "board.h" #include "board.h"
#include <stdlib.h>
#define PAWN_MAXMOVES 4
#define KNIGHT_MAXMOVES 8
#define BISHOP_MAXMOVES 13
#define ROOK_MAXMOVES 14
#define QUEEN_MAXMOVES 27
#define KING_MAXMOVES 8
PieceColors piece_getColor(const Pieces piece) { return Black & piece; } PieceColors piece_getColor(const Pieces piece) { return Black & piece; }
Piece piece_getColorlessPiece(const Pieces piece) { Piece piece_getColorlessPiece(const Pieces piece) {
@@ -17,6 +25,45 @@ int board_getIndex(const Coordinates coords) {
return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8); return (8 - (coords.row + 7) % 8) * 8 - (8 - (coords.column + 7) % 8);
} }
void piece_getDiagonalMoves(const Coordinates relative_coords,
Coordinates *moves, uint *moves_i) {
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 = relative_coords.row + (temps[i].y * mul);
new_coordinates.column = relative_coords.column + (temps[i].x * mul);
mul++;
index = board_getIndex(new_coordinates);
if (index != -1)
moves[(*moves_i)++] = new_coordinates;
}
}
}
void piece_getLinearMoves(const Coordinates relative_coords, Coordinates *moves,
uint *moves_i) {
for (uint i = 0; i < ROOK_MAXMOVES; i++) {
if (i + 1 != relative_coords.column) {
Coordinates horizontal_coordinates = {relative_coords.row, i + 1};
if (board_getIndex(horizontal_coordinates) != -1)
moves[(*moves_i)++] = horizontal_coordinates;
}
if (i + 1 != relative_coords.row) {
Coordinates vertical_coordinates = {.row = i + 1,
.column = relative_coords.column};
if (board_getIndex(vertical_coordinates) != -1)
moves[(*moves_i)++] = vertical_coordinates;
}
}
}
Coordinates *piece_getMoves(const Board board, const Pieces piece, Coordinates *piece_getMoves(const Board board, const Pieces piece,
const Coordinates piece_coords) { const Coordinates piece_coords) {
Piece colorless_piece = piece_getColorlessPiece(piece); Piece colorless_piece = piece_getColorlessPiece(piece);
@@ -24,10 +71,11 @@ Coordinates *piece_getMoves(const Board board, const Pieces piece,
int board_index = board_getIndex(piece_coords); int board_index = board_getIndex(piece_coords);
if (board_index == -1) if (board_index == -1)
return NULL; return NULL;
switch (colorless_piece) { switch (colorless_piece) {
case Pawn: { case Pawn: {
Coordinates *moves = malloc(3 * sizeof(Coordinates)); Coordinates *moves = malloc(PAWN_MAXMOVES * sizeof(Coordinates));
uint moves_i; 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 == Black
? piece_coords.row - 1 ? piece_coords.row - 1
@@ -46,15 +94,15 @@ Coordinates *piece_getMoves(const Board board, const Pieces piece,
return moves; return moves;
} }
case Knight: { case Knight: {
Coordinates *moves = malloc(8 * sizeof(Coordinates)); Coordinates *moves = malloc(KNIGHT_MAXMOVES * sizeof(Coordinates));
uint moves_i; uint moves_i = 0;
struct temp { struct temp {
int x; int x;
int y; int y;
}; };
struct temp temps[8] = {{-1, 2}, {1, 2}, {2, 1}, {2, -1}, struct temp temps[8] = {{-1, 2}, {1, 2}, {2, 1}, {2, -1},
{1, -2}, {-1, -2}, {-2, -1}, {-2, 1}}; {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}};
for (uint i = 0; i < 8; i++) { for (uint i = 0; i < KNIGHT_MAXMOVES; i++) {
Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y, Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y,
.column = .column =
piece_coords.column + temps[i].x}; piece_coords.column + temps[i].x};
@@ -65,57 +113,27 @@ Coordinates *piece_getMoves(const Board board, const Pieces piece,
return moves; return moves;
}; };
case Bishop: { case Bishop: {
Coordinates *moves = malloc(13 * sizeof(Coordinates)); Coordinates *moves = malloc(BISHOP_MAXMOVES * sizeof(Coordinates));
uint moves_i = 0; uint moves_i = 0;
struct temp { piece_getDiagonalMoves(piece_coords, moves, &moves_i);
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; return moves;
}; };
case Rook: { case Rook: {
uint max = 14; Coordinates *moves = malloc(ROOK_MAXMOVES * sizeof(Coordinates));
Coordinates *moves = malloc(max * sizeof(Coordinates)); uint moves_i = 0;
uint moves_i; piece_getLinearMoves(piece_coords, moves, &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; return moves;
}; };
case King: { case King: {
Coordinates *moves = malloc(8 * sizeof(Coordinates)); Coordinates *moves = malloc(KING_MAXMOVES * sizeof(Coordinates));
uint moves_i; uint moves_i = 0;
struct temp { struct temp {
int x; int x;
int y; int y;
}; };
struct temp temps[8] = {{-1, 1}, {0, 1}, {1, 1}, {1, 0}, struct temp temps[8] = {{-1, 1}, {0, 1}, {1, 1}, {1, 0},
{1, -1}, {0, -1}, {-1, -1}, {-1, 0}}; {1, -1}, {0, -1}, {-1, -1}, {-1, 0}};
for (uint i = 0; i < 8; i++) { for (uint i = 0; i < KING_MAXMOVES; i++) {
Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y, Coordinates new_coordinates = {.row = piece_coords.row + temps[i].y,
.column = .column =
piece_coords.column + temps[i].x}; piece_coords.column + temps[i].x};
@@ -123,10 +141,14 @@ Coordinates *piece_getMoves(const Board board, const Pieces piece,
moves[moves_i++] = new_coordinates; moves[moves_i++] = new_coordinates;
} }
return moves; return moves;
} break; }
case Queen: { case Queen: {
Coordinates *moves = malloc(QUEEN_MAXMOVES * sizeof(Coordinates));
} break; uint moves_i = 0;
piece_getDiagonalMoves(piece_coords, moves, &moves_i);
piece_getLinearMoves(piece_coords, moves, &moves_i);
return moves;
}
} }
return NULL; return NULL;

View File

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