1. Technology

How to write a Rock Scissors Paper Bot in C or C++


To enter the Ongoing Programming Challenge #1 you just have to supply a single source code file called skeleton.c or skeleton.cpp. The rest of this article describe show to write it.

When you email it in, I will 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.c or skeleton12.cpp - 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.zip (updated 28 Aug 2007) which contains skeleton.h and both skeleton.c and skeleton.cpp so you can choose your preferred programming language. These have been set up to make life easier for you.

There are 5 functions that you need to flesh out. The first four are purely for reporting purposes. It's the fifth one GetMove() that needs all the work.

 #define SKELETON_API __declspec(dllexport)
 SKELETON_API char * __cdecl GetBotName(void) ; // returns name of your Bot
 SKELETON_API char * __cdecl GetPlayerDetails(void) ; // returns name of your Bot
 SKELETON_API char * __cdecl GetBotDate(void) ; // return date of your bot (ie last change).
 SKELETON_API float __cdecl GetBotVersion(void) ; // return version of your bot
 SKELETON_API char GetMove(char * Moves,char * 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 Moves is 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). As each hand is played (there are 100 hands per match), the hand played from each player is appended to a string 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, Moves will be 0 length. Moves is a zero byte terminated, i.e. char * (i.e. a text string) or an an array of bytes.

Please do not change anything that would break the code. In particular do not add any calling conventions to functions as that will hide the functions.

  • For a bit of background, you can read a mini tutorial about Calling Conventions but they aren't needed for this. All the functions use cdecl.
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.


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.

Testing Your DLL

Download this zip file (Updated 29 Aug 2007). It contains an Win 32 exe (compiled by me on a virus free PC) which you should extract to a folder. It is for Windows, 2000, XP. It has not been tested on Microsoft Vista but if anyone can test it and let me know it's ok or not.

When you run it, select the folder where your skeleton.dll is located. 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 'R'.

 Botname = MyBot
 Date = 2007/08/10
 Version = 1.0
 Player Details = David Bolton(UK) Aged 48
 Move 1 = R
 Move 2 = R
 Move 3 = R
 Move 4 = R
 Move 5 = R
 Move 6 = R
 Move 7 = R
 Move 8 = R
 Move 9 = R
 Move 10 = R
If any function, say GetBotName() is misspelt, you'll see lines output like this:
 Unable to find GetBotName 
That usually means you've put the name in wrong or added a calling convention.

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

Good luck!

  1. About.com
  2. Technology
  3. C / C++ / C#
  4. Projects
  5. Ongoing Challenges
  6. How to write a Rock Scissors Paper Bot in C or C++

©2014 About.com. All rights reserved.