#include <LiquidCrystal.h>
#include <stdlib.h> // for random functions
LiquidCrystal lcd(12, 11, 10, 9, 8, 7);
int const BUTTON_PAPER = 2; // PAPER
int const BUTTON_SCISSORS = 3; // SCISSORS
int const BUTTON_ROCK = 4; // ROCK
int const LIGHT = 5;
bool pressed = false;
int const ROCK = 1;
int const PAPER = 2;
int const SCISSORS = 3;
String names[3] = {"Rock", "Paper", "Scissors"};
uint8_t rock_char[8] = {
0b00100,
0b01110,
0b11111,
0b11111,
0b11111,
0b01110,
0b00100,
0b00000
};
uint8_t paper_char[8] = {
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111,
0b11111
};
uint8_t scissors_char[8] = {
0b10001,
0b01010,
0b10101,
0b01010,
0b11011,
0b10001,
0b00000,
0b00000
};
void setup() {
pinMode(BUTTON_PAPER, INPUT_PULLUP);
pinMode(BUTTON_SCISSORS, INPUT_PULLUP);
pinMode(BUTTON_ROCK, INPUT_PULLUP);
pinMode(LIGHT, OUTPUT);
lcd.createChar(1, rock_char);
lcd.createChar(2, paper_char);
lcd.createChar(3, scissors_char);
lcd.begin(16, 4);
lcd.print("Rock \x01 Paper \x02 Scissors \x03 ");
Serial.begin(9600);
delay(3000); // Make longer for production
lcd.clear();
lcd.print("Blue: Rock, Green: Paper, Red: Scissors");
delay(3000);
lcd.clear();
}
void loop() {
lcd.print("Make your choice!");
int ai_choice = ai_choice_random();
int player_choice;
// Wait for a button press
while (digitalRead(BUTTON_PAPER) == HIGH &&
digitalRead(BUTTON_ROCK) == HIGH &&
digitalRead(BUTTON_SCISSORS) == HIGH) {
// Do nothing (wait)
}
if (digitalRead(BUTTON_PAPER) == LOW) {
player_choice = PAPER;
} else if (digitalRead(BUTTON_ROCK) == LOW) {
player_choice = ROCK;
} else if (digitalRead(BUTTON_SCISSORS) == LOW) {
player_choice = SCISSORS;
}
int score = scoring(player_choice, ai_choice);
if (score == 1) {
yay(player_choice, ai_choice);
} else if (score == 2) {
welp(player_choice, ai_choice);
} else if (score == 3) {
tie(player_choice, ai_choice);
} else {
lcd.print("Error");
delay(4000);
lcd.clear();
}
}
int scoring(int player_choice, int ai_choice) {
if (player_choice > 3 || player_choice <= 0) {
Serial.println("Bad player");
return 0; // print error message
}
if (ai_choice > 3 || ai_choice <= 0) {
Serial.println("Bad AI");
return 4; // print error message
}
int difference = ((player_choice - ai_choice) +3 ) % 3;
if (difference == 1) {
return 1; // player wins
} else if (difference == 2) {
return 2; //ai wins
} else {
return 3; // tie
}
}
int ai_choice_random() {
randomSeed(analogRead(0));
int random_choice = random(1, 4);
return random_choice;
}
void yay(int player_choice, int ai_choice) {
// YAAAY
lcd.clear();
for (int i = 0; i < 40; i++) {
lcd.print("YOU WIN! ");
lcd.print(String("Player: ") + names[player_choice -1]);
lcd.print(String(" AI: ") + names[ai_choice -1]);
digitalWrite(LIGHT, HIGH);
delay(100);
lcd.clear();
digitalWrite(LIGHT, LOW);
delay(100);
}
lcd.clear();
}
void welp(int player_choice, int ai_choice) {
// welp...
lcd.clear();
lcd.print("you lose... ");
lcd.print(String("Player: ") + names[player_choice -1]);
lcd.print(String(" AI: ") + names[ai_choice -1]);
delay(4000);
lcd.clear();
}
void tie(int player_choice, int ai_choice) {
lcd.clear();
lcd.print("Tie! ");
lcd.print(String("Player: ") + names[player_choice -1]);
lcd.print(String(" AI: ") + names[ai_choice -1]);
delay(4000);
lcd.clear();
}