From 23468ffe0597388a1fcc2ba5731f00220f00906a Mon Sep 17 00:00:00 2001 From: Yorick Barbanneau Date: Fri, 22 Dec 2023 01:16:55 +0100 Subject: [PATCH] Add recurse game mode And display statistics after --- src/game.py | 86 +++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 12 deletions(-) diff --git a/src/game.py b/src/game.py index 4285146..58ecafd 100755 --- a/src/game.py +++ b/src/game.py @@ -83,6 +83,12 @@ def parse_aguments(): default=[-5, 2, 10,25] ) + parser.add_argument('-r', '--recursions', + help='Number parties to play', + type=int, + default=1 + ) + parser.add_argument('--show-weights-table', help='Display weight table used in \'weight\' and \'full\' heuristic calculation and exit', action='store_true', @@ -177,16 +183,72 @@ if __name__ == '__main__': 'randomize_moves': args.black_randomize_moves } ) - while ( not game.is_game_over()): - if game._nextPlayer == 1: - move = bplayer.get_move(game) + recursions = args.recursions + parties = [] + while recursions > 0: + while ( not game.is_game_over()): + if game._nextPlayer == 1: + move = bplayer.get_move(game) + else: + move = wplayer.get_move(game) + # Display informations only id we are not in recurse mode + if args.recursions == 1: + print("Player {} move: {},{}".format( + "Black (X)" if move[0] == 2 else "White (O)", + move[1], + move[2] + )) + game.push(move) + + parties.append([recursions, game._nbBLACK, game._nbWHITE]) + score = game._nbBLACK - game._nbWHITE + if score == 0: + winner = "No winner" + elif score > 0: + winner = "Black" else: - move = wplayer.get_move(game) - print("Player {} move: {},{}".format( - "Black (X)" if move[0] == 2 else "White (O)", - move[1], - move[2] - )) - game.push(move) - print("Game end - score black:{}, white:{}\n".format(game._nbBLACK, game._nbWHITE)) - print(game.show_board()) + winner = "White" + + print("\nGAME OVER\n---\nWINNER: {} | black:{} | white:{}".format( + winner, + game._nbBLACK, + game._nbWHITE + )) + print("\n{}".format(game.show_board())) + game.reset() + recursions -= 1 + + # Make somes statistics + if args.recursions > 1: + numbers = len(parties) + black = 0 + white = 0 + null = 0 + for p in parties: + black += 1 if p[1] > p[2] else 0 + white += 1 if p[1] < p[2] else 0 + null += 1 if p[1] == p[2] else 0 + print("Stats\n---") + print("Parties: {}".format(numbers)) + print("Black: {:>2} | ratio: {:>6} | engine: {}".format( + black, + black * 100 / numbers, + bplayer._get_class_name(), + + )) + + print("White: {:>2} | ratio: {:>6} | engine: {}".format( + white, + white * 100 / numbers, + wplayer._get_class_name(), + )) + + print("Null: {:>2} | ratio: {:>6}".format( + null, + bplayer._get_class_name(), + null * 100 / numbers + )) + print("---\nBlack player options: {}\nWhite player options: {}".format( + bplayer.options, wplayer.options + )) +