sexta-feira, maio 31, 2013

Advanced Python Class: "Blackjack"



Game Specification:


"Phase one


Download the program template for this mini-project and review the class definition for the Card class. This class is already implemented so your task is to familiarize yourself with the code. Start by pasting the Card class definition into the provided testing template and verifying that our implementation works as expected.

Implement the methods __init__, __str__, add_card for the Hand class. We suggest modeling a hand as a list of cards.

For help in implementing the __str__ method for hands, refer back to practice exercise number four from last week.

Remember to use the string method for cards to convert each card object into a string. Once you have implemented the Hand class, test it using the provided testing template.

Implement the methods for the Deck class listed in the mini-project template. We suggest modeling a deck of cards as list of cards. You can generate this list using a pair of nested for loops or a list comprehension.

Remember to use the Card initializer to create your cards.

Use random.shuffle() to shuffle this deck of cards. Once you have implemented the Deck class, test your Deck class using the provided testing template.

mplement the handler for a "Deal" button that shuffles the deck and deals the two cards to both the dealer and the player. The event handler deal for this button should shuffle the deck (stored as a global variable), create new player and dealer hands (stored as global variables), and add two cards to each hand. To transfer a card from the deck to a hand, you should use the deal_card method of the Deck class and the add_card method of Hand class in combination.The resulting hands should be printed to the console with an appropriate message indicating which hand is which.

Implement the get_value method for the Hand class. You should use the provided VALUE dictionary to look up the value of a single card in conjunction with the logic explained in the video lecture for this project to compute the value of a hand.

Once you have implemented the get_value method, test it using the provided testing template .

Remember that the deck is randomized after shuffling, so the output of the testing template should match the output in the comments in form but not in exact value.

Implement the handler for a "Hit" button. If the value of the hand is less than or equal to 21, clicking this button adds an extra card to player's hand. If the value exceeds 21 after being hit, print "You have busted".

Implement the handler for a "Stand" button. If the player has busted, remind the player that they have busted. Otherwise, repeatedly hit the dealer until his hand has value 17 or more (using a while loop). If the dealer busts, let the player know.

Otherwise, compare the value of the player's and dealer's hands.

If the value of the player's hand is less than or equal to the dealer's hand, the dealer wins. Otherwise the player has won.

Remember the dealer wins ties in our version.

With this design, the player needs to explicitly press "Deal" to start a new deal. This choice will make using the canvas to build an image-based version of Blackjack easier. At this point, we would suggest testing your implementation of Blackjack extensively.

Phase two


In the second phase of your implementation, you will add five features:

Implement your own draw method for the Hand class using the draw method of the Card class. We suggest drawing a hand as a horizontal sequence of cards where the parameter pos is the position of the upper left corner of the leftmost card. To simplify your code, you may assume that only the first five cards of a player's hand need to be visible on the canvas.

Replace printing in the console by drawing text messages on the canvas. We suggest adding a global outcome string that is drawn in the draw handler using draw_text. These messages should prompt the player to take some require action and have a form similar to "Hit or stand?" and "New deal?".

Also, draw the title of the game, "Blackjack", somewhere on the canvas.

Add logic using the global variable in_play that keeps track of whether the player's hand is still being played. If the round is still in play, you should draw an image of the back of a card (provided in the template) over the dealer's first (hole) card to hide it. Once the round is over, the dealer's
hole card should be displayed.

Add a score counter that keeps track of wins and losses for your Blackjack session. In the simplest case (see our demo), the program displays wins minus losses. However, you are welcome to implement a more sophisticated betting/scoring system.

Modify the logic for the "Deal" button to create and shufflea new deck (or restock and shuffle an existing deck) each time the "Deal" button is clicked. This change avoids the situation where the deck becomes empty during play.

Finally, modify the deal function such that, if the "Deal" button is clicked during the middle of a round, the program reports that the player lost the round and updates the score
appropriately.
"

Theoretical Approach:


The most important stuff regarding this game's engine, was the way I had to internalize the card tiles. My own theoretical approach was (taken from my class notes):


This card structure was represented by the following 3 classes ("card", "hand" and "deck"):

# define card class
class Card:
    def __init__(self, suit, rank):
        if (suit in SUITS) and (rank in RANKS):
            self.suit = suit
            self.rank = rank
        else:
            self.suit = None
            self.rank = None
            print "Invalid card: ", suit, rank

    def __str__(self):
        return self.suit + self.rank

    def get_suit(self):
        return self.suit

    def get_rank(self):
        return self.rank

    def draw(self, canvas, pos):
        card_loc = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(self.rank), 
                    CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(self.suit))
        canvas.draw_image(card_images, card_loc, CARD_SIZE, [pos[0] + CARD_CENTER[0], pos[1] + CARD_CENTER[1]], CARD_SIZE)
        
# define hand class
class Hand:
    def __init__(self):
        self.hand = []
        self.rank1 = []
        self.suit1 = []
        self.position = []
        self.hundred = 0
        self.count = -1

    def __str__(self):
        s = ""
        i = 0
        while i in range(0, len(self.hand)):
            s += (str(self.hand[i]) + " ")
            i += 1
        return s

    def add_card(self, card):
        self.hundred += 100
        self.position.append(self.hundred)
        self.rank1.append(card.get_rank())
        self.suit1.append(card.get_suit())
        self.hand.append(card.get_suit() + card.get_rank())

    # count aces as 1, if the hand has an ace, then add 10 to hand value if it doesn't bust
    def get_value(self):
        self.value = 0
        for i in range(0, len(self.rank1)):
            if (self.rank1[i] == 'A' and self.value <= 10):
                self.value += 11
            else:
                self.value += VALUES.get(self.rank1[i])
        return self.value

    def draw(self, canvas, p, r, s):
        card_location = (CARD_CENTER[0] + CARD_SIZE[0] * RANKS.index(r), 
                    CARD_CENTER[1] + CARD_SIZE[1] * SUITS.index(s))
        canvas.draw_image(card_images, card_location, CARD_SIZE, [p[0] + CARD_CENTER[0], p[1] + CARD_CENTER[1]], CARD_SIZE)  
    
    def drawback(self, canvas, p):
        card_location1 = (CARD_BACK_CENTER[0], CARD_BACK_CENTER[1])
        canvas.draw_image(card_back, card_location1, CARD_BACK_SIZE, [p[0] + CARD_BACK_CENTER[0], p[1] + CARD_BACK_CENTER[1]], CARD_BACK_SIZE)


# define deck class
class Deck:
    def __init__(self):
        self.choice = -1
        del self.deck
        self.deck = []
        for i in range(1, 14):
            j = str(i)
            if (j == '1'):
                self.deck.append(Card('C', 'A').get_suit() + " " + Card('C', 'A').get_rank())
                self.deck.append(Card('S', 'A').get_suit() + " " + Card('C', 'A').get_rank())
                self.deck.append(Card('H', 'A').get_suit() + " " + Card('C', 'A').get_rank())
                self.deck.append(Card('D', 'A').get_suit() + " " + Card('C', 'A').get_rank())
            elif (j == '10'):
                self.deck.append(Card('C', 'T').get_suit() + " " + Card('C', 'T').get_rank())
                self.deck.append(Card('S', 'T').get_suit() + " " + Card('C', 'T').get_rank())
                self.deck.append(Card('H', 'T').get_suit() + " " + Card('C', 'T').get_rank())
                self.deck.append(Card('D', 'T').get_suit() + " " + Card('C', 'T').get_rank())
            elif (j == '11'):
                self.deck.append(Card('C', 'J').get_suit() + " " + Card('C', 'J').get_rank())
                self.deck.append(Card('S', 'J').get_suit() + " " + Card('C', 'J').get_rank())
                self.deck.append(Card('H', 'J').get_suit() + " " + Card('C', 'J').get_rank())
                self.deck.append(Card('D', 'J').get_suit() + " " + Card('C', 'J').get_rank())
            elif (j == '12'):
                self.deck.append(Card('C', 'Q').get_suit() + " " + Card('C', 'Q').get_rank())
                self.deck.append(Card('S', 'Q').get_suit() + " " + Card('C', 'Q').get_rank())
                self.deck.append(Card('H', 'Q').get_suit() + " " + Card('C', 'Q').get_rank())
                self.deck.append(Card('D', 'Q').get_suit() + " " + Card('C', 'Q').get_rank())
            elif (j == '13'):
                self.deck.append(Card('C', 'K').get_suit() + " " + Card('C', 'K').get_rank())
                self.deck.append(Card('S', 'K').get_suit() + " " + Card('C', 'K').get_rank())
                self.deck.append(Card('H', 'K').get_suit() + " " + Card('C', 'K').get_rank())
                self.deck.append(Card('D', 'K').get_suit() + " " + Card('C', 'K').get_rank())
            else:
                self.deck.append(Card('C', j).get_suit() + " " + Card('C', j).get_rank())
                self.deck.append(Card('S', j).get_suit() + " " + Card('C', j).get_rank())
                self.deck.append(Card('H', j).get_suit() + " " + Card('C', j).get_rank())
                self.deck.append(Card('D', j).get_suit() + 

The rest of the Python program I implemented is your run-of-the-mill source code, i.e., nothing new under the sun ...

NB: The source code is available here. This link can also be used to play the game.

"The Massada Complex" by Avraham Azrieli

The Masada Complex - Avraham AzrieliBeach reading of the worst kind...

Outrageous conspiracies abound in this Zionist story with its two-dimensional characters. Full of implausibilities. Enough said!

segunda-feira, maio 27, 2013

"Fifty Grand" by Adrian McKinty

Fifty Grand - Adrian McKinty
McKinty is on a roll! No misstep so far!

The opening chapter of the book is antological.

The story is a strong one, with wonderful characterization told through a first person narrative by Detective Mercado. Supporting the plot is a brisk pace that sears at the opening, then pulls back, hammers the reader again, pulls back once more, slows down, and then the breathtaking Grand Finale.

McKinty creates a Havana, unlike any I've read.

The plot is relatively simple but the characters, the setting, and the tension all make it succeed and feel original, and the Prose, the Prose Dear Readers. That's what really sets this book apart in my mind (as the previous reviewed books). 

McKinty belongs to that rare breed of Writers that can write Prose as if it were Poetry. He's really a Poet of the Prose.

quinta-feira, maio 23, 2013

Advanced Python Class: "Memory Game"




Mini-project description - Memory
Memory is a card game in which the player deals out a set of cards face down. In Memory, a turn (or a move) consists of the player flipping over two cards. If they match, the player leaves them face up. If they don't match, the player flips the cards back face down. The goal of Memory is to end up with all of the cards flipped face up in the minimum number of turns. For this project, we will keep our model for Memory fairly simple. A Memory deck consists of eight pairs of matching cards.
Mini-project development process
1.    Model the deck of cards used in Memory as a list consisting of 16 numbers with each number lying in the range [0,8) and appearing twice. We suggest that you create this list by concatenating two list with range [0,8) together. Use the Docs to locate the list concatenation operator.
2.    Write a draw handler that iterates through the Memory deck using a for loop and uses draw_text to draw the number associated with each card on the canvas. The result should be a horizontal sequence of evenly-spaced numbers drawn on the canvas.
3.    Shuffle the deck using random.shuffle(). Remember to debug your canvas drawing code before shuffling to make debugging easier.
4.    Next, modify the draw handler to either draw a blank green rectangle or the card's value. To implement this behavior, we suggest that you create a second list called exposed. In the exposed list, the ith entry should be True if the ith card is face up and its value is visible or False if the ithcard is face down and it's value is hidden. We suggest that you initialize exposed to some known values while testing your drawing code with this modification.
5.    Now, add functionality to determine which card you have clicked on with your mouse. Add an event handler for mouse clicks that takes the position of the mouse click and prints the index of the card that you have clicked on to the console. To make determining which card you have clicked on easy, we suggest sizing the canvas so that the sequence of cards entirely fills the canvas.
6.    Modify the event handler for mouse clicks to flip cards based on the location of the mouse click. If the player clicked on the ith card, you can change the value of exposed[i] from False to True. If the card is already exposed, you should ignore the mouseclick. At this point, the basic infrastructure for Memory is done.
7.    You now need to add game logic to the mouse click handler for selecting two cards and determining if they match. We suggest following the game logic discussed in the class. State 0 corresponds to the start of the game. In state 0, if you click on a card, that card is exposed, and you switch to state 1. State 1 corresponds to a single exposed unpaired card. In state 1, if you click on an unexposed card, that card is exposed and you switch to state 2. State 2 corresponds to the end of a turn. In state 2, if you click on an unexposed card, that card is exposed and you switch to state 1.
8.    Note that in state 2, you also have to determine if the previous two cards are paired or unpaired. If they are unpaired, you have to flip them back over so that they are hidden before moving to state 1. We suggest that you use two global variables to store the index of each of the two cards that were clicked in the previous turn.
9.    Add a counter that keeps track of the number of turns and uses set_text to update this counter as a label in the control panel. (BTW, Joe's record is 12 turns.)  This counter should be incremented after either the first or second card is flipped during a turn.
10.  Finally, implement the init() function (if you have not already) so that the "Reset" button reshuffles the cards, resets the turn counter and restarts the game. All cards should start the game hidden.
11.  (Optional) You may replace the draw_text for each card by a draw_image that uses one of eight different images.

Once the run button is clicked in CodeSkulptor, the game should start. You should not have to hit the "Reset" button to start. Once the game is over, you should hit the "Reset" button to restart the game. 


While this project may seem daunting at first glance, our full implementation took well under 100 lines with comments and spaces. If you feel a little bit intimidated, focus on developing your project to step six. Our experience is that, at this point, you will begin to see your game come together and the going will get much easier.
Grading Rubric - 11 pts total (scaled to 100)
·         1 pt - The game correctly draws 16 cards on the canvas (horizontally or as a grid). Using images in place of textual numbers is fine. However, it is the submitter's responsibility to ensure that custom images load during peer assessment.
·         1 pt - The cards appear in eight unique pairs.
·         1 pt - The game ignores clicks on exposed cards.
·         1 pt - At the start of the game, a click on a card exposes the card that was clicked on.
·      1 pt - If one upaired card is exposed, a click on a second unexposed card exposes the card that was clicked on.
·        1 pt - If two unpaired cards are exposed, a click on an unexposed card exposes the card that was clicked on and flips the two unpaired cards over.
·     1 pt - If all exposed cards are paired, a click on an unexposed card exposes the card that was clicked on and does not flip any other cards.
·         1 pt - Cards paired by two clicks in the same turn remain exposed until the start of the next game.
·       1 pt - The game correctly updates and displays the number of turns in the current game in a label displayed in the control area. The counter may be incremented after either the first or second card is flipped during a turn.
·         1 pt - The game includes a "Reset" button that resets the turn counter and restarts the game.
·         1 pt - The deck is also randomly shuffled each time the "Reset" button is pressed, so that the cards are in a different order each game.


# 5th Class Mini-Project - Memory game
# Manuel Augusto Antao - due on Sun 26 May 6:00 am
# Delivered: 23.05.2013
# Mini-project specification:

import simplegui
import random

#Global variables
WIDTH = 800
#HEIGHT = 100
HEIGHT = 110
CARD_WIDTH = 50
IDENT_1 = 2
IDENT_2 = 20
moves = 0
deck = []
last_try = []
exposed_count = 0
guessed_pairs = 0
Vencedor = ""


# helper function to initialize globals
def init():
    Vencedor.set_text("")
    global deck, moves, last_try, exposed_count, guessed_pairs
    moves = 0
    last_try = []
    exposed_count = 0
    guessed_pairs = 0
    deck = []
    numbers = [x % 8 for x in range(16)]
    random.shuffle(numbers)
    for i in range(16):
        deck.append([CARD_WIDTH * i, numbers[i], False, False])
           
#helper function to flip all unguessed cards face down
def all_down():
    for card in deck:
        if not card[3]:
            card[2] = False

# define event handlers
def mouseclick(pos):
    global exposed_count, last_try, moves, guessed_pairs
    conta = 0
    x, y = pos
    if exposed_count == 0:
        for card in deck:
            if 0 < (x - card[0]) < CARD_WIDTH:
                 if not card[2]:
                    card[2] = True
                    last_try = card
                    exposed_count = 1
                    moves += 1
                    #if moves >= 5:
                        #print "You're a looser"
                    #    Vencedor.set_text("You're a looser")
                        
    elif exposed_count == 1:
        for card in deck:
            if 0 < (x - card[0]) < CARD_WIDTH:
                 if not card[2]:
                    card[2] = True
                    exposed_count = 2
                    if last_try[1] == card[1]:
                        last_try[3] = True
                        card[3] = True
                        guessed_pairs += 1
                        Vencedor.set_text("Guessed Pairs = "+str(guessed_pairs))
                    moves += 1
                    
    else:
        Vencedor.set_text("Keep Guessing!")
        for card in deck:
            if 0 < (x - card[0]) < CARD_WIDTH:
                 if not card[2]:
                    all_down()
                    card[2] = True
                    last_try = card
                    exposed_count = 1
                    moves += 1
                    #if moves >= 5:
                        #print "looser"
                        #Vencedor.set_text("You're a looser")
    #conta = c + 1
    #if moves > 28:
    #   Vencedor.set_text ("Memoria de cordel...")
                           
# cards are logically 50x100 pixels in size
def draw(canvas):
   
    for card in deck:
        l.set_text("Moves = " + str(moves // 2))
        if card[2]:
            canvas.draw_text(str(card[1]),
                         (card[0] + IDENT_1, HEIGHT - IDENT_2),
                         70, "White")
        else:
            canvas.draw_polygon([(card[0], 0),
                                (card[0] + CARD_WIDTH, 0),
                                (card[0] + CARD_WIDTH, HEIGHT),(card[0], HEIGHT)], 1, "White", "Maroon")
                             
# create frame and add a button and labels
frame = simplegui.create_frame("Memory Game @Manuel Augusto Antao - 26.05.2013", WIDTH, HEIGHT)
frame.add_button("Restart", init)
l=frame.add_label("Moves = 0")
Vencedor = frame.add_label("")

# initialize global variables
init()

# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)


# get things rolling
frame.start()


# Always remember to review the grading rubric