#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#include <EEPROM.h>
String compchoice;
int score = 0;
int topScoreB;
/*
Red Button stands for "Rock".
Green Button stands for "Paper".
Blue Button stands for "Scissor".
*/
void setup() {
pinMode(PB1, INPUT_PULLUP);
pinMode(PB3, INPUT_PULLUP);
pinMode(PB4, INPUT_PULLUP);
oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
oled.setFont(FONT8X16); //Setting the Font as 8x16
oled.clear(); //Clearing the OLED Display
oled.on(); //Switching on the OLED Display
randomSeed(analogRead(0));
}
void loop() {
game(); //Calling the "game()" function
}
void game() {
int i = 1;
while (i<=5) {
oled.setCursor(0, 1);
oled.print(F("Top Score is " ));
oled.print(topScoreB);
oled.setCursor(0, 3);
oled.print(F("Current Score is " ));
oled.print(score);
delay(2000);
oled.clear();
oled.setCursor(0,1);
oled.print(F("Rock, Paper, Scissors?"));
delay(2000);
oled.clear();
int r = random(1, 4);
if (r == 1) {
compchoice = "Rock";
} else {
if (r == 2) {
compchoice = "Paper";
} else {
if (r == 3) {
compchoice = "Scissors";
}
}
}
int pb1 = digitalRead(PB1);
int pb3 = digitalRead(PB3);
int pb4 = digitalRead(PB4);
oled.setCursor(0, 1);
while (pb3 == LOW) {
if (compchoice == "Rock") {
oled.print("Computer entered Rock. ");
oled.println("You Won:)");
delay(3000);
oled.clear();
score++;
game();
} else {
if (compchoice == "Paper") {
oled.print("Computer entered Paper. ");
oled.println("Tie!!");
delay(3000);
oled.clear();
game();
} else {
if (compchoice == "Scissors") {
oled.print("Computer entered Scissors. ");
oled.println("You Lost:(");
delay(3000);
oled.clear();
score -= 1;
game();
}
}
}
};
while (pb1 == LOW) {
if (compchoice == "Rock") {
oled.print("Computer entered Rock. ");
oled.println("Tie!!");
delay(3000);
oled.clear();
game();
} else {
if (compchoice == "Paper") {
oled.print("Computer entered Paper. ");
oled.println("You Lost:(");
delay(3000);
oled.clear();
score -= 1;
game();
} else {
if (compchoice == "Scissors") {
oled.print("Computer entered Scissors. ");
oled.println("You Won:)");
delay(3000);
oled.clear();
score++;
game();
}
}
}
};
while (pb4 == LOW) {
if (compchoice == "Rock") {
oled.print("Computer entered Rock. ");
oled.println("You Lost:(");
delay(3000);
oled.clear();
score -= 1;
game();
} else {
if (compchoice == "Paper") {
oled.print("Computer entered Paper. ");
oled.println("You Won:)");
delay(3000);
oled.clear();
score++;
game();
} else {
if (compchoice == "Scissors") {
oled.print("Computer entered Scissors. ");
oled.println("Tie!!");
delay(3000);
oled.clear();
game();
}
}
}
}
topScoreB = EEPROM.read(0);
topScoreB = topScoreB << 8;
topScoreB = topScoreB | EEPROM.read(1);
if (score > topScoreB) {
topScoreB = score;
EEPROM.write(1,score & 0xFF);
EEPROM.write(0,(score>>8) & 0xFF);
};
}
}