Current section

Files

Jump to
gchess src gchess.erl
Raw

src/gchess.erl

-module(gchess).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([perft/2, main/0]).
-spec perft(gleam@erlang@process:subject(game_server:message()), integer()) -> integer().
perft(Game_server_subject, Depth) ->
case Depth of
0 ->
1;
_ ->
Moves = game_server:all_legal_moves(Game_server_subject),
Nodes@2 = gleam@list:fold(
Moves,
0,
fun(Nodes, Move) ->
game_server:apply_move_raw(Game_server_subject, Move),
Nodes@1 = Nodes + perft(Game_server_subject, Depth - 1),
game_server:undo_move(Game_server_subject),
Nodes@1
end
),
Nodes@2
end.
-spec main() -> nil.
main() ->
Server = game_server:new_server(),
game_server:new_game_from_fen(
Server,
<<"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1"/utf8>>
),
game_server:disable_status(Server),
Start = os:system_time(second),
Perft_result = perft(Server, 3),
End = os:system_time(second),
gleam@io:print(<<"Perft result: "/utf8>>),
gleam@io:print(gleam@int:to_string(Perft_result)),
gleam@io:print(<<"\n"/utf8>>),
gleam@io:print(<<"Time: "/utf8>>),
gleam@io:print(gleam@int:to_string(End - Start)),
gleam@io:print(<<" seconds"/utf8>>),
gleam@io:print(<<"\n"/utf8>>).