Demo entry 6311837

py

   

Submitted by anonymous on Oct 25, 2016 at 01:09
Language: Python 3. Code size: 6.8 kB.

 # Course:  IT1 1120
 # Assignment 3
 # Wang, Jianquan
 # 8870206
# In this implementation a card (that is not a 10) is represented
# by a 2 character string, where the 1st character represents a rank and the 2nd a suit.
# Each card of rank 10 is represented as a 3 character string, first two are the rank and the 3rd is a suit.

import random

def wait_for_player():
    '''()->None
    Pauses the program until the user presses enter
    '''
    try:
         input("\nPress enter to continue. ")
         print()
    except SyntaxError:
         pass


def make_deck():
    '''()->list of str
        Returns a list of strings representing the playing deck,
        with one queen missing.
    '''
    deck=[]
    suits = ['\u2660', '\u2661', '\u2662', '\u2663']
    ranks = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
    for suit in suits:
        for rank in ranks:
            deck.append(rank+suit)
    deck.remove('Q\u2663') # remove a queen as the game requires
    return deck

def shuffle_deck(deck):
    '''(list of str)->None
       Shuffles the given list of strings representing the playing deck    
    '''
    random.shuffle(deck)

#####################################

def deal_cards(deck):
    
    '''(list of str)-> tuple of (list of str,list of str)

    Returns two lists representing two decks that are obtained
    after the dealer deals the cards from the given deck.
    The first list represents dealer's i.e. computer's deck
    and the second represents the other player's i.e user's list.
    '''
    dealer=[]
    other=[]

    # COMPLETE THE BODY OF THIS FUNCTION ACCORDING TO THE DESCRIPTION ABOVE
    # YOUR CODE GOES HERE

    for i in range(len(deck)):
        if i<=24:
            dealer.append(deck[i])
        else:
            other.append(deck[i])
    
        
    return (dealer, other)
 


def remove_pairs(l):
    '''
     (list of str)->list of str

     Returns a copy of list l where all the pairs from l are removed AND
     the elements of the new list shuffled

     Precondition: elements of l are cards represented as strings described above

     Testing:
     Note that for the individual calls below, the function should
     return the displayed list but not necessarily in the order given in the examples.

     >>> remove_pairs(['9♠', '5♠', 'K♢', 'A♣', 'K♣', 'K♡', '2♠', 'Q♠', 'K♠', 'Q♢', 'J♠', 'A♡', '4♣', '5♣', '7♡', 'A♠', '10♣', 'Q♡', '8♡', '9♢', '10♢', 'J♡', '10♡', 'J♣', '3♡'])
     ['10♣', '2♠', '3♡', '4♣', '7♡', '8♡', 'A♣', 'J♣', 'Q♢']
     >>> remove_pairs(['10♣', '2♣', '5♢', '6♣', '9♣', 'A♢', '10♢'])
     ['2♣', '5♢', '6♣', '9♣', 'A♢']
    '''

    no_pairs=[]

    # COMPLETE THE BODY OF THIS FUNCTION ACCORDING TO THE DESCRIPTION ABOVE
    # YOUR CODE GOES HERE

    k=[]
    
    for i in range(len(l)):
        k.append(l[i][0])
    for i in range(len(k)):
        if k.count(k[i])==1:
            no_pairs.append(l[i])
        elif k.count(k[i])==3:
            no_pairs.append(l[i])
            k[i] = ''
    
    random.shuffle(no_pairs)
    return no_pairs

def print_deck(deck):
    '''
    (list)-None
    Prints elements of a given list deck separated by a space
    '''

    # COMPLETE THE BODY OF THIS FUNCTION ACCORDING TO THE DESCRIPTION ABOVE
    # YOUR CODE GOES HERE

    for i in range(len(deck)-1):
        print(deck[i],end=' ')
    print(deck[len(deck)-1])

    
def get_valid_input(n):
    '''
    (int)->int
    Returns an integer given by the user that is at least 1 and at most n.
    Keeps on asking for valid input as long as the user gives integer outside of the range [1,n]
     
    Precondition: n>=1
    '''

    # COMPLETE THE BODY OF THIS FUNCTION ACCORDING TO THE DESCRIPTION ABOVE
    # YOUR CODE GOES HERE
    print('I have '+str(n)+' cards. If 1 stands for my first card and\n'+str(n)+' for my last card, which of my cards would you like?')
    while(1):
        m=int(input('Give me an integer between 1 and '+str(n)+':'))
        if 1<=m<=n:
             return m
def play_game():
    '''()->None
    This function plays the game'''
    
    deck=make_deck()
    shuffle_deck(deck)
    tmp=deal_cards(deck)
    dealer=tmp[0]
    human=tmp[1]

    print("Hello. My name is Robot and I am the dealer.")
    print("Welcome to my card game!")
    print("Your current deck of cards is:")
    print_deck(human)
    print("Do not worry. I cannot see the order of your cards")

    print("Now discard all the pairs from your deck. I will do the same.")
    wait_for_player()

    dealer=remove_pairs(dealer)
    human=remove_pairs(human)

    # COMPLETE THE play_game function HERE
    # YOUR CODE GOES HERE

    roundNumber=1
    while(1):
        print('***********************************************************')
        if roundNumber % 2 != 0:
            print('Your turn.')
            print()
            
            print('Your current deck of cards is:')
            print()
            print(human)
            print()
            n=len(dealer)
            number=get_valid_input(n)
            if int(number)==1:
               print('You asked for my '+str(number)+'st card.')
            elif int(number)==2:
                print('You asked for my '+str(number)+'nd card.')
            elif int(number)==3:
                print('You asked for my '+str(number)+'rd card.')
            else:
                print('You asked for my '+str(number)+'th card.')
            print('Here it is. It is '+dealer[int(number)-1])
            print()
            human.append(dealer[int(number)-1])
            print('With '+dealer[int(number)-1]+' added, your current deck of cards is:')
            dealer.remove(dealer[int(number)-1])
            print()
            print(human)
            print()
            print('And after discarding pairs and shuffling, your deck is:')
            print()
            human=remove_pairs(human)
            print(human)

            wait_for_player()
            
        else:
            print('My turn.')
            print()
            n=random.randint(1,len(human))
            if n==1:
               print('I took your '+str(n)+'st card.')
            elif n==2:
                print('I took your '+str(n)+'nd card.')
            elif number==3:
                print('I took your '+str(n)+'rd card.')
            else:
                print('I took your '+str(n)+'th card.')
            dealer.append(human[n-1])
            human.remove(human[n-1])
            dealer=remove_pairs(dealer)
            wait_for_player()
        roundNumber += 1
        if len(human)==0:
            print('Ups. You do not have any more cards')
            print('Congratulations! You, Human, win')
            break
        elif len(dealer)==0:
            print('Ups. I do not have any more cards')
            print('You lost! I, Robot, win')
            break
	 

# main
play_game()

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).