Cherry_man1

Untitled

Oct 2nd, 2014
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.09 KB | None | 0 0
  1.  
  2. import random
  3.  
  4.  
  5. class Card(object):
  6.     '''genertates the cards'''
  7.     def __init__(self,value,face = False):
  8.         self._value = value
  9.         self._face = face
  10.        
  11.        
  12.     def getValue(self):#gets the value
  13.         return self._value
  14.    
  15.     def getFace(self):#sets the face
  16.         return self._face
  17.    
  18.     def setFace(self):
  19.         self._face = True
  20.    
  21. class Deck(object):
  22.    
  23.     def __init__(self, pair):
  24.         self._pair = pair
  25.         self._cards = []
  26.         for card in range(self._pair):
  27.             '''generates the crads'''
  28.             c1 = Card(card)
  29.             self._cards.append(c1)
  30.             c2 = Card(card)
  31.             self._cards.append(c2)
  32.            
  33.            
  34.        
  35.     def shuffle():
  36.         random.shuffle(self._cards)#Shuffles the cards
  37.    
  38.     def _deal():
  39.         if len(self) == 0:
  40.             return None
  41.         else:
  42.             return self._cards.pop(0)
  43.        
  44.    
  45.     def _get(self,card):
  46.         return self._cards[card]._getFace()
  47.    
  48.     def __len__(self):
  49.         return len(self._cards)
  50.    
  51.    
  52.    
  53. class Game(object):
  54.     def __init__(self, rows, columns):
  55.        
  56.         self._rows = rows
  57.         self._columns = columns
  58.         self._deck = Deck((rows * columns)//2)
  59.         self._board = []
  60.         for row in range(self._rows):
  61.             self._board.append([0] * self._columns)
  62.            
  63.     def populateBoard(self):
  64.         self._deck.shuffle()
  65.         for row in self._rows:
  66.             for column in self._columns:
  67.                 self._board[row][column] = self._deck._deal()
  68.        
  69.     def displayBoard(self):
  70.         for row in self._rows:
  71.             for columns in self._columns:
  72.                 if self._board[row][column]._getFace() == False:
  73.                     print('*')
  74.                 else:
  75.                     print(self._board[row][columns]._getValue())
  76.                 print()
  77.                
  78.    
  79.     def isGameOver(self):
  80.         cardface = True
  81.         for row in self._rows:
  82.             for column in self._columns:
  83.                 if self._board[row][column] == False:
  84.                     cardface = True
  85.         return cardface
  86.        
  87.        
  88.     def play(self):
  89.        '''Do while game over is not true then check over the board again and ask for the input then display the input '''
  90.        while True:
  91.             if self.isGameOver() == False:
  92.                 break
  93.            
  94.                        
  95.             x = input('Enter the coordinate for first card')
  96.                
  97.             y = input('Enter the coordinate for second card')
  98.             x = x.split(' ')
  99.             y = y.split(' ')
  100.             x = (int(x[0]), int(x[1]))
  101.             y = (int(y[0]), int(y[1]))
  102.             if x != y:
  103.                 print('Not and identical pair. Found' + x._getValue() + 'at' + x + 'and'+ y._getValue())
  104.                      
  105.             else:
  106.                 self._board = [x][y]._getFace()
  107.            
  108.             self._displayBoard()
  109.  
  110.  
  111. def main():
  112.     while True:
  113.         # Force user to enter valid value for number of rows
  114.         while True:
  115.             rows = input("Enter number of rows ")
  116.             if rows.isdigit() and ( 1 <= int(rows) <= 9):
  117.                 rows = int(rows)
  118.                 break
  119.             else:
  120.                 print ("    ***Number of rows must be between 1 and 9! Try again.***")
  121.                 # Adding *** and indenting error message makes it easier for the user to see
  122.  
  123.         # Force user to enter valid value for number of columns
  124.         while True:
  125.             columns = input("Enter number of columns ")
  126.             if columns.isdigit() and ( 1 <= int(columns) <= 9):
  127.                 columns = int(columns)
  128.                 break
  129.             else:
  130.                 print ("    ***Number of columns must be between 1 and 9! Try again.***")
  131.  
  132.         if rows * columns % 2 == 0:
  133.             break
  134.         else:
  135.             print ("    ***The value of rows X columns must be even. Try again.***")
  136.  
  137.     game = Game(rows, columns)
  138.     game.play()
  139.  
  140. if __name__ == "__main__":
  141.     main()
Advertisement
Add Comment
Please, Sign In to add comment