Shop OBEX P1 Docs P2 Docs Learn Events
Rock Paper Scissors — Parallax Forums

Rock Paper Scissors

xanaduxanadu Posts: 3,347
edited 2014-09-04 22:31 in Robotics
Basic Stamp 2 vs Propeller, rock paper scissors style. Both are generating random numbers assigned to rock, paper and scissors. The BS2 uses a CdS photo-resistor and capacitor to seed its random function. The Propeller uses Real Random object to induce and measure PLL jitter, which changes with temperature. The outcome of game is effected by both ambient light, temps, and some luck, probably.


The BS2 side listens via half-duplex serial for the Propeller to tell it to play a round. When told the BS2 samples the RC circuit and seeds a random number. The output of that number corresponds with a numerical value for rock, paper or scissors which it then sends back to the Propeller.


After the Propeller collects the outcome of the BS2’s turn it generates its own random number. The propeller compares the BS2’s result with its own, and scores the round accordingly.


For serial, Stamp pin 15 is connected in to Propeller pin 0 using a 2.2k in series and a 4.7k tied to 3.3v to pull it high. I used the Simple Serial OBEX object and SEROUT BS2 command.


And now, let the games begin!

[video=youtube_share;o0TqaFko5Us]


IMG_0284.JPG


BS2:
' {$STAMP BS2}
' {$PBASIC 2.5}


'BS2 Player Program


'Cons


RC PIN 0


btx  CON 15
baud   CON $54
timeout CON 3000




'VARs




command  VAR Word
result  VAR Byte




DO
  SERIN btx, baud, timeout, fail, [command]          'receive from propeller
  PAUSE 20
  IF command = 4 THEN GOSUB Run
LOOP


Fail:
   RETURN


Run:
  HIGH RC                             ' charge the cap
  PAUSE 1                             '   for 1 ms
  RCTIME RC, 1, result                ' measure RC discharge time
  PAUSE 50
  RANDOM result                         ' generate random number
  PAUSE 1000
  IF result =>  0 AND result < 50 THEN GOSUB rock
  IF result => 50 AND result < 100 THEN GOSUB paper
  IF result => 100 AND result < 150 THEN GOSUB scissors
  IF result => 150 AND result < 200 THEN GOSUB rock
  IF result => 200 AND result < 250 THEN GOSUB paper
  IF result => 250 AND result < 300 THEN GOSUB scissors
  RETURN


rock:
  SEROUT btx, baud, [1]           'send 1 to prop
  PAUSE 1000
  HIGH 3
  PAUSE 500
  LOW 3
  RETURN


paper:                            'send 2 to prop
  SEROUT btx, baud, [2]
  PAUSE 1000
  HIGH 4
  PAUSE 500
  LOW 4
  RETURN


scissors:                         'send 3 to prop
  SEROUT btx, baud, [3]
  PAUSE 1000
  HIGH 5
  PAUSE 500
  LOW 5
  RETURN



Propeller:
{{


}}


CON


  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000


 EMICsIN  = 14               'EMIC II Serial In
 EMICsOUT = 15               'EMIC II Serial Out


VAR


 byte rrr
 word number
 byte BS2round
 byte PropRound
 byte BS2score
 byte Propscore




OBJ


  pst     : "Parallax Serial Terminal"
  rr      : "RealRandom"
  Serial  : "FullDuplexSerial"
  bs2     : "Simple_Serial"
  lcd     : "SparkFun_serial_lcd"        




PUB start 


  lcd.init(1,9600,2,20)
  lcd.displayOn               
  lcd.cursor(0)
  lcd.backlight(true)
  lcd.clrln(1)
  lcd.clrln(0)
 
  lcd.str(string("STAMP    PROP"))


 
 dira [16] := 1 'Rock LEDS
 dira [17] := 1 'Paper
 dira [18] := 1 'Scissors


   bs2score := 0
  propscore := 0
 
 rr.start                        'start RealRandom


 pst.Start(9600)               'start PST
 
 '---Init EMIC II---
  serial.Start(EMICsOUT, EMICsIN, %0000, 9_600)         'Start serial port, normal mode, 9600bps
  serial.TX(pst#NL)                                     'Send a CR in case the system is already up
  repeat until serial.RxCheck == ":"                    'When the Emic 2 has initialized and is ready, it will send a single ':' character, so wait here until we receive it
  
  waitcnt(clkfreq / 100 + cnt)                          'Delay 10mS
  serial.RxFlush                                        'Flush the receive buffer
  
  serial.Str(String("N0", pst#NL))                      'voice   *try n4
  repeat until serial.RxCheck == ":"
  
  serial.Str(String("V18", pst#NL))                      'volume 18 max
  repeat until serial.RxCheck == ":"
  
  serial.Str(String("W300", pst#NL))                    'words per minute
  repeat until serial.RxCheck == ":"


  serial.TX("S")
  serial.Str(string("\/\/\/\/\/\/ reboot "))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"


  begin
  
  program








PUB Program


  propround := 0
  bs2round  := 0
  
  startgame
   
  repeat


    listen




PUB StartGame


  BS2.init(-1, 0, 9600)         'start serial send


  BS2.tx(4)


  waitcnt(clkfreq*1+cnt)




PUB Listen | com


 BS2.init(0, -1, 9600)         'start serial rx 


    
 repeat
 
    com := BS2.rx


    if com == 1
                                                   ' rock
         BS2round := 1
         bs2rock
         main
         
    if com == 2
                                                   ' paper
         BS2round := 2
         bs2paper
         main
         
    if com == 3
                                                   ' scissor
         BS2round := 3
         bs2scissors
         main 
     
PUB Score


 if bs2round == propround 'tie
   tie


   
 if bs2round == 1 and propround == 2   'rock vs paper WIN
   PropScore := Propscore + 1
   propwins


 
 if bs2round == 1 and propround == 3   'rock vs scissors LOSE
   BS2Score := BS2Score + 1
   bs2wins
   
 if bs2round == 2 and propround == 1   'paper vs rock LOSE
   BS2Score := BS2Score + 1
   bs2wins
   
 if bs2round == 2 and propround == 3   'paper vs scissors WIN
   PropScore := Propscore + 1
   propwins
   
 if bs2round == 3 and propround == 1   'scissors vs rock WIN
   PropScore := Propscore + 1
   propwins
   
 if bs2round == 3 and propround == 2   'scissors vs paper LOSE
   BS2Score := BS2Score + 1
   bs2wins


 Pst.Str(string(" BS2Score: "))
 pst.dec(bs2score)
 
 Pst.Str(string(" Prop Score: "))
 pst.dec(propscore)


 lcd.gotoxy(0, 1)               
 lcd.dec(bs2score)


 lcd.gotoxy(9, 1)               
 lcd.dec(propscore)






 program


PUB tie


  serial.TX("S")
  serial.Str(string("\/\/ Tie "))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"
  waitcnt(clkfreq*1+cnt) 


PUB bs2wins


  serial.TX("S")
  serial.Str(string("\/\/ Stamp Wins "))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"
  waitcnt(clkfreq*1+cnt)


PUB propwins


  serial.TX("S")
  serial.Str(string("\/\/\/ Propeller Wins "))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"
  waitcnt(clkfreq*1+cnt)   


PUB bs2Rock


  serial.TX("S")
  serial.Str(string("\/\/\/ Stamp Rock "))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"


PUB bs2Paper
 
  serial.TX("S")
  serial.Str(string("\/\/\/ Stamp Paper "))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"


PUB bs2Scissors
   
  serial.TX("S")
  serial.Str(string("\/\/\/ Stamp Scissors "))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"




PUB Begin


  serial.TX("S")
  serial.Str(string("\/\/\/ Basic Stamp Two Versus Propeller One "))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"


  waitcnt(clkfreq/500+cnt)


  serial.TX("S")
  serial.Str(string("\/\/\/ Rock Paper Scissors "))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"


  waitcnt(clkfreq/500+cnt)


  serial.TX("S")
  serial.Str(string("\/\/\/ Let's begin "))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"


 
PUB main


   rrr := rr.random


   getnum
   


                                         
PUB getnum


 waitcnt(clkfreq * 1 + cnt)
 rrr := rr.random
 
  IF rrr < 50 
    rock
  IF rrr => 50 AND rrr < 100 
    paper
  IF rrr => 100 AND rrr < 150 
    scissors
  IF rrr => 150 AND rrr < 200 
    rock
  IF rrr => 200 AND rrr < 250 
    paper
  IF rrr => 250 AND rrr < 500 
    scissors


PUB Rock
  serial.TX("S")
  serial.Str(string("\/\/\/ Prop Rock"))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"


outa [16] := 1
waitcnt(clkfreq/2+cnt)
outa [16] := 0


propround := 1
score


PUB Paper
  serial.TX("S")
  serial.Str(string("\/\/\/ Prop Paper"))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"


outa [17] := 1
waitcnt(clkfreq/2+cnt)
outa [17] := 0


propround := 2
score


PUB Scissors
  serial.TX("S")
  serial.Str(string("\/\/\/ Prop Scissors"))             
  serial.TX(pst#NL)
  repeat until serial.RxCheck == ":"


outa [18] := 1
waitcnt(clkfreq/2+cnt)
outa [18] := 0


propround := 3
score
    

Comments

  • PublisonPublison Posts: 12,366
    edited 2014-09-03 13:52
    Very Nice!!
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2014-09-03 16:07
    A refreshing change of pace! And something unique, at least to me! Although I am starting to wonder if the Propeller has been programmed to cheat. It has a 5:1 win/loss ratio.

    Edit: And then in round 2 it was exactly the opposite! Interesting results! I started to think the Propeller was programmed to take a dive in round 2. :innocent:

    Edit 2: I showed this to my wife and she commented that to impress her it would have to play Rock, Paper, Scissors, Lizard, Spock. :blank:
  • xanaduxanadu Posts: 3,347
    edited 2014-09-03 17:05
    Thanks. No cheating yet, maybe in a later revision!

    Rock, Paper, Scissors, Lizard, Spock. Hahaha, the resident Dr Cooper here said the same thing.
  • WBA ConsultingWBA Consulting Posts: 2,933
    edited 2014-09-03 17:39
    Absolutely brilliant! I really like the project, well executed.

    I think that a contest could be made of the topic "dueling rock/paper/scissors microcontrollers game".
  • NWCCTVNWCCTV Posts: 3,629
    edited 2014-09-03 22:51
    WOW!!!!!!!!!! JUST WOW!!!!!!!!!!!!!! If I could do something like that I would quit my day job and retire!!!!
  • ercoerco Posts: 20,255
    edited 2014-09-04 05:48
    Awesome work, Xanadu!

    Maybe you found the right contest to finally solve the Prop vs Arduino debate!
  • Martin_HMartin_H Posts: 4,051
    edited 2014-09-04 06:52
    This is a riot.
  • xanaduxanadu Posts: 3,347
    edited 2014-09-04 17:57
    Thanks guys!
  • TtailspinTtailspin Posts: 1,326
    edited 2014-09-04 22:31
    That's too funny.:thumb::thumb:
Sign In or Register to comment.