From a83f1cb27331523b3da97ebff81c7f172ab22c95 Mon Sep 17 00:00:00 2001 From: Pavel 'LEdoian' Turinsky Date: Sun, 7 May 2023 02:51:46 +0200 Subject: [PATCH] A simple eager/greedy strategy bot Also kinda serves as a benchmark: ledoian@Promethium /dev/pts/8 560 /home/ledoian/Dev/Carpet (master) $ time python3.10 eager.py Out of 10000 games, 7158 won, 2842 lost. real 0m43.314s user 0m43.255s sys 0m0.049s ledoian@Promethium /dev/pts/8 561 /home/ledoian/Dev/Carpet (master) $ time python3.11 eager.py Out of 10000 games, 7198 won, 2802 lost. real 0m46.816s user 0m46.434s sys 0m0.067s ledoian@Promethium /dev/pts/8 562 /home/ledoian/Dev/Carpet (master) $ time python3.12 eager.py Out of 10000 games, 7226 won, 2774 lost. real 0m42.048s user 0m41.653s sys 0m0.075s ledoian@Promethium /dev/pts/8 563 /home/ledoian/Dev/Carpet (master) $ time pypy3 eager.py Out of 10000 games, 7050 won, 2950 lost. real 0m11.247s user 0m11.085s sys 0m0.158s --- eager.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 eager.py diff --git a/eager.py b/eager.py new file mode 100644 index 0000000..045ef97 --- /dev/null +++ b/eager.py @@ -0,0 +1,40 @@ +import carpet + +ITERATIONS = 10000 + +def try_drop(game): + success = True + while success: + success = False + for suit in carpet.Suit: + value = game.foundations[suit] + if (suit, value + 1) in game.carpet: + success = True + game.autodrop() + if len(game.offers) > 0 and (suit, value + 1) == game.offers[-1]: + success = True + game.drop_card_from_offer() + +stats = {'wins': 0, 'losses': 0} + +for i in range(ITERATIONS): + game = carpet.Game() + while game.game_result() is None: + # Alg: if we can drop, we drop. If not and we can get card, we get. Else we flip. + try_drop(game) + if game.free_cells > 0 and len(game.offers) > 0: + game.get_card_from_deck() + elif len(game.deck) > 0: + game.flip_deck() + elif game.game_result is None: # otherwise this is last round + print('Did we lose?') + game.print() + if game.game_result(): + #print(f'Game {i} won in {game.moves} moves.') + stats['wins'] += 1 + else: + #print(f'Game {i} lost in {game.moves} moves.') + stats['losses'] += 1 + +print(f'Out of {ITERATIONS} games, {stats["wins"]} won, {stats["losses"]} lost.') +