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.
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.
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.
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.
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.
Judge the game
Judgement is very easy. That is just referring an array judge.
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!