1. Home
  2. Computing & Technology
  3. C / C++ / C#

How to write a Rock Scissors Paper Bot in C#

By David Bolton, About.com

This Challenge finished in March 2008 and is no longer run

To enter the Ongoing Programming Challenge #1 you just have to supply a single source code file called skeleton.cs. The rest of this article describe show to write it. Most of the hard work is written- you just have to download the file, add your code and email it to me.

Note:You can enter your bot at any time until the contest finishes. It doesn't matter that the contest has already started. Positions are judged on the last week's results.

When you email it in, I'll assign you a number and email you that number back. If in future weeks you alter your bot and resubmit it, then when resending, please send the file with your entry number appended like skeleton5.cs that is your bot's "Shirt Number" in soccer or football parlance! You can give your bot a name, up to 20 characters; just alter the text in the GetBotName() function, but please keep names clean. Naughty or bad names will be disqualified before they get a chance to show what they're made of.

You must download this file- skeleton.cs (updated 14 August 2007) which is the C# source code file.

There are 5 functions that you need to flesh out, plus a DebugStr string property for debugging. The first four functions are purely for reporting purposes. Eg give your bot a name in GetBotName(), your name, plus age, country in GetPlayerDetails() etc (you don't have to provide the country or age if you don't want!). It's the fifth function GetMove() that needs all the work.

public static string GetBotName()
public static string GetPlayerDetails()
public static float GetBotVersion()
public static string GetBotDate()
char GetMove(String Moves, String MyMoves);
GetMove() must return a single uppercase character
  • 'R' for Rock
  • 'S' for Scissors
  • 'P' for Paper
Returning anything else will lose the hand!

The parameter a string that contains a list of the opponent's previous hands - each a character 'R', 'S', 'P' or ' ' (a single space for forfeit hands) plus a count of the moves. As each hand is played (there are 100 hands per match), the hands played from each player are kept and passed in to the GetMove() call.

E.g. After 5 hands have been played the string might look like this: Moves : RSPRR

Don't forget, on the first hand, the length of Moves will be 0.

The program that runs the ladder each week maintains a list of players and their scores. Once a week, the manager program will process the list from top to bottom, playing each one against the one below it in a game of 100 hands and totalling how many hands were won and lost. Each hand won gains a point, and you lose a point for each hand lost or 2 points if you forfeit. So your final score will be between 200 and -400. Everyone except the first and last position (from the previous week) will then have their score halved. The first and last positions only played half the number of hands that everyone else did.

The scores are then sorted, with a point added here or there so no two players have exactly the same score and that determines the ranking for next week which will be published. Whoever is in first place will have their code made available for download. Although it's a ladder, the position is determine purely by the number of hands won and lost. You can submit a new bot whenever you like but please try and limit it to once per week. New bots (only one per player) can join the contest at any time.

Forefeiting

A hand is forfeit if your bot cannot return a move: 'R', 'S' or 'P' within three seconds.

Devising a strategy

You could pick a move randomly, or perhaps use your opponent's last but one move, or try and analyze all their previous moves and attempt to anticipate the next one.

Why a DLL?

I wanted the Match runner application to work more or less the same way, regardless of the underlying language. I've written the Match runner in C# and it uses a wrapper class for the C and C++ versions and loads the C# version directly.

Testing Your DLL

Download this zip file (updated 29 August 2007). It contains a small .NET 2.0 exe that calls the dll's functions. Note by default, it expects the file skeleton.dll to be located in a folder called c:\cptest. I've also uploaded the full project files (updated 29 August 2007 ) with source code for this test program.

When you run it, it will call each of the 4 Information functions first. Then it will call GetMove() ten times with random moves and output the results to a text file called results.txt and will look like this. My sample dll's GetMove() just returns 'S'. The output will look something like this.

Bot Name = MyCSBot
Bot Version = 1.0
Bot Date = 4/8/2007
Player Details = David Bolton(UK) Aged 48
Move 1 = S
Move 2 = S
Move 3 = S
Move 4 = S
Move 5 = S
Move 6 = S
Move 7 = S
Move 8 = S
Move 9 = S
Move 10 = S

If it runs ok, you can submit the source code file. To cplus.guide@about.com?subject=RSP Contest Entry

Good luck!

Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. Projects
  5. Ongoing Challenges
  6. How to write a Rock Scissors Paper Bot in C#>

©2009 About.com, a part of The New York Times Company.

All rights reserved.