thinkpad20

Untitled

Apr 4th, 2012
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. class Game(object):
  2.     def __init__(self, deck):
  3.         self.deck = deck
  4.         self.table = Table(deck)
  5.         print "%d cards in deck." % len(deck)
  6.     def playGame(self):
  7.         self.table.dealCards()
  8.         numMoves = 0
  9.         print self.table
  10.         while not self.table.isGameComplete():
  11.             print "Move from where to where?"
  12.             source = raw_input("Source:")
  13.             dest = raw_input("Destination:")
  14.             cardMoved = self.table.moveCard(source,dest)
  15.             while not cardMoved:#cardMoved will become true if the user has won the game.
  16.                 print "Illegal move. Move from where to where?"
  17.                 source = raw_input("Source:")
  18.                 dest = raw_input("Destination:")
  19.             numMoves += 1
  20.         print "Congrats! It took you %d moves to solve this game." % numMoves
  21.  
  22.  
  23. class Table(object):
  24.     """The table where FreeCell is played. Consists of 8 CardStacks,
  25.       4 FreeCells and 4 BuildLists. Initialized with a deck of cards."""
  26. #
  27. # truncated section includes declarations of internal variables (16 card stacks in total)
  28. #
  29.     def moveCard(self,source,dest):
  30.         cardMoved = False
  31.         card = source.getTopCard()
  32.         if dest.allowAdd(card):# card stack has a method that returns True if it's a legal move.
  33.             self.source.takeCard()
  34.             self.dest.addCard(card)
  35.             cardMoved = True
  36.         print self
  37.         if self.isGameComplete():# this will be true if the user has built up all 4 suits.
  38.             print "Game complete! Yay!"
  39.         return cardMoved
Advertisement
Add Comment
Please, Sign In to add comment