Matsch

For cherry_man1

Oct 3rd, 2014
27
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.33 KB | None | 0 0
  1. # Notes - for cherry_man1
  2. #
  3. # I'm making an assumption based on your code that you're trying to do a card marching game like Memory?
  4. #
  5. # Your classes are doing too much. Instead of instantiating the cards in the deck when you create Deck, use a factory and pass in the cards. This way your code is more flexible.
  6. # General rule of thumb, instantiate what you need and then pass it in to where you need to use it. You don't want to bind the creation and use of the object to the same class.
  7. # I've made some changes to your original code. There are a ton more things you could do here to improve the code but I'll let you figure some of those out.
  8.  
  9. import random
  10.  
  11.  
  12. class Card(object):
  13.     def __init__(self, value, face=False):
  14.         self._value = value
  15.         self._face = face
  16.  
  17.     def getValue(self):
  18.         return self._value
  19.  
  20.     def getFace(self):
  21.         return self._face
  22.  
  23.     def setFace(self, face):
  24.         self._face = face
  25.  
  26.     def flip(self):
  27.         self._face = (self._face is not True)
  28.  
  29.  
  30. class Deck(object):
  31.     _cards = []
  32.  
  33.     def __init__(self, cards):
  34.         self._cards = cards
  35.  
  36.     def shuffle(self):
  37.         # Shuffles the cards
  38.         random.shuffle(self._cards)
  39.  
  40.     def deal(self):
  41.         if len(self) == 0:
  42.             return None
  43.         else:
  44.             return self._cards.pop(0)
  45.  
  46.     def __getitem__(self, item):
  47.         return self._cards[item]
  48.  
  49.     def __len__(self):
  50.         return len(self._cards)
  51.  
  52.  
  53. class Game(object):
  54.     def __init__(self, board, deck):
  55.         self._board = board
  56.         self._deck = deck
  57.            
  58.     def populateBoard(self):
  59.         self._deck.shuffle()
  60.         for row in range(len(self._board)):
  61.             for column in range(len(self._board[row])):
  62.                 self._board[row][column] = self._deck.deal()
  63.        
  64.     def displayBoard(self):
  65.         for row in range(len(self._board)):
  66.             for column in range(len(self._board[row])):
  67.                 if self._board[row][column].getFace() is False:
  68.                     print('*')
  69.                 else:
  70.                     print(self._board[row][column].getValue())
  71.  
  72.     def isGameOver(self):
  73.         game_over = True
  74.         for row in range(len(self._board)):
  75.             for column in range(len(self._board[row])):
  76.                 if self._board[row][column].getFace() is False:
  77.                     game_over = False
  78.         return game_over
  79.        
  80.     def play(self):
  81.         '''Do while game over is not true then check over the board again and ask for the input then display the input'''
  82.         while True:
  83.             self.displayBoard()
  84.  
  85.             if self.isGameOver():
  86.                 print("Hooray! You finished!")
  87.                 raise SystemExit(0)
  88.                        
  89.             x = input('Enter the coordinate for first card (eg. x,y)')
  90.             while True:
  91.                 y = input('Enter the coordinate for second card (eg. x,y)')
  92.                 if x == y:
  93.                     print('You cannot choose the same coordinate twice')
  94.                 else:
  95.                     break
  96.  
  97.             card1 = self._board[x[0]][x[1]]
  98.             card2 = self._board[y[0]][y[1]]
  99.             if card1.getValue() == card2.getValue():
  100.                 card1.flip()
  101.                 card2.flip()
  102.             else:
  103.                 print('Not an identical pair.')
  104.  
  105.  
  106. def deck_factory(num_of_pairs):
  107.     '''
  108.    Generates a deck with X number of pairs (ie. pairs = 10, so 20 cards will be produced)
  109.    :param num_of_pairs:
  110.    :return:
  111.    '''
  112.     _cards = []
  113.     for card in range(num_of_pairs):
  114.         # Generate the cards
  115.         c1 = Card(card)
  116.         _cards.append(c1)
  117.         c2 = Card(card)
  118.         _cards.append(c2)
  119.     deck = Deck(_cards)
  120.     return deck
  121.  
  122.  
  123. def board_factory(rows, columns):
  124.     '''
  125.    Generates a blank board
  126.    :return: nested list of list[row][col]
  127.    '''
  128.     return [[None for row in range(rows)] for col in range(columns)]
  129.  
  130.  
  131. def game_factory(size):
  132.     '''
  133.    Factory for creating a game using some assumptions
  134.    :param board_size: Number representing one side of the board, the board is assumed to be square
  135.    :return: new Game object
  136.    '''
  137.     board = board_factory(size, size)
  138.     deck = deck_factory((size * size) // 2)
  139.     return Game(board, deck)
  140.  
  141.  
  142. def main():
  143.     while True:
  144.         # Force user to enter valid value for number of rows
  145.         while True:
  146.             rows = input("Enter number of rows ")
  147.             if rows > 0 < 10:
  148.                 rows = int(rows)
  149.                 break
  150.             else:
  151.                 print("    ***Number of rows must be between 1 and 9! Try again.***")
  152.                 # Adding *** and indenting error message makes it easier for the user to see
  153.  
  154.         # Force user to enter valid value for number of columns
  155.         while True:
  156.             columns = input("Enter number of columns ")
  157.             if columns > 0 < 10:
  158.                 columns = int(columns)
  159.                 break
  160.             else:
  161.                 print("    ***Number of columns must be between 1 and 9! Try again.***")
  162.  
  163.         if rows * columns % 2 == 0:
  164.             break
  165.         else:
  166.             print("    ***The value of rows X columns must be even. Try again.***")
  167.  
  168.     game = game_factory(rows)
  169.     game.populateBoard()
  170.     game.play()
  171.  
  172. if __name__ == "__main__":
  173.     main()
Advertisement
Add Comment
Please, Sign In to add comment