AI Vision with PictoBlox, lesson 12 of 15

Rock, paper, scissors against the AI

intermediate8 min read

Introduction

You've trained a model that knows rock, paper and scissors. Now let's play! In this lesson Tobi counts down "rock… paper… scissors… shoot!", reads your hand sign with your model, makes his own random choice, and keeps score.

The model recognises your sign: rock. The computer picks at random, then the rule book (your if blocks) decides, like a referee: rock beats scissors, scissors beat paper, paper beats rock.

Working principle

  • The model reports your sign as a class: rock, paper, scissors or nothing.
  • Tobi picks his sign with pick random (1) to (3), turned into a word using a list.
  • The rules are a set of if blocks that compare the two signs.
  • A countdown gives you time to make your sign, and we only read the camera at the moment you "shoot".
  • Fair play: Tobi picks before reading your hand, so he can't cheat!

What you'll need

  • The RPS model you trained and exported in the last lesson
  • PictoBlox with a webcam
  • Three costumes for Tobi, or a new sprite, showing rock, paper and scissors (draw them in the Costumes tab)

Working in real life

  1. Open the project with your exported RPS model.
  2. Make a list called signs with three items: rock, paper, scissors.
  3. Make variables: tobi, you, you win, my score, tobi score.
  4. Draw three costumes for a sprite called Tobi Hand, named exactly rock, paper and scissors.
  5. Build the script below and click the green flag. Press space for each round.

Code

when green flag clicked
turn [on] video on stage with (0) % transparency
set [my score v] to (0)
set [tobi score v] to (0)
 
when [space] key pressed
say [Rock…] for (0.6) seconds
say [Paper…] for (0.6) seconds
say [Scissors…] for (0.6) seconds
set [tobi v] to (item (pick random (1) to (3)) of [signs v])
analyse image from [camera]
set [you v] to (get identified class)
switch costume to (tobi)
say [Shoot!] for (0.5) seconds
 
set [you win v] to [no]
if <<(you) = [rock]> and <(tobi) = [scissors]>> then
  set [you win v] to [yes]
end
if <<(you) = [scissors]> and <(tobi) = [paper]>> then
  set [you win v] to [yes]
end
if <<(you) = [paper]> and <(tobi) = [rock]>> then
  set [you win v] to [yes]
end
 
if <(you) = [nothing]> then
  say [I didn't see your hand!] for (2) seconds
else
  if <(you) = (tobi)> then
    say [It's a draw!] for (2) seconds
  else
    if <(you win) = [yes]> then
      change [my score v] by (1)
      say (join [You win! ] (join (you) (join [ beats ] (tobi)))) for (2) seconds
    else
      change [tobi score v] by (1)
      say (join [I win! ] (join (tobi) (join [ beats ] (you)))) for (2) seconds
    end
  end
end

The three if blocks in the middle are the rule book: one for each way you can win.

Try it

  • Make the game first-to-five: when someone reaches 5, announce the champion and stop.
  • Make Tobi sneaky: instead of picking randomly, Tobi plays whatever would have beaten your last sign. Can you still beat him?
  • Show your sign too slowly on purpose. What does the model see? How could you make the game fairer?

Check what you learnt

  1. In this game, what is the AI model's job?

  2. Why does Tobi pick his sign before reading your hand?

  3. You show paper and Tobi shows rock. Who wins?

Conclusion

  • A trained model can be used in any project, just like the ready-made AI blocks.
  • The AI sees; your code (the if blocks) decides what to do with what it sees.
  • Timing matters: read the camera at the right moment.

What next?

Make AI control something real: AI switches on a light: PictoBlox with Arduino.