#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <stdlib.h>
int gameMode;
bool game= true;
long int loopCount=1;
int p1;
int p2;
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define but1 2
#define but2 4
#define but3 7
#define ROCK 1
#define PAPER 2
#define SCISSOR 3
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
const unsigned char myBitmap[] = { /* bmp */ };
static const unsigned char PROGMEM rps[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x03, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0xC0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x07, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x70, 0x00, 0x00,
0x00, 0x00, 0x06, 0x00, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x0E, 0x00, 0x00, 0x30, 0x00, 0x00,
0x00, 0x00, 0x0E, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x00, 0x00, 0x30, 0x00, 0x00,
0x00, 0x00, 0x0C, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x40, 0x00, 0x30, 0x00, 0x00,
0x00, 0x00, 0x0E, 0xE0, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x0E, 0xE0, 0x00, 0x30, 0x00, 0x00,
0x00, 0x00, 0x07, 0xE0, 0x03, 0x30, 0x00, 0x00, 0x00, 0x00, 0x07, 0xE0, 0x23, 0xB0, 0x00, 0x00,
0x00, 0x00, 0x01, 0xE0, 0x73, 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE6, 0x77, 0xE0, 0x00, 0x00,
0x00, 0x00, 0x00, 0xC7, 0x7F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xCF, 0x3E, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xCF, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xCF, 0x38, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0xCF, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x9F, 0x98, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0x9D, 0x9C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x99, 0x9C, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x99, 0x8C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x39, 0xCC, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0xB9, 0xDC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xF0, 0xFC, 0x00, 0x00, 0x00,
0x00, 0x00, 0x01, 0xE0, 0x78, 0x38, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00,
0x00, 0x03, 0x80, 0x00, 0x00, 0xFE, 0x00, 0x00, 0x00, 0x07, 0xC0, 0x00, 0x07, 0xE7, 0x18, 0x00,
0x00, 0x0F, 0xFC, 0x00, 0x0F, 0xE3, 0xFE, 0x00, 0x00, 0x1C, 0x7E, 0x00, 0x0E, 0x71, 0xFF, 0x00,
0x00, 0x38, 0xF7, 0xC0, 0x0E, 0x38, 0xE3, 0x80, 0x00, 0x70, 0xE7, 0xE0, 0x1F, 0x1C, 0x71, 0xC0,
0x00, 0xE0, 0x07, 0x70, 0x3F, 0x8E, 0x38, 0xE0, 0x01, 0xC0, 0x06, 0x70, 0x3B, 0xC7, 0x1C, 0x70,
0x01, 0xC0, 0x00, 0x78, 0x39, 0xE7, 0x8E, 0x38, 0x03, 0x80, 0x00, 0xFC, 0x1C, 0xF3, 0xC7, 0x1C,
0x03, 0x80, 0x00, 0x5C, 0x1E, 0x79, 0xC2, 0x0C, 0x03, 0x00, 0x00, 0x0C, 0x3F, 0x3C, 0x00, 0x00,
0x03, 0x00, 0x00, 0x1C, 0x77, 0x9E, 0x00, 0x00, 0x03, 0x00, 0x00, 0x38, 0x73, 0xCE, 0x00, 0x00,
0x03, 0x00, 0x00, 0x78, 0x71, 0xC0, 0x00, 0x00, 0x07, 0x00, 0x00, 0xFC, 0x38, 0xE0, 0x00, 0x00,
0x0E, 0x00, 0x01, 0xCC, 0x1C, 0x70, 0x00, 0x00, 0x1C, 0x00, 0x01, 0x8C, 0x0E, 0x00, 0x00, 0x00,
0x38, 0x00, 0x00, 0x1C, 0x07, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x38, 0x03, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0x70, 0x01, 0xC0, 0x00, 0x00, 0x00, 0x00, 0x00, 0xE0, 0x00, 0xE0, 0x00, 0x00,
0x00, 0x00, 0x01, 0xC0, 0x00, 0x70, 0x00, 0x00, 0x00, 0x00, 0x1F, 0x80, 0x00, 0x38, 0x00, 0x00,
0x00, 0x01, 0xFF, 0x00, 0x00, 0x1E, 0x00, 0x00, 0x00, 0x03, 0xFC, 0x00, 0x00, 0x0F, 0xF0, 0x00,
0x00, 0x07, 0x00, 0x00, 0x00, 0x07, 0xFE, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0xFE, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup() {
Serial.begin(115200);
pinMode(but1, INPUT);
pinMode(but2, INPUT);
pinMode(but3, INPUT);
}
void loop() {
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(1000);
display.clearDisplay();
drawCircles();
display.display();
// delay(5000);
display.clearDisplay();
if(loopCount == 1) {
display.clearDisplay();
displayOLED(1,27,20,"IoT Project");
displayOLED(1,12,30,"Rock Paper Scissor");
// display.drawRect(1,1,126,62,WHITE);
display.display();
int temp = readButt();
delay(500); //Wait 5 Sec
display.clearDisplay();
display.drawBitmap(31 , 0, rps, 64, 64,1);
// display.invertDisplay(true);
display.display();
delay(5000);
}
display.clearDisplay();
displayOLED(1,10,20,"1.Single Player");
displayOLED(1,10,30,"2.Two Players");
display.clearDisplay();
gameMode = readButt(); //Check which mode is selected
display.clearDisplay();
if(gameMode==1) displayOLED(1,20,20,"Single Player");
else if(gameMode==2) displayOLED(1,20,20,"Two Players");
else displayOLED(1,20,20,"Baryon Mode");
delay(2000);
game = true;
do{
display.clearDisplay();
displayOLED(2,10,20,"Player 1");
displayOLED(1,10,35,"Rock Paper Scissor");
p1 = readButt();
delay(1000);
display.clearDisplay();
displayOLED(2,10,20,"Player 2");
displayOLED(1,10,35,"Rock Paper Scissor");
if(gameMode==1) {
p2 = askAI();
} else p2 = readButt();
delay(1000);
game = checkWin(p1,p2); //checking for winner
delay(3000);
}while(game == false);
loopCount++;
}
void displayOLED(int textSize,int setX, int setY, char str[20]) {
display.setFont();
display.setTextColor(WHITE);
display.setTextSize(textSize);
display.setTextColor(WHITE);
display.setCursor(setX, setY);
display.print(str);
display.display();
}
void drawCircles(void) {
display.clearDisplay();
for(int16_t i=5; i<90; i+=5) {
display.drawCircle(63,31, i, WHITE);
display.display();
delay(10);
display.clearDisplay();
}
}
int readButt() {
// gameMode = 0;
// p1=0;
int val=0;
while(1) {
if(digitalRead(but1)==HIGH && digitalRead(but2)==LOW && digitalRead(but3)==LOW) {
Serial.println("returning 1 ");
val = 1;
break;
}
if(digitalRead(but1)==LOW && digitalRead(but2)==HIGH && digitalRead(but3)==LOW) {
Serial.println("returning 2");
val = 2;
break;
}
if(digitalRead(but1)==LOW && digitalRead(but2)==LOW && digitalRead(but3)==HIGH) {
Serial.println("returning 3 ");
val = 3;
break;
}
}
return val;
}
int askAI() {
int srand = (rand()%3) + 1;
Serial.print("returning ");
Serial.println(srand);
return srand;
}
bool checkWin(int p1, int p2) {
display.clearDisplay();
if(p1==p2) {
displayOLED(2,20,20,"Draw");
displayOLED(1,20,35,"Restart");
return false;
}
if(p1==ROCK && p2==PAPER) {
displayOLED(1,20,20,"Rock looses to Paper");
return true;
} else if(p1==ROCK && p2==SCISSOR) {
displayOLED(1,20,20,"Rock beats Scissor");
return true;
} else if(p1==PAPER && p2==ROCK) {
displayOLED(1,20,20,"Paper beats Rock");
return true;
} else if(p1==PAPER && p2==SCISSOR) {
displayOLED(1,20,20,"Paper looses to Scissor");
return true;
} else if(p1==SCISSOR && p2==ROCK) {
displayOLED(1,20,20,"Scissor looses to Rock");
return true;
} else if(p1==SCISSOR && p2==PAPER) {
displayOLED(1,20,20,"Scissor beats Paper");
return true;
}
}
Comments