Posted By

mladoux on 03/18/14


Tagged


Versions (?)

Memory


 / Published in: Python
 

Python 3 Homework - Memory Game

  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3.  
  4. ''' Player Class '''
  5. class Player:
  6.  
  7. ''' Initialize Player '''
  8. def __init__(self):
  9.  
  10. self.moves = 0
  11. self.flipped = []
  12.  
  13.  
  14. ''' Turn over tile '''
  15. def flipTile(self, tile):
  16.  
  17. tile.flip()
  18. self.moves += 1
  19. self.flipped.append(tile)
  20.  
  21. ''' Check if one round is complete '''
  22. def roundComplete(self):
  23.  
  24. return len(self.flipped) == 2
  25.  
  26. ''' Retrieve flipped tiles '''
  27. def getPair(self):
  28.  
  29. return (self.flipped.pop(), self.flipped.pop())
  30.  
  31. ''' Cover unmatched pairs '''
  32. def coverPair(self, tile_pair):
  33. for tile in tile_pair:
  34. tile.flip()
  35.  
  36. ''' Board Class '''
  37. class Board(list):
  38.  
  39. ''' Initialize Board '''
  40. def __init__(self, tiles):
  41.  
  42. super().__init__(tiles)
  43.  
  44. ''' Modify string output '''
  45. def __str__(self):
  46. return " ".join(str(t) for t in self) + \
  47. "\n0 1 2 3 4 5 6 7 8 9"
  48.  
  49. ''' Check if board is complete '''
  50. def isComplete(self):
  51. for tile in self:
  52. if tile.isCovered():
  53. ''' If even one Tile is uncovered, then game is still going '''
  54. return False
  55.  
  56. ''' if we got this far, board is complete '''
  57. return True
  58.  
  59.  
  60. ''' Tile Class '''
  61. class Tile:
  62.  
  63. ''' Initialize the Tiles '''
  64. def __init__(self, char):
  65.  
  66. ''' uncovered value of character '''
  67. self.char = char
  68.  
  69. ''' set default output as the covered state '''
  70. self.image = '_'
  71.  
  72. ''' Change string evaluation '''
  73. def __str__(self):
  74. ''' return the image state '''
  75. return self.image
  76.  
  77. ''' Evaluate equality '''
  78. def __eq__(self, other):
  79. return self.image == other.image
  80.  
  81. ''' Evaluate inequality '''
  82. def __ne__(self, other):
  83. return not self.__eq__(other)
  84.  
  85. ''' Flip tile '''
  86. def flip(self):
  87. if self.image == '_':
  88. self.image = self.char
  89. else:
  90. self.image = '_'
  91.  
  92. ''' Check tile state '''
  93. def isCovered(self):
  94. return self.image == '_'
  95.  
  96. ''' Only run the rest if called directly '''
  97. if __name__ == "__main__":
  98.  
  99. ''' Initialize Player '''
  100. player = Player()
  101.  
  102. ''' Set up the board '''
  103. board = Board([Tile(t) for t in ['A', 'B', 'C', 'D', 'E'] * 2])
  104.  
  105. ''' Randomize the tiles '''
  106. import random
  107. random.shuffle(board)
  108.  
  109. ''' Evaluate input '''
  110. def validInput(user_input):
  111.  
  112. ''' Check if input is a number '''
  113. if user_input.isdigit():
  114.  
  115. ''' Convert user_input to a integer '''
  116. idx = int(user_input)
  117.  
  118. ''' check if idx is in range '''
  119. if idx < len(board):
  120.  
  121. ''' check if tile is flipped '''
  122. if not board[idx].isCovered():
  123. print("Tile already flipped!")
  124. return False
  125.  
  126. ''' All tests passed '''
  127. return True
  128.  
  129. else:
  130. print("User input out of range")
  131. return False
  132.  
  133. else:
  134. ''' Input not a number, ignore '''
  135. return False
  136.  
  137.  
  138.  
  139. ''' Start Game '''
  140. print('\n+----------------+')
  141. print('| M E M O R Y |')
  142. print('+----------------+\n')
  143.  
  144. while not board.isComplete():
  145.  
  146. ''' Print Game Board '''
  147. print(board)
  148.  
  149. ''' Check if we completed a round '''
  150. if player.roundComplete():
  151. a,b = player.getPair()
  152.  
  153. ''' If flipped tiles do not match, flip them back '''
  154. if a != b:
  155. player.coverPair((a,b))
  156.  
  157. ''' Get user input '''
  158. user_input = input("\nChoice: ")
  159.  
  160. ''' Check for quit '''
  161. if user_input == "q" or user_input == "Q":
  162. break
  163.  
  164. ''' Check if input is valid '''
  165. if validInput(user_input):
  166.  
  167. ''' convert user_input to an integer '''
  168. idx = int(user_input)
  169.  
  170. ''' Get Tile '''
  171. tile = board[idx]
  172.  
  173. ''' flip tile '''
  174. player.flipTile(tile)
  175.  
  176. else:
  177. print("Invalid Input!")
  178.  
  179. ''' Game Over!!! '''
  180. if board.isComplete():
  181.  
  182. ''' Print only if player completes game '''
  183. print('\n+----------------+')
  184. print('| W I N N E R ! |')
  185. print('+----------------+\n')
  186. print(board)
  187. print("You won in {:d} moves!".format(player.moves))
  188.  
  189. else:
  190.  
  191. ''' Quitters never prosper! '''
  192. print('\n+----------------+')
  193. print('| F A I L U R E |')
  194. print('+----------------+\n')
  195. print("Winners don't quit, LOSER!!!")

Report this snippet  

You need to login to post a comment.