// C++ code for Arduino UNO and UNO ajacent boards //TXT boy game console #include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); //declares pins used by LCD #define IN1 6 #define IN2 8 #define IN3 7 int Dturn = 0; int player = 0; int playerNum = 0; int dealer = 0; int menupress = 0; int bState1 = 0; int bState2 = 0; int bState3 = 0; int Dchance = 0; int gameFinnish = 0; int randNum = 0; int CPUchoice = 0; int mathA = 0; int mathB = 0; int mathC = 0; int asnwerRNG = 0; int fail = 0; int num1 = 0; int num2 = 0; unsigned long startTime = 0; #define RNG A0 #define SPKR 9 enum ProgramState { //states for game switching MENU_STATE, BLJ_STATE, GUESS_STATE, RPS_STATE, REACT_STATE, QUIZ_STATE, BALL_STATE }; int pgm_state = MENU_STATE; const char* Msg[] = { //messages for magic 8 ball "Yes", "No", "Try again", "Maybe", "Definitely", "Most likely", "Probably not" }; void setup() { pinMode(RNG, INPUT); randomSeed(analogRead(RNG)); // sets number generation seed lcd.begin(16, 2); // Set up the number of columns and rows on the LCD. pinMode( IN1, INPUT);// all IN are buttons pinMode( IN2, INPUT); pinMode( IN3, INPUT); pinMode(SPKR, OUTPUT); tone(SPKR, 200, 600); delay(100); tone(SPKR, 600, 500); lcd.setCursor(0, 0); lcd.print(" SELECT GAME"); lcd.setCursor(0, 2); lcd.print(" PRESS BUTTON 1");//sets up start menu Serial.begin(9600); } void loop() { if (pgm_state == MENU_STATE) { bState1 = digitalRead(IN1); bState2 = digitalRead(IN2); bState3 = digitalRead(IN3); // Read inputs if (bState1 == HIGH) { menupress += 1; //position on menu tone(SPKR, 600, 20); delay(200);// delay for accuracy } if (menupress == 1) { BLJ(); //start blackjack start menu } if (menupress == 2) { numGuess(); //start number guess game } if (menupress == 3){ RPS(); // start rock paper scisors } if (menupress == 4){ react(); } if (menupress == 5){ quiz(); } if (menupress == 6){ RNGball(); } if (menupress > 6) { menupress = 1; // if greater than planned selection, go back to start of list } } if (pgm_state == BLJ_STATE) { Gam1(); } if (pgm_state == GUESS_STATE){ Gam2(); } if (pgm_state == RPS_STATE){ Gam3(); } if (pgm_state == REACT_STATE){ Gam4(); } if (pgm_state == QUIZ_STATE){ Gam5(); } if (pgm_state == BALL_STATE){ Gam6(); } } void BLJ() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("PLAY BLACKJACK?"); lcd.setCursor(0, 2); lcd.print("3 = yes 1=next"); delay(100); bState3 = digitalRead(IN3);// read 3rd button if (bState3 == 1) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Press 1 to start."); pgm_state = BLJ_STATE; } } void numGuess() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("PLAY NUMGUESS?"); lcd.setCursor(0, 2); lcd.print("3 = yes 1=next"); delay(100); bState3 = digitalRead(IN3);// read 3rd button if (bState3 == 1) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Guess 1-100"); randNum = random(1, 100); Serial.println(randNum); //numGuess game debugging pgm_state = GUESS_STATE; } } void RPS() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("PLAY RPS?"); lcd.setCursor(0, 2); lcd.print("3 = yes 1=next"); delay(100); bState3 = digitalRead(IN3);// read 3rd button if (bState3 == 1) { lcd.clear(); lcd.setCursor(0, 0); lcd.print("1=rock 2=paper"); lcd.setCursor(0, 2); lcd.print(" 3=scicors"); randNum = random(1, 3); Serial.println(randNum); //numGuess game debugging delay(1000); pgm_state = RPS_STATE; } } void react() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("PLAY Reaction?"); lcd.setCursor(0, 2); lcd.print("3 = yes 1=next"); delay(100); bState3 = digitalRead(IN3);// read 3rd button if (bState3 == 1) { lcd.clear(); lcd.setCursor(0, 2); lcd.print("Press 3 to start.");; CPUchoice = random(1000, 1600); Serial.println(CPUchoice); //numGuess game debugging delay(1000); pgm_state = REACT_STATE; } } void quiz(){ lcd.clear(); lcd.setCursor(0, 0); lcd.print("PLAY MATH QUIZ?"); lcd.setCursor(0, 2); lcd.print("3 = yes 1=next"); delay(100); bState3 = digitalRead(IN3);// read 3rd button if (bState3 == 1) { lcd.clear(); lcd.setCursor(0, 0); lcd.print(" Keep going"); lcd.setCursor(0, 2); lcd.print("until incorect"); randNum = random(1, 3); delay(1000); pgm_state = QUIZ_STATE; } } void RNGball(){ lcd.clear(); lcd.setCursor(0, 0); lcd.print("PLAY 8 BALL?"); lcd.setCursor(0, 2); lcd.print("3 = yes 1=next"); delay(100); bState3 = digitalRead(IN3);// read 3rd button if (bState3 == 1) { lcd.clear(); lcd.setCursor(0, 2); lcd.print("Press 3 for guidence.");; String randomMessage = Msg[random (0, 6)]; pgm_state = BALL_STATE; } } void Gam1() { Dchance = random(0, 20); bState1 = digitalRead(IN1); bState2 = digitalRead(IN2); bState3 = digitalRead(IN3); // Read inputs // 1. DEALING if ((bState1 == 1) && (player + dealer == 0)) { player = random(2, 21); dealer = random(2, 21); refreshBLJ(); delay(200); } // 2. PLAYER HITS (Button 3) else if (player > 0 && bState3 == 1) { tone(SPKR, 600, 20); player += random(1, 10); if (player > 21) { lcd.clear(); lcd.print(" DEALER WINS!"); lcd.setCursor(0, 2); lcd.print("YOU WENT OVER 21"); delay(2000); endGame(); } else { refreshBLJ(); delay(300); } } else if (player > 0 && bState2 == 1) { dealerAI(); } } void dealerAI() { // Use a while loop so the dealer keeps taking cards until 17 while (dealer < 17) { dealer += random(1, 10); tone(SPKR, 600, 20); refreshBLJ(); delay(800); // Wait so player can see the dealer's progress } // Final Scoring Logic lcd.clear(); if (dealer > 21 || player > dealer) { lcd.print(" YOU WIN!"); tone(SPKR, 1000, 800); } else if (dealer > player) { lcd.print(" DEALER WINS!"); tone(SPKR, 200, 800); } else { lcd.print(" PUSH! (TIE)"); tone(SPKR, 600, 800); } delay(3000); endGame(); } // Helper function to update LCD for blackjack void refreshBLJ() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Player"); lcd.setCursor(0, 2); lcd.print(player); lcd.setCursor(8, 0); lcd.print("Dealer"); lcd.setCursor(10, 2); lcd.print(dealer); } void Gam2() { bState1 = digitalRead(IN1); bState2 = digitalRead(IN2); bState3 = digitalRead(IN3); // Read inputs if (bState2 == 1) { playerNum++; lcd.clear(); tone(SPKR, 600, 20); lcd.setCursor(0, 2); lcd.print(playerNum);delay(150); } if (digitalRead(IN1) == HIGH) { playerNum--; tone(SPKR, 600, 20); lcd.clear(); lcd.setCursor(0, 2); lcd.print(playerNum); delay(150); } if ((bState3 == 1) && (playerNum > 0)) { tone(SPKR, 600, 20); lcd.clear(); // clears screen in prep to display if player is right if (playerNum == randNum) { lcd.print("CORRECT!"); lcd.setCursor(4, 2); lcd.print("Guesses:"); lcd.setCursor(9, 2); lcd.print(player); tone(SPKR, 500, 400); delay(200); tone(SPKR, 1000, 500); delay(2000); endGame();// Return to menu } else if (playerNum < randNum) { lcd.print("HIGHER"); // Feedback logic player ++; delay(400); } else { lcd.print("LOWER"); player ++; delay(400); } lcd.clear(); } } void Gam3() { bState1 = digitalRead(IN1); bState2 = digitalRead(IN2); bState3 = digitalRead(IN3); // Read inputs if (bState1 == 1){ tone(SPKR, 600, 20); player = 1; play(); }else if (bState2 == 1){ tone(SPKR, 600, 20); player = 2; play(); } else if (bState3 == 1){ tone(SPKR, 600, 20); player = 3; Serial.println(player); play(); } } void play() { if ( player == randNum){ lcd.clear(); lcd.print("TIE!"); delay(2000); endGame(); }else if((player == 1) && (randNum == 2)){ lcd.clear(); lcd.print("YOU LOSE!"); delay(2000); endGame(); }else if((player == 2) && (randNum == 1)){ lcd.clear(); lcd.print("YOU WIN!!"); delay(2000); endGame(); }else if((player == 2) && (randNum == 3)){ lcd.clear(); lcd.print("YOU LOSE!"); delay(2000); endGame(); }else if((player == 3) && (randNum == 2)){ lcd.clear(); lcd.print("YOU WIN!!"); delay(2600); endGame(); } } void Gam4() { bState3 = digitalRead(IN3); if (bState3 == 1) { lcd.clear(); lcd.print("ready..."); delay(CPUchoice); tone(SPKR, 1000, 800); lcd.clear(); lcd.print("PRESS BUTTON 1!!"); unsigned long startTime = millis(); // Localize for accuracy // Check the pin directly in the loop while (digitalRead(IN1) == LOW); unsigned long reactionTime = millis() - startTime; lcd.clear(); lcd.print("Your time was:"); lcd.setCursor(0, 2); lcd.print(reactionTime / 1000.0); lcd.setCursor(7, 2); lcd.print(" seconds"); tone(SPKR, 500, 400); delay(200); tone(SPKR, 1000, 500); delay(2000); endGame(); } } void Gam5() { bState1 = digitalRead(IN1); bState2 = digitalRead(IN2); bState3 = digitalRead(IN3); // Read inputs if (num1 + num2 == 0){ Mathset(); } if ((bState3 == 1) && (mathC == asnwerRNG)){ delay(100); player + 1; num1 = 0; num2 = 0; } //if the player is right if ((bState1 == 1) && (mathA == asnwerRNG)){ delay(100); player + 1; Mathset(); } if ((bState2 == 1) && (mathB == asnwerRNG)){ delay(100); player + 1; Mathset(); } // if the player is wrong if ((bState3 == 1) && (mathC != asnwerRNG)){ delay(100); mathEnd(); } if ((bState1 == 1) && (mathA != asnwerRNG)){ delay(100); mathEnd(); } if ((bState2 == 1) && (mathB != asnwerRNG)){ delay(100); mathEnd(); } } void Mathset(){ num1 = random(1, 100); num2 = random(1,100); asnwerRNG = num1 + num2; if( randNum == 1){ mathA = asnwerRNG; mathB = asnwerRNG - random(1, 20); }else if( randNum == 2){ mathB = asnwerRNG; }else{ mathC = asnwerRNG; } lcd.clear(); lcd.setCursor(0, 0); lcd.print(num1); lcd.setCursor(3, 0); lcd.print("+"); lcd.setCursor(4, 0); lcd.print(num2); lcd.setCursor(7, 0); lcd.print("= ?"); lcd.setCursor(0, 2); lcd.print(mathA); lcd.setCursor(5, 2); lcd.print(mathB); lcd.setCursor(9, 2); lcd.print(mathC); } void mathEnd() { lcd.clear(); lcd.setCursor(0, 0); lcd.print("GAME OVER!"); lcd.setCursor(0, 2); lcd.print("Your score:"); lcd.setCursor(11, 2); lcd.print(player); delay(2000); endGame(); } void Gam6() { bState1 = digitalRead(IN1); bState3 = digitalRead(IN3); // declare inputs delay(100); //button debouncing if (bState3 == 1) { tone(SPKR, 600, 20); lcd.clear(); lcd.setCursor(0, 0); int index = random(0, 6); delay(600); // because of cance of repeating messages, delay to show it is a new response lcd.print(Msg[index]); delay(100); } if (bState1 == 1){ endGame(); } } void endGame(){ // reset all potentally used varaibles startTime = 0; playerNum = 0; dealer = 0; player = 0; Dchance = 0; randNum = 0; CPUchoice = 0; asnwerRNG = 0; mathA = 0; mathB = 0; mathC = 0; menupress = 0; pgm_state = MENU_STATE; // reset state tone(SPKR, 523, 500); tone(SPKR, 600, 500); lcd.clear(); lcd.setCursor(0, 0); lcd.print(" SELECT GAME"); lcd.setCursor(0, 2); lcd.print(" PRESS BUTTON 1");//sets up start menu }