Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project— Rock-paper-scissors-lizard-Spock

Mini-project description — Rock-paper-scissors-lizard-Spock

Rock-paper-scissors is a hand game that is played by two people. The players count to three in unison and simultaneously "throw” one of three hand signals that correspond to rock, paper or scissors. The winner is determined by the rules:

  • Rock smashes scissors
  • Scissors cuts paper
  • Paper covers rock

Rock-paper-scissors is a surprisingly popular game that many people play seriously (see the Wikipedia article for details). Due to the fact that a tie happens around 1/3 of the time, several variants of Rock-Paper-Scissors exist that include more choices to make ties less likely.

Rock-paper-scissors-lizard-Spock (RPSLS) is a variant of Rock-paper-scissors that allows five choices. Each choice wins against two other choices, loses against two other choices and ties against itself. Much of RPSLS‘s popularity is that it has been featured in 3 episodes of the TV series "The Big Bang Theory". The Wikipedia entry for RPSLS gives the complete description of the details of the game.

In our first mini-project, we will build a Python function rpsls(name) that takes as input the string name, which is one of "rock""paper","scissors""lizard", or "Spock". The function then simulates playing a round of Rock-paper-scissors-lizard-Spock by generating its own random choice from these alternatives and then determining the winner using a simple rule that we will next describe.

While Rock-paper-scissor-lizard-Spock has a set of ten rules that logically determine who wins a round of RPSLS, coding up these rules would require a large number (5x5=25) of if/elif/else clauses in your mini-project code. A simpler method for determining the winner is to assign each of the five choices a number:

  • 0 — rock
  • 1 — Spock
  • 2 — paper
  • 3 — lizard
  • 4 — scissors

In this expanded list, each choice wins against the preceding two choices and loses against the following two choices (if rock and scissors are thought of as being adjacent using modular arithmetic).

In all of the mini-projects for this class, we will provide a walk through of the steps involved in building your project to aid its development. A template for your mini-project is available here. Please work from this template.

http://www.codeskulptor.org/#user40_8h6EmKBtPG3oWfv_0.py

# Rock-paper-scissors-lizard-Spock template

# The key idea of this program is to equate the strings
# "rock", "paper", "scissors", "lizard", "Spock" to numbers
# as follows:
#
# 0 - rock
# 1 - Spock
# 2 - paper
# 3 - lizard
# 4 - scissors

# helper functions

def name_to_number(name):
    # delete the following pass statement and fill in your code below
    if(name == "rock"):
        return 0
    elif(name == "Spock"):
        return 1
    elif(name == "paper"):
        return 2
    elif(name == "lizard"):
        return 3
    elif(name == "scissors"):
        return 4
    else:
        return "error"

    # convert name to number using if/elif/else
    # don‘t forget to return the result!

def number_to_name(number):
    # delete the following pass statement and fill in your code below
    if(number == 0):
        return "rock"
    elif(number == 1):
        return "Spock"
    elif(number == 2):
        return "paper"
    elif(number == 3):
        return "lizard"
    elif(number == 4):
        return "scissors"
    else:
        return "error"

    # convert number to a name using if/elif/else
    # don‘t forget to return the result!

def rpsls(player_choice):
    # delete the following pass statement and fill in your code below

    # print a blank line to separate consecutive games
    print ""
    # print out the message for the player‘s choice
    print "Player chooses",player_choice
    # convert the player‘s choice to player_number using the function name_to_number()
    player_number = name_to_number(player_choice)
    # compute random guess for comp_number using random.randrange()
    import random
    comp_number = random.randrange(0, 5)
    # convert comp_number to comp_choice using the function number_to_name()
    comp_choice = number_to_name(comp_number)
    # print out the message for computer‘s choice
    print "Computer chooses", comp_choice
    # compute difference of comp_number and player_number modulo five
    diff = comp_number - player_number % 5;
    # use if/elif/else to determine winner, print winner message
    if (diff == -3 or diff == -4 or diff == 1 or diff == 2):
        print "Computer wins!"
    elif (diff == 3 or diff == 4 or diff == -1 or diff == -2):
        print "Player wins!"
    elif (diff == 0):
        print "Player and computer tie!"
    else:
        print "error"

# test your code - THESE CALLS MUST BE PRESENT IN YOUR SUBMITTED CODE
rpsls("rock")
rpsls("Spock")
rpsls("paper")
rpsls("lizard")
rpsls("scissors")

# always remember to check your completed program against the grading rubric
时间: 2024-10-08 01:07:56

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project— Rock-paper-scissors-lizard-Spock的相关文章

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project — “Guess the number” game

Mini-project description — “Guess the number” game One of the simplest two-player games is “Guess the number”. The first player thinks of a secret number in some known range while the second player attempts to guess the number. After each guess, the

【python】An Introduction to Interactive Programming in Python(week two)

This is a note for https://class.coursera.org/interactivepython-005 In week two, I have learned: 1.event-drvien programing 4 event types: Input: button, textbox Keyborad: key up, key down Mouse: click, drag Timer example: # Example of a simple event-

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #4 —"Pong"

In this project, we will build a version of Pong, one of the first arcade video games (1972). While Pong is not particularly exciting compared to today's video games, Pong is relatively simple to build and provides a nice opportunity to work on the s

Coursera-An Introduction to Interactive Programming in Python (Part 1)-Mini-project #3 —"Stopwatch: The Game"

Mini-project description - "Stopwatch: The Game" Our mini-project for this week will focus on combining text drawing in the canvas with timers to build a simple digital stopwatch that keeps track of the time in tenths of a second. The stopwatch

Interactive Programming in Python Week7 Code style

1 def draw(c): 2 global paddle1_pos, paddle2_pos 3 4 paddle_width = 80 5 6 if paddle_width/2 <= paddle1_pos + paddle1_vel <= width - paddle_width/2: 7 paddle1_pos += paddle1_vel 8 if paddle_width/2 <= paddle2_pos + paddle2_vel <= width - paddl

Note 2 for &lt;Pratical Programming : An Introduction to Computer Science Using Python 3&gt;

Book Imformation : <Pratical Programming : An Introduction to Computer Science Using Python 3> 2nd Edtion Author : Paul Gries,Jennifer Campbell,Jason Montojo Page : Chapter 2.3 to Chapter 2.5 1.A type consists of two things: (1).a set of values (2).

MTH5001: Introduction to Computer Programming

projectMarch 14, 20191 MTH5001: Introduction to Computer Programming 2018/191.1 Final Report Project: "Networks"1.1.1 Instructions:First, please type your name and student number into the Markdown cell below:Name:Student number:You must write yo

Core Java Volume I — 4.1. Introduction to Object-Oriented Programming

4.1. Introduction to Object-Oriented ProgrammingObject-oriented programming, or OOP for short, is the dominant programming paradigm these days, having replaced the "structured," procedural programming techniques that were developed in the 1970s.

MITx: 6.00.1x Introduction to Computer Science and Programming Using Python Week 2: Simple Programs 4. Functions

ESTIMATED TIME TO COMPLETE: 18 minutes We can use the idea of bisection search to determine if a character is in a string, so long as the string is sorted in alphabetical order. First, test the middle character of a string against the character you'r