A chess library for Gleam
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Document generator script

+10 -2
+10 -2
dev/starfish_dev.gleam
··· 1 + //// Code generation for various parts of the engine 2 + 1 3 import gleam/int 2 4 import simplifile as file 3 5 import starfish/internal/board 4 6 5 7 const generated_file_path = "src/starfish/internal/generated.gleam" 6 - 7 - const piece_count = 12 8 8 9 9 pub fn main() { 10 10 let imports = ··· 15 15 file.write(to: generated_file_path, contents: generated_code) 16 16 } 17 17 18 + /// 60 bits is the highest we can go on Erlang where bitwise operations are fast, 19 + /// so we limit hashes to 60 bits to ensure computing hashes is as fast as possible. 18 20 const max_60_bit_int = 576_460_752_303_423_500 19 21 22 + const piece_count = 12 23 + 20 24 fn generate_hash_data() -> String { 21 25 let function_head = 22 26 "pub fn hash_for_piece(piece: board.Piece, colour: board.Colour, position: Int) -> Int { 23 27 case piece, colour, position {" 24 28 let length = piece_count * board.size 25 29 let hash_for_piece = generate_hash_data_loop(function_head, 0, length) 30 + // Generate a random hash to indicate that it's black's turn to move 26 31 let black_to_move = 27 32 "pub const black_to_move_hash = 0x" 28 33 <> int.to_base16(int.random(max_60_bit_int)) ··· 36 41 False -> { 37 42 let position = generated / piece_count 38 43 let position = case position == board.size - 1 { 44 + // The last square needs to be a catch-all to ensure the `case` expression 45 + // compiles. 39 46 True -> "_" 40 47 False -> int.to_string(position) 41 48 } ··· 60 67 <> ", " 61 68 <> position 62 69 <> " -> 0x" 70 + // Generate a random 60-bit hash for this combination of piece and position 63 71 <> int.to_base16(int.random(max_60_bit_int)) 64 72 generate_hash_data_loop(acc, generated + 1, length) 65 73 }