Hi,
In my program, the size of the board can change at runtime, but I know the bounds are between 5 and 20 (inclusive). I have the following function:
pub fn findBestMove(size: comptime_int) Threat
I want to create an array of "function calls" that allows me to invoke the appropriate function based on the size determined at runtime. The compiler should generate the function 15 times, each time with a different size.
Here’s my attempt to achieve that, but it doesn’t work:
const AIMapping = *const fn (comptime_int) ai.Threat;
fn generateFunction(comptime N: usize) AIMapping {
return fn () ai.Threat {
ai.findBestMove(N)
};
}
fn generateAIMap() [15]AIMapping {
return comptime {
var arr: [15]AIMapping = undefined;
for (0..15) |i| {
// Generate and assign the specialized function for index i
arr[i] = generateFunction(i);
}
arr;
};
}
const AIMap = generateAIMap();
pub fn AIPlay() [2]u16 {
const empty_cell = AIMap[board.game_board.width];
board.game_board.setCellByCoordinates(empty_cell.col, empty_cell.row, board.Cell.own);
return .{empty_cell.col, empty_cell.row};
}```
How can I achieve what I want ?