Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Full Game
- '''
- NOTES: This Football simulator is designed to allow one human player to play vs one AI player
- For the purposes of the intro to Python club, many factors of the real game of football are simplified or omitted.
- '''
- import random
- # INSTANCE VARIABLES
- teamNamesArray = ['Patriots', 'Cowboys', 'Jets', 'Packers', 'Giants', 'Buccaneers', 'Falcons', 'Titans', 'Jaguars'] # List of AI team names
- teamName_AI = ""
- teamName_User = ""
- score_AI = 0
- score_User = 0
- game_playsPerQuarter = 12 # Adjust this to change the length of the game. 8 is somewhat short. 20 is a longer game.
- game_playCounter = 1 # Keeps track of current play count in quarter
- game_CurrentDown = 1
- game_CurrentQuarter = 1
- game_FieldPosition = 0 # Values 0-100. If Yard line >=100, player scores.
- game_FieldPositionNeededForConversion = 0 # The yard line the offense must reach in order to earn a 1st down
- game_Possession = 1 # 0 = AI, 1 = Human Player
- game_initialReceivingTeam = 0 # TBD later. 0 = AI, 1 = Human Player
- game_isOver = False
- def displayGameState():
- global score_AI
- global score_User
- global game_playsPerQuarter
- global game_playCounter
- global game_CurrentDown
- global game_CurrentQuarter
- global game_FieldPosition
- global game_FieldPositionNeededForConversion
- global game_Possession
- global game_isOver
- teamInPossession = teamName_AI
- if game_Possession == 1:
- teamInPossession = teamName_User
- yardlineDescriptor = "on their own "+str(game_FieldPosition)+" yard line."
- if game_FieldPosition > 50:
- yardlineDescriptor = "on their opponent's " + str(100-game_FieldPosition) + " yard line."
- print("\n-- Current Game State --")
- print("Quarter: "+str(game_CurrentQuarter))
- print("Plays Left Until End of Quarter: "+str(game_playsPerQuarter-game_playCounter))
- print("The "+teamInPossession+" are in possession of the ball "+yardlineDescriptor)
- print("Current Down: " + str(game_CurrentDown))
- if game_FieldPosition < 90:
- print("They need " + str(game_FieldPositionNeededForConversion-game_FieldPosition) + " yards for a first down.")
- else:
- print("They need " + str(100 - game_FieldPosition) + " more yards for a touchdown.")
- # Pass The Results of Every Play To This Function To Update Instance Variables and Game State
- # Return TRUE if a change of possession occurred that requires a kickoff. Return FALSE in all other cases
- def runPlay(yardsGained):
- global score_AI
- global score_User
- global game_playsPerQuarter
- global game_playCounter
- global game_CurrentDown
- global game_CurrentQuarter
- global game_FieldPosition
- global game_FieldPositionNeededForConversion
- global game_Possession
- global game_isOver
- response = [0] # response[0] = turnoverOccurred
- if (game_FieldPosition + yardsGained) >= 100: # Someone has scored a touchdown
- if game_Possession == 0:
- print (teamName_AI+" (AI) have scored!")
- score_AI += 7
- print ("The score is now "+teamName_User + ": "+str(score_User)+" - "+teamName_AI + ": "+str(score_AI))
- game_Possession = 1
- else:
- print (teamName_User + " (You) have scored!")
- score_User += 7
- print ("The score is now "+teamName_User + ": "+str(score_User)+" - "+teamName_AI + ": "+str(score_AI))
- game_Possession = 0
- game_FieldPosition = 20
- game_CurrentDown = 0
- return True
- elif (game_FieldPosition + yardsGained) >= game_FieldPositionNeededForConversion: # Someone has achieved a 1st down
- if game_Possession == 0:
- print ("The "+teamName_AI+" (AI) have converted for a first down!")
- else:
- print ("The "+teamName_User + " (You) have converted for a first down!")
- game_CurrentDown = 0
- game_FieldPosition += yardsGained
- game_FieldPositionNeededForConversion = game_FieldPosition+10
- else:
- game_FieldPosition += yardsGained
- game_playCounter += 1
- game_CurrentDown += 1
- # Swap possession if a team did not convert on 4th down
- if game_CurrentDown > 4:
- if game_Possession == 0:
- print ("\n**********************\nThe "+teamName_AI+" (AI) have failed to convert for a first down! Changing Possession.")
- else:
- print ("\n**********************\nThe " + teamName_User + " (You) have failed to convert for a first down! Changing Possession.")
- changePossession()
- if game_playCounter >= game_playsPerQuarter:
- if game_CurrentQuarter == 1:
- print ("*** The First Quarter Has Come To An End! ***")
- print ("The score is now " + teamName_User + ": " + str(score_User) + " - " + teamName_AI + ": " + str(score_AI))
- game_CurrentQuarter += 1
- game_playCounter = 1
- elif game_CurrentQuarter == 2:
- print ("*** The Second Quarter Has Come To An End! ***")
- print ("The score is now " + teamName_User + ": " + str(score_User) + " - " + teamName_AI + ": " + str(score_AI))
- game_CurrentQuarter += 1
- game_playCounter = 1
- if game_initialReceivingTeam == game_Possession: # The team that kicked initially should now receive
- changePossession()
- return True
- elif game_CurrentQuarter == 3:
- print ("*** The Third Quarter Has Come To An End! ***")
- print ("The score is now " + teamName_User + ": " + str(score_User) + " - " + teamName_AI + ": " + str(score_AI))
- game_CurrentQuarter += 1
- game_playCounter = 1
- else:
- print ("*** The Fourth Quarter Has Come To An End! ***")
- if score_User > score_AI:
- print ("You Won!")
- print ("The score is now " + teamName_User + ": " + str(score_User) + " - " + teamName_AI + ": " + str(score_AI))
- game_isOver = True
- return False
- def runOffensivePlay(playCall): # 0 = run, 1 = short pass, 2 = long pass
- yardsGained = 0
- # Possibility of Sack
- sackQBChance = 4
- maxYardLossOnSack = -6
- if int(playCall) == 1:
- sackQBChance = 6
- elif int(playCall) == 2:
- sackQBChance = 11
- maxYardLossOnSack = -10
- wasSacked = False
- if(random.randint(0, 100) <= sackQBChance):
- wasSacked = True
- if(wasSacked):
- yardsGained = random.randint(maxYardLossOnSack, 0)
- print('QB Sack!\nYards Gained:'+str(yardsGained))
- return yardsGained
- #Possibility of successful yards gained
- completionChance = 100
- maxYardGainOnPlay = 7
- if int(playCall) == 1:
- completionChance = 80
- maxYardGainOnPlay = 9
- elif int(playCall) == 2:
- completionChance = 50
- maxYardGainOnPlay = 18
- passComplete = False
- if (random.randint(0, 100) <= completionChance):
- passComplete = True
- if (passComplete):
- yardsGained = random.randint(0, maxYardGainOnPlay)
- if int(playCall) == 0:
- print('Run was Successful!\nYards Gained:'+str(yardsGained))
- else:
- print('Pass Complete!\nYards Gained:'+str(yardsGained))
- return yardsGained
- print('Incomplete Pass!\nYards Gained: '+str(yardsGained))
- return yardsGained
- # Simulate the Coin Toss at Start of Game
- def coinToss():
- print("The referee will now perform the coin flip.")
- coinToss = raw_input("Heads or Tails? ")
- flip = random.randint(0, 1)
- if coinToss.lower == "heads":
- userGuess = 1
- else:
- userGuess = 0
- if userGuess == flip:
- return True
- else:
- return False
- # Determine if the player kicks or receives the ball following coin toss
- def kickOrReceive(didWinToss):
- # return of 0 == Player Kicks
- # return of 1 == Player Receives
- if didWinToss: # Player won the coin flip
- kickOrReceiveChoice = raw_input("Would you like to kick or receive? (Enter 0 to kick, 1 to receive) ")
- if kickOrReceiveChoice == 0:
- return 0
- else:
- return 1
- else: # Player lost the coin flip, so AI gets to select kick or receive
- opponentSelection = random.randint(0, 100)
- if opponentSelection <= 4: # 4% chance that AI decides to kick
- return 1
- else:
- return 0
- # Simulate Kick Return
- def kickReturn(): # Returns TRUE if a kickoff is needed. Otherwise returns FALSE
- global score_AI
- global score_User
- global game_FieldPosition
- global game_FieldPositionNeededForConversion
- global game_Possession
- distanceReturned = randomizeKick()
- game_FieldPosition = distanceReturned
- game_FieldPositionNeededForConversion = game_FieldPosition+10
- if distanceReturned >= 100:
- if game_Possession == 0:
- score_AI += 7
- else:
- score_User += 7
- changePossession()
- return True
- return False
- # Function to randomize the distance the kickoff was returned for
- def randomizeKick():
- catchBallOn = random.randint(0, 20)
- choice = random.randint(1, 100)
- if (choice <= 70): # 70% Chance of a short return
- runDistance = random.randint(0, 20)
- print ("The ball was caught on the " + str(catchBallOn) + " yard line and run for "+str(runDistance)+" yards.")
- return catchBallOn+runDistance
- elif (choice <= 99): #29% chance of a long return
- runDistance = random.randint(21, 40)
- print ("The ball was caught on the " + str(catchBallOn) + " yard line and run for " + str(runDistance) + " yards.")
- return catchBallOn + runDistance
- else: # 1% Chance of a kick returned for a touchdown
- print ("The ball was caught on the " + str(catchBallOn) + " yard line and run back for a TOUCHDOWN!")
- return 100
- # Sometimes we will need to change possession of the ball
- def changePossession():
- global game_Possession
- global game_CurrentDown
- global game_FieldPosition
- global game_FieldPositionNeededForConversion
- # Change Possession
- if game_Possession == 0:
- game_Possession = 1
- else:
- game_Possession = 0
- # Reset Downs
- game_CurrentDown = 1
- # Change Field Position (To Reflect Ball Going Other Direction)
- game_FieldPosition = 100 - game_FieldPosition
- # Update Position Needed To Get First Down
- game_FieldPositionNeededForConversion = game_FieldPosition+10
- ###################################################
- ## End of Function Definitions, Game Code Begins ##
- ###################################################
- # Program Start
- # Set Up Teams
- print("WELCOME TO FOOTBALL SIMULATOR 2017!!!")
- teamName_User = raw_input("Please Enter a Team Name: ")
- teamName_AI = teamNamesArray[random.randint(0, len(teamNamesArray)-1)]
- print("Today's Match Up Will Be Between the "+teamName_AI+" and the "+teamName_User)
- # Coin Toss
- coinTossResults = coinToss()
- if coinTossResults:
- print("The "+teamName_User+" Have Won The Coin Toss!")
- else:
- print("The "+teamName_AI+" Have Won The Coin Toss!")
- game_initialReceivingTeam = kickOrReceive(coinTossResults)
- if game_initialReceivingTeam == 0:
- print("\n\nThe "+teamName_User+" Will Be Kicking Off. The "+teamName_AI+" will be receiving the ball")
- game_Possession = 0
- else:
- print("\n\nThe "+teamName_AI+" Will Be Kicking Off. The "+teamName_User+" will be receiving the ball")
- game_Possession = 1
- # Simulate Kick Return
- kickReturn()
- while game_isOver == False:
- print ("\n*******************************")
- if game_Possession == 1:
- playChoice = raw_input('What play would you like to run?\n0 = RUN, 1 = SHORT PASS, 2 = LONG PASS:')
- playChoice = int(playChoice)
- runPlay(runOffensivePlay(playChoice))
- if game_isOver == False:
- displayGameState()
- else:
- playChoice = random.randint(0,2)
- runPlay(runOffensivePlay(playChoice))
- if game_isOver == False:
- displayGameState()
- raw_input('Press Enter To Continue')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement