Small Logo

Robert's Technology Journal

RSS Icon

Online home of Robert V. Bolton

You are here: Home » Programming » Rock, Paper, Scissors Python Challenge

Rock, Paper, Scissors Python Challenge

If you’ve have been following my blog lately, then you know that I have begun learning python. For the basics of Python I’ve been working my way through Core Python Programming 2nd edition by Wesley J. Chun. It’s a great book and I really enjoy reading it; my favorite parts are the exercises at the end of each chapter. These exercises are meant to reinforce the concepts presented throughout the chapters, but a few of the exercises are to challenge the reader. One such exercises I recently completed was the Rock, Paper, Scissors game. If your not familiar with Rock, Paper, Scissors consider yourself cheated out of a childhood (Here’s the Wikipedia article). I’m not going to cover my entire program just highlight a few key areas (The complete Source Code is located here). Also stick around I have a new challenge I’m working on to bring this game up to the next level.

Random Computer Weapon Selection

At first I thought I would need to assign numeric values to rock,paper, and scissors then randomly select a number using the randrange method. Then I discovered the choice method. This method randomly choices something from a list. All I had to do was create a list with rock, paper, and scissors and use the choice method to assign a variable containing the computer’s choice and I was good to go. Check it out below, and don’t forget to import random.

import random
 
def computerChoice():
    computerWeapons = ['Rock','Paper','Scissors']
    computerWeapon = random.choice(computerWeapons)
    print 'Computer has selected %s' % computerWeapon
    return computerWeapon

Deciding Who Wins

I admit before I finished this exercise I looked at a few examples others have done; just to get a few ideas of course. Every example I could find had a large if/else block that was used to determine the winner. I knew their had to be an easier way, so I went back to basics. I broke the problem down into yes or no questions. For example if a player selected a rock I needed to know if the computer selected paper because that’s the only thing that could beat the player. By using this logic I first tested which weapon the player selected and then tested to see if the computer had a selected a weapon that could beat it. All of this fit nicely into 19 lines of code. A far better approach then the other examples I had found.

def determineWinner(playerWeapon,computerWeapon):
    if playerWeapon == computerWeapon:
        winner = 'Draw'
    else:
        if playerWeapon == 'Rock':
            if computerWeapon == 'Scissors':
                winner = 'Player'
            else:
                winner = 'Computer'
        elif playerWeapon == 'Paper':
            if computerWeapon =='Rock':
                winner = 'Player'
            else:
                winner = 'Computer'
        else:
            if computerWeapon == 'Paper':
                winner = 'Player'
            else:
                winner = 'Computer'
    return winner

Bring on the Next Challenge

To bring this game up a notch I challenged myself to use 25 weapons instead of the three traditional weapons. You can find a list of the outcomes for each weapon here. Just so you know there are 300 possible outcomes for the game. I think I have an easy way of determining a winner, but I would love to see what others come up with. So if you think you can solve this problem let me know how you do.

No TweetBacks yet. (Be the first to Tweet this post)
Share and Enjoy:
  • Digg
  • del.icio.us
  • Reddit
  • StumbleUpon
  • Google Bookmarks
  • Ping.fm
Trackbacks: http://www.robertvbolton.com/rock-paper-scissors-python-challenge/trackback/

About Robert V. Bolton

Robert (KE7ZEA) lives with his wife and two boys along the Wasatch Front in the small community of Syracuse, Utah. He currently works for the Center for High Performance Computing at the University of Utah as a Unix/Macintosh Systems Administrator. His interest include open source software, cluster computing, and amateur radio.

One Response

  1. [...] This post was mentioned on Twitter by Robert V Bolton, GB7MBC. GB7MBC said: (Robert): Rock, Paper, Scissors Python Challenge: If you’ve have been following my blog lately, then you know tha http://url4.eu/10RMu [...]

Leave a Reply

Related Post

Popular Articles

Other Topics of Intrest

  • Amateur Raido

    Covering a range of amateur radio topics including radio projects, antenna design, and radio operations.

  • Apple

    All things Apple, but with more focus on the UNIX side of OS X.

  • Conferences

    Information, updates, and reviews of technology conferences that I attend.

  • Everyday Life

    Insight into the technology that effects my everyday life.

  • Fedora

    An RPM-based, general purpose operating system built on top of the Linux kernel, developed by the community-supported Fedora Project and sponsored by Red Hat Topics are related to my involvement with the Fedora project.

  • Programming

    A journey into the realm of computer programing. Topics can include programming languages, techniques, and theories of computation.

  • Technology

    Insight into the technology that effects our everyday life. Topics can include social media, tends in personal technology, and the effect of technology on society.