#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
This is an example of pyCardDeck, it's not meant to be complete poker script,
but rather a showcase of pyCardDeck's usage.
"""

import sys
import pyCardDeck
from typing import List
from pyCardDeck.cards import PokerCard

class Player:

    def __init__(self, name: str):
        self.hand = []
        self.name = name

    def __str__(self):
        return self.name

class BlackjackGame:

    def __init__(self, players: List[Player]):
        self.deck = pyCardDeck.Deck()
        self.deck.load_standard_deck()
        self.players = players
        self.scores = {}
        print("Created a game with {} players.".format(len(self.players)))

    def blackjack(self):
        """
        The main blackjack game sequence.

        Each player takes an entire turn before moving on.

        If each player gets a turn and no one has won, the player or players
        with the highest score below 21 are declared the winner.
        """
        print("Setting up...")
        print("Shuffling...")
        self.deck.shuffle()
        print("All shuffled!")
        print("Dealing...")
        self.deal()
        print("\nLet's play!")
        for player in self.players:
            print("{}'s turn...".format(player.name))
            self.play(player)
        else:
            print("That's the last turn. Determining the winner...")
            self.find_winner()

    def deal(self):
        """
        Deals two cards to each player.
        """
        for _ in range(2):
            for p in self.players:
                newcard = self.deck.draw()
                p.hand.append(newcard)
                print("Dealt {} the {}.".format(p.name, str(newcard)))

    def find_winner(self):
        """
        Finds the highest score, then finds which player(s) have that score,
        and reports them as the winner.
        """
        winners = []
        try:
            win_score = max(self.scores.values())
            for key in self.scores.keys():
                if self.scores[key] == win_score:
                    winners.append(key)
                else:
                    pass
            winstring = " & ".join(winners)
            print("And the winner is...{}!".format(winstring))
        except ValueError:
            print("Whoops! Everybody lost!")

    def hit(self, player):
        """
        Adds a card to the player's hand and states which card was drawn.
        """
        newcard = self.deck.draw()
        player.hand.append(newcard)
        print("   Drew the {}.".format(str(newcard)))

    def play(self, player):
        """
        An individual player's turn.

        If the player's cards are an ace and a ten or court card,
        the player has a blackjack and wins.

        If a player's cards total more than 21, the player loses.

        Otherwise, it takes the sum of their cards and determines whether
        to hit or stand based on their current score.
        """
        while True:
            points = sum_hand(player.hand)
            if points < 17:
                print("   Hit.")
                self.hit(player)
            elif points == 21:
                print("   {} wins!".format(player.name))
                sys.exit(0) # End if someone wins
            elif points > 21:
                print("   Bust!")
                break
            else:  # Stand if between 17 and 20 (inclusive)
                print("   Standing at {} points.".format(str(points)))
                self.scores[player.name] = points
                break

def sum_hand(hand: list):
    """
    Converts ranks of cards into point values for scoring purposes.
    'K', 'Q', and 'J' are converted to 10.
    'A' is converted to 1 (for simplicity), but if the first hand is an ace
    and a 10-valued card, the player wins with a blackjack.
    """
    vals = [card.rank for card in hand]
    intvals = []
    while len(vals) > 0:
        value = vals.pop()
        try:
            intvals.append(int(value))
        except ValueError:
            if value in ['K', 'Q', 'J']:
                intvals.append(10)
            elif value == 'A':
                intvals.append(1)  # Keep it simple for the sake of example
    if intvals == [1, 10] or intvals == [10, 1]:
        print("   Blackjack!")
        return(21)
    else:
        points = sum(intvals)
        print("   Current score: {}".format(str(points)))
        return(points)

# def generate_deck() -> List[PokerCard]:
#     """
#     Function that generates the deck, instead of writing down 50 cards, we use iteration
#     to generate the cards for use
#
#     :return:    List with all 50 poker playing cards
#     :rtype:     List[PokerCard]
#     """
#     suits = ['Hearts', 'Diamonds', 'Clubs', 'Spades']
#     ranks = {'A': 'Ace',
#              '2': 'Two',
#              '3': 'Three',
#              '4': 'Four',
#              '5': 'Five',
#              '6': 'Six',
#              '7': 'Seven',
#              '8': 'Eight',
#              '9': 'Nine',
#              '10': 'Ten',
#              'J': 'Jack',
#              'Q': 'Queen',
#              'K': 'King'}
#     cards = []
#     for suit in suits:
#         for rank, name in ranks.items():
#             cards.append(PokerCard(suit, rank, name))
#     print('Generated deck of cards for the table.')
#     return cards

if __name__ == "__main__":
    game = BlackjackGame([Player("Kit"), Player("Anya"), Player("Iris"),
        Player("Simon")])
    game.blackjack()

@@ -4449,923 +4449,8 @@
 s)%0A%0A
-# def generate_deck() -%3E List%5BPokerCard%5D:%0A#     %22%22%22%0A#     Function that generates the deck, instead of writing down 50 cards, we use iteration%0A#     to generate the cards for use%0A#%0A#     :return:    List with all 50 poker playing cards%0A#     :rtype:     List%5BPokerCard%5D%0A#     %22%22%22%0A#     suits = %5B'Hearts', 'Diamonds', 'Clubs', 'Spades'%5D%0A#     ranks = %7B'A': 'Ace',%0A#              '2': 'Two',%0A#              '3': 'Three',%0A#              '4': 'Four',%0A#              '5': 'Five',%0A#              '6': 'Six',%0A#              '7': 'Seven',%0A#              '8': 'Eight',%0A#              '9': 'Nine',%0A#              '10': 'Ten',%0A#              'J': 'Jack',%0A#              'Q': 'Queen',%0A#              'K': 'King'%7D%0A#     cards = %5B%5D%0A#     for suit in suits:%0A#         for rank, name in ranks.items():%0A#             cards.append(PokerCard(suit, rank, name))%0A#     print('Generated deck of cards for the table.')%0A#     return cards%0A
 %0Aif 

Remove commented block.