Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Game(object):
- def __init__(self, deck):
- self.deck = deck
- self.table = Table(deck)
- print "%d cards in deck." % len(deck)
- def playGame(self):
- self.table.dealCards()
- numMoves = 0
- print self.table
- while not self.table.isGameComplete():
- print "Move from where to where?"
- source = raw_input("Source:")
- dest = raw_input("Destination:")
- cardMoved = self.table.moveCard(source,dest)
- while not cardMoved:#cardMoved will become true if the user has won the game.
- print "Illegal move. Move from where to where?"
- source = raw_input("Source:")
- dest = raw_input("Destination:")
- numMoves += 1
- print "Congrats! It took you %d moves to solve this game." % numMoves
- class Table(object):
- """The table where FreeCell is played. Consists of 8 CardStacks,
- 4 FreeCells and 4 BuildLists. Initialized with a deck of cards."""
- #
- # truncated section includes declarations of internal variables (16 card stacks in total)
- #
- def moveCard(self,source,dest):
- cardMoved = False
- card = source.getTopCard()
- if dest.allowAdd(card):# card stack has a method that returns True if it's a legal move.
- self.source.takeCard()
- self.dest.addCard(card)
- cardMoved = True
- print self
- if self.isGameComplete():# this will be true if the user has built up all 4 suits.
- print "Game complete! Yay!"
- return cardMoved
Advertisement
Add Comment
Please, Sign In to add comment