Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Ian Estabrook
- #estabro3
- #No extra credit features have been implemented. Sadly.
- import wx, random
- #Part 1: Basic Game Components
- class Card(object):
- def __init__(self,suit,rank):
- self.rank = rank
- self.suit = suit
- file_name = "cards_gif/"+self.suit
- if rank < 11:
- file_name+=str(rank)
- elif rank == 11:
- file_name+="j"
- elif rank == 12:
- file_name+="q"
- elif rank ==13:
- file_name+="k"
- file_name+=".gif"
- self.image = wx.Image(file_name,wx.BITMAP_TYPE_ANY).ConvertToBitmap()
- def __str__(self):
- suit = {"c":"Clubs","d":"Diamonds","h":"Hearts","s":"Spades"} #These are dictionaries containing the suits and ranks so we can index them later
- rank = {1:"Ace",2:"Two",3:"Three",4:"Four",5:"Five",6:"Six",7:"Seven",8:"Eight",9:"Nine",10:"Ten",11:"Jack",12:"Queen",13:"King"}
- mysuit = suit[self.suit] #Index to find the card's suit
- myrank = rank[self.rank] #Index to find the card's rank
- return myrank + " of "+mysuit
- def get_img(self):
- return self.image #Return the image attribute of the card, which should have something to do with intializing the image in the __init__ constructor
- def __cmp__(self,other):
- return self.rank-other.rank #For use in the is_straight comparison
- class Pile(object):
- def __init__(self):
- self.cards = []
- def add_card(self,card):
- self.cards.append(card)
- def draw_card(self):
- return self.cards.pop()
- def top_card(self):
- return self.cards[-1]
- def shuffle(self):
- random.shuffle(self.cards)
- class Deck(Pile):
- def __init__(self):
- super(Deck,self).__init__()
- self.suit = ["c","d","h","s"]
- for suit in self.suit:
- for rank in range(1,14):
- card = Card(suit,rank) #Create a card
- self.add_card(card) #Add that card to the deck
- self.shuffle() #Shuffle the deck
- #Part 2: Scoring Poker Hands
- class Hand(Pile):
- def is_flush(self):
- first_suit = self.cards[0].suit #The first suit is the first card
- for card in self.cards: #For a card in the pile/deck
- if (card.suit != first_suit): #If the card doesn't match the suit
- return False #Nope
- return True #Yay!
- def is_straight(self): #Cards next to each other have to differ by 1
- self.cards.sort() #Sort the cards in numeric order
- for i in range(0,4): #For a card in a hand of 5
- if (self.cards[i+1].rank-self.cards[i].rank!=1): #Check to see if the next one is only 1 greater
- return False #Lamesauce - it's not
- return True #w00t - it is!
- def score(self):
- histogram = {}
- for card in self.cards:
- if card.rank not in histogram:
- histogram[card.rank] = 0
- histogram[card.rank]+=1 #Each entry, or key value pair, represents a rank:how many times it's appeared
- if len(histogram) == 2:
- for rank in histogram:
- if histogram[rank] == 3:
- return 10
- return "Full house!"
- if histogram[rank] == 4:
- return 16
- return "Four of a kind!"
- if len(histogram) == 3:
- for rank in histogram:
- if histogram[rank] == 3:
- return 6
- return "Three of a kind!"
- if histogram[rank] == 2:
- return 3
- return "Two pair!"
- if len(histogram) == 4:
- for rank in histogram:
- return 1
- return "One pair!"
- if len(histogram) == 5:
- if self.is_straight() == True and self.is_flush() == True and self.cards[0] == 1 and self.cards[1] == 10:
- return 50
- if self.is_flush() == True and self.is_straight() == True:
- return 30
- return "Straight flush!"
- return "Royal flush!"
- if self.is_flush() == True:
- return 5
- return "Flush!"
- if self.is_straight() == True:
- return 12
- return "Straight!"
- return 0
- return "No pair."
- if len(histogram) != 5:
- raise ValueError("You don't have five cards in your hand! Draw moar!")
- #Part 3: Basic Interface Objects
- class DeckButton(wx.BitmapButton): #creates a class that inherits from bitmap button
- def __init__(self,parent): #wx widgets always have a parent argument, where the wx widget lives
- super(DeckButton,self).__init__(parent, -1)
- self.deck = Deck() #Build the deck
- top = self.deck.top_card() #Build a card
- self.SetBitmapLabel(top.get_img()) #Set the image of the card to what gets displayed on the button
- def draw_card(self):
- result = self.deck.draw_card()
- top = self.deck.top_card()
- self.SetBitmapLabel(top.get_img())
- return result
- def restart(self):
- self.deck = Deck()
- top = self.deck.top_card()
- self.SetBitmapLabel(top.get_img())
- class CellButton(wx.BitmapButton):
- def __init__(self,parent):
- super(CellButton,self).__init__(parent)
- back = wx.Image("cards_gif/b1fv.gif",wx.BITMAP_TYPE_ANY).ConvertToBitmap()
- self.SetBitmapLabel(back)
- self.occupied = False #Does the cell contain a card?
- self.card = None #What card occupies the cell?
- def restart(self):
- self.card = None
- self.occupied = False
- back = wx.Image("cards_gif/b1fv.gif",wx.BITMAP_TYPE_ANY).ConvertToBitmap()
- self.SetBitmapLabel(back)
- def add_card(self,card):
- self.card = card
- self.occupied = True
- self.SetBitmapLabel(card.get_img())
- #After the deck and cell button work, we can get the __init__() function for the game going
- #Part 4: Game Interface
- class Game(wx.Frame):
- def __init__(self,parent,id):
- super(Game,self).__init__(parent,id,"Poker Solitare")
- Deck_Panel = wx.Panel(self,-1)
- self.Deck = DeckButton(Deck_Panel)
- Deck_Sizer = wx.BoxSizer(wx.HORIZONTAL)
- Deck_Sizer.Add(self.Deck)
- Deck_Panel.SetSizer(Deck_Sizer)
- self.status=self.CreateStatusBar()
- Cell_Panel = wx.Panel(self,-1)
- Grid_Sizer = wx.GridSizer(5,5,3,3)
- self.Cells = []
- for i in range(5):
- row_list = []
- for j in range(5):
- cell = CellButton(Cell_Panel)
- Grid_Sizer.Add(cell)
- self.Bind(wx.EVT_BUTTON,self.place_card,cell)
- row_list.append(cell)
- self.Cells.append(row_list)
- Cell_Panel.SetSizer(Grid_Sizer)
- Sizer=wx.BoxSizer(wx.HORIZONTAL)
- Sizer.Add(Deck_Panel)
- Sizer.Add(Cell_Panel)
- self.SetSizer(Sizer)
- self.Fit()
- #The code in here vvv interacts with self.Bind(wx.EVT_BUTTON...) which is why we reference it
- def check_state(self):
- score = 0
- for i in range(5):
- score+=self.score_col(i)
- score+=self.score_row(i)
- self.status.SetStatusText("Score"+" "+ str(score))
- if self.check_end() == True:
- output="Game over, man. GAME OVER."
- message=wx.MessageDialog(self,output,"Message for you, sir!",wx.OK)
- message.ShowModal()
- message.Destroy()
- self.restart()
- def place_card(self,evt):
- for i in range(5):
- for j in range(5):
- cell = self.Cells[i][j] #Row i, column j
- if evt.GetId() == cell.GetId() and cell.occupied == False:
- card = self.Deck.draw_card()
- cell.add_card(card)
- self.check_state()
- def score_col(self,col_num):
- score = 0
- hand = Hand()
- for i in range(5):
- card = self.Cells[i][col_num].card #This gives me the 5 cards for that column number
- hand.add_card(card) #This adds a card to my hand
- if self.Cells[i][col_num].occupied == False:
- return 0
- return hand.score()
- def score_row(self,row_num):
- score = 0
- hand = Hand()
- for i in range(5):
- card = self.Cells[row_num][i].card #This gives me the 5 cards for that column number
- hand.add_card(card) #This adds a card to my hand
- if self.Cells[row_num][i].occupied == False:
- return 0
- return hand.score()
- def check_end(self):
- for i in range(5):
- for j in range(5):
- cell = self.Cells[i][j]
- if cell.occupied == False:
- return False
- return True
- def restart(self):
- self.Deck.restart()
- for i in range(5):
- for j in range(5):
- self.Cells[i][j].restart()
- #Two panels will go inside our game class (which is a frame)
- #Gridsizer for cells panel, Boxsizer for the deck, and another Boxsizer for the whole thing
- #So three panels total
- #Test the game with the code below
- if __name__=="__main__":
- app=wx.App()
- game=Game(None,-1)
- game.Show()
- app.MainLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement