# Code for NCEA Programming standard, level 2 # Name: Peter Andreae # This is sort of hard, I reckon! from tkinter import * import random #ScissorsPaperRock # Plays ScissorsPaperRock with multiple players. # It will play a game of five rounds with a player, # record whether the player won, drew, or lost, # and then offers to play with the next player. # # It keeps track of the scores for each player: # 3 for a win, 1 for a draw, 0 for a loss. # At the beginning of each game, it displays all the # scores of all the players, and then asks the next # player for their name. # # When playing a round, it works out the computer's choice, # asks the user for their choice, then displays # the player's choice, the computer's choice, and who won. # # # Module Design: # # playScissorsPaperRock: top level module # repeatedly displays scores, # asks for next player, # plays a game with that player # updates the score for that player. # # displayScores (players, scores) # Step through the scores lists: # for each player, print name and score to text pane # # # playGame() - plays one game, returning the players score. # initialise gameScore to 0; # repeat 5 times # call playRound, adding result to gameScore # if gameScore > 0 report player won and return 3 # if gameScore < 0 report player lost and return 0 # else report draw and return 1 # # # playRound: plays one round, returning a score # computerChoice = random number from 1 to 3 # ask player for their choice (R, P, or S) # call computeResults and store result in score # call displayChoices to display the choices and the result # return the score (-1, 0, 1) # # displayChoices(computerChoice, playerChoice, message): given two choices, displays the choices and the result # write "Player" and "Computer" along top of graphics pane # display image for playerChoice on left side of graphics pane # display image for computerChoice on right side of graphics pane # # computeResult(computerChoice, playerChoice): given two choices, computes who won # if choices are the same, return 0 # if playerChoice is better than computerChoice, return 1 # else return 0 window = Tk() canvas = Canvas(window, width=600, height=350, bg = 'white') canvas.pack() def main() : message("Scissors Paper Rock!") playScissorsPaperRock() def playScissorsPaperRock() : players = [] # list of players while True : displayScores(players) name = input("What is your name? (or exit): ") if name == "exit" : return message("Hello "+name+". Let's play a game!") score = playGame(name) if name in names : i = names.index(name) scores[i] = scores[i]+score break else : names.append(name) scores.append(score) # Step through the scores lists: # for each player, print name and score to text pane def displayScores (scores) : print() print("All scores:") for i in range(len(players)) : print(" ", players[i], " \tscore:", str(scores[i])) print() # playGame() - plays one game, returning the players score. # initialise gameScore to 0 # repeat 5 times # call playRound, adding result to gameScore # if gameScore > 0 report player won and return 3 # if gameScore < 0 report player lost and return 0 # else report draw and return 1 def playGame(name) : gameScore = 0 score = 0 for i in range(5) : gameScore = gameScore + playRound(name) print() print("Game over") if gameScore > 0 : print("You won; you get 3 points.") score = 3 elif gameScore < 0 : print("I won; you get no points.") score = 0 else : print("The game was a draw, you get 1 point.") score = 1 return score # playRound: plays one round, returning a score # computerChoice = random number from 1 to 3 # ask player for their choice (R, P, or S) # call computeResults and store result in score # call displayChoices to display the choices and the result # return the score (-1, 0, 1) def playRound(name) : rand = random.random()*3 if rand < 1 : computerChoice = "paper" elif rand > 2 : computerChoice = "scissors" else : computerChoice = "rock" # ask for the player's choice while True : print() playerChoice = input("Your choice (rock, paper, scissors): ").lower() if playerChoice[0] == "r" : playerChoice = "rock" break elif playerChoice[0] == "p" : playerChoice = "paper" break elif playerChoice[0] == "s" : playerChoice = "scissors" break print("That was not a valid choice; must be one of r, p, or s.") result = computeResult(computerChoice, playerChoice) displayChoices(name, computerChoice, playerChoice, result) return result # Given two choices, computes who won # if choices are the same, return 0 # if playerChoice is better than computerChoice, return 1 # else return -1 def computeResult(comp, player) : if comp == player : return 0 elif (player == "scissors" and comp == "paper") or (player == "paper" and comp == "rock") or (player == "rock" and comp == "scissors") : return 1 else : return -1 def message(msg) : canvas.create_rectangle(0,0,600,40, outline="white", fill="white") canvas.create_text(300,20, text=msg, font=("Helvetica", 16)) canvas.update() # Given two choices, displays the choices and the result # write "Player" and "Computer" along top of graphics pane # display image for playerChoice on left side of graphics pane # display image for computerChoice on right side of graphics pane computerX = 150 #center of computers picture playerX = 450 #center of the players picture pictureY = 160 labelY = 340 def displayChoices(name, computerChoice, playerChoice, result) : canvas.delete(ALL) canvas.update() canvas.create_text(computerX, labelY, text="Computer: "+computerChoice, font=("Helvetica", 16)) canvas.create_text(playerX, labelY, text=name+": "+playerChoice, font=("Helvetica", 16)) canvas.create_image(computerX, pictureY, image=getImage(0,computerChoice) ) canvas.create_image(playerX, pictureY, image=getImage(1,playerChoice) ) canvas.update() if result == 1 : message("You won") elif result == -1 : message("I won") else : message("It was a draw") Scissors = [PhotoImage(file="scissors-left.gif"),PhotoImage(file="scissors-right.gif")] Paper = [PhotoImage(file="paper-left.gif"), PhotoImage(file="paper-right.gif")] Rock = [PhotoImage(file="rock-left.gif"), PhotoImage(file="rock-right.gif")] def getImage(side, choice) : if choice == "scissors" : return Scissors[side] elif choice == "paper" : return Paper[side] else : return Rock[side] main()