generate Direction Consts
This commit is contained in:
@ -1,15 +1,25 @@
|
||||
cmake_minimum_required(VERSION 3.18)
|
||||
project(chess C)
|
||||
file(GLOB SOURCES "src/*.c")
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
include_directories(include)
|
||||
|
||||
add_executable(genDirectionConsts src/generateCode/directionConsts.c)
|
||||
add_custom_command(
|
||||
OUTPUT directionConsts.h # Output file from code generation
|
||||
COMMAND genDirectionConsts > src/chess/generated/directionConsts.h
|
||||
DEPENDS genDirectionConsts # Depends on the code generation executable
|
||||
)
|
||||
add_custom_target(generateCode DEPENDS directionConsts.h)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
pkg_check_modules(GTK4 REQUIRED IMPORTED_TARGET gtk4)
|
||||
pkg_check_modules(LIBRSVG REQUIRED IMPORTED_TARGET librsvg-2.0)
|
||||
|
||||
file(GLOB SOURCES "src/chess/*.c")
|
||||
add_executable(${PROJECT_NAME} ${SOURCES})
|
||||
set_property(TARGET ${PROJECT_NAME} PROPERTY C_STANDARD 99)
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE PkgConfig::GTK4 PkgConfig::LIBRSVG)
|
||||
add_dependencies(${PROJECT_NAME} generateCode)
|
||||
|
||||
# Enable LTO for release build type
|
||||
if(CMAKE_BUILD_TYPE MATCHES "Release")
|
||||
@ -25,4 +35,5 @@ endif()
|
||||
add_executable(chessNoComputer ${SOURCES})
|
||||
set_property(TARGET chessNoComputer PROPERTY C_STANDARD 99)
|
||||
target_link_libraries(chessNoComputer PRIVATE PkgConfig::GTK4 PkgConfig::LIBRSVG)
|
||||
add_dependencies(chessNoComputer generateCode)
|
||||
target_compile_definitions(chessNoComputer PUBLIC NO_COMPUTER)
|
||||
|
||||
@ -30,4 +30,8 @@ struct piece_t {
|
||||
bool color;
|
||||
};
|
||||
|
||||
enum directions {
|
||||
NORTH, NORTHWEST, WEST, SOUTHWEST, SOUTH, SOUTHEAST, EAST, NORTHEAST, DIRECTION_LENGTH
|
||||
};
|
||||
|
||||
#endif
|
||||
@ -1,7 +1,7 @@
|
||||
#include <bits/stdint-least.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "types.h"
|
||||
#include <chess/types.h>
|
||||
|
||||
bool bitsetGet(uint_least64_t bitset, uint_least8_t i) {
|
||||
return (bitset >> i) & 1u;
|
||||
@ -1,6 +1,6 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "types.h"
|
||||
#include <chess/types.h>
|
||||
|
||||
bool bitsetGet(uint_least64_t bitset, uint_least8_t i);
|
||||
uint_least64_t bitsetClear(uint_least64_t bitset, uint_least8_t i);
|
||||
0
src/chess/generated/.gitkeep
Normal file
0
src/chess/generated/.gitkeep
Normal file
@ -8,7 +8,7 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "types.h"
|
||||
#include <chess/types.h>
|
||||
#include "move.h"
|
||||
#include "bitboard.h"
|
||||
|
||||
@ -263,8 +263,6 @@ int main(int argc, char **argv) {
|
||||
GtkApplication *app;
|
||||
int stat;
|
||||
|
||||
genDirectionConsts();
|
||||
|
||||
app = gtk_application_new("org.zinkel.chess", G_APPLICATION_DEFAULT_FLAGS);
|
||||
g_signal_connect(app, "activate", G_CALLBACK (app_activate), NULL);
|
||||
stat = g_application_run(G_APPLICATION(app), argc, argv);
|
||||
@ -1,9 +1,10 @@
|
||||
#include <bits/stdint-least.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "types.h"
|
||||
#include <chess/types.h>
|
||||
#include "move.h"
|
||||
#include "bitboard.h"
|
||||
#include "generated/directionConsts.h"
|
||||
|
||||
#define trailingBits(num) __builtin_ctzll(num)
|
||||
|
||||
@ -13,47 +14,12 @@ struct addMoveCtx_t {
|
||||
uint_least8_t *movesLength;
|
||||
};
|
||||
|
||||
enum directions {
|
||||
NORTH, NORTHWEST, WEST, SOUTHWEST, SOUTH, SOUTHEAST, EAST, NORTHEAST, DIRECTION_LENGTH
|
||||
};
|
||||
|
||||
const static uint_least8_t MAIN_DIRECTION[] = {NORTH, WEST, EAST, SOUTH};
|
||||
|
||||
static uint_least8_t directionOffset[TOTAL_BOARD_SIZE * DIRECTION_LENGTH];
|
||||
static int_least8_t directionModifier[DIRECTION_LENGTH];
|
||||
|
||||
static uint_least8_t getDirectionOffset(uint_least8_t field, uint_least8_t direction) {
|
||||
return directionOffset[field * DIRECTION_LENGTH + direction];
|
||||
}
|
||||
|
||||
static uint_least8_t min(uint_least8_t a, uint_least8_t b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
void genDirectionConsts() {
|
||||
for(uint_least16_t field = 0; field < TOTAL_BOARD_SIZE; ++field) {
|
||||
uint_least8_t file = field % BOARD_SIZE;
|
||||
uint_least8_t rank = field / BOARD_SIZE;
|
||||
uint_least8_t *offsetField = directionOffset + field * DIRECTION_LENGTH;
|
||||
offsetField[NORTH] = rank;
|
||||
offsetField[WEST] = file;
|
||||
offsetField[NORTHWEST] = min(file, rank);
|
||||
offsetField[EAST] = BOARD_SIZE - file - 1;
|
||||
offsetField[SOUTH] = BOARD_SIZE - rank - 1;
|
||||
offsetField[SOUTHEAST] = min(offsetField[SOUTH], offsetField[EAST]);
|
||||
offsetField[SOUTHWEST] = min(offsetField[SOUTH], file);
|
||||
offsetField[NORTHEAST] = min(rank, offsetField[EAST]);
|
||||
}
|
||||
directionModifier[NORTH] = -BOARD_SIZE;
|
||||
directionModifier[SOUTH] = BOARD_SIZE;
|
||||
directionModifier[WEST] = -1;
|
||||
directionModifier[EAST] = 1;
|
||||
directionModifier[SOUTHEAST] = directionModifier[SOUTH] + directionModifier[EAST];
|
||||
directionModifier[SOUTHWEST] = directionModifier[SOUTH] + directionModifier[WEST];
|
||||
directionModifier[NORTHEAST] = directionModifier[NORTH] + directionModifier[EAST];
|
||||
directionModifier[NORTHWEST] = directionModifier[NORTH] + directionModifier[WEST];
|
||||
}
|
||||
|
||||
static uint_least8_t getCapturePos(uint_least8_t src, uint_least8_t dst, uint_least8_t spezialMove) {
|
||||
if(spezialMove != EN_PASSANT) return dst;
|
||||
const uint_least8_t file = dst % 8;
|
||||
@ -61,6 +27,9 @@ static uint_least8_t getCapturePos(uint_least8_t src, uint_least8_t dst, uint_le
|
||||
return rank * BOARD_SIZE + file;
|
||||
}
|
||||
|
||||
static bool inCheck(uint_least64_t *board, uint_least8_t field) {
|
||||
}
|
||||
|
||||
static void addMove(struct addMoveCtx_t ctx, struct piece_t movedPiece, uint_least8_t src,
|
||||
uint_least8_t dst, uint_least8_t spezialMove) {
|
||||
uint_least64_t *board = ctx.board;
|
||||
@ -96,7 +65,8 @@ static void moveSliding(const uint_least8_t *direction, uint_least8_t directionL
|
||||
for(uint_least8_t currentField = field + modifier, i = 0;
|
||||
i < directionOffset[field * DIRECTION_LENGTH + direction[j]]; ++i, currentField += modifier) {
|
||||
addMove(ctx, movedPiece, field, currentField, 0);
|
||||
if(bitboardGetAllPieces(board, currentField)) {
|
||||
if(bitboardGetAllPieces(board, currentField)) { // ziehe die info wo das nächste piece steht
|
||||
// über bit ops?
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -2,7 +2,7 @@
|
||||
#define CHESS_MOVE_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "types.h"
|
||||
#include <chess/types.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#define MAX_VALID_MOVES 218
|
||||
54
src/generateCode/directionConsts.c
Normal file
54
src/generateCode/directionConsts.c
Normal file
@ -0,0 +1,54 @@
|
||||
#include <stdio.h>
|
||||
#include <chess/types.h>
|
||||
#include <stddef.h>
|
||||
|
||||
static void defineArray(const char *declaration, long long arr[], size_t size) {
|
||||
printf("%s[%zu] = { ", declaration, size);
|
||||
for (int i = 0; i < size; ++i) {
|
||||
printf("%lld", arr[i]);
|
||||
if (i < size - 1) {
|
||||
printf(", ");
|
||||
}
|
||||
}
|
||||
printf(" };\n");
|
||||
}
|
||||
|
||||
static long long min(long long a, long long b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
printf("#ifndef CHESS_GENERATED_DIRECTION_CONSTS_H\n"
|
||||
"#define CHESS_GENERATED_DIRECTION_CONSTS_H\n"
|
||||
"#include <stdint.h>\n");
|
||||
{
|
||||
long long directionOffset[TOTAL_BOARD_SIZE * DIRECTION_LENGTH];
|
||||
for(uint_least16_t field = 0; field < TOTAL_BOARD_SIZE; ++field) {
|
||||
uint_least8_t file = field % BOARD_SIZE;
|
||||
uint_least8_t rank = field / BOARD_SIZE;
|
||||
long long *offsetField = directionOffset + field * DIRECTION_LENGTH;
|
||||
offsetField[NORTH] = rank;
|
||||
offsetField[WEST] = file;
|
||||
offsetField[NORTHWEST] = min(file, rank);
|
||||
offsetField[EAST] = BOARD_SIZE - file - 1;
|
||||
offsetField[SOUTH] = BOARD_SIZE - rank - 1;
|
||||
offsetField[SOUTHEAST] = min(offsetField[SOUTH], offsetField[EAST]);
|
||||
offsetField[SOUTHWEST] = min(offsetField[SOUTH], file);
|
||||
offsetField[NORTHEAST] = min(rank, offsetField[EAST]);
|
||||
}
|
||||
defineArray("const static uint_least8_t directionOffset", directionOffset, LENGTH(directionOffset));
|
||||
}
|
||||
{
|
||||
long long directionModifier[DIRECTION_LENGTH];
|
||||
directionModifier[NORTH] = -BOARD_SIZE;
|
||||
directionModifier[SOUTH] = BOARD_SIZE;
|
||||
directionModifier[WEST] = -1;
|
||||
directionModifier[EAST] = 1;
|
||||
directionModifier[SOUTHEAST] = directionModifier[SOUTH] + directionModifier[EAST];
|
||||
directionModifier[SOUTHWEST] = directionModifier[SOUTH] + directionModifier[WEST];
|
||||
directionModifier[NORTHEAST] = directionModifier[NORTH] + directionModifier[EAST];
|
||||
directionModifier[NORTHWEST] = directionModifier[NORTH] + directionModifier[WEST];
|
||||
defineArray("const static uint_least8_t directionModifier", directionModifier, LENGTH(directionModifier));
|
||||
}
|
||||
printf("#endif\n");
|
||||
}
|
||||
Reference in New Issue
Block a user