Skip to content

Commit

Permalink
Refactor the main function
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostyX committed Dec 14, 2024
1 parent f20a08c commit ad373f5
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions src/crooked_rook.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit ad373f5

Please sign in to comment.