Home > games, python > Rock-Paper-Scissors: You vs. the Computer

Rock-Paper-Scissors: You vs. the Computer

Computers mimic human reasoning by building on simple rules and statistical averages. Test your strategy against the computer in this rock-paper-scissors game illustrating basic artificial intelligence. Choose from two different modes: novice, where the computer learns to play from scratch, and veteran, where the computer pits over 200,000 rounds of previous experience against you.” (source)

The novice opponent is not too difficult, but it learns quickly. The veteran opponent is tough though, I had the impression that it can read my mind :) The problem is that we, humans, don’t choose our next move randomly and the computer can analyze our strategy.

Choose randomly

After playing with the veteran for a while, I came up with the idea: “Hey, if I could choose randomly, the computer couldn’t analyze my moves”. So here is a simple random number generator:

C:~> python
Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import random
>>> li=[1,2,3]
>>> random.choice(li)
>>> random.choice(li)
>>> random.choice(li)

In the “python shell”, you can use the up arrow to call back the previous command(s). With this “cheat” you can surprise the veteran computer :)

I read about this game in this post (in Hungarian).

Update (20110309):

I made a very simple GUI that returns a random number from the interval [1,3] :) Jack’s remark from the comments was taken into account. I’ve never used PyGtk before, I was curious what it’s like. You can get the source code here. Tip: launch the application, right click on it on the task bar, and set “Always On Top” on. This way you can use it together with the game in the browser, you won’t have to switch between the two.

Update (20131007):
There is a Rock Paper Scissors Programming Competition, see the site http://www.rpscontest.com/. Here is an example that its author sent to reddit too.

  1. March 8, 2011 at 19:47

    Using randint is quite a bit more clear and less code.

    from random import randint

    randint(1, 3)

  1. No trackbacks yet.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: