from random import *

def create_deck():
    deck = []
    ranklist = ["2","3","4","5","6","7","8","9","10","J","Q","K","A"]
    suitlist = ["clubs", "diamonds", "hearts" , "spades"]
    for suit in range(0,4):
        for rank in range (0,13):
            card = []
            card.append(ranklist[rank])
            card.append(suitlist[suit])
            deck.append(card)
    return deck

def get_rank(card):
    ranklist = ["2", ... ,"A"]
    return ranklist.index(card[0])

def get_suit(card):
    suitlist = ["clubs", ... , "spades"]
    return suitlist.index(card[1])

def pick_card(deck):
    card_number = randint(0,51)
    return deck[card_number]

# Example 1: shuffling a deck and dealing a hand of 5 cards

def permute(deck):
    for i in range(0,len(deck)-1):
        r = randint(i,len(deck)-1)
        deck[i],deck[r] = deck[r], deck[i]
    return deck

def test_permute():
    deck = create_deck()
    hand = permute(deck)[0:5]
    print(hand)

# Example 2: checking a hand for a "flush"

def all_spades(hand):
    for i in range(0,len(hand)):
        if (hand[i].get_suit() != 3):
            return False
    return True

def all_hearts(hand):
    for i in range(0,len(hand)):
        if (hand[i].get_suit() != 2):
            return False
    return True

def all_diamonds(hand):
    for i in range(0,len(hand)):
        if (hand[i].get_suit() != 1):
            return False
    return True

def all_clubs(hand):
    for i in range(0,len(hand)):
        if (hand[i].get_suit() != 0):
            return False
    return True

def flush(hand):
    if all_spades(hand):
        return True
    if all_hearts(hand):
        return True
    if all_diamonds(hand):
        return True
    if all_clubs(hand):
        return True
    return False

def test_flush():
    deck = create_deck()
    hand = permute(deck)[0:5]
    print(hand)
    if flush(hand):
        print("You have a flush!")
    else:
        print("You don't have a flush!")

