Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Notes - for cherry_man1
- #
- # I'm making an assumption based on your code that you're trying to do a card marching game like Memory?
- #
- # 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.
- # 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.
- # 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.
- import random
- class Card(object):
- def __init__(self, value, face=False):
- self._value = value
- self._face = face
- def getValue(self):
- return self._value
- def getFace(self):
- return self._face
- def setFace(self, face):
- self._face = face
- def flip(self):
- self._face = (self._face is not True)
- class Deck(object):
- _cards = []
- def __init__(self, cards):
- self._cards = cards
- def shuffle(self):
- # Shuffles the cards
- random.shuffle(self._cards)
- def deal(self):
- if len(self) == 0:
- return None
- else:
- return self._cards.pop(0)
- def __getitem__(self, item):
- return self._cards[item]
- def __len__(self):
- return len(self._cards)
- class Game(object):
- def __init__(self, board, deck):
- self._board = board
- self._deck = deck
- def populateBoard(self):
- self._deck.shuffle()
- for row in range(len(self._board)):
- for column in range(len(self._board[row])):
- self._board[row][column] = self._deck.deal()
- def displayBoard(self):
- for row in range(len(self._board)):
- for column in range(len(self._board[row])):
- if self._board[row][column].getFace() is False:
- print('*')
- else:
- print(self._board[row][column].getValue())
- def isGameOver(self):
- game_over = True
- for row in range(len(self._board)):
- for column in range(len(self._board[row])):
- if self._board[row][column].getFace() is False:
- game_over = False
- return game_over
- def play(self):
- '''Do while game over is not true then check over the board again and ask for the input then display the input'''
- while True:
- self.displayBoard()
- if self.isGameOver():
- print("Hooray! You finished!")
- raise SystemExit(0)
- x = input('Enter the coordinate for first card (eg. x,y)')
- while True:
- y = input('Enter the coordinate for second card (eg. x,y)')
- if x == y:
- print('You cannot choose the same coordinate twice')
- else:
- break
- card1 = self._board[x[0]][x[1]]
- card2 = self._board[y[0]][y[1]]
- if card1.getValue() == card2.getValue():
- card1.flip()
- card2.flip()
- else:
- print('Not an identical pair.')
- def deck_factory(num_of_pairs):
- '''
- Generates a deck with X number of pairs (ie. pairs = 10, so 20 cards will be produced)
- :param num_of_pairs:
- :return:
- '''
- _cards = []
- for card in range(num_of_pairs):
- # Generate the cards
- c1 = Card(card)
- _cards.append(c1)
- c2 = Card(card)
- _cards.append(c2)
- deck = Deck(_cards)
- return deck
- def board_factory(rows, columns):
- '''
- Generates a blank board
- :return: nested list of list[row][col]
- '''
- return [[None for row in range(rows)] for col in range(columns)]
- def game_factory(size):
- '''
- Factory for creating a game using some assumptions
- :param board_size: Number representing one side of the board, the board is assumed to be square
- :return: new Game object
- '''
- board = board_factory(size, size)
- deck = deck_factory((size * size) // 2)
- return Game(board, deck)
- def main():
- while True:
- # Force user to enter valid value for number of rows
- while True:
- rows = input("Enter number of rows ")
- if rows > 0 < 10:
- rows = int(rows)
- break
- else:
- print(" ***Number of rows must be between 1 and 9! Try again.***")
- # Adding *** and indenting error message makes it easier for the user to see
- # Force user to enter valid value for number of columns
- while True:
- columns = input("Enter number of columns ")
- if columns > 0 < 10:
- columns = int(columns)
- break
- else:
- print(" ***Number of columns must be between 1 and 9! Try again.***")
- if rows * columns % 2 == 0:
- break
- else:
- print(" ***The value of rows X columns must be even. Try again.***")
- game = game_factory(rows)
- game.populateBoard()
- game.play()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment