Created
October 12, 2014 01:25
-
-
Save billychappell/2b25e8666bfa8a7769bc to your computer and use it in GitHub Desktop.
RPS AI -- Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
#Setting initial variables | |
winCount = 0 | |
throwCount = 0 | |
lossCount = 0 | |
drawCount = 0 | |
#The player's previous selection determines the current state for the next game. The initial state is used as the current state for the first game where there is no "last move." | |
initialState = ["rock", "paper", "scissors"] | |
rockState = ["rock", "paper", "scissors"] | |
paperState = ["rock", "paper", "scissors"] | |
scissorState = ["rock", "paper", "scissors"] | |
#Setting the current state to initial state for the first game. | |
currentState = initialState | |
#Declaring main function. Computer throws first, then player. Throws are compared and results for current game are determined. Final results are used to record player's choice in current state and switch to new state for next round. | |
def main(): | |
print("Let's play Rock, Paper, Scissors!") | |
compThrow = getCompGuess() | |
playerThrow = getPlayerThrow() | |
outcome = getOutcome(compThrow, playerThrow) | |
results = getResults(outcome, compThrow, playerThrow, winCount, lossCount, drawCount) | |
print("W/L/D") | |
print(results) | |
getNewState(playerThrow, rockState, paperState, scissorState, currentState) | |
restart() | |
#The computer uses the current state to predict player's next move based on his/her choices in the past. | |
def getCompGuess(): | |
compGuess = random.choice(currentState) | |
#Based on computer's prediction, the appropriate counter-move is chosen. | |
if compGuess == "rock": | |
compThrow = "paper" | |
elif compGuess == "paper": | |
compThrow = "scissors" | |
elif compGuess == "scissors": | |
compThrow = "rock" | |
return compThrow | |
#Asks player to input his/her selection. | |
def getPlayerThrow(): | |
playerThrow = input('Choose your weapon: rock, paper or scissors') | |
return playerThrow | |
#Compares the computer's choice against the player's to determine a winner. | |
def getOutcome(compThrow, playerThrow): | |
if compThrow == "rock" and playerThrow == "paper": | |
print("You win!") | |
return "You win!" | |
elif compThrow == "paper" and playerThrow == "scissors": | |
print("You win!") | |
return "You win!" | |
elif compThrow == "scissors" and playerThrow == "rock": | |
print("You win!") | |
return "You win!" | |
elif compThrow == "scissors" and playerThrow == "paper": | |
print("You lose!") | |
return "You lose!" | |
elif compThrow == "paper" and playerThrow == "rock": | |
print("You lose!") | |
return "You lose!" | |
elif compThrow == "rock" and playerThrow == "scissors": | |
print("You lose!") | |
return "You lose!" | |
elif compThrow == playerThrow: | |
print("Draw!") | |
return "Draw!" | |
def getResults(outcome, compThrow, playerThrow, winCount, lossCount, drawCount): | |
if outcome == "You win!": | |
winCount += 1 | |
elif outcome == "You lose!": | |
lossCount += 1 | |
elif outcome == "Draw!": | |
drawCount += 1 | |
return "%s/%s/%s" % (winCount, lossCount, drawCount) | |
def getNewState(playerThrow, rockState, paperState, scissorState, currentState): | |
if playerThrow == "rock": | |
currentState.extend("rock") | |
return currentState | |
return currentState | |
elif playerThrow == "scissors": | |
currentState.extend("scissors") | |
currentState = scissorState | |
return currentState | |
elif playerThrow == "paper": | |
currentState.extend("paper") | |
currentState = scissorState | |
return currentState | |
def restart(): | |
answer = input("Would you like to play again? y/n") | |
if answer == "y": | |
main() | |
elif answer == "n": | |
print("Goodbye!") | |
else: | |
print("Sorry! I didn't understand your answer. Enter y for yes or n for no.") | |
restart() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment