Task

Implement the classic children's game Rock-paper-scissors, as well as a simple predictive   AI   (artificial intelligence)   player.

Rock Paper Scissors is a two player game.

Each player chooses one of rock, paper or scissors, without knowing the other player's choice.

The winner is decided by a set of rules:

   Rock beats scissors
   Scissors beat paper
   Paper beats rock


If both players choose the same thing, there is no winner for that round.

For this task, the computer will be one of the players.

The operator will select Rock, Paper or Scissors and the computer will keep a record of the choice frequency, and use that information to make a weighted random choice in an attempt to defeat its opponent.



Extra credit

Support additional choices   additional weapons.




 
dim rps( 2), g$( 3)
 
g$( 0) ="rock": g$( 1) ="paper": g$( 2) ="scissors"
global total
 
total =0: draw  =0: pwin  =0: cwin  =0
 
rps( 0) =1: rps( 1) =1: rps( 2) =1          '   at first computer will play all three with equal probability
 
c =int( 3 *rnd( 1))                         '   first time, computer response is random
 
'   In the following code we set three integer %ages for the <human>'s biassed throws.
                                            '   set up the <human> player's key frequencies as %ages.
                                            '   Done like this it's easy to mimic the <human> input of q$
                                            '   It allows only integer %age frequency- not an important thing.
rockP =45                                   '   <<<<<<<<< Set here the <human>'s %age of 'rock' choices.
for i =1 to rockP: qF$ =qF$ +"R": next i
 
paprP =24                                    '   <<<<<<<<< Set here the %age of 'paper' choices.
for i =1 to paprP: qF$ =qF$ +"P": next i
 
scisP =100 -rockP -paprP                    '   <<<<<<<<< %age 'scissors' calculated to make 100%
for i =1 to scisP: qF$ =qF$ +"S": next i
'print qF$
 
do
  'do: input q$:loop until instr( "RPSrpsQq", q$)   '   for actual human input... possibly biassed.         <<<<<<<<<<<<< REM one or the other line...
  q$ =mid$( qF$, int( 100 *rnd( 1)), 1)              '   simulated <human> input with controlled bias.      <<<<<<<<<<<<<
 
  if total >10000 then q$ ="Q"
 
  g =int( ( instr( "RrPpSsQq", q$) -1) / 2)
  if g >2 then [endGame]
 
  total    =total   +1
  rps( g)  =rps( g) +1  '   accumulate plays the <human> has chosen. ( & hence their (biassed?) frequencies.)
  'print "  You chose "; g$( g); " and I chose "; g$( c); ". "
 
  select case g -c
    case 0
      draw =draw +1  ':print "It's a draw"
    case 1, -2
      pwin =pwin +1  ':print "You win!"
    case -1, 2
      cwin =cwin +1  ':print "I win!"
   end select
 
   r =int( total *rnd( 1))      '   Now select computer's choice for next confrontation.
   select case                  '   Using the accumulating data about <human>'s frequencies so far.
        case r <=rps( 0)
            c =1
        case r <=( rps( 0) +rps( 1))
            c =2
        case else
            c =0
   end select
 
   scan
loop until 0
 
[endGame]
    print
    print "  You won "; pwin; ", and I won "; cwin; ". There were "; draw; " draws."
    if cwin >pwin then print "      I AM THE CHAMPION!!" else print "        You won this time."
    print
    print "  At first I assumed you'd throw each equally often."
    print "  This means I'd win 1 time in 3; draw 1 in 3; lose 1 in 3."
    print "  However if I detect a bias in your throws,...."
    print "  ....this allows me to anticipate your most likely throw & on average beat it."
    print
    print "  In fact, keyboard layout & human frailty mean even if you TRY to be even & random..."
    print "  ... you may be typing replies with large bias. In 100 tries I gave 48 'P's, 29 'R' & 23 'S'!"
    print
    print "    This time I played.."
    print "      Rock "; using( "##.##", rps( 2)* 100 /total); "%    Paper "; using( "##.##", rps( 0) *100 /total); "%    Scissors "; using( "##.##", rps( 1) *100 /total); "%."
    print
    print "  ( PS. I have since learned your actual bias had been.."
    print "      Rock "; using( "##.##", rockP); "%    Paper "; using( "##.##", paprP ); "%    Scissors "; using( "##.##", ( 100 -rockP -paprP)); "%.)"
    print
    print "  The advantage I can gain gets bigger the further the 'human' entries are biassed."
    print "  The results statistically smooth out better with large runs."
    print "  Try 10,000,000, & have a cuppa while you wait."
    print
    print "  Can you see what will happen if, say, the 'human' is set to give 'Rock' EVERY time?"
    print "  Try different %ages by altering the marked code lines."
    print
    print "  Thanks for playing!"
    end
 