Rock, Paper, Scissors

Inspired by Google Home and it’s ability to play rock, paper, scissors, I decided I’d quite like to play this with Sparki 🙂 after all, he is my favourite robot buddy! So here is the code for 5 rounds of rock, paper, scissors before a break, then he’ll ask you if you’d like to play again. Unfortunately, I haven’t wrapped my head around keeping score, I keep doing something wrong with my variables and they wont update with each round. So I just left it without scorekeeping for now.

#include <Sparki.h> // include the sparki library

void setup() {
  sparki.servo(60);
}

void loop()
{
  sparki.clearLCD();
  sparki.servo(60);
  sparki.ping();
  if(sparki.ping()<10)
  {
    sparki.moveForward(3);
    sparki.moveBackward(3);
  sparki.drawString(10, 2, "Hello Human");
  sparki.updateLCD();
  delay(2000);
  sparki.clearLCD();
  sparki.drawString(20, 2, "Let's play");
  sparki.drawString(10, 5, "rock paper scissors");
  sparki.updateLCD();
  delay(2000);
  sparki.clearLCD();
  sparki.drawString(20, 2, "Choose RIGHT for yes");
  sparki.drawString(20, 5, "and LEFT for no");
  sparki.updateLCD();
  delay(5000);
  if (sparki.lightRight() < sparki.lightLeft())
  {
    delay(2000);
    sparki.RGB(RGB_RED);
    delay(200);
    sparki.RGB(RGB_ORANGE);
    delay(200);
    sparki.RGB(RGB_YELLOW);
    delay(200);
    for (int i = 0; i < 5; i=i+1)
    {
    sparki.drawString(20, 2, "Choose RIGHT for ROCK");
    sparki.drawString(16, 5, "Choose LEFT for PAPER");
    sparki.drawString(16, 8, "Choose CENTER for SCISSORS");
    sparki.updateLCD();
    delay(5000);
    if ((sparki.lightRight() < sparki.lightLeft()) && (sparki.lightRight() < sparki.lightCenter()))
    {
      rock();
    }
    else if ((sparki.lightRight() > sparki.lightLeft()) && (sparki.lightLeft() < sparki.lightCenter()))
    {
      paper();
    }
    else
    {
      scissors();

    }
  }

}}
delay(10000);
}

void rock()
{
  sparki.RGB(RGB_RED);
  sparki.clearLCD();
  sparki.drawString(20, 2,"You choose ROCK");
  sparki.updateLCD();
  delay(2000);
  sparki.RGB(RGB_OFF);
  sparki.clearLCD();
  sparki.drawString(10, 1, "This is my choice");
  sparki.updateLCD();
  delay(1000);
  randomSeed(analogRead(1));
  int response2b = random(1, 30);

  if (response2b < 10)
  {
    sparki.clearLCD();
    sparki.drawString(20, 2, "I choose ROCK");
    sparki.updateLCD();
    delay(1000);
    sparki.clearLCD();
    sparki.drawString(20, 2, "It was a draw!");
    sparki.updateLCD();
    delay(2000);
    hum();
  }
  if ((response2b < 20) && (response2b > 10))
  {
    sparki.clearLCD();
     sparki.drawString(20, 2,"I choose PAPER");
    sparki.updateLCD();
    delay(1000);
    sparki.clearLCD();
    sparki.drawString(20, 2, "Yey I won");
    sparki.updateLCD();
    delay(2000);
    dance();
  }
  if (response2b > 20)
  { sparki.clearLCD();
     sparki.drawString(20, 2,"I choose SCISSORS");
    sparki.updateLCD();
    delay(1000);
    sparki.clearLCD();
    sparki.drawString(20, 2, "You won");
    sparki.updateLCD();
    delay(2000);
    dance2();
  }
}
void paper()
{
  sparki.RGB(RGB_ORANGE);
  sparki.clearLCD();
   sparki.drawString(20, 2," You choose PAPER");
  sparki.updateLCD();
  delay(2000);
  sparki.RGB(RGB_OFF);
  sparki.clearLCD();
  sparki.drawString(10, 1, "This is my choice");
  sparki.updateLCD();
  delay(1000);
  randomSeed(analogRead(1));
  int response2b = random(1, 30);

  if (response2b < 10)
  {
    sparki.clearLCD();
     sparki.drawString(20, 2," I choose ROCK");
    sparki.updateLCD();
    delay(1000);
    sparki.clearLCD();
    sparki.drawString(20, 2, "You won");
    sparki.updateLCD();
    delay(2000);
    dance2();
  }
  if ((response2b < 20) && (response2b > 10))
  {
    sparki.clearLCD();
    sparki.drawString(20, 2," I choose PAPER");
    sparki.updateLCD();
    delay(1000);
    sparki.clearLCD();
    sparki.drawString(20, 2, "It was a draw");
    sparki.updateLCD();
    delay(2000);
    hum();
  }
  if (response2b > 20)
  { sparki.clearLCD();
    sparki.drawString(20, 2," I choose SCISSORS");
    sparki.updateLCD();
    delay(1000);
    sparki.clearLCD();
    sparki.drawString(20, 2, "I win");
    sparki.updateLCD();
    delay(2000);
    dance();
  }
}
void scissors()
{
  sparki.RGB(RGB_YELLOW);
  sparki.clearLCD();
  sparki.drawString(20, 2,"You choose");
  sparki.drawString(20, 4,"SCISSORS");
  sparki.updateLCD();
  delay(2000);
sparki.RGB(RGB_OFF);
  sparki.clearLCD();
  sparki.drawString(10, 1, "This is my choice");
  sparki.updateLCD();
  delay(1000);
  randomSeed(analogRead(1));
  int response2b = random(1, 30);

  if (response2b < 10)
  {
    sparki.clearLCD();
    sparki.drawString(20, 2,"I choose ROCK");
    sparki.updateLCD();
    delay(1000);
    sparki.clearLCD();
    sparki.drawString(20, 2, "I won");
    sparki.updateLCD();
    delay(2000);
    dance();
  }
  if ((response2b < 20) && (response2b > 10))
  {
    sparki.clearLCD();
    sparki.drawString(20, 2," I choose PAPER");
    sparki.updateLCD();
    delay(1000);
    sparki.clearLCD();
    sparki.drawString(20, 2, "You won");
    sparki.updateLCD();
    delay(2000);
    dance2();
  }
  if (response2b > 20)
  { sparki.clearLCD();
    sparki.drawString(20, 2,"I choose");
    sparki.drawString(20, 4,"SCISSORS");
    sparki.updateLCD();
    delay(1000);
    sparki.clearLCD();
    sparki.drawString(20, 2, "It was a draw");
    sparki.updateLCD();
    delay(2000);
    hum();
  }
}

void dance()
{
 sparki.moveForward(3);
 sparki.beep(840, 400);
 delay(150);
 sparki.beep(640, 400);
 delay(150);
 sparki.beep(440, 400);
 delay(150);
 sparki.moveBackward(3);
}
void dance2()
{
  sparki.moveLeft(10);
 sparki.beep(340, 400);
 delay(150);
 sparki.beep(640, 800);
 delay(150);
 sparki.beep(440, 800);
 delay(150);
 sparki.moveRight(10);
}
void hum()
{
  sparki.beep(340, 400);
  delay(150);
 sparki.beep(440, 400);
 delay(150);
 sparki.beep(640, 400);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s