#define S0 12
#define S1 13
#define S2 14
String pick [] {"Rock","Paper","Scissors"};
int numNames = sizeof(pick) / sizeof(pick[0]);
int roundnum = 0;
int score1 = 0;
int score2 = 0;
boolean button1State = LOW;
boolean button2State = LOW;
boolean button3State = LOW;
boolean state = true;
int btn1State = HIGH;
int btn2State = HIGH;
int btn3State = HIGH;
unsigned long lastBtn1Time = 0;
unsigned long lastBtn2Time = 0;
unsigned long lastBtn3Time = 0;
const unsigned long debounceDelay = 50;
void setup() {
// put your setup code here, to run once:
randomSeed(analogRead(0));
pinMode(S0, INPUT_PULLUP);
pinMode(S1, INPUT_PULLUP);
pinMode(S2, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("Welcome to Rock, Paper, Scissor Game!!");
Serial.println("Please Choose A Button!!");
Serial.println("--------------------------------------");
}
void loop() {
unsigned long currentMillis = millis();
int btn1Value = digitalRead(S0);
int btn2Value = digitalRead(S1);
int btn3Value = digitalRead(S2);
if (btn1Value != btn1State && currentMillis - lastBtn1Time > debounceDelay) {
btn1State = btn1Value;
lastBtn1Time = currentMillis;
if (btn1Value == LOW) {
Serial.println("Button 1");
String chose = "Rock";
Serial.println("You Picked " + chose); // print a message to the serial monitor
int randomIndex = random(0, numNames); // generate a random index
String randomPick = pick[randomIndex]; // get the name at the random index
Serial.println("Enemy Picked "+ randomPick); // print the random name to the serial monitor
delay(1000);
Serial.println("--------------------------------------");
delay(1000);
// decision(String randomPick, String chose, boolean& state)
decision(randomPick,chose);
Serial.println("\nSCORE!! \nYou: " + String(score1) + " Enemy: " + String(score2));
Serial.println("--------------------------------------");
delay(1000);
state = true; // set state to true to pause the loop
}
}
if (btn2Value != btn2State && currentMillis - lastBtn2Time > debounceDelay) {
btn2State = btn2Value;
lastBtn2Time = currentMillis;
if (btn2Value == LOW) {
Serial.println("Button 2");
String chose = "Paper";
Serial.println("You Picked " + chose); // print a message to the serial monitor
int randomIndex = random(0, numNames); // generate a random index
String randomPick = pick[randomIndex]; // get the name at the random index
Serial.println("Enemy Picked "+ randomPick); // print the random name to the serial monitor
delay(1000);
Serial.println("--------------------------------------");
delay(1000);
// decision(String randomPick, String chose, boolean& state)
decision(randomPick,chose);
Serial.println("\nSCORE!! \nYou: " + String(score1) + " Enemy: " + String(score2));
Serial.println("--------------------------------------");
delay(1000);
state = true; // set state to true to pause the loop
}
}
if (btn3Value != btn3State && currentMillis - lastBtn3Time > debounceDelay) {
btn3State = btn3Value;
lastBtn3Time = currentMillis;
if (btn3Value == LOW) {
Serial.println("Button 3");
String chose = "Scissors";
Serial.println("You Picked " + chose); // print a message to the serial monitor
int randomIndex = random(0, numNames); // generate a random index
String randomPick = pick[randomIndex]; // get the name at the random index
Serial.println("Enemy Picked "+ randomPick); // print the random name to the serial monitor
delay(1000);
Serial.println("--------------------------------------");
delay(1000);
// decision(String randomPick, String chose, boolean& state)
decision(randomPick,chose);
Serial.println("\nSCORE!! \nYou: " + String(score1) + " Enemy: " + String(score2));
Serial.println("--------------------------------------");
delay(1000);
state = true; // set state to true to pause the loop
}
}
}
void decision(String randomPick, String chose){
Serial.println(chose + " (YOU) VS. " + String(randomPick) + " (COM) ");
if (chose == randomPick) {
Serial.println("Tie!");
return;
}
else if ((chose == "Rock" && randomPick == "Scissors") ||
(chose == "Paper" && randomPick == "Rock") ||
(chose == "Scissors" && randomPick == "Paper")) {
score1++;
Serial.println("You Win!");
if (score1 >= 5) {
score1 = 0;
score2 = 0;
Serial.println("You have won the game! Score reset.");
}
return;
}
else {
Serial.println("Computer Wins!");
score2++;
if (score2 >= 5) {
score1 = 0;
score2 = 0;
Serial.println("You have lost the game! Score reset.");
}
return;
}
}