//Inlcude libraries
#include <LedControl.h>
//Set the 1st 74HC595 IC pins
int clockPin = 0;
int latchPin = 2;
int dataPin = 15;
//Set the 2nd 74HC595 IC pins
int clockPin1 = 17;
int latchPin1 = 16;
int dataPin1 = 4;
//Set the LED Matrix pins
const int DIN = 23;
const int CS = 5;
const int CLK = 18;
//Set button pins
const int ROCK_PIN = 19;
const int PAPER_PIN = 21;
const int SCISSOR_PIN = 22;
//Set 7 segment digit decoder
int num[10]={63,9,94,91,105,115,119,25,127,123};
//Set LED Matrix Symbols using byte values
byte scisor[8]={0x08,0x08,0x08,0x0B,0xFF,0x08,0x18,0x18};
byte rock[8]={0x00,0x3C,0x7E,0xFF,0xFF,0x7E,0x3C,0x00};
byte paper[8]={0xF8,0x8C,0x8E,0x8F,0x81,0x81,0x81,0xFF};
byte questionMark[8]={0x18,0x24,0x42,0x02,0x04,0x18,0x00,0x18};
byte sad[8]={0x3C,0x42,0xA5,0x81,0x99,0xA5,0x42,0x3C};
byte smile[8]={0x3C,0x42,0xA5,0x81,0xA5,0x99,0x42,0x3C};
byte nuetral[8]={0x3C,0x42,0xA5,0x81,0x81,0xBD,0x42,0x3C};
byte countdown3[8]={0x7E,0x7E,0x06,0x3E,0x3E,0x06,0x7E,0x7E};
byte countdown2[8]={0x7E,0x7E,0x06,0x06,0x7E,0x60,0x7E,0x7E};
byte countdown1[8]={0x1C,0x3C,0x0C,0x0C,0x0C,0x0C,0x0C,0x0C,};
//Create LedControl instance
LedControl lc=LedControl(DIN,CLK,CS,0);
void setup() {
//Set button pins to INPUT
pinMode(ROCK_PIN, INPUT_PULLUP);
pinMode(PAPER_PIN, INPUT_PULLUP);
pinMode(SCISSOR_PIN, INPUT_PULLUP);
//Set pins to output
pinMode(clockPin, OUTPUT);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin1, OUTPUT);
pinMode(latchPin1, OUTPUT);
pinMode(dataPin1, OUTPUT);
lc.shutdown(0,false); //The MAX72XX is in power-saving mode on startup
lc.setIntensity(0,5); // Set the brightness to maximum value
lc.clearDisplay(0); // and clear the display
// display a value of 0 to both segment display in the beginning of the program
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, num[0]);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST, num[0]);
digitalWrite(latchPin1, HIGH);
}
void loop() {
// variable for the player and bot scores
int pscore = 0;
int bscore = 0;
//read the players move and display it to the matrix
//calls the countdown and botMove functions
while(1){
printByte(questionMark);
int pmove = readMove();
int bmove = 0;
if(pmove == 3){
printByte(scisor);
delay(2000);
countdown();
bmove = botMove();
} else if(pmove == 1){
printByte(rock);
delay(2000);
countdown();
bmove = botMove();
} else if(pmove == 2){
printByte(paper);
delay(2000);
countdown();
bmove = botMove();
} else {
continue;
}
//determine the outcome of the match
if (evalWin(pmove, bmove) == 3){
printByte(nuetral);
delay(2000);
continue;
} else if(evalWin(pmove, bmove) == 2){
printByte(sad);
delay(2000);
bscore++;
} else if(evalWin(pmove, bmove) == 1){
printByte(smile);
delay(2000);
pscore++;
}
//Reset the match
if (pscore == 9 || bscore == 9){
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, num[0]);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST, num[0]);
digitalWrite(latchPin1, HIGH);
break;
}
//display the score of the player and bot
else {
digitalWrite(latchPin, LOW);
shiftOut(dataPin, clockPin, LSBFIRST, num[bscore]);
digitalWrite(latchPin, HIGH);
digitalWrite(latchPin1, LOW);
shiftOut(dataPin1, clockPin1, LSBFIRST, num[pscore]);
digitalWrite(latchPin1, HIGH);
continue;
}
}
}
//Displays the byte value into symbol to the LED Matrix
void printByte(byte character [])
{
int i = 0;
for(i=0;i<8;i++)
{
lc.setRow(0,i,character[i]);
}
}
//Gets input from button
int readMove(){
// Read the button states
int rockState = digitalRead(ROCK_PIN);
int paperState = digitalRead(PAPER_PIN);
int scissorState = digitalRead(SCISSOR_PIN);
char playerMove;
if (rockState == LOW) {
return 1;
//Serial.println("ROCK!");
}
if (paperState == LOW) {
return 2;
//Serial.println("PAPER!");
}
if (scissorState == LOW) {
return 3;
//Serial.println("SCISSOR!");
}
return 4;
// Delay to avoid bouncing
delay(200);
}
//function to display the countdown as a symbol for waiting for the bot to pick a move
void countdown(){
printByte(countdown3);
delay(1000);
printByte(countdown2);
delay(1000);
printByte(countdown1);
delay(1000);
}
//function to pick a random number range from (1-3)and display the pick move of bot
int botMove(){
int randPick = random(1,4);
if (randPick == 1){
printByte(rock);
}else if (randPick == 2){
printByte(paper);
}else if (randPick == 3){
printByte(scisor);
}
delay(3000);
return randPick;
}
//Returns the 3 possible outcome of the match
int evalWin(int p,int b){
if(p==b){
return 3;
} else if(p == 3 && b == 1){
return 2;
} else if(p == 1 && b == 2){
return 2;
} else if(p == 2 && b == 3){
return 2;
} else if(p == 2 && b == 1){
return 1;
} else if(p == 3 && b == 2){
return 1;
} else if(p == 1 && b == 3){
return 1;
}
}