Advertisement
Guest User

Untitled

a guest
May 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.09 KB | None | 0 0
  1. # Full Game
  2. '''
  3. NOTES: This Football simulator is designed to allow one human player to play vs one AI player
  4. For the purposes of the intro to Python club, many factors of the real game of football are simplified or omitted.
  5. '''
  6.  
  7. import random
  8.  
  9. # INSTANCE VARIABLES
  10. teamNamesArray = ['Patriots', 'Cowboys', 'Jets', 'Packers', 'Giants', 'Buccaneers', 'Falcons', 'Titans', 'Jaguars'] # List of AI team names
  11. teamName_AI = ""
  12. teamName_User = ""
  13. score_AI = 0
  14. score_User = 0
  15. game_playsPerQuarter = 12 # Adjust this to change the length of the game. 8 is somewhat short. 20 is a longer game.
  16. game_playCounter = 1 # Keeps track of current play count in quarter
  17. game_CurrentDown = 1
  18. game_CurrentQuarter = 1
  19. game_FieldPosition = 0 # Values 0-100. If Yard line >=100, player scores.
  20. game_FieldPositionNeededForConversion = 0 # The yard line the offense must reach in order to earn a 1st down
  21. game_Possession = 1 # 0 = AI, 1 = Human Player
  22. game_initialReceivingTeam = 0 # TBD later. 0 = AI, 1 = Human Player
  23. game_isOver = False
  24.  
  25.  
  26. def displayGameState():
  27. global score_AI
  28. global score_User
  29. global game_playsPerQuarter
  30. global game_playCounter
  31. global game_CurrentDown
  32. global game_CurrentQuarter
  33. global game_FieldPosition
  34. global game_FieldPositionNeededForConversion
  35. global game_Possession
  36. global game_isOver
  37.  
  38.  
  39. teamInPossession = teamName_AI
  40. if game_Possession == 1:
  41. teamInPossession = teamName_User
  42.  
  43. yardlineDescriptor = "on their own "+str(game_FieldPosition)+" yard line."
  44. if game_FieldPosition > 50:
  45. yardlineDescriptor = "on their opponent's " + str(100-game_FieldPosition) + " yard line."
  46.  
  47. print("\n-- Current Game State --")
  48. print("Quarter: "+str(game_CurrentQuarter))
  49. print("Plays Left Until End of Quarter: "+str(game_playsPerQuarter-game_playCounter))
  50. print("The "+teamInPossession+" are in possession of the ball "+yardlineDescriptor)
  51. print("Current Down: " + str(game_CurrentDown))
  52.  
  53. if game_FieldPosition < 90:
  54. print("They need " + str(game_FieldPositionNeededForConversion-game_FieldPosition) + " yards for a first down.")
  55. else:
  56. print("They need " + str(100 - game_FieldPosition) + " more yards for a touchdown.")
  57.  
  58.  
  59. # Pass The Results of Every Play To This Function To Update Instance Variables and Game State
  60. # Return TRUE if a change of possession occurred that requires a kickoff. Return FALSE in all other cases
  61. def runPlay(yardsGained):
  62. global score_AI
  63. global score_User
  64. global game_playsPerQuarter
  65. global game_playCounter
  66. global game_CurrentDown
  67. global game_CurrentQuarter
  68. global game_FieldPosition
  69. global game_FieldPositionNeededForConversion
  70. global game_Possession
  71. global game_isOver
  72.  
  73. response = [0] # response[0] = turnoverOccurred
  74.  
  75. if (game_FieldPosition + yardsGained) >= 100: # Someone has scored a touchdown
  76. if game_Possession == 0:
  77. print (teamName_AI+" (AI) have scored!")
  78. score_AI += 7
  79. print ("The score is now "+teamName_User + ": "+str(score_User)+" - "+teamName_AI + ": "+str(score_AI))
  80. game_Possession = 1
  81. else:
  82. print (teamName_User + " (You) have scored!")
  83. score_User += 7
  84. print ("The score is now "+teamName_User + ": "+str(score_User)+" - "+teamName_AI + ": "+str(score_AI))
  85. game_Possession = 0
  86. game_FieldPosition = 20
  87. game_CurrentDown = 0
  88. return True
  89.  
  90. elif (game_FieldPosition + yardsGained) >= game_FieldPositionNeededForConversion: # Someone has achieved a 1st down
  91. if game_Possession == 0:
  92. print ("The "+teamName_AI+" (AI) have converted for a first down!")
  93. else:
  94. print ("The "+teamName_User + " (You) have converted for a first down!")
  95. game_CurrentDown = 0
  96. game_FieldPosition += yardsGained
  97. game_FieldPositionNeededForConversion = game_FieldPosition+10
  98.  
  99. else:
  100. game_FieldPosition += yardsGained
  101.  
  102. game_playCounter += 1
  103. game_CurrentDown += 1
  104.  
  105. # Swap possession if a team did not convert on 4th down
  106. if game_CurrentDown > 4:
  107. if game_Possession == 0:
  108. print ("\n**********************\nThe "+teamName_AI+" (AI) have failed to convert for a first down! Changing Possession.")
  109. else:
  110. print ("\n**********************\nThe " + teamName_User + " (You) have failed to convert for a first down! Changing Possession.")
  111. changePossession()
  112.  
  113. if game_playCounter >= game_playsPerQuarter:
  114. if game_CurrentQuarter == 1:
  115. print ("*** The First Quarter Has Come To An End! ***")
  116. print ("The score is now " + teamName_User + ": " + str(score_User) + " - " + teamName_AI + ": " + str(score_AI))
  117. game_CurrentQuarter += 1
  118. game_playCounter = 1
  119. elif game_CurrentQuarter == 2:
  120. print ("*** The Second Quarter Has Come To An End! ***")
  121. print ("The score is now " + teamName_User + ": " + str(score_User) + " - " + teamName_AI + ": " + str(score_AI))
  122. game_CurrentQuarter += 1
  123. game_playCounter = 1
  124. if game_initialReceivingTeam == game_Possession: # The team that kicked initially should now receive
  125. changePossession()
  126. return True
  127. elif game_CurrentQuarter == 3:
  128. print ("*** The Third Quarter Has Come To An End! ***")
  129. print ("The score is now " + teamName_User + ": " + str(score_User) + " - " + teamName_AI + ": " + str(score_AI))
  130. game_CurrentQuarter += 1
  131. game_playCounter = 1
  132. else:
  133. print ("*** The Fourth Quarter Has Come To An End! ***")
  134. if score_User > score_AI:
  135. print ("You Won!")
  136. print ("The score is now " + teamName_User + ": " + str(score_User) + " - " + teamName_AI + ": " + str(score_AI))
  137. game_isOver = True
  138.  
  139. return False
  140.  
  141.  
  142. def runOffensivePlay(playCall): # 0 = run, 1 = short pass, 2 = long pass
  143. yardsGained = 0
  144.  
  145. # Possibility of Sack
  146. sackQBChance = 4
  147. maxYardLossOnSack = -6
  148.  
  149. if int(playCall) == 1:
  150. sackQBChance = 6
  151. elif int(playCall) == 2:
  152. sackQBChance = 11
  153. maxYardLossOnSack = -10
  154.  
  155. wasSacked = False
  156.  
  157. if(random.randint(0, 100) <= sackQBChance):
  158. wasSacked = True
  159.  
  160. if(wasSacked):
  161. yardsGained = random.randint(maxYardLossOnSack, 0)
  162. print('QB Sack!\nYards Gained:'+str(yardsGained))
  163. return yardsGained
  164.  
  165. #Possibility of successful yards gained
  166. completionChance = 100
  167. maxYardGainOnPlay = 7
  168.  
  169. if int(playCall) == 1:
  170. completionChance = 80
  171. maxYardGainOnPlay = 9
  172. elif int(playCall) == 2:
  173. completionChance = 50
  174. maxYardGainOnPlay = 18
  175.  
  176.  
  177. passComplete = False
  178.  
  179. if (random.randint(0, 100) <= completionChance):
  180. passComplete = True
  181.  
  182. if (passComplete):
  183. yardsGained = random.randint(0, maxYardGainOnPlay)
  184. if int(playCall) == 0:
  185. print('Run was Successful!\nYards Gained:'+str(yardsGained))
  186. else:
  187. print('Pass Complete!\nYards Gained:'+str(yardsGained))
  188. return yardsGained
  189.  
  190. print('Incomplete Pass!\nYards Gained: '+str(yardsGained))
  191. return yardsGained
  192.  
  193.  
  194.  
  195. # Simulate the Coin Toss at Start of Game
  196. def coinToss():
  197. print("The referee will now perform the coin flip.")
  198. coinToss = raw_input("Heads or Tails? ")
  199. flip = random.randint(0, 1)
  200. if coinToss.lower == "heads":
  201. userGuess = 1
  202. else:
  203. userGuess = 0
  204.  
  205. if userGuess == flip:
  206. return True
  207. else:
  208. return False
  209.  
  210.  
  211. # Determine if the player kicks or receives the ball following coin toss
  212. def kickOrReceive(didWinToss):
  213. # return of 0 == Player Kicks
  214. # return of 1 == Player Receives
  215. if didWinToss: # Player won the coin flip
  216. kickOrReceiveChoice = raw_input("Would you like to kick or receive? (Enter 0 to kick, 1 to receive) ")
  217. if kickOrReceiveChoice == 0:
  218. return 0
  219. else:
  220. return 1
  221. else: # Player lost the coin flip, so AI gets to select kick or receive
  222. opponentSelection = random.randint(0, 100)
  223. if opponentSelection <= 4: # 4% chance that AI decides to kick
  224. return 1
  225. else:
  226. return 0
  227.  
  228.  
  229. # Simulate Kick Return
  230. def kickReturn(): # Returns TRUE if a kickoff is needed. Otherwise returns FALSE
  231. global score_AI
  232. global score_User
  233. global game_FieldPosition
  234. global game_FieldPositionNeededForConversion
  235. global game_Possession
  236.  
  237. distanceReturned = randomizeKick()
  238. game_FieldPosition = distanceReturned
  239. game_FieldPositionNeededForConversion = game_FieldPosition+10
  240.  
  241. if distanceReturned >= 100:
  242. if game_Possession == 0:
  243. score_AI += 7
  244. else:
  245. score_User += 7
  246.  
  247. changePossession()
  248. return True
  249. return False
  250.  
  251. # Function to randomize the distance the kickoff was returned for
  252. def randomizeKick():
  253. catchBallOn = random.randint(0, 20)
  254. choice = random.randint(1, 100)
  255. if (choice <= 70): # 70% Chance of a short return
  256. runDistance = random.randint(0, 20)
  257. print ("The ball was caught on the " + str(catchBallOn) + " yard line and run for "+str(runDistance)+" yards.")
  258. return catchBallOn+runDistance
  259. elif (choice <= 99): #29% chance of a long return
  260. runDistance = random.randint(21, 40)
  261. print ("The ball was caught on the " + str(catchBallOn) + " yard line and run for " + str(runDistance) + " yards.")
  262. return catchBallOn + runDistance
  263. else: # 1% Chance of a kick returned for a touchdown
  264. print ("The ball was caught on the " + str(catchBallOn) + " yard line and run back for a TOUCHDOWN!")
  265. return 100
  266.  
  267.  
  268. # Sometimes we will need to change possession of the ball
  269. def changePossession():
  270. global game_Possession
  271. global game_CurrentDown
  272. global game_FieldPosition
  273. global game_FieldPositionNeededForConversion
  274.  
  275. # Change Possession
  276. if game_Possession == 0:
  277. game_Possession = 1
  278. else:
  279. game_Possession = 0
  280.  
  281. # Reset Downs
  282. game_CurrentDown = 1
  283.  
  284. # Change Field Position (To Reflect Ball Going Other Direction)
  285. game_FieldPosition = 100 - game_FieldPosition
  286.  
  287. # Update Position Needed To Get First Down
  288. game_FieldPositionNeededForConversion = game_FieldPosition+10
  289.  
  290.  
  291. ###################################################
  292. ## End of Function Definitions, Game Code Begins ##
  293. ###################################################
  294.  
  295. # Program Start
  296. # Set Up Teams
  297. print("WELCOME TO FOOTBALL SIMULATOR 2017!!!")
  298. teamName_User = raw_input("Please Enter a Team Name: ")
  299. teamName_AI = teamNamesArray[random.randint(0, len(teamNamesArray)-1)]
  300. print("Today's Match Up Will Be Between the "+teamName_AI+" and the "+teamName_User)
  301.  
  302.  
  303. # Coin Toss
  304. coinTossResults = coinToss()
  305. if coinTossResults:
  306. print("The "+teamName_User+" Have Won The Coin Toss!")
  307. else:
  308. print("The "+teamName_AI+" Have Won The Coin Toss!")
  309.  
  310.  
  311. game_initialReceivingTeam = kickOrReceive(coinTossResults)
  312. if game_initialReceivingTeam == 0:
  313. print("\n\nThe "+teamName_User+" Will Be Kicking Off. The "+teamName_AI+" will be receiving the ball")
  314. game_Possession = 0
  315. else:
  316. print("\n\nThe "+teamName_AI+" Will Be Kicking Off. The "+teamName_User+" will be receiving the ball")
  317. game_Possession = 1
  318.  
  319.  
  320. # Simulate Kick Return
  321. kickReturn()
  322.  
  323.  
  324. while game_isOver == False:
  325. print ("\n*******************************")
  326. if game_Possession == 1:
  327. playChoice = raw_input('What play would you like to run?\n0 = RUN, 1 = SHORT PASS, 2 = LONG PASS:')
  328. playChoice = int(playChoice)
  329. runPlay(runOffensivePlay(playChoice))
  330. if game_isOver == False:
  331. displayGameState()
  332. else:
  333. playChoice = random.randint(0,2)
  334. runPlay(runOffensivePlay(playChoice))
  335. if game_isOver == False:
  336. displayGameState()
  337. raw_input('Press Enter To Continue')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement