Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.88 KB | None | 0 0
  1. import random
  2.  
  3. class RockPaperScissors(object):
  4. def setUserOption(self,val):
  5. self.__option = val
  6.  
  7. def getValueFromList(self,option):
  8. l = ['rock','scissors','paper']
  9. return l[option]
  10.  
  11. def __getRandomValue(self,max):
  12. val = random.randint(1,max)
  13. return self.getValueFromList(val-1)
  14.  
  15. def __getResult(self,t):
  16. self.__d = {('rock','scissors'):'rock breaks scissors - User has won',('rock','paper'):'rock is captured by paper - User has won',('scissors','paper'):'Scissors cut paper - User has won',
  17. ('scissors','rock'):'rock breaks scissors - Computer won',('paper','rock'):'rock is captured by paper - Computer won',('paper','scissors'):'Scissors cut paper - Computer won'}
  18. return self.__d[t]
  19.  
  20. def __computeResult(self,computerOption):
  21. if computerOption == self.__option:
  22. return 'The user and computer choose the same option'
  23. else:
  24. return self.__getResult((self.__option,computerOption))
  25.  
  26. def printResult(self):
  27. computerOption = self.__getRandomValue(3)
  28. print 'User choice: ',self.__option
  29. print 'Computer choice: ',computerOption
  30. print self.__computeResult(computerOption)
  31.  
  32. if __name__ == "__main__":
  33. print 'Welcome to rock paper scissors'
  34. print '1. Rock, 2. Paper, 3. Scissor'
  35. val = int(raw_input('Enter your choice Number: '))
  36. if val >=1 and val <= 3:
  37. obj = RockPaperScissors()
  38. obj.setUserOption(obj.getValueFromList(val-1))
  39. obj.printResult()
  40. else:
  41. raise ValueError('You are supposed to enter the choice given in the menu')
  42.  
  43. class Rock inherits from Element
  44. type = 'rock'
  45. def compare(Element)
  46. if type == 'paper'
  47. return 'LOSE'
  48. elsif type == 'scissors'
  49. return 'WIN'
  50. else
  51. return 'TIE'
  52.  
  53. # The goal for an element is to just know when it wins, when it loses, and how to figure that out.
  54. class Element:
  55. _name = ""
  56. _wins = {}
  57. _loses = {}
  58.  
  59. def get_name(self):
  60. return self._name
  61.  
  62. def add_win(self, losingElementName, action):
  63. self._wins[losingElementName] = action
  64.  
  65. def add_loss(self, winningElementName, action):
  66. self._loses[winningElementName] = action
  67.  
  68. def compare(self, element):
  69. if element.get_name() in self._wins.keys():
  70. return self._name + " " + self._wins[element.get_name()] + " " + element.get_name()
  71. elif element.get_name() in self._loses.keys():
  72. return None
  73. else:
  74. return "Tie"
  75.  
  76. def __init__(self, name):
  77. self._name = name
  78. self._wins = {}
  79. self._loses = {}
  80.  
  81. # The player's only responsibility is to make a selection from a given set. Whether it be computer or human.
  82. class Player:
  83. _type = ''
  84. _selection = ''
  85.  
  86. def make_selection(self, arrayOfOptions):
  87. index = -1
  88. if (self._type == 'Computer'):
  89. index = random.randint(0, len(arrayOfOptions) - 1)
  90. else:
  91. index = int(raw_input('Enter the number of your selection: ')) - 1
  92. self._selection = arrayOfOptions[index]
  93. return self._type + ' selected ' + self._selection + '.'
  94.  
  95. def get_selection(self):
  96. return self._selection
  97.  
  98. def __init__(self, playerType):
  99. self._type = playerType
  100. self._selection = ''
  101.  
  102. # A game should have players, game pieces, and know what to do with them.
  103. class PlayGame:
  104. _player1 = Player('Human')
  105. _player2 = Player('Computer')
  106.  
  107. _elements = {}
  108.  
  109. def print_result(self, element1, element2):
  110. val = element1.compare(element2)
  111. if (val != None):
  112. print "YOU WIN! (" + val + ")" # win or tie
  113. else:
  114. print "You lose. (" + element2.compare(element1) + ")"
  115.  
  116.  
  117. def fire_when_ready(self):
  118. counter = 1
  119. for e in self._elements.keys():
  120. print str(counter) + ". " + e
  121. counter = counter + 1
  122. print ""
  123. print "Shoot!"
  124. print ""
  125.  
  126. print self._player1.make_selection(self._elements.keys())
  127. print self._player2.make_selection(self._elements.keys())
  128.  
  129. element1 = self._elements[self._player1.get_selection()]
  130. element2 = self._elements[self._player2.get_selection()]
  131.  
  132. self.print_result(element1, element2)
  133.  
  134.  
  135. def load_element(self, elementName1, action, elementName2):
  136. winningElementObject = None
  137. newElementObject = None
  138.  
  139. if (elementName1 in self._elements):
  140. winningElementObject = self._elements[elementName1]
  141. winningElementObject.add_win(elementName2, action)
  142. else:
  143. newElementObject = Element(elementName1)
  144. newElementObject.add_win(elementName2, action)
  145. self._elements[elementName1] = newElementObject
  146.  
  147. if (elementName2 in self._elements):
  148. losingElementObject = self._elements[elementName2]
  149. losingElementObject.add_loss(elementName1, action)
  150. else:
  151. newElementObject = Element(elementName2)
  152. newElementObject.add_loss(elementName1, action)
  153. self._elements[elementName2] = newElementObject
  154.  
  155.  
  156. def __init__(self, filepath):
  157. # get elements from data storage
  158. f = open(filepath)
  159. for line in f:
  160. data = line.split(' ')
  161. self.load_element(data[0], data[1], data[2].replace('n', ''))
  162.  
  163. if __name__ == "__main__":
  164. print "Welcome"
  165. game = PlayGame('data.txt')
  166. print ""
  167. print "Get ready!"
  168. print ""
  169. game.fire_when_ready()
  170.  
  171. scissors cut paper
  172. paper covers rock
  173. rock crushes lizard
  174. lizard poisons spock
  175. spock smashes scissors
  176. scissors decapitates lizard
  177. lizard eats paper
  178. paper disproves spock
  179. spock vaporizes rock
  180. rock crushes scissors
  181.  
  182. val = 0
  183. while val < 1 and val > 3
  184. val = int(raw_input('Enter your choice Number: '))
  185.  
  186. l = ['dummy', 'rock', 'paper', 'scissors']
  187.  
  188. class RockPaperScissors(object):
  189. OPTIONS=['rock','paper','scissors']
  190. ...
  191.  
  192. class RockPaperScissorsLizardSpock(RockPaperScissors):
  193. OPTIONS=['rock','paper','scissors','lizard','spock']
  194. ...
  195.  
  196. def __getRandomValue(self,max):
  197. val = random.randint(1,max)
  198. return self.getValueFromList(val-1)
  199.  
  200. from random import choice
  201. if __name__ == "__main__":
  202. while True:
  203. print "1. Rock 2. Paper 3. Scissors"
  204. val, comp = int(raw_input('Enter your choice Number: ')), choice(range(1,4))
  205. if val == comp:
  206. print "Draw. Try again"
  207. elif val == 1:
  208. if comp == 2:
  209. print "You lose"
  210. if comp == 3:
  211. print "#WINNING"
  212. elif val == 2:
  213. if comp == 3:
  214. print "You lose"
  215. if comp == 1:
  216. print "#WINNING"
  217. elif val == 3:
  218. if comp == 2:
  219. print "You lose"
  220. if comp == 1:
  221. print "#WINNING"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement