Small Basic Game Programming – Let’s start with RPS game

I’m going to write about game programming in Small Basic for several weeks on this blog.  For the first time, I’d like to pick up simple text base RPS (Rock-Paper-Scissors) game.

Main loop

This code is the main loop of this game.  Lines 4-6 are one game.  And While condition is always “True”, so this is endless loop to repeat games.

 1.' Rock Paper Scissors
 3.While "True"
 4.  CPU()
 5.  You()
 6.  Judge()


Subroutine Init is to initialize for arrays.  These arrays need only one time initialization out of the main loop.  The array hand is to input numbers.  The 2-D array judge is for game judge.

 8.Sub Init
 9.  hand = "1=Rock;2=Paper;3=Scissors;"
10.  judge["Rock"] = "Rock=Draw;Paper=Win;Scissors=Lose;"
11.  judge["Paper"] = "Rock=Lose;Paper=Draw;Scissors=Win;"
12.  judge["Scissors"] = "Rock=Win;Paper=Lose;Scissors=Draw;"

Random number

In game programs, we call game AI (Artificial Intelligence) for the strategic routine for CPU side.  In game AI, random number is used sometimes.  In Small Basic, Math.GetRandomNumber() operation generates a random number.  In following subroutine, a number from 1 to 3 is generated and set into a variable n. 

14.Sub CPU
15.  n = Math.GetRandomNumber(3)
16.  cpu = hand[n]

Input number

Following routine is for human side.  Te make it easier, this requires only number input for your hand.  For displaying input number just after the prompt, TextWindow.Write() operation is used instead of TextWindow.WriteLine() operation.  And to ensure the input is a number, TextWindow.ReadNumber() operation is used instead of Text.Window.Read() operation.  In line 23, if n is not from 1 to 3, null text “” is set to the variable you.

18.Sub You
19.  error = "True"
20.  While error
21.    TextWindow.Write("1(Rock), 2(Paper), 3(Scissors)? ")
22.    n = TextWindow.ReadNumber()
23.    you = hand[n]
24.    If you <> "" Then
25.      error = "False"
26.    EndIf
27.  EndWhile

Judge the game

Judgement is very easy.  That is just referring an array judge.

29.Sub Judge
30.  ' param you - your hand
31.  ' param cpu - CPU's hand
32.  TextWindow.WriteLine("CPU:" + cpu)
33.  TextWindow.WriteLine("You:" + you)
34.  TextWindow.WriteLine(judge[cpu][you])

Whole program is published as GXP440-0.  This game has only 35 lines.  Using technique introduced here, you may create new games something like rolling dice or so.  Let’s start game programming!

Comments (5)

  1. RPS – Role Playing Scissors?

    Great game, Nonki1 Thanks for breaking it up and explaining it like this!

  2. anonymouscommenter says:

    Nice idea.  How would you make it so the variables themselves are Rock/Paper/Scissors and the game act on these strings? In what ways would make the game better or worse?

  3. Thanks, Dave.  I think there are two points of view about programming.  One is for user, the other is for programmer.  For both, being simple makes program easier.  But simple for user and simple for programmer are different.  I used an array judge for programmer side point of view.  I had an option to use If-Then-Else to judge game instead of this array.  In addition, for both point of view, it is very important to write correct program which works properly.

  4. Sean Liming says:

    How random is the random number generator? Or does this follow the typical pseudo random generator design?

  5. Hello Sean.

    I'm not sure about inside of Small Basic.  But my guess is as follows.

    Math.Random may use .NET Random Class.  You can check about .NET Random Class in:…/system.random%28v=vs.110%29.aspx