示例#1
0
def get_deck_from_json_file(directory):
    file = open(directory)

    jdeck = json.loads(remove_linebreaks(file))

    deck = Deck()
    deck.pilot = jdeck['deck']['pilot']
    deck.score = jdeck['deck']['score']

    for item in jdeck['deck']['mainboard']:
        deck.mainboard.append([item['count'], item['card']])
    for item in jdeck['deck']['sideboard']:
        deck.sideboard.append([item['count'], item['card']])

    return deck
示例#2
0
def eval_genomes(genome, config):
    # Grab our globals
    global G_gen
    global G_hall_of_fame

    # Increment generation
    G_gen += 1

    # Make our empty lists of players, genomes, and N networks
    players = []
    nets = []
    ge = []

    # Set an impossibly low best fitness, so we can easily find our best of each generation
    best_fitness = -1000
    best_player = Player()

    # For each genome...
    # Give them a starting fitness of 0

    g = genome

    g.fitness = 0

    # Add players, neural networks, and genomes to our own lists
    players.append(Player())
    nets.append(network(g, config))
    ge.append(g)

    # For every hand we want to play in a simulation...
    for i in range(C_HANDS_PER_GENERATION * 1000):
        # Let's use a new deck each time
        deck = Deck()
        dealer = Player()

        # Deal two cards to each player
        players, deck = deal_two_each(players, deck)

        for _ in range(2):
            deck.deal_to(dealer)

        for j, player in enumerate(players):
            d = Deck()
            d.cards = []
            for card in deck.cards:
                d.cards.append(card)

            # Activation function
            inputs = player.score, player.has_ace, dealer.hand[0].value
            output = nets[j].activate(inputs)

            # While we have less than 17 points, hit, except 10% of the time stay
            while output[0] > 0.5 and player.score < 21:
                player.hit(d)
                ge[j].fitness += 0.1

        # Standard dealer rules, hit on and up to 16
        while dealer.score <= 16:
            dealer.hit(deck)

        players, ge, msg = reward_genomes_for_wins(players, dealer, ge)

        for j, g in enumerate(ge):
            if g.fitness > best_fitness:
                best_fitness = g.fitness
                best_player = players[j]

        # Display the results of the final game
        if i + 1 == C_HANDS_PER_GENERATION:
            display_game_results(best_player, dealer, i, msg)

        # Make sure their hands are empty
        for player in players:
            player.clear_hand()
        dealer.clear_hand()

    # Display the results of the whole simulation
    display_sim_results(best_player)

    return ge[0]
示例#3
0
def game():

    global joueur1
    global joueur2
    global tour
    global deck_joueur1
    global deck_joueur2
    global deck_list_cards

    joueur1 = Player(1, '')
    joueur2 = Player(2, '')
    deck_joueur1 = Deck('Deck de test 1')
    deck_joueur2 = Deck('Deck de test 2')

    def add_card_to_hand(deck, joueur, delete=False):
        card_from_hand_to_deck = random.choice(deck.get_cards_from_deck())
        card_to_add = Card(
            card_from_hand_to_deck[1], card_from_hand_to_deck[2],
            card_from_hand_to_deck[3], card_from_hand_to_deck[4],
            card_from_hand_to_deck[5], card_from_hand_to_deck[6],
            card_from_hand_to_deck[7], card_from_hand_to_deck[8])
        joueur.hand.append(card_to_add)

        if delete:
            deck.del_card_from_deck(card_from_hand_to_deck)

    def end_turn():
        global tour
        print('Fin du tour')
        print('Tour ' + str(tour))

        if tour % 2 == 0:
            rect_end_turn = pygame.Rect(10, 400, 1260, 100)

            text_tools.draw_text(
                'Tour ' + str(tour + 1) + ' - C\'est au joueur 1 de jouer !',
                font_title, (255, 255, 255), screen, 250, 320)
            pygame.display.flip()
            joueur1.add_action_to_stock()
            joueur1.add_gold_to_stock()
            joueur1.add_mana_to_stock()
        elif tour % 2 == 1:
            rect_end_turn = pygame.Rect(10, 400, 1260, 100)

            text_tools.draw_text(
                'Tour ' + str(tour + 1) + ' - C\'est au joueur 2 de jouer !',
                font_title, (255, 255, 255), screen, 250, 320)
            pygame.display.flip()

            joueur2.add_action_to_stock()
            joueur2.add_gold_to_stock()
            joueur2.add_mana_to_stock()
        tour += 1
        time.sleep(2)

    def action_depending_of_card(player, enemy, player_hand, player_deck):
        bonus_damages = 0
        progressing = True
        done = False

        rect_info = pygame.Rect(10, 400, 1260, 100)

        for j in range(0, len(player.get_player_hand())):
            # On joue la carte avec le bouton gauche
            if player_hand[j].collidepoint(
                (mx, my)) and event.button == 1 and player.hand[j]:

                if 25 < enemy.shield < 30:
                    bonus_damages = 0.05
                elif 20 < enemy.shield <= 25:
                    bonus_damages = 0.10
                elif 15 < enemy.shield <= 20:
                    bonus_damages = 0.15
                elif 10 < enemy.shield <= 15:
                    bonus_damages = 0.20
                elif 5 < enemy.shield <= 10:
                    bonus_damages = 0.25
                elif 0 < enemy.shield <= 5:
                    bonus_damages = 0.30

                # print('shield : ' + str(enemy.shield) + ' - bonus dégats : ' + str(bonus_damages))

                if player.hand[j].ressource_type == 'PA':
                    if player.action_stock >= player.hand[j].cost:
                        # print('Vous pouvez jouer la carte')
                        if player.hand[j].effect == 'Shield':
                            if player.hand[j].target == 'Self':
                                if player.shield < 30:
                                    print('Le joueur ' +
                                          str(player.player_index) +
                                          ' gagne ' +
                                          str(player.hand[j].value) +
                                          ' points de shield')
                                    player.shield += player.hand[j].value
                                    if player.shield > 30:
                                        player.shield = 30
                                    progressing = False
                                else:

                                    text_tools.draw_text(
                                        'Vos points de bouclier sont au maximum',
                                        font_title, (255, 255, 255), screen,
                                        250, 420)
                                    pygame.display.flip()
                                    time.sleep(1)
                            elif enemy.shield > abs(player.hand[j].value):
                                if enemy.shield > 0:
                                    enemy.shield += player.hand[j].value
                                    print('Le joueur ' +
                                          str(enemy.player_index) + ' perd ' +
                                          str(abs(player.hand[j].value)) +
                                          ' points de shield')
                                    if enemy.shield < 0:
                                        enemy.shield = 0
                                    progressing = False
                                else:

                                    text_tools.draw_text(
                                        'L\'adversaire n\'a plus de bouclier !',
                                        font_title, (255, 255, 255), screen,
                                        250, 420)
                                    pygame.display.flip()
                                    time.sleep(1)
                        elif player.hand[j].effect == 'Life':
                            if player.hand[j].target == 'Self':
                                if player.hp < 100:
                                    print('Le joueur ' +
                                          str(player.player_index) +
                                          ' gagne ' +
                                          str(player.hand[j].value) +
                                          ' points de vie')
                                    player.hp += player.hand[j].value
                                    if player.hp > 100:
                                        player.hp = 100
                                    progressing = False
                                else:

                                    text_tools.draw_text(
                                        'Vos points de vie sont au maximum',
                                        font_title, (255, 255, 255), screen,
                                        250, 420)
                                    pygame.display.flip()
                                    time.sleep(1)
                            elif enemy.hp > abs(player.hand[j].value):
                                enemy.hp += int(player.hand[j].value *
                                                (1 + bonus_damages))
                                print('Le joueur ' + str(enemy.player_index) +
                                      ' perd ' + str(
                                          abs(
                                              int(player.hand[j].value *
                                                  (1 + bonus_damages)))) +
                                      ' points de vie')
                                progressing = False
                            elif enemy.hp <= abs(player.hand[j].value):
                                enemy.hp += int(player.hand[j].value *
                                                (1 + bonus_damages))
                                print('Le joueur ' + str(enemy.player_index) +
                                      ' perd ' + str(
                                          abs(
                                              int(player.hand[j].value *
                                                  (1 + bonus_damages)))) +
                                      ' points de vie')
                                print(
                                    'Le joueur ' + str(enemy.player_index) +
                                    ' n\'a plus de points de vie ! Le joueur '
                                    + str(player.player_index) +
                                    ' remporte la partie !')
                                done = end_game()
                                return done

                        if progressing == False:
                            player.action_stock -= player.hand[j].cost
                            player.hand.remove(player.hand[j])
                            if player_deck.get_nb_cards_in_deck() > 0:
                                add_card_to_hand(player_deck, player, True)
                            else:

                                text_tools.draw_text(
                                    'Le joueur n\'a plus de cartes dans son deck !',
                                    font_title, (255, 255, 255), screen, 250,
                                    420)
                                pygame.display.flip()
                                time.sleep(1)

                            end_turn()
                        # print(str(tour))
                    else:

                        text_tools.draw_text('Pas assez de ressouces',
                                             font_title, (255, 255, 255),
                                             screen, 250, 320)
                        pygame.display.flip()
                        time.sleep(1)
                elif player.hand[j].ressource_type == 'PM':
                    if player.mana_stock >= player.hand[j].cost:
                        # print('Vous pouvez jouer la carte')
                        if player.hand[j].effect == 'Shield':
                            if player.hand[j].target == 'Self':
                                if player.shield < 30:
                                    print('Le joueur ' +
                                          str(player.player_index) +
                                          ' gagne ' +
                                          str(player.hand[j].value) +
                                          ' points de shield')
                                    player.shield += player.hand[j].value
                                    if player.shield > 30:
                                        player.shield = 30
                                    progressing = False
                                else:

                                    text_tools.draw_text(
                                        'Vos points de bouclier sont au maximum',
                                        font_title, (255, 255, 255), screen,
                                        250, 420)
                                    pygame.display.flip()
                                    time.sleep(1)
                            elif enemy.shield > abs(player.hand[j].value):
                                if enemy.shield > 0:
                                    enemy.shield += player.hand[j].value
                                    print('Le joueur ' +
                                          str(enemy.player_index) + ' perd ' +
                                          str(abs(player.hand[j].value)) +
                                          ' points de shield')
                                    if enemy.shield < 0:
                                        enemy.shield = 0
                                    progressing = False
                                else:

                                    text_tools.draw_text(
                                        'L\'adversaire n\'a plus de bouclier !',
                                        font_title, (255, 255, 255), screen,
                                        250, 420)
                                    pygame.display.flip()
                                    time.sleep(1)
                        elif player.hand[j].effect == 'Life':
                            if player.hand[j].target == 'Self':
                                if player.hp < 100:
                                    print('Le joueur ' +
                                          str(player.player_index) +
                                          ' gagne ' +
                                          str(player.hand[j].value) +
                                          ' points de vie')
                                    player.hp += player.hand[j].value
                                    if player.hp > 100:
                                        player.hp = 100
                                    progressing = False
                                else:

                                    text_tools.draw_text(
                                        'Vos points de vie sont au maximum',
                                        font_title, (255, 255, 255), screen,
                                        250, 420)
                                    pygame.display.flip()
                                    time.sleep(1)
                            elif enemy.hp > abs(player.hand[j].value):
                                enemy.hp += int(player.hand[j].value *
                                                (1 + bonus_damages))
                                print('Le joueur ' + str(enemy.player_index) +
                                      ' perd ' + str(
                                          abs(
                                              int(player.hand[j].value *
                                                  (1 + bonus_damages)))) +
                                      ' points de vie')
                                progressing = False
                            elif enemy.hp <= abs(player.hand[j].value):
                                enemy.hp += int(player.hand[j].value *
                                                (1 + bonus_damages))
                                print('Le joueur ' + str(enemy.player_index) +
                                      ' perd ' + str(
                                          abs(
                                              int(player.hand[j].value *
                                                  (1 + bonus_damages)))) +
                                      ' points de vie')
                                print(
                                    'Le joueur ' + str(enemy.player_index) +
                                    ' n\'a plus de points de vie ! Le joueur '
                                    + str(player.player_index) +
                                    ' remporte la partie !')
                                done = end_game()
                                return done
                        if progressing == False:
                            player.mana_stock -= player.hand[j].cost
                            player.hand.remove(player.hand[j])
                            if player_deck.get_nb_cards_in_deck() > 0:
                                add_card_to_hand(player_deck, player, True)
                            else:

                                text_tools.draw_text(
                                    'Le joueur n\'a plus de cartes dans son deck !',
                                    font_title, (255, 255, 255), screen, 250,
                                    420)
                                pygame.display.flip()
                                time.sleep(1)

                            end_turn()
                        # print(str(tour))
                    else:

                        text_tools.draw_text('Pas assez de ressouces',
                                             font_title, (255, 255, 255),
                                             screen, 250, 320)
                        pygame.display.flip()
                        time.sleep(1)
                elif player.hand[j].ressource_type == 'PO':
                    if player.gold_stock >= player.hand[j].cost:
                        # print('Vous pouvez jouer la carte')
                        if player.hand[j].effect == 'Shield':
                            if player.hand[j].target == 'Self':
                                if player.shield < 30:
                                    print('Le joueur ' +
                                          str(player.player_index) +
                                          ' gagne ' +
                                          str(player.hand[j].value) +
                                          ' points de shield')
                                    player.shield += player.hand[j].value
                                    if player.shield > 30:
                                        player.shield = 30
                                    progressing = False
                                else:

                                    text_tools.draw_text(
                                        'Vos points de bouclier sont au maximum',
                                        font_title, (255, 255, 255), screen,
                                        250, 420)
                                    pygame.display.flip()
                                    time.sleep(1)
                            elif enemy.shield > abs(player.hand[j].value):
                                if enemy.shield > 0:
                                    enemy.shield += player.hand[j].value
                                    print('Le joueur ' +
                                          str(enemy.player_index) + ' perd ' +
                                          str(abs(player.hand[j].value)) +
                                          ' points de shield')
                                    if enemy.shield < 0:
                                        enemy.shield = 0
                                    progressing = False
                                else:

                                    text_tools.draw_text(
                                        'L\'adversaire n\'a plus de bouclier !',
                                        font_title, (255, 255, 255), screen,
                                        250, 420)
                                    pygame.display.flip()
                                    time.sleep(1)
                        elif player.hand[j].effect == 'Life':
                            if player.hand[j].target == 'Self':
                                if player.hp < 100:
                                    print('Le joueur ' +
                                          str(player.player_index) +
                                          ' gagne ' +
                                          str(player.hand[j].value) +
                                          ' points de vie')
                                    player.hp += player.hand[j].value
                                    if player.hp > 100:
                                        player.hp = 100
                                    progressing = False
                                else:

                                    text_tools.draw_text(
                                        'Vos points de vie sont au maximum',
                                        font_title, (255, 255, 255), screen,
                                        250, 420)
                                    pygame.display.flip()
                                    time.sleep(1)
                            elif enemy.hp > abs(player.hand[j].value):
                                enemy.hp += int(player.hand[j].value *
                                                (1 + bonus_damages))
                                print('Le joueur ' + str(enemy.player_index) +
                                      ' perd ' + str(
                                          abs(
                                              int(player.hand[j].value *
                                                  (1 + bonus_damages)))) +
                                      ' points de vie')
                                progressing = False
                            elif enemy.hp <= abs(player.hand[j].value):
                                enemy.hp += int(player.hand[j].value *
                                                (1 + bonus_damages))
                                print('Le joueur ' + str(enemy.player_index) +
                                      ' perd ' + str(
                                          abs(
                                              int(player.hand[j].value *
                                                  (1 + bonus_damages)))) +
                                      ' points de vie')
                                print(
                                    'Le joueur ' + str(enemy.player_index) +
                                    ' n\'a plus de points de vie ! Le joueur '
                                    + str(player.player_index) +
                                    ' remporte la partie !')
                                done = end_game()
                                return done

                        if progressing == False:
                            player.gold_stock -= player.hand[j].cost
                            player.hand.remove(player.hand[j])
                            if player_deck.get_nb_cards_in_deck() > 0:
                                add_card_to_hand(player_deck, player, True)
                            else:

                                text_tools.draw_text(
                                    'Le joueur n\'a plus de cartes dans son deck !',
                                    font_title, (255, 255, 255), screen, 250,
                                    320)
                                pygame.display.flip()
                                time.sleep(1)

                            end_turn()
                        # print(str(tour))
                    else:

                        text_tools.draw_text('Pas assez de ressouces',
                                             font_title, (255, 255, 255),
                                             screen, 250, 320)
                        pygame.display.flip()
                        time.sleep(1)
                # joueur2.hand.remove(joueur2.hand[j])

            # Discard avec le bouton droit
            elif player_hand[j].collidepoint(
                (mx, my)) and event.button == 3 and player.hand[j]:
                print('Clic sur la ' + str(j + 1) +
                      'eme carte de ma main du joueur ' +
                      str(player.player_index))
                print('discard ' + player.hand[j].name)
                player.hand.remove(player.hand[j])

                if player_deck.get_nb_cards_in_deck() > 0:
                    add_card_to_hand(player_deck, player, True)

                end_turn()
                # print(str(tour))

    usernames = get_username()

    joueur1.change_name(usernames[0])
    joueur2.change_name(usernames[1])
    joueur1.add_action_to_stock()
    joueur1.add_gold_to_stock()
    joueur1.add_mana_to_stock()

    cards = mysql_connexion.readCards()

    for card in cards:
        deck_list_cards.add_card_to_deck(card)

    for i in range(0, deck_len):
        card_from_list_to_deck = random.choice(
            deck_list_cards.get_cards_from_deck())
        deck_joueur1.add_card_to_deck(card_from_list_to_deck)

        card_from_list_to_deck2 = random.choice(
            deck_list_cards.get_cards_from_deck())
        deck_joueur2.add_card_to_deck(card_from_list_to_deck2)

    tour = 1

    # On remplit la main de départ des joueurs
    # Main du joueur1
    for i in range(0, 7):
        add_card_to_hand(deck_joueur1, joueur1)
        # deck_joueur1.del_card_from_deck(card_from_hand_to_deck)

    # Main du joueur2
    for j in range(0, 7):
        add_card_to_hand(deck_joueur2, joueur2)

    continuer = True
    while continuer:
        mx, my = pygame.mouse.get_pos()
        game_interface(joueur1, joueur2)
        print_hand(joueur1)
        print_hand(joueur2)

        # done = end_game()
        # if done:
        #     continuer = False

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    continuer = False
            if event.type == MOUSEBUTTONDOWN:
                if tour % 2 == 1:
                    done = action_depending_of_card(joueur1, joueur2, hand1,
                                                    deck_joueur1)
                elif tour % 2 == 0:
                    done = action_depending_of_card(joueur2, joueur1, hand2,
                                                    deck_joueur2)

                if done:
                    continuer = False

                if rect_end_turn.collidepoint((mx, my)) and event.button == 1:
                    end_turn()
                    # print(str(tour))

        pygame.display.update()
示例#4
0
from Classes.Player import Player
from Classes.Card import Card

pygame.init()
pygame.font.init()

screen = pygame.display.set_mode((1280, 800))
font_title = pygame.font.SysFont('Helvetic', 75)
font_retour = pygame.font.SysFont('Times,Arial', 30)
font_text = pygame.font.SysFont('Times,Arial', 15)
font_text_small = pygame.font.SysFont('Times', 12)

joueur1 = None
joueur2 = None

deck_list_cards = Deck('Liste de toutes les cartes')
deck_joueur1 = None
deck_joueur2 = None
hand1 = {}
hand2 = {}
player1_username = ''
player2_username = ''
rect_end_turn = None
tour = None
deck_len = 32

heart = pygame.image.load("ressources/images/heart.png").convert_alpha()
heart_small = pygame.transform.scale(heart, (44, 40))
heart_very_small = pygame.transform.scale(heart, (25, 23))

shield = pygame.image.load("ressources/images/shield.png").convert_alpha()
示例#5
0
    deck.new_deck()
    player.add_card(deck)
    window = None

pygame.init()
pygame.font.init()
pygame.display.set_mode((800, 600))
pygame.display.set_caption("Blackjack")
screen = pygame.display.get_surface()

# Позиции
pos_player = [254, 350]
pos_dealer = [254, 110]
pos_deck = [650, 110]

deck = Deck(pos_deck)

bankroll = 3000

player = Player(pos_player)
player.set_deck(deck)

dealer = Dealer(pos_dealer)
dealer.set_deck(deck)

chips = ChipsWindow(bankroll)
choice = ChoiceForm(screen)
bets = BetForm(screen, bankroll)
window = None

player.add_card(deck)
示例#6
0
def get_deck_from_soup(soup):
    results = soup.find(id="content-detail-page-of-an-article")

    piles = results.find_all("div", {"class": "deck-list-text"})
    pilots = results.find_all("span", {"class": "deck-meta"})

    decks = []

    for counter, pilot in enumerate(pilots):
        decks.append(Deck())
        pilotinfo = pilot.find('h4').text.split(' ', 1)
        if len(pilotinfo) == 2:
            decks[counter].score = pilotinfo[1]
        decks[counter].pilot = pilotinfo[0]

        categories = [{
            "class": "sorted-by-planeswalker clearfix element"
        }, {
            "class": "sorted-by-creature clearfix element"
        }, {
            "class": "sorted-by-sorcery clearfix element"
        }, {
            "class": "sorted-by-instant clearfix element"
        }, {
            "class": "sorted-by-enchantment clearfix element"
        }, {
            "class": "sorted-by-artifact clearfix element"
        }, {
            "class": "sorted-by-Other clearfix element"
        }, {
            "class": "sorted-by-land clearfix element"
        }]

        pile = piles[counter].find(
            "div", {"class": "sorted-by-overview-container sortedContainer"})
        sb = piles[counter].find(
            "div", {"class": "sorted-by-sideboard-container clearfix element"})

        for category in categories:
            tmp = pile.find("div", category)

            if isinstance(tmp, type(pile)):  # no es elegante para nada
                number = []
                name = []

                number = tmp.find_all("span", {"class": "card-count"})
                names = tmp.find_all("span", {"class": "card-name"})

                for idx, name in enumerate(names):
                    decks[counter].mainboard.append(
                        (number[idx].text, name.text))

        if isinstance(sb, type(pile)):  # no es elegante para nada
            number = []
            name = []

            number = sb.find_all("span", {"class": "card-count"})
            names = sb.find_all("span", {"class": "card-name"})

            for idx, name in enumerate(names):
                decks[counter].sideboard.append((number[idx].text, name.text))

    return decks