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

How Do I write a Bot for the Rock, Scissors and Paper Challenge?

By , About.com Guide

This Challenge finished in March 2008 and is no longer run

I thought I'd encourage people to enter by describing how to write a Bot; it's actually very easy but because it is a dll, I could understand people maybe thinking it was difficult. The challenge is described in the main article below and if you haven't seen the weekly results, take a look.

There is nothing magical about a Windows dll. It is just a file like an exe except you can't run it. It contains code such as functions and variables. When you create a dll, you describe exactly what is made visible to the outside world. Which functions can be called from a program. These are known as export or interface functions and only these can be called by an external caller.

Because Windows has evolved over a twenty year period in nearly a dozen versions running on many different 16, 32 and 64 bit processors, there has to be a degree of consistency in dll functions that you write and how those functions are called. It doesn't matter if you write your dll in C, C++ or even C#, it must stick to the specified convention which is called cdecl.

Bot Interface

This is the interface for the C and C++ bot.
#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) ;
The SKELETON_API #define tells the compiler that any function defined with it has the text __declspec(dllexport) added to the function. This means that this function is exported from the dll. If you wish any program to call the dll functions, those functions must be exported. You may also read about the use of a .DEF file but this way avoids the need for that.

char * __cdecl GetBotName(void)
This is just a function header for the function GetBotName(void). The word __cdecl means that the compiler generates special code whenever GetBotName() is called. In the sample skeleton.cpp (or .c) this is what the function looks like. Put anything you like (nothing rude please!) in the string.
SKELETON_API char * __cdecl GetBotName(void) {
  'return "Edward Rockhands(C++)";
}
I did consider Edward Paperhands as well! Likewise you should fill in text strings for GetPlayerDetails() and GetBotDate(). GetBotVersion returns the version number as a float like 1.2f. Every time you change your bot, up the version number. A good scheme is to update the value after the decimal point for minor bug fixes and changes and the value before for major changes or rewrites.

Game Play

This is all done in the function GetMove(char * Moves). Passed in is a text string which is (hand#)-1 chars long. On the first hand, it is 0 chars long, on the 100th it is 99. There are 100 hands in each match.

Note From August 26 2007, a second parameter will be added to GetMove(). It will become:

SKELETON_API char GetMove(char * Moves,char * MyMoves) ;
MyMoves is the same as Moves except it is your moves in the current match.

When this function exits, it must return 'R', 'S' or 'P' as the result. So writing a bot boils down to this:

  1. Flesh out the 4 information functions. Three lines of text plus a number!
  2. Devise some algorithm to generate 'R', 'S' or 'P' from your opponent's (and/or) your moves.
There are very many ways to do this- the fun is discovering which is best, or at least beats the other bots!

MyCSharpBot in the contest is one I wrote and it is a reference bot, i.e. it will never change. Written in C#, the GetMove() function looks like this below. All it does is total up the opponent's moves and if the opponent played rock more often then it chooses paper, if the opponent chose paper more often the scissors was chosen otherwise it picked rock.

public static char GetMove(String Moves)
{
   int r = 0;
   int s = 0;
   int p = 0;
   for (int i=0;i<Moves.Length;i++) {
        if ( Moves[ i ]=='R' ) r++;
        if ( Moves[ i ]=='S' ) s++;
        if ( Moves[ i ]=='P' ) p++;
       }
   if (r > p && r > s)
       return 'P';
   else
     if (p > s && p > r)
           return 'S';
         else
           return 'R';
}
By comparison, Darko's first bot did this!
public static char GetMove(String Moves)
if ( Moves.Lengtgth==0)
   return 'R';
returns (Moves[Moves.Length-1]); }
I.e. apart from the first move it just used the opponents last move. It didn't do too badly. Mind you his next bot was almost 400 lines long and was top in week 2.

You can find exact specifications and downloadable skeleton dlls for C, C++ and C# in the links below.

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

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

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

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. Projects
  5. Programming Challenges
  6. How Do I write a Bot for the Rock, Scissors and Paper Challenge?>

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

All rights reserved.