Much improved rock, paper, scissors

Follow the instructions on the LCD screen. Use a torch to make your choice between rock, paper and scissors. This could easily be replicated with any LCD screen and 3 light sensors.

In this version, Sparki will also keep score. Although this will be displayed in the very top left hand corner instead of the middle of the screen as you would expect. This is because I haven’t yet learnt how to convert a variable of an integer value into a string to be displayed using the drawString(co-ordinates) command.

Here is the code:

#include // include the sparki library
int player = 0;
int robot = 0;
void setup() {
}
void rock()
{
sparki.RGB(RGB_RED);
sparki.beep(340, 400);
delay(150);
sparki.beep(640, 800);
delay(150);
sparki.clearLCD();
sparki.drawString(20, 2, "You choose ROCK");
sparki.updateLCD();
delay(1000);
sparki.RGB(RGB_OFF);
randomSeed(analogRead(1));
int response2b = random(1, 30);
if (response2b < 10)
{
rockChoice();
draw();
hum();
}
if ((response2b < 20) && (response2b > 10))
{
paperChoice();
win();
dance();
}
if (response2b > 20)
{ scissorsChoice();
lose();
dance2();
}
}
void paper()
{
sparki.RGB(RGB_ORANGE);
sparki.beep(340, 400);
delay(150);
sparki.beep(340, 400);
delay(150);
sparki.clearLCD();
sparki.drawString(20, 2, " You choose PAPER");
sparki.updateLCD();
delay(1000);
sparki.RGB(RGB_OFF);
randomSeed(analogRead(1));
int response2b = random(1, 30);
if (response2b < 10)
{
rockChoice();
lose();
dance2();
}
if ((response2b < 20) && (response2b > 10))
{
paperChoice();
draw();
hum();
}
if (response2b > 20)
{
scissorsChoice();
win();
dance();
}
}
void scissors()
{
sparki.RGB(RGB_YELLOW);
sparki.beep(640, 400);
delay(150);
sparki.beep(340, 800);
delay(150);
sparki.clearLCD();
sparki.drawString(20, 2, "You choose");
sparki.drawString(20, 4, "SCISSORS");
sparki.updateLCD();
delay(1000);
sparki.RGB(RGB_OFF);
randomSeed(analogRead(1));
int response2b = random(1, 30);
if (response2b < 10)
{
rockChoice();
win();
dance();
}
if ((response2b < 20) && (response2b > 10))
{
paperChoice();
lose();
dance2();
}
if (response2b > 20)
{
scissorsChoice();
draw();
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);
}
void rockChoice()
{
sparki.clearLCD();
sparki.drawString(20, 2, "I choose ROCK");
sparki.updateLCD();
delay(1000);
}
void paperChoice()
{
sparki.clearLCD();
sparki.drawString(20, 2, " I choose PAPER");
sparki.updateLCD();
delay(1000);
}
void scissorsChoice()
{
sparki.clearLCD();
sparki.drawString(20, 2, "I choose");
sparki.drawString(20, 4, "SCISSORS");
sparki.updateLCD();
delay(1000);
}
void win()
{
sparki.clearLCD();
sparki.drawString(20, 2, "I won");
sparki.updateLCD();
delay(1000);
robot = robot + 1;
dance();
}
void lose()
{
sparki.clearLCD();
sparki.drawString(20, 2, "You won");
sparki.updateLCD();
delay(1000);
player = player + 1;
dance2();
}
void draw()
{
sparki.clearLCD();
sparki.drawString(20, 2, "It was a draw");
sparki.updateLCD();
delay(1000);
hum();
}
void loop()
{
sparki.clearLCD();
sparki.drawString(20, 2, "Let's play");
sparki.drawString(10, 5, "Rock Paper Scissors");
sparki.updateLCD();
delay(3000);
sparki.RGB(RGB_RED);
delay(200);
sparki.RGB(RGB_ORANGE);
delay(200);
sparki.RGB(RGB_YELLOW);
delay(200);
for (int i = 0; i < 3; i = i + 1) { sparki.clearLCD(); sparki.drawString(20, 1, "RIGHT = ROCK"); sparki.drawString(18, 4, "CENTER = SCISSORS"); sparki.drawString(20, 7, "LEFT = PAPER"); sparki.updateLCD(); delay(8000); if ((sparki.lightRight() > sparki.lightLeft()) && (sparki.lightRight() > sparki.lightCenter()))
{
rock();
}
else if ((sparki.lightRight() < sparki.lightLeft()) && (sparki.lightLeft() > sparki.lightCenter()))
{
paper();
}
else if ((sparki.lightCenter() > sparki.lightRight()) && (sparki.lightCenter() > sparki.lightLeft()))
{
scissors();
}
sparki.clearLCD();
sparki.drawString(20, 2, "You scored");
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.print(player);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.drawString(20, 2, "I scored");
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.print(robot);
sparki.updateLCD();
delay(1000);
}
if (player > robot)
{
sparki.clearLCD();
sparki.drawString(20, 2, "Well done!");
sparki.updateLCD();
delay(1000);
}
else if (player < robot)
{
sparki.clearLCD();
sparki.drawString(20, 2, "Yey I won!");
sparki.updateLCD();
delay(1000);
}
else
{
draw();
}
delay(10000);
}

Leave a comment