def test_clue_and_agir(self): game = hanabi.Game(5) ai = new_ai.Recommander(game) carte = hanabi.deck.Card(hanabi.deck.Color.Red, 1) # des cartes pour tester carte2 = hanabi.deck.Card(hanabi.deck.Color.Red, 2) carte3 = hanabi.deck.Card(hanabi.deck.Color.Red, 3) carte4 = hanabi.deck.Card(hanabi.deck.Color.Red, 4) carte5 = hanabi.deck.Card(hanabi.deck.Color.Red, 5) cartes = [carte, carte2, carte3, carte4, carte5] for k in range(0, 5): for i in range(0, 4): game.hands[k].cards[i] = cartes[k] game.hands[4].cards[2] = carte2 game.hands[3].cards[3] = carte print(game.hands) print(game.deck) game.print_piles() ai.list_players = ai.list_players[1:] + [ai.list_players[0]] Valeur_joueurs = [0] * 4 Somme_valeur = 0 print(game.current_player_name) self.assertEqual( ai.clue(game, Somme_valeur, Valeur_joueurs, [card for card in ai.other_players_cards], 5, 4), 7) self.assertEqual( ai.agir(game, 0, 5, 4, Valeur_joueurs, Somme_valeur, [card for card in ai.other_players_cards]), 'c2B')
def test_give_a_hint_1(self): """ Test si joueur 1 2 et 3 doivent jouer la carte 1 et joueur 4 la carte 2, la recommandation doit etre 0+0+0+1=1 qui donne en format indice : 'c12' """ game = hanabi.Game(5) ai = hanabi.ai.RecommendationStrategy(game) c1 = hanabi.deck.Card(hanabi.deck.Color.Blue, 5) c2 = hanabi.deck.Card(hanabi.deck.Color.Red, 1) c3 = hanabi.deck.Card(hanabi.deck.Color.Green, 1) c4 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 1) c5 = hanabi.deck.Card(hanabi.deck.Color.Red, 5) c6 = hanabi.deck.Card(hanabi.deck.Color.Green, 5) c7 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 5) game.piles[hanabi.deck.Color.Blue] = 4 game.piles[hanabi.deck.Color.Red] = 4 game.piles[hanabi.deck.Color.Green] = 4 game.piles[hanabi.deck.Color.Yellow] = 4 ok1 = hanabi.deck.Deck([c1, c2, c3, c4]) hand1 = hanabi.deck.Hand(ok1, 4) ok2 = hanabi.deck.Deck([c5, c2, c3, c4]) hand2 = hanabi.deck.Hand(ok2, 4) ok3 = hanabi.deck.Deck([c6, c2, c3, c4]) hand3 = hanabi.deck.Hand(ok3, 4) ok4 = hanabi.deck.Deck([c2, c7, c3, c4]) hand4 = hanabi.deck.Hand(ok4, 4) hanabi.ai.RecommendationStrategy.other_hands = [ hand1, hand2, hand3, hand4 ] self.assertEqual(hanabi.ai.RecommendationStrategy.give_a_hint(ai), 'c12')
def start_game(server, chat_id, user_id): if chat_id not in server.games: server.bot.sendMessage(chat_id, "No game created for this chat") return if user_id != server.games[chat_id].admin: server.bot.sendMessage(chat_id, "You cannot start this game") player_to_user = server.games[chat_id].player_to_user if len(server.games[chat_id].player_to_user) < 2: server.bot.sendMessage(chat_id, "Too few players") return players = [] for name in player_to_user.keys(): players.append(name) server.games[chat_id].game = hanabi.Game(players) server.bot.sendMessage(chat_id, "Game started with players " + str(players)) # send a view to all the players chat_game = server.games[chat_id] send_game_views(server.bot, chat_game) server.bot.sendMessage(chat_id, "Game started!") return
def test_nb_players(self): for i in range(2, 6): game = hanabi.Game(i) game.quiet = True ai = hanabi.ai.Cheater(game) game.ai = ai game.run()
def test_deduce_my_moves_1(self): game = hanabi.Game(5) RS = hanabi.ai.RecommendationStrategy(game) c1 = hanabi.deck.Card(hanabi.deck.Color.Blue, 5) c2 = hanabi.deck.Card(hanabi.deck.Color.Red, 1) c3 = hanabi.deck.Card(hanabi.deck.Color.Green, 1) c4 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 1) c5 = hanabi.deck.Card(hanabi.deck.Color.Red, 5) c6 = hanabi.deck.Card(hanabi.deck.Color.Green, 5) c7 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 5) game.piles[hanabi.deck.Color.Blue] = 4 game.piles[hanabi.deck.Color.Red] = 4 game.piles[hanabi.deck.Color.Green] = 4 game.piles[hanabi.deck.Color.Yellow] = 4 ok1 = hanabi.deck.Deck([c1, c2, c3, c4]) hand1 = hanabi.deck.Hand(ok1, 4) ok2 = hanabi.deck.Deck([c5, c2, c3, c4]) hand2 = hanabi.deck.Hand(ok2, 4) ok3 = hanabi.deck.Deck([c6, c2, c3, c4]) hand3 = hanabi.deck.Hand(ok3, 4) ok4 = hanabi.deck.Deck([c2, c4, c3, c4]) hand4 = hanabi.deck.Hand(ok4, 4) hanabi.ai.RecommendationStrategy.other_hands = [ hand1, hand2, hand3, hand4 ] self.assertEqual( hanabi.ai.RecommendationStrategy.deduce_my_moves(RS, 'c12', 4), 'p2')
def n_tests(game, n): t1 = t.time() L = [] compteur = 0 compteur_2 = 0 for i in range(n): game.reset() game = hanabi.Game(5) game.quiet = True ai = new_ai.HatGuessIA(game) game.ai = ai game.run() L.append(game.score) if game.score >= 24: compteur += 1 if game.score >= 25: compteur_2 += 1 Moyenne = stat.mean(L) Variance = stat.variance(L) print(L, "\nMoyenne = ", Moyenne, " and Variance = ", Variance, " Nombre de partie à plus de 24 coups = ", compteur, " Nombre de partie à 25 coups = ", compteur_2) t2 = t.time() print("Temps de Calcul pour ", n, " parties = ", t2 - t1, " secondes")
def test_variation(self): deck = hanabi.CardDeck(variation='classic') with self.assertRaises(KeyError): deck.variation = 'test_KeyError' game = hanabi.Game() self.assertEqual(game.variation, 'classic') with self.assertRaises(AttributeError): game.variation = 'test_AttributeError'
def test_possibilities(self): game = hanabi.Game() player = game.players[0] possibilities = player.possibilities self.assertEqual( possibilities, [[set(game.colors), set([1, 2, 3, 4, 5])], [set(game.colors), set([1, 2, 3, 4, 5])], [set(game.colors), set([1, 2, 3, 4, 5])], [set(game.colors), set([1, 2, 3, 4, 5])], [set(game.colors), set([1, 2, 3, 4, 5])]])
def test_play(self): game = hanabi.Game() player = game.players[0] hand = [('r', 1), ('w', 4), ('w', 4), ('b', 5), ('y', 3)] player.hand = hand played = {'r': 1, 'g': 0, 'w': 0, 'b': 0, 'y': 0} discarded = {'r': [], 'g': [], 'w': [], 'b': [], 'y': []} game.play(player, 0) self.assertEqual(game.played, played, 'playable') self.assertEqual(game.discarded, discarded, 'playable') self.assertEqual(game.score, 1, 'playable 1') self.assertEqual(game.current_max, 25, 'playable') self.assertEqual(game.tokens, 8, 'playable') self.assertEqual(game.fuses, 0, 'playable') discarded['w'].append(4) game.play(player, 0) self.assertEqual(game.played, played, 'unplayable 1') self.assertEqual(game.discarded, discarded, 'unplayable 1') self.assertEqual(game.current_max, 25, 'unplayable 1') self.assertEqual(game.tokens, 8, 'unplayable 1') self.assertEqual(game.fuses, 1, 'unplayable 1') discarded['w'].append(4) game.play(player, 0) self.assertEqual(game.played, played, 'unplayable 2') self.assertEqual(game.discarded, discarded, 'unplayable 2') self.assertEqual(game.current_max, 23, 'unplayable 2') self.assertEqual(game.tokens, 8, 'unplayable 2') self.assertEqual(game.fuses, 2, 'unplayable 2') game.played['b'] = 4 game.tokens = 4 played = {'r': 1, 'g': 0, 'w': 0, 'b': 5, 'y': 0} game.play(player, 0) self.assertEqual(game.played, played, 'playable 5') self.assertEqual(game.discarded, discarded, 'playable 5') self.assertEqual(game.current_max, 23, 'playable 5') self.assertEqual(game.tokens, 5, 'playable 5') self.assertEqual(game.fuses, 2, 'playable 5') discarded['y'].append(3) game.play(player, 0) self.assertEqual(game.played, played, 'unplayable 3') self.assertEqual(game.discarded, discarded, 'unplayable 3') self.assertEqual(game.score, 6, 'unplayable 3') self.assertEqual(game.current_max, 23, 'unplayable 3') self.assertEqual(game.tokens, 5, 'unplayable 3') self.assertEqual(game.fuses, 2, 'unplayable 3') def test_turn(self): game = hanabi.Game() player = game.players[0]
def test_gameStart(self): tokens = 8 fuses = 0 hand_size = 5 game = hanabi.Game() self.assertEqual(tokens, game.tokens, msg='tokens') self.assertEqual(fuses, game.fuses, msg='fuses') for player in game.players: self.assertEqual(hand_size, len(player.hand), msg='hand size') self.assertEqual(len(game.deck), len(game.full_deck()) - 15) self.assertEqual(game.current_player, game.players[0])
def test_play(self): game = hanabi.Game(5) RS = hanabi.ai.RecommendationStrategy(game) c1 = hanabi.deck.Card(hanabi.deck.Color.Green, 1) c2 = hanabi.deck.Card(hanabi.deck.Color.Blue, 4) c3 = hanabi.deck.Card(hanabi.deck.Color.Green, 4) c4 = hanabi.deck.Card(hanabi.deck.Color.Red, 4) c5 = hanabi.deck.Card(hanabi.deck.Color.Red, 1) c6 = hanabi.deck.Card(hanabi.deck.Color.Green, 5) c7 = hanabi.deck.Card(hanabi.deck.Color.Blue, 5) c8 = hanabi.deck.Card(hanabi.deck.Color.Red, 5) c9 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 1) c10 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 4) c11 = hanabi.deck.Card(hanabi.deck.Color.Blue, 4) c12 = hanabi.deck.Card(hanabi.deck.Color.Red, 4) c13 = hanabi.deck.Card(hanabi.deck.Color.Blue, 1) c14 = hanabi.deck.Card(hanabi.deck.Color.Red, 3) c15 = hanabi.deck.Card(hanabi.deck.Color.Red, 3) c16 = hanabi.deck.Card(hanabi.deck.Color.Green, 4) c17 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 4) c18 = hanabi.deck.Card(hanabi.deck.Color.Green, 3) c19 = hanabi.deck.Card(hanabi.deck.Color.Red, 1) c20 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 3) game.piles[hanabi.deck.Color.Blue] = 0 game.piles[hanabi.deck.Color.Red] = 1 game.piles[hanabi.deck.Color.Green] = 0 game.piles[hanabi.deck.Color.Yellow] = 0 ok1 = hanabi.deck.Deck([c1, c2, c3, c4]) hand1 = hanabi.deck.Hand(ok1, 4) ok2 = hanabi.deck.Deck([c5, c6, c7, c8]) hand2 = hanabi.deck.Hand(ok2, 4) ok3 = hanabi.deck.Deck([c9, c10, c11, c12]) hand3 = hanabi.deck.Hand(ok3, 4) ok4 = hanabi.deck.Deck([c13, c14, c15, c16]) hand4 = hanabi.deck.Hand(ok4, 4) game.red_coins = 2 game.blue_coins = 5 game.moves = ['c13', 'p1'] game.memoire = [-1, 'p1', 'p3', 'p1', 'p1'] hanabi.ai.RecommendationStrategy.other_hands = [ hand1, hand2, hand3, hand4 ] self.assertEqual(hanabi.ai.RecommendationStrategy.play(RS), 'cr1')
def test_discard(self): game = hanabi.Game() player = game.players[0] card = player.hand[0] with self.assertRaises(ValueError): game.discard(player, 0) game.hint(player, 'g') game.discard(player, 0) discarded = game.discarded[card[0]][0] self.assertEqual(card[1], discarded) self.assertEqual(game.tokens, 8) self.assertEqual(player.possibilities[-1], game.single_possibilities(variation=game.variation)) self.assertEqual(len(player.hand), 5)
def testfin(self): game = hanabi.Game(5) ai = my_ai.HatGuessIA(game) game.ai = ai game.quiet = True game.run() verif = False #Si la variable reste à False c'est que le jeu s'arrete sans raison valable. if game.red_coins == 3 or len(game.deck.cards) == 0: verif = True self.assertEqual(verif, True)
def test_deduce_my_moves_2(self): game = hanabi.Game(5) RS = hanabi.ai.RecommendationStrategy(game) c1 = hanabi.deck.Card(hanabi.deck.Color.Blue, 2) c2 = hanabi.deck.Card(hanabi.deck.Color.Blue, 1) c3 = hanabi.deck.Card(hanabi.deck.Color.Green, 1) c4 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 1) c5 = hanabi.deck.Card(hanabi.deck.Color.Red, 1) c6 = hanabi.deck.Card(hanabi.deck.Color.Blue, 5) c7 = hanabi.deck.Card(hanabi.deck.Color.Green, 5) c8 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 5) c9 = hanabi.deck.Card(hanabi.deck.Color.Red, 1) c10 = hanabi.deck.Card(hanabi.deck.Color.Green, 2) c11 = hanabi.deck.Card(hanabi.deck.Color.Blue, 1) c12 = hanabi.deck.Card(hanabi.deck.Color.Green, 1) c13 = hanabi.deck.Card(hanabi.deck.Color.Green, 4) c14 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 1) c15 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 2) c16 = hanabi.deck.Card(hanabi.deck.Color.Red, 5) c17 = hanabi.deck.Card(hanabi.deck.Color.Green, 3) c18 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 3) c19 = hanabi.deck.Card(hanabi.deck.Color.Red, 4) c20 = hanabi.deck.Card(hanabi.deck.Color.Blue, 4) game.piles[hanabi.deck.Color.Blue] = 1 game.piles[hanabi.deck.Color.Red] = 1 game.piles[hanabi.deck.Color.Green] = 1 game.piles[hanabi.deck.Color.Yellow] = 1 ok1 = hanabi.deck.Deck([c1, c2, c3, c4]) hand1 = hanabi.deck.Hand(ok1, 4) ok2 = hanabi.deck.Deck([c5, c6, c7, c8]) hand2 = hanabi.deck.Hand(ok2, 4) ok3 = hanabi.deck.Deck([c9, c10, c11, c12]) hand3 = hanabi.deck.Hand(ok3, 4) ok4 = hanabi.deck.Deck([c13, c14, c15, c16]) hand4 = hanabi.deck.Hand(ok4, 4) ok5 = hanabi.deck.Deck([c17, c18, c19, c20]) hand5 = hanabi.deck.Hand(ok5, 4) hanabi.ai.RecommendationStrategy.other_hands = [ hand3, hand4, hand5, hand1 ] self.assertEqual( hanabi.ai.RecommendationStrategy.deduce_my_moves(RS, 'cr4', 3), 'd1')
def test_deduce_number_6(self): game = hanabi.Game(5) ai = hanabi.ai.RecommendationStrategy(game) c1 = hanabi.deck.Card(hanabi.deck.Color.Blue, 1) c2 = hanabi.deck.Card(hanabi.deck.Color.Red, 5) c3 = hanabi.deck.Card(hanabi.deck.Color.Green, 1) c4 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 1) game.piles[hanabi.deck.Color.Blue] = 4 game.piles[hanabi.deck.Color.Red] = 4 game.piles[hanabi.deck.Color.Green] = 4 game.piles[hanabi.deck.Color.Yellow] = 4 ok1 = hanabi.deck.Deck([c1, c2, c3, c4]) hand1 = hanabi.deck.Hand(ok1, 4) self.assertEqual( hanabi.ai.RecommendationStrategy.deduce_number(ai, hand1), 1)
def testType(self): game = hanabi.Game(5) ai = my_ai.HatGuessIA(game) game.ai = ai res = ai.play() print() print(res) print() verif = True #Tant que verif est True, le test est valide. if res[0] != 'p' and res[0] != 'c' and res[0] != p: verif = False if len(res) > 3: verif = False self.assertEqual(verif, True)
def test_hint(self): game = hanabi.Game() hand = [('r', 1), ('w', 4), ('w', 3), ('b', 1), ('y', 1)] red_hint = [[set(['r']), set(range(1, 6))], [set(['b', 'g', 'y', 'w']), set(range(1, 6))], [set(['b', 'g', 'y', 'w']), set(range(1, 6))], [set(['b', 'g', 'y', 'w']), set(range(1, 6))], [set(['b', 'g', 'y', 'w']), set(range(1, 6))]] white_hint = [[set(['r', 'b', 'g', 'y']), set(range(1, 6))], [set(['w']), set(range(1, 6))], [set(['w']), set(range(1, 6))], [set(['r', 'b', 'g', 'y']), set(range(1, 6))], [set(['r', 'b', 'g', 'y']), set(range(1, 6))]] one_hint = [[set(game.colors), set([1])], [set(game.colors), set(range(2, 6))], [set(game.colors), set(range(2, 6))], [set(game.colors), set([1])], [set(game.colors), set([1])]] player1 = game.players[0] player1.hand = hand game.hint(player1, 'r') self.assertEqual(red_hint, player1.possibilities) player1.possibilities = hanabi.CardDeck.all_possibilities(len(hand)) game.hint(player1, 'w') self.assertEqual(white_hint, player1.possibilities) player1.possibilities = hanabi.CardDeck.all_possibilities(len(hand)) game.hint(player1, 1) self.assertEqual(one_hint, player1.possibilities) self.assertEqual(game.tokens, 5) for i in range(5): game.hint(player1, 1) with self.assertRaises(ValueError): game.hint(player1, 1)
def test_deduce_number_1(self): """ Test si il y a un 5 jouable, recommende de le jouer """ game = hanabi.Game(5) ai = hanabi.ai.RecommendationStrategy(game) c1 = hanabi.deck.Card(hanabi.deck.Color.Green, 4) c2 = hanabi.deck.Card(hanabi.deck.Color.Red, 2) c3 = hanabi.deck.Card(hanabi.deck.Color.Blue, 5) c4 = hanabi.deck.Card(hanabi.deck.Color.Blue, 2) ok = hanabi.deck.Deck([c1, c2, c3, c4]) hand = hanabi.deck.Hand(ok, 4) game.piles[hanabi.deck.Color.Blue] = 4 game.piles[hanabi.deck.Color.Red] = 1 game.piles[hanabi.deck.Color.Green] = 3 game.piles[hanabi.deck.Color.Yellow] = 1 self.assertEqual( hanabi.ai.RecommendationStrategy.deduce_number(ai, hand), 2)
def test_deduce_number_3(self): """ Test si toutes les cartes sont indispensables, recommande de jeter la premiere """ game = hanabi.Game(5) ai = hanabi.ai.RecommendationStrategy(game) c1 = hanabi.deck.Card(hanabi.deck.Color.Blue, 5) c2 = hanabi.deck.Card(hanabi.deck.Color.Red, 5) c3 = hanabi.deck.Card(hanabi.deck.Color.Green, 5) c4 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 5) ok = hanabi.deck.Deck([c1, c2, c3, c4]) hand = hanabi.deck.Hand(ok, 4) game.piles[hanabi.deck.Color.Blue] = 2 game.piles[hanabi.deck.Color.Red] = 2 game.piles[hanabi.deck.Color.Green] = 2 game.piles[hanabi.deck.Color.Yellow] = 2 self.assertEqual( hanabi.ai.RecommendationStrategy.deduce_number(ai, hand), 4)
def test_deduce_number_4(self): """ Test si aucune cartes nest morte et aucune carte nest jouable, mais que les cartes 1 2 et 4 sont indispensables, recommande de jeter la 3 """ game = hanabi.Game(5) ai = hanabi.ai.RecommendationStrategy(game) c1 = hanabi.deck.Card(hanabi.deck.Color.Blue, 5) c2 = hanabi.deck.Card(hanabi.deck.Color.Red, 5) c3 = hanabi.deck.Card(hanabi.deck.Color.Green, 4) c4 = hanabi.deck.Card(hanabi.deck.Color.Yellow, 5) ok = hanabi.deck.Deck([c1, c2, c3, c4]) hand = hanabi.deck.Hand(ok, 4) game.piles[hanabi.deck.Color.Blue] = 2 game.piles[hanabi.deck.Color.Red] = 2 game.piles[hanabi.deck.Color.Green] = 2 game.piles[hanabi.deck.Color.Yellow] = 2 self.assertEqual( hanabi.ai.RecommendationStrategy.deduce_number(ai, hand), 6)
def main(): players = [] while True: num = len(players) + 1 player = input(f'Player {num} name: ') if player == '': if num <= 2: print('Must have at least two players') continue else: break players.append(player) game = hanabi.Game(players=players) turn_val = game.current_player while type(turn_val) == hanabi.Player: # Reset Values hint_player = None value_prompt = 'card number: ' choice = None value = None print(game) while choice is None: choice = input('Choose hint, play, or discard: ') if choice not in ['hint', 'play', 'discard']: print('Please select between hint, play, and discard.') choice = None if choice == 'hint': while hint_player is None: val = input('Choose hint target: ') for player in game.players: if val == player.name: hint_player = player if hint_player is None: print('Player not found. Please re-select a player.') value_prompt = 'hint: ' value = input(f'Choose {value_prompt}') try: value = int(value) except Exception: pass turn_val = game.turn(turn_val, choice, value, hint_player) print('Final Score: ', turn_val)
def n_tests(game, n): t1 = t.time() L = [] compteur = 0 compteur_2 = 0 for i in range(n): game.reset() game = hanabi.Game(5) game.quiet = True ai = new_ai.Recommander(game) game.ai = ai game.run() L.append(game.score) if game.score >= 21: compteur += 1 if game.score >= 25: compteur_2 += 1 Moyenne = stat.mean(L) Variance = stat.variance(L) print(L, "\nMoyenne = ", Moyenne, " and Variance = ", Variance, " Nombre de partie à plus de 24 coups = ", compteur, " Nombre de partie à 25 coups = ", compteur_2) t2 = t.time() print("Temps de Calcul pour ", n, " parties = ", t2 - t1, " secondes") plt.hist(L, rwidth=0.4, edgecolor='black', align='left', bins=[i for i in range(15, 27)], normed=1) V = str(Variance) m = str(Moyenne) plt.title("Résultats pour " + str(n) + " parties avec stratégie risquée \n\n" + "V = " + V + " et m = " + m + "\n" + str(compteur_2 / n) + str('%') + " de parties à 25 coups" + "\n" + str(compteur / n) + str('%') + " de parties à plus de 21 coups") plt.show()
import hanabi game = hanabi.Game(2) # 2 players ai = hanabi.ai.Cheater(game) # pour jouer toute une partie game.ai = ai game.run()
import hanabi ''' cette aldorithme fait joi la meme commande tout le temps a 100 reprises pour chque play possible et rend la moyenne des scores de plus il rend le meilleur des scores avec les play qui l'ont marqué (chaque play apparqitera autant de fois qui l'a marque le meilleur score) ''' choices = {"p1": 0, "p2": 0, "p3": 0, "p4": 0, "p5": 0} best_score = 0 best_scorers = [] for p in choices: moyenne = 0 for i in range(1000): game = hanabi.Game(2) game.ai = p game.run() gs = game.score moyenne = moyenne + gs if gs == best_score: best_scorers.append(p) elif gs > best_score: best_score = gs best_scorers = [p] choices[p] = moyenne / 1000 print('\n') for p in choices: print(p, "a un score moyen de", choices[p], "\n") print("Le meilleur score est de ", best_score, "il a ete marqué par",
import hanabi import hanabi.ai as AI from recommendation_ai import Recommend ''' Un algo de stat basique pour etudier l 'AI de recommendation ''' #on initialise chacune de nos variable moyenne = 0 best_score_count = 0 scores = [0] * 26 maxi = 0 game = hanabi.Game(5) for i in range(1000): #on effectue l'étude statistique sur 1000 parties game.reset() game = hanabi.Game(5) #on lance les parties les une après les autres ai = Recommend(game) game.ai = ai game.run() j = game.score moyenne = moyenne + game.score #traitement de la moyenne if j > maxi: #traitement du score maximun maxi = j best_score_count = 1 if j == maxi: best_score_count += 1 scores[game.score] += 1 #on renvoie les résultats
def __init__(self): self.game = hanabi.Game(2) self.game.ai = Robot_4(self.game) self.game.ai.quiet = True #This creates the main window of an application window = tk.Tk() window.title = ("Welcome to Hanabi") window.geometry("1850x1000") window.columnconfigure(50, minsize=640) for i in range(5, 31): if i not in [9, 14, 19, 24, 29]: window.columnconfigure(i, minsize=30) window.rowconfigure(20, minsize=50) ##Charge les images : def Resize_Image(image, maxsize): r1 = image.size[0] / maxsize[0] # width ratio r2 = image.size[1] / maxsize[1] # height ratio ratio = max(r1, r2) newsize = (int(image.size[0] / r1), int(image.size[1] / r2)) image = image.resize(newsize, Image.ANTIALIAS) return image #define background : background_image = ImageTk.PhotoImage( Image.open(images_dir + "paper-background.jpg")) self.background_label = tk.Label(window, image=background_image) self.background_label.place(x=0, y=0, relwidth=1, relheight=1) self.dico_color = { "Blue": "#0005a3", "Purple": "#7a1594", "Green": "green", "Yellow": "#c99700", "Red": "#a80a0a" } image_looser = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "looser.jpg"), (1850, 1250))) self.background_looser = tk.Label(window, image=image_looser) self.image_back = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "back.png"), (170, 230))) self.image_back_selected = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "back_selected.png"), (170, 230))) self.image_B1 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "B1.PNG"), (170, 230))) self.image_B2 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "B2.PNG"), (170, 230))) self.image_B3 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "B3.PNG"), (170, 230))) self.image_B4 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "B4.PNG"), (170, 230))) self.image_B5 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "B5.PNG"), (170, 230))) self.image_R1 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "R1.PNG"), (170, 230))) self.image_R2 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "R2.PNG"), (170, 230))) self.image_R3 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "R3.PNG"), (170, 230))) self.image_R4 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "R4.PNG"), (170, 230))) self.image_R5 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "R5.PNG"), (170, 230))) self.image_G1 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "G1.PNG"), (170, 230))) self.image_G2 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "G2.PNG"), (170, 230))) self.image_G3 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "G3.PNG"), (170, 230))) self.image_G4 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "G4.PNG"), (170, 230))) self.image_G5 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "G5.PNG"), (170, 230))) self.image_P1 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "P1.PNG"), (170, 230))) self.image_P2 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "P2.PNG"), (170, 230))) self.image_P3 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "P3.PNG"), (170, 230))) self.image_P4 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "P4.PNG"), (170, 230))) self.image_P5 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "P5.PNG"), (170, 230))) self.image_Y1 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "Y1.PNG"), (170, 230))) self.image_Y2 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "Y2.PNG"), (170, 230))) self.image_Y3 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "Y3.PNG"), (170, 230))) self.image_Y4 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "Y4.PNG"), (170, 230))) self.image_Y5 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "Y5.PNG"), (170, 230))) self.image_empty_B = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "empty blue.jpg"), (180, 70))) self.image_empty_R = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "empty red.jpg"), (180, 70))) self.image_empty_Y = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "empty yellow.jpg"), (180, 70))) self.image_empty_P = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "empty purple.jpg"), (180, 70))) self.image_empty_G = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "empty green.jpg"), (180, 70))) self.image_clue1 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "clue1.PNG"), (50, 50))) self.image_clue2 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "clue2.PNG"), (50, 50))) self.image_clue3 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "clue3.PNG"), (50, 50))) self.image_clue4 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "clue4.PNG"), (50, 50))) self.image_clue5 = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "clue5.PNG"), (50, 50))) self.image_clueB = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "clueB.PNG"), (50, 50))) self.image_clueR = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "clueR.PNG"), (50, 50))) self.image_clueY = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "clueY.PNG"), (50, 50))) self.image_clueP = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "clueP.PNG"), (50, 50))) self.image_clueG = ImageTk.PhotoImage( Resize_Image(Image.open(images_dir + "clueG.PNG"), (50, 50))) self.dico_images = { "B1": self.image_B1, "B2": self.image_B2, "B3": self.image_B3, "B4": self.image_B4, "B5": self.image_B5, "R1": self.image_R1, "R2": self.image_R2, "R3": self.image_R3, "R4": self.image_R4, "R5": self.image_R5, "G1": self.image_G1, "G2": self.image_G2, "G3": self.image_G3, "G4": self.image_G4, "G5": self.image_G5, "P1": self.image_P1, "P2": self.image_P2, "P3": self.image_P3, "P4": self.image_P4, "P5": self.image_P5, "Y1": self.image_Y1, "Y2": self.image_Y2, "Y3": self.image_Y3, "Y4": self.image_Y4, "Y5": self.image_Y5, "1": self.image_clue1, "2": self.image_clue2, "3": self.image_clue3, "4": self.image_clue4, "5": self.image_clue5, "B": self.image_clueB, "R": self.image_clueR, "G": self.image_clueG, "P": self.image_clueP, "Y": self.image_clueY } ## Widgets : self.button_discard = tk.Button(window, text="Discard", padx=85, pady=10, command=lambda: self.card_discarded()) self.button_discard.grid(row=30, column=5, columnspan=15, sticky=tk.S) self.button_play = tk.Button(window, text="Play Card", padx=80, pady=10, command=lambda: self.card_played()) self.button_play.grid(row=30, column=15, columnspan=15, sticky=tk.S) self.button_quit = tk.Button(window, text="Quit", command=window.destroy, padx=90, pady=10) self.button_clue_number = tk.Button(window, width=33, height=2, fg="#ffcbb3", bg="#db7337", font=("Courier", 21)) self.button_clue_color = tk.Button(window, width=35, height=2, fg="#ffcbb3", font=("Courier", 20)) #Main courante : tk.Label(window, text="Your Hand", fg="#ffcbb3", bg="#db7337", font=("Courier", 16), width=9).grid(row=30, column=5, columnspan=5, sticky=tk.SW) self.button_card_1 = tk.Button(window, image=self.image_back, borderwidth=10, command=lambda: self.card_selection(1)) self.button_card_2 = tk.Button(window, borderwidth=10, image=self.image_back, command=lambda: self.card_selection(2)) self.button_card_3 = tk.Button(window, borderwidth=10, image=self.image_back, command=lambda: self.card_selection(3)) self.button_card_4 = tk.Button(window, borderwidth=10, image=self.image_back, command=lambda: self.card_selection(4)) self.button_card_5 = tk.Button(window, borderwidth=10, image=self.image_back, command=lambda: self.card_selection(5)) self.button_card_1.grid(row=50, column=5, columnspan=5, sticky=tk.W) self.button_card_2.grid(row=50, column=10, columnspan=5) self.button_card_3.grid(row=50, column=15, columnspan=5) self.button_card_4.grid(row=50, column=20, columnspan=5) self.button_card_5.grid(row=50, column=25, columnspan=5) self.dico_buttons = { 1: self.button_card_1, 2: self.button_card_2, 3: self.button_card_3, 4: self.button_card_4, 5: self.button_card_5 } self.button_card_1.selected = 0 self.button_card_2.selected = 0 self.button_card_3.selected = 0 self.button_card_4.selected = 0 self.button_card_5.selected = 0 self.label_number_clue1 = tk.Label(window, borderwidth=0) self.label_number_clue2 = tk.Label(window, borderwidth=0) self.label_number_clue3 = tk.Label(window, borderwidth=0) self.label_number_clue4 = tk.Label(window, borderwidth=0) self.label_number_clue5 = tk.Label(window, borderwidth=0) self.label_color_clue1 = tk.Label(window, borderwidth=0) self.label_color_clue2 = tk.Label(window, borderwidth=0) self.label_color_clue3 = tk.Label(window, borderwidth=0) self.label_color_clue4 = tk.Label(window, borderwidth=0) self.label_color_clue5 = tk.Label(window, borderwidth=0) self.dico_clues = { 1: [self.label_number_clue1, self.label_color_clue1], 2: [self.label_number_clue2, self.label_color_clue2], 3: [self.label_number_clue3, self.label_color_clue3], 4: [self.label_number_clue4, self.label_color_clue4], 5: [self.label_number_clue5, self.label_color_clue5] } #Main partenaire : tk.Label(window, text="Partner's Hand", fg="#ffcbb3", bg="#db7337", font=("Courier", 16), width=14).grid(row=0, column=5, columnspan=5, sticky=tk.SW) self.button_partner_1 = tk.Button( window, image=self.image_back, borderwidth=10, command=lambda: self.partner_selection(1)) self.button_partner_2 = tk.Button( window, borderwidth=10, image=self.image_back, command=lambda: self.partner_selection(2)) self.button_partner_3 = tk.Button( window, borderwidth=10, image=self.image_back, command=lambda: self.partner_selection(3)) self.button_partner_4 = tk.Button( window, borderwidth=10, image=self.image_back, command=lambda: self.partner_selection(4)) self.button_partner_5 = tk.Button( window, borderwidth=10, image=self.image_back, command=lambda: self.partner_selection(5)) self.button_partner_1.grid(row=10, rowspan=10, column=5, columnspan=5) self.button_partner_2.grid(row=10, rowspan=10, column=10, columnspan=5) self.button_partner_3.grid(row=10, rowspan=10, column=15, columnspan=5) self.button_partner_4.grid(row=10, rowspan=10, column=20, columnspan=5) self.button_partner_5.grid(row=10, rowspan=10, column=25, columnspan=5) self.button_partner_1.selected = 0 self.button_partner_2.selected = 0 self.button_partner_3.selected = 0 self.button_partner_4.selected = 0 self.button_partner_5.selected = 0 self.dico_partner_buttons = { 1: self.button_partner_1, 2: self.button_partner_2, 3: self.button_partner_3, 4: self.button_partner_4, 5: self.button_partner_5 } self.label_number_partner_clue1 = tk.Label(window, borderwidth=0) self.label_number_partner_clue2 = tk.Label(window, borderwidth=0) self.label_number_partner_clue3 = tk.Label(window, borderwidth=0) self.label_number_partner_clue4 = tk.Label(window, borderwidth=0) self.label_number_partner_clue5 = tk.Label(window, borderwidth=0) self.label_color_partner_clue1 = tk.Label(window, borderwidth=0) self.label_color_partner_clue2 = tk.Label(window, borderwidth=0) self.label_color_partner_clue3 = tk.Label(window, borderwidth=0) self.label_color_partner_clue4 = tk.Label(window, borderwidth=0) self.label_color_partner_clue5 = tk.Label(window, borderwidth=0) self.dico_partner_clues = { 1: [self.label_number_partner_clue1, self.label_color_partner_clue1], 2: [self.label_number_partner_clue2, self.label_color_partner_clue2], 3: [self.label_number_partner_clue3, self.label_color_partner_clue3], 4: [self.label_number_partner_clue4, self.label_color_partner_clue4], 5: [self.label_number_partner_clue5, self.label_color_partner_clue5] } #Piles self.label_pile_B = tk.Label(window, image=self.image_empty_B, anchor=tk.NW, height=60, width=170) self.label_pile_G = tk.Label(window, image=self.image_empty_G, anchor=tk.NW, height=60, width=170) self.label_pile_R = tk.Label(window, image=self.image_empty_R, anchor=tk.NW, height=60, width=170) self.label_pile_Y = tk.Label(window, image=self.image_empty_Y, anchor=tk.NW, height=60, width=170) self.label_pile_P = tk.Label(window, image=self.image_empty_P, anchor=tk.NW, height=60, width=170) self.label_pile_B.grid(row=22, column=100) self.label_pile_G.grid(row=24, column=100) self.label_pile_R.grid(row=26, column=100) self.label_pile_Y.grid(row=28, column=100) self.label_pile_P.grid(row=30, column=100) self.dico_piles = { 'B': self.label_pile_B, 'G': self.label_pile_G, 'R': self.label_pile_R, 'Y': self.label_pile_Y, 'P': self.label_pile_P } #Other self.label_blue_coins = tk.Label(window, text="Blue coins : 8", fg="blue", bg="#f2c777", font=("Courier", 20)) self.label_blue_coins.grid(row=22, column=5, columnspan=10, sticky=tk.SW) self.label_mistakes = tk.Label(window, text="Mistakes : 0", fg="red", bg="#f2c777", font=("Courier", 20)) self.label_mistakes.grid(row=24, column=5, columnspan=10, sticky=tk.NW) self.label_deck = tk.Label(window, text=str(len(self.game.deck.cards)) + " cards remaining", fg="black", bg="#f2c777", font=("Courier", 20)) self.label_deck.grid(row=24, column=10, columnspan=15, sticky=tk.N) self.myFriendlyLabel = tk.Label( window, fg="#ffcbb3", bg="#db7337", text="Select Partner's cards to give a clue", font=("Courier", 20), width=40, height=3) self.myFriendlyLabel.grid(row=24, rowspan=10, column=5, columnspan=25, pady=(0, 0)) self.label_discard_pile = tk.Message(window, width=750, bg="#db7337", fg="black", font=("Courier", 30), text="Discard Pile :", anchor=tk.NW, relief=tk.RIDGE) self.label_discard_pile.grid(row=50, column=50, columnspan=100) self.selection = None self.not_finished = True tk.Label(window, text="Welcome ! Let's play Hanabi", fg="#ffcbb3", bg="#d15e30", font="none 22 bold").grid(row=0, column=5, columnspan=25, pady=(0, 10)) partner_hand = self.game.hands[1] for ind_card in range(len(partner_hand)): self.dico_partner_buttons[ind_card + 1].configure( image=self.dico_images[str(partner_hand.cards[ind_card])]) #Start the GUI window.mainloop()
def test_A1(self): game = hanabi.Game(2) game.quiet = True game.turn('p3') # check that we can play blindly
def setUp(self): self.unshuffled_game = hanabi.Game() self.random_game = hanabi.Game() # ... group G here! self.predefined_game = hanabi.Game()
import hanabi import hanabi.ai import statistics as stat import hanabi.un_ai2 as new_ai import time as t game = hanabi.Game(5) ai = new_ai.Recommander(game) game.ai = ai import matplotlib.pyplot as plt def n_tests(game, n): t1 = t.time() L = [] compteur = 0 compteur_2 = 0 for i in range(n): game.reset() game = hanabi.Game(5) game.quiet = True ai = new_ai.Recommander(game) game.ai = ai game.run() L.append(game.score) if game.score >= 21: compteur += 1 if game.score >= 25: compteur_2 += 1 Moyenne = stat.mean(L) Variance = stat.variance(L)
def test_turn(self): game = hanabi.Game() player = game.players[0]