// original https://wokwi.com/projects/360013286102169601 by https://wokwi.com/makers/krishnadev_cn
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "splash_screens.h"
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 oled_display1(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
/*
Buttons: Red - "Rock".
Green - "Paper".
Blue - "Scissor".
Yellow - "Play Again".
*/
//
// explanation of the scoring array
//
// COMPUTER choice
// R P S
// ___________
// PLAYER 0 R | 1 | 0 | 2 | player loss = 0
// choice |---+---+---| tie = 1
// 1 P | 2 | 1 | 0 | win = 2
// |---+---+---|
// 2 S | 0 | 2 | 1 | address array this way: outcome[yourChoice][compChoice]
// -----------
byte resultGrid[3][3] = { {1, 0, 2}, // {0=loss, 1=tie, 2=win}
{2, 1, 0},
{0, 2, 1}
};
String choice [] = { "Rock", "Paper", "Scissors" }; // these are the choice names printed on screen
String result [] = { "You Lost:(", "Tie!!", "You Won:)" }; // these are the game-end messages
byte compChoice;
byte yourChoice;
byte outcome;
void setup() {
Serial.begin(9600);
pinMode(A0, INPUT_PULLUP);
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
if (!oled_display1.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
oled_display1.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(100);
oled_display1.clearDisplay();
oled_display1.setTextSize(2);
oled_display1.setTextColor(WHITE);
oled_display1.setCursor(0, 10);
oled_display1.println(choice[0]); // rock
oled_display1.println(choice[1]); // paper
oled_display1.println(choice[2]); // scissors
oled_display1.display();
delay(1000);
oled_display1.clearDisplay();
oled_display1.display();
randomSeed(analogRead(0));
}
void loop() {
game();
}
void game() {
int c0 = digitalRead(A0);
int c1 = digitalRead(A1);
int c2 = digitalRead(A2);
yourChoice = (c0 == LOW)? 0 : (c1 == LOW)? 1 : (c2 == LOW)? 2 : 3; // C++ ternary condition operator
if (yourChoice == 3) { delay(20); return; } // no button pressed
compChoice = random(0, 3);
outcome = resultGrid[yourChoice][compChoice]; // this is the whole win/lose/tie decision machine
printGameInfo(); // print info to serial monitor
oled_display1.setCursor(0, 0);
oled_display1.setTextColor(WHITE);
oled_display1.print(F("Computer : "));
oled_display1.println(choice[compChoice]);
oled_display1.println();
oled_display1.println(result[outcome]);
oled_display1.display();
delay(2000);
oled_display1.clearDisplay();
oled_display1.drawBitmap(0, 0, splash[outcome], 128, 64, WHITE);
oled_display1.display();
delay(500);
oled_display1.clearDisplay();
oled_display1.display();
}
void printGameInfo(){ // print some info to serial monitor
Serial.print(" You : ");
Serial.print(yourChoice);
Serial.print(" : ");
Serial.print(choice[yourChoice]);
Serial.print("\tComputer : ");
Serial.print(compChoice);
Serial.print(" : ");
Serial.print(choice[compChoice]);
Serial.print("\t\tresult : ");
Serial.print(outcome);
Serial.print(" : ");
Serial.println(result[outcome]);
}