# Jared Wasserman
# 6/4/2013
# This class stores a group of playing cards
# Python translation of Arup's Java code

from Card import Card
from random import randrange

class CardList: 

    TOTALCARDS = 52

    # Empty Card List, capable of storing up tp 52 cards.
    def __init__(self):
        self.my_cards = [-1] * CardList.TOTALCARDS
        self.num_cards = 0
    

    # Adds c as the last card in the CardList.
    def add_card(self, c): 
        self.my_cards[self.num_cards] = c
        self.num_cards += 1
    

    # Pre-condition: 0 <= index < num_cards for this object.
    # Removes a card from the index slot and returns it.
    def remove_card(self, index=None):
        # By default removes last card in deck
        if (None == index):
            index = self.num_cards - 1
            
        c = self.my_cards[index]
        self.my_cards[index] = self.my_cards[self.num_cards - 1]
        self.num_cards -= 1
        return c
    

    # Shuffles this collections of cards in random order.
    def shuffle(self): 
        # Repeatedly swap randomly chosen cards.
        for i in range(200):
            place1 = randrange(0, self.num_cards)
            place2 = randrange(0, self.num_cards)
    
            temp = self.my_cards[place1]
            self.my_cards[place1] = self.my_cards[place2]
            self.my_cards[place2] = temp
        
    
    # Returns the number of cards in this list that match kind.
    def num_matches(self, kind):
        # Go through each card.
        cnt = 0
        for i in range(self.num_cards):
            if (self.my_cards[i].same_kind(kind)):
                cnt += 1

        # Return the number of matches.
        return cnt
    

    # Create the a full deck of cards in this CardList.
    def make_deck(self): 
        for i in range(4):
            for j in range(13):
                self.my_cards[13 * i + j] = Card(i, j)

        self.num_cards = CardList.TOTALCARDS
    

    # Returns a string representation of this CardList.
    def __str__(self):
        ans = ""
        for i in range(self.num_cards):
            ans = ans + str(i + 1) + ". " + str(self.my_cards[i]) + "\n"

        return ans
    
