diff --git a/src/crooked_rook.gleam b/src/crooked_rook.gleam index 961268b..e2fd7bf 100644 --- a/src/crooked_rook.gleam +++ b/src/crooked_rook.gleam @@ -27,44 +27,54 @@ fn ask_move() -> String { } } -pub fn main() { - "\u{2656} Hello from Crooked Rook!" - |> ansi.bg_white - |> ansi.black - |> io.println - - io.println("Opponent is playing white") - io.println("You are playing black") - let game = new_game() - - let position = "e2e4" - io.println("First move by the opponent: " <> position) - first_move(game, position) +fn print_morse(move: String) -> Nil { + case morsey.encode(move) { + Ok(symbols) -> io.println("Morse code: " <> morsey.to_string(symbols)) + Error(morsey.InvalidCharacter(_)) -> Nil + } +} +fn with_spinner(function, text) { let spinner = - spinner.new("Calculating best move") + spinner.new(text) |> spinner.with_colour(ansi.magenta) |> spinner.start - let best = best_move(game) + let result = function() spinner.stop(spinner) + result +} - io.println("You should play: " <> best) - move(game, best) - case morsey.encode(best) { - Ok(symbols) -> io.println("Morse code: " <> morsey.to_string(symbols)) - Error(morsey.InvalidCharacter(char)) -> - io.println_error("Invalid character: " <> char) - } +fn best_move_with_spinner(game) { + fn() { best_move(game) } + |> with_spinner("Calculating best move") +} +fn repl(game, first_round) { let position = ask_move() - move(game, position) + case first_round { + True -> first_move(game, position) + False -> move(game, position) + } - let best = best_move(game) + let best = best_move_with_spinner(game) io.println("You should play: " <> best) - move(game, best) - case morsey.encode(best) { - Ok(symbols) -> io.println("Morse code: " <> morsey.to_string(symbols)) - Error(morsey.InvalidCharacter(char)) -> - io.println_error("Invalid character: " <> char) + print_morse(best) + case first_round { + True -> first_move(game, position) + False -> move(game, position) } + + repl(game, False) +} + +pub fn main() { + "\u{2656} Hello from Crooked Rook!" + |> ansi.bg_white + |> ansi.black + |> io.println + + io.println("Opponent is playing white") + io.println("You are playing black") + let game = new_game() + repl(game, True) }