A tool for testing starfish

Run games with reversed colours also

Changed files
+58 -35
src
+58 -35
src/starfish_testing.gleam
··· 22 22 |> result.map(string.split(_, "\n")) 23 23 as "Failed to read game file" 24 24 25 - io.println("Loaded games. Running game 0...") 25 + io.println("Loaded games.") 26 26 27 - let outcomes = 28 - list.index_map(games, fn(game, i) { 29 - let outcome = run_game(game) 30 - io.println( 31 - "Finished game " 32 - <> int.to_string(i) 33 - <> ", outcome " 34 - <> string.inspect(outcome), 35 - ) 36 - io.println("Running game " <> int.to_string(i + 1) <> "...") 37 - outcome 38 - }) 27 + let outcomes = list.index_map(games, run_game) |> list.flatten 39 28 40 29 let #(wins, draws, losses) = 41 30 list.fold(outcomes, #(0, 0, 0), fn(tuple, outcome) { 42 31 let #(wins, draws, losses) = tuple 43 32 case outcome { 44 - White -> #(wins + 1, draws, losses) 33 + Updated -> #(wins + 1, draws, losses) 45 34 Draw -> #(wins, draws + 1, losses) 46 - Black -> #(wins, draws, losses + 1) 35 + Original -> #(wins, draws, losses + 1) 47 36 } 48 37 }) 49 38 50 39 io.println( 51 - "Wins: " 40 + "Updated version won " 52 41 <> int.to_string(wins) 53 - <> "\nDraws: " 42 + <> " times.\nThe bots drew " 54 43 <> int.to_string(draws) 55 - <> "\nLosses: " 56 - <> int.to_string(losses), 44 + <> " times.\nOriginal version won " 45 + <> int.to_string(losses) 46 + <> " times.", 57 47 ) 58 48 59 49 Nil 60 50 } 61 51 62 52 type Outcome { 63 - White 64 - Black 53 + Updated 54 + Original 65 55 Draw 66 56 } 67 57 68 - fn run_game(fen: String) -> Outcome { 58 + fn run_game(fen: String, i: Int) -> List(Outcome) { 69 59 let assert Ok(game) = starfish.try_from_fen(fen) 70 60 as "Failed to parse fen string" 71 - run_game_loop(game) 61 + 62 + io.println("Running match " <> int.to_string(i) <> ", position " <> fen) 63 + 64 + let first_outcome = run_game_loop(game, UpdatedPlaysWhite) 65 + 66 + io.println("Finished first game of match, " <> print_outcome(first_outcome)) 67 + 68 + io.println("Running again with reversed colours...") 69 + 70 + let second_outcome = run_game_loop(game, UpdatedPlaysBlack) 71 + 72 + io.println("Finished second game of match, " <> print_outcome(second_outcome)) 73 + 74 + [first_outcome, second_outcome] 72 75 } 73 76 74 - fn run_game_loop(game: starfish.Game) -> Outcome { 75 - case starfish.state(game) { 76 - starfish.BlackWin -> Black 77 - starfish.Draw(_) -> Draw 78 - starfish.WhiteWin -> White 79 - starfish.Continue -> { 80 - let best_move = get_best_move(game) 77 + fn print_outcome(outcome: Outcome) -> String { 78 + case outcome { 79 + Draw -> "it was a draw." 80 + Original -> "original version won." 81 + Updated -> "updated version won." 82 + } 83 + } 84 + 85 + type Configuration { 86 + UpdatedPlaysWhite 87 + UpdatedPlaysBlack 88 + } 81 89 82 - run_game_loop(starfish.apply_move(game, best_move)) 90 + fn run_game_loop(game: starfish.Game, configuration: Configuration) -> Outcome { 91 + case starfish.state(game), configuration { 92 + starfish.Draw(_), _ -> Draw 93 + starfish.BlackWin, UpdatedPlaysBlack | starfish.WhiteWin, UpdatedPlaysWhite -> 94 + Updated 95 + starfish.BlackWin, UpdatedPlaysWhite | starfish.WhiteWin, UpdatedPlaysBlack -> 96 + Original 97 + starfish.Continue, _ -> { 98 + let best_move = get_best_move(game, configuration) 99 + 100 + run_game_loop(starfish.apply_move(game, best_move), configuration) 83 101 } 84 102 } 85 103 } 86 104 87 - fn get_best_move(game: starfish.Game) -> Move(Legal) { 88 - let url = case game.to_move { 89 - board.White -> "http://0.0.0.0:8000/move" 90 - board.Black -> "http://0.0.0.0:8001/move" 105 + fn get_best_move( 106 + game: starfish.Game, 107 + configuration: Configuration, 108 + ) -> Move(Legal) { 109 + let url = case game.to_move, configuration { 110 + board.White, UpdatedPlaysWhite | board.Black, UpdatedPlaysBlack -> 111 + "http://0.0.0.0:8000/move" 112 + board.Black, UpdatedPlaysWhite | board.White, UpdatedPlaysBlack -> 113 + "http://0.0.0.0:8001/move" 91 114 } 92 115 93 116 let assert Ok(request) = request.to(url)