Exemplo n.º 1
0
Arquivo: vegas.py Projeto: mjoscl/cs-i
def final_vp_check(number_of_cards, discard1, discard2, vp):
    """
    this function is called once the deck is exhausted. it examines the two
    top cards on each of the discard piles to see if they are the next in
    sequence for the victory pile. This action is repeated a number of times
    equal to twice the number of cards to ensure that both the discard1 and
    discard2 decisions have been evaluated as many times as needed
    :param number_of_cards:the number of cards the deck started with
    :param type: int
    :param discard1:this is the discard1 pile
    :param type: stack
    :param discard2:this is the discard2 pile
    :param type: stack
    :param vp:this is the victory pile
    :param type: stack
    """
    number_of_changes = 0
    for counter in range(2 * number_of_cards):
        if myStack.emptyStack(discard1) == False:
            if (myStack.top(discard1) == (myStack.top(vp) + 1)):
                number_of_changes += 1
                print("Moving", myStack.top(discard1), "to vp with card",
                      myStack.top(vp))
                myStack.push(vp, myStack.top(discard1))
                myStack.pop(discard1)
        if myStack.emptyStack(discard2) == False:
            if (myStack.top(discard2) == (myStack.top(vp) + 1)):
                number_of_changes += 1
                print("Moving", myStack.top(discard2), "to vp with card",
                      myStack.top(vp))
                myStack.push(vp, myStack.top(discard2))
                myStack.pop(discard2)
Exemplo n.º 2
0
def enqueue(queue, element):
    """
    This function modifies the queue and adds the element to the back (tail)
    of the queue by utilizing or altering oner or both of the internal stacks to
    ultimately 'enqueue' the queue. the enqueue is simulated by pushing an
    element to stack2
    :param queue: the queue via stacks class object
    :param type: queue
    :param element:the object value to add to the 'queue'
    :param type: object
    :return: the object that was added to the queue via stacks
    :rtype: object
    """
    if emptyQueue(queue):
        myStack.push(queue.stack1,element)
        myStack.push(queue.stack2,element)
    else:
        myStack.push(queue.stack2,element)
        temp_list=[]
        while not myStack.emptyStack(queue.stack1):
            myStack.pop(queue.stack1)
        while not myStack.emptyStack(queue.stack2):
            myStack.push(queue.stack1,myStack.top(queue.stack2))
            temp_list+=[myStack.top(queue.stack2)]
            myStack.pop(queue.stack2)
        for element in temp_list[::-1]:
            myStack.push(queue.stack2,element)
    queue.size += 1
    return element
Exemplo n.º 3
0
def front(queue):
    """
    this function takes the queue as a single argument and returns the
    equivalent first element to be removed, i.e., stack1 will be peeked
    :param queue: the queue via stacks class object being manipulated
    :param type: queue
    :return: the element that was a the front of the queue via stacks
    :rtype:object
    """
    if emptyQueue(queue):
        raise IndexError("queue is empty")
    return myStack.top(queue.stack1)
Exemplo n.º 4
0
def dequeue(queue):
    """
    This function modifies the queue and removes the element to the front (head)
    of the queue by utilizing or altering one or both of the internal stacks to
    ultimately 'dequeue' the queue. the dequeue is simulated by popping an
    element from stack1. the popped, or 'dequeue'd' element is returned
    :param queue:
    :param type: queue
    :return: the element removed from the queue via stacks
    :rtype: object
    """
    if emptyQueue(queue):
        raise IndexError("queue is empty")
    else:
        removed_element=myStack.top(queue.stack1)
        myStack.pop(queue.stack1)
        queue.size-=1
        temp_list=[]
        while not myStack.emptyStack(queue.stack2):
            temp_list+=[myStack.top(queue.stack2)]
            myStack.pop(queue.stack2)
        for element in temp_list[::-1]:
            if element != removed_element:
                myStack.push(queue.stack2,element)
Exemplo n.º 5
0
Arquivo: vegas.py Projeto: mjoscl/cs-i
def decide_card_destination(deck, discard1, discard2, vp):
    """
    this function takes in the shuffled deck as a queue, the two discard stacks,
    and the victory pile stack. the top card is removed the the decision process
    starts. the algorithm is as follows (- indicates level on decission tree):
    -Q: is this the next card on the victory pile?
    -A: card is the next for the victory pile
        -add the card on the victory pile (push to vp stack)
    -A: card is not the next for the victory pile
    --Q:which discard pile should this card go on?
    --A:if both discard piles are empty, push to discard1 stack
    --A:if discard pile 1 has cards, but discard pile 2 is empty, push to 2
    --A:if both discard piles have cards, decide which is best for card
    ---Q:which discard pile should card go on, since both have cards already?
    ---A:if the card value > than both discard pile top cards OR less than both
         discard pile top cards, push card to the discard pile with the
         smallest difference between it and the card
    ---A:if the card value is in between the values of the top card on the
         two discard piles, push the card to the discard stack that has the
         larger of the two numbers, allowing for the card with the smaller
         value to be accessible
    :param card: the value of the dealt card from the deck queue
    :param type: int
    :param discard1: the discard 1 pile
    :param type:stack
    :param discard2:the discard 2 pile
    :param type:stack
    :param vp: the victory pile
    :param type:stack
    """
    card = myQueue.front(deck)
    myQueue.dequeue(deck)
    if card == (vp.size + 1):
        myStack.push(vp, card)
    else:
        if discard1.size == 0 and discard2.size == 0:
            myStack.push(discard1, card)
        elif discard1.size > 0 and discard2.size == 0:
            myStack.push(discard2, card)
        elif discard1.size > 0 and discard2.size > 0:
            if (card > myStack.top(discard1) and card > myStack.top(discard2)
                ) or (card < myStack.top(discard1)
                      and card < myStack.top(discard2)):
                difference1 = abs(card - myStack.top(discard1))
                difference2 = abs(card - myStack.top(discard2))
                if (difference1 < difference2) or (difference1 == difference2):
                    myStack.push(discard1, card)
                elif difference1 > difference2:
                    myStack.push(discard2, card)
            elif (card > myStack.top(discard1) and card < myStack.top(discard2)
                  ) or (card < myStack.top(discard1)
                        and card > myStack.top(discard2)):
                if myStack.top(discard1) > myStack.top(discard2):
                    myStack.push(discard1, card)
                else:
                    myStack.push(discard2, card)