Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- class Card(object):
- '''genertates the cards'''
- def __init__(self,value,face = False):
- self._value = value
- self._face = face
- def getValue(self):#gets the value
- return self._value
- def getFace(self):#sets the face
- return self._face
- def setFace(self):
- self._face = True
- class Deck(object):
- def __init__(self, pair):
- self._pair = pair
- self._cards = []
- for card in range(self._pair):
- '''generates the crads'''
- c1 = Card(card)
- self._cards.append(c1)
- c2 = Card(card)
- self._cards.append(c2)
- def shuffle():
- random.shuffle(self._cards)#Shuffles the cards
- def _deal():
- if len(self) == 0:
- return None
- else:
- return self._cards.pop(0)
- def _get(self,card):
- return self._cards[card]._getFace()
- def __len__(self):
- return len(self._cards)
- class Game(object):
- def __init__(self, rows, columns):
- self._rows = rows
- self._columns = columns
- self._deck = Deck((rows * columns)//2)
- self._board = []
- for row in range(self._rows):
- self._board.append([0] * self._columns)
- def populateBoard(self):
- self._deck.shuffle()
- for row in self._rows:
- for column in self._columns:
- self._board[row][column] = self._deck._deal()
- def displayBoard(self):
- for row in self._rows:
- for columns in self._columns:
- if self._board[row][column]._getFace() == False:
- print('*')
- else:
- print(self._board[row][columns]._getValue())
- print()
- def isGameOver(self):
- cardface = True
- for row in self._rows:
- for column in self._columns:
- if self._board[row][column] == False:
- cardface = True
- return cardface
- 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:
- if self.isGameOver() == False:
- break
- x = input('Enter the coordinate for first card')
- y = input('Enter the coordinate for second card')
- x = x.split(' ')
- y = y.split(' ')
- x = (int(x[0]), int(x[1]))
- y = (int(y[0]), int(y[1]))
- if x != y:
- print('Not and identical pair. Found' + x._getValue() + 'at' + x + 'and'+ y._getValue())
- else:
- self._board = [x][y]._getFace()
- self._displayBoard()
- def main():
- while True:
- # Force user to enter valid value for number of rows
- while True:
- rows = input("Enter number of rows ")
- if rows.isdigit() and ( 1 <= int(rows) <= 9):
- 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.isdigit() and ( 1 <= int(columns) <= 9):
- 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(rows, columns)
- game.play()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment