# Rock,Scissors or Paper game ###### tags: `Rock,Scissor Or Paper` [ToC] > Let's Play this game against computer :video_game: > With help of colored keys we can choose what we need to choose and then the computer choose radomly what to choose and pass the both to game engin that return the result as Player wins , CPU wins or Equal! ## :memo: How we build it? ### Step 1: The Hardware * 1 unit Arduino UNO R3. * 3 units Buttons. * 3 Units 10K Ohm.(can be less and can be one to connect as shred ground). * 1 Unit buzzer ( Piezo). * 1 unit Adafruit_SSD1306 i2C Oled Module. :rocket: ### Step 2: Connecting and Anrduino code ![](https://i.imgur.com/gPiQcvA.png) ``` // File: 004-Roc-Secior_Paper_game // Summary: This program is simulation to Rock, Scissors, Paper game // We gonna play it with computer , and use RAND() function to generate // a random nummber then take mod% by three to garanty a result as [0,1,or2]. // we ganna use 3 functions : // 1) the game engine Rock_Scissors_Paper() : that study the options and // return result of 1 for player one winning case , 2 : for player 2 (CPU) win // or 3 in case of equality or 4 if the player just withdraw! // 2) CPU rand choice printer : easily print the CPU random generated value of // our three options // 3) Which_Button : that return which Button we presed ( BLue button for ROck, Red for // Sscior and Whit for Paper) // the Leds : Green when Player wins , Red when CPU wins and Bue when Equal! // table Lighting onece the game end ( at 10 of each) and need to reset Arduino. // The sound : 3 diferent Melodies for Player wins , CPU wins and for Equal // // Version: 1.0 // Owner: Ammar alnahhas // --------------------------------------------------------------- // Log: 2020-10-31 Created the file. // 2020-10-31 Tested all values and Nich values also! // // --------------------------------------------------------------- // update : : 2020-11-13 : Show results on i2C OLED screen // // File: Myi2c // Summary: This Program is to tes i2c with Oled Screen Monocolor Adafruit_SSD1306 // I created 2 Functions to : // Void My_loop_Flash_text(const char *Text , int posX, int posY, int Font_Size) // and // void My_loop_Flash_Number(double My_Number , int posX, int posY, int Font_Size) // that over load to integer as well! // Inti. Oled Screen : #include <SPI.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 32 // OLED display height, in pixels // Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); // My Melodies #define NOTE_Player_Wins 523 #define NOTE_CPU_Wins 587 #define NOTE_Equal 659 // input Buttoms Pin const byte myPin2 =2; const byte myPin3=3; const byte myPin4=4; // output Leds pin const byte myPin10=10; const byte myPin11=11; const byte myPin12=12; // Buzzer const byte myBuzzer =8; //Game parameter int Player_scour; int Player_option; int CPU_scour; int CPU_option; int result; void setup() { // Game parameter Player_scour = 0; Player_option = 0; CPU_scour =0; CPU_option = 0; result = 0; // input Pin pinMode(myPin2,INPUT); pinMode(myPin3,INPUT); pinMode(myPin4,INPUT); // output pin pinMode(myPin10,OUTPUT); pinMode(myPin11,OUTPUT); pinMode(myPin12,OUTPUT); pinMode(myBuzzer,OUTPUT); // Computer output Serial.begin(9600); // Welcoming screen on Serial Serial.println(" Rock Scissors Paper"); Serial.println(" ====================="); // Screen output // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x32 Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } // Welcoming Screen on Oled // Show initial display buffer contents on the screen -- // the library initializes this with an Adafruit splash screen. //display.display(); //delay(2000); // Pause for 2 seconds display.clearDisplay(); display.setTextSize(1); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(0, 10); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.write(" Rock Scissors Paper"); display.display(); delay(2000); display.setTextSize(1); // Normal 1:1 pixel scale display.setCursor(0,20 ); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.write(" ==================="); display.display(); delay(2000); My_loop_Flash_text(" LET'S GO " ,0,10,2); display.clearDisplay(); My_loop_Flash_text("Blue = Rock" , 16,10,1); display.clearDisplay(); My_loop_Flash_text("Red = Scissors" , 16,10,1); display.clearDisplay(); My_loop_Flash_text("White = Papper" , 16,10,1); display.clearDisplay(); My_loop_Flash_text("Choose Your Button" , 16,10,1); } void loop() { if ( (Player_scour< 10) && ( CPU_scour<10)){ Player_option = Which_Button(); CPU_option = rand()%3; result = Rock_Scissors_Paper (Player_option, CPU_option); switch (result) { case 1: // the Player is winner // Call function to translate and print the CPU option CPU_Op_printer(CPU_option); // printing winner message on Serial Serial.println(" YOU win this round!!!"); // printing winner message on Oled screen My_loop_Flash_text("YOU win this round!" , 0,10,1); display.display(); // Light Green digitalWrite(myPin10,HIGH); tone(myBuzzer,NOTE_Player_Wins); delay(1000); noTone(myBuzzer); delay(2000); digitalWrite(myPin10,LOW); Player_scour++; // increase the score by 1 to player // print the current result Serial.print(" The scoure is (YOU - CPU)"); Serial.print(Player_scour); Serial.print(" : "); Serial.println(CPU_scour); Serial.println("=========================================="); Serial.println(); // Print results on screen display.clearDisplay(); My_loop_Flash_text(" You : CPU " , 0,5,2); My_loop_Flash_Number_con (Player_scour , 30, 20, 1) ; My_loop_Flash_Number_con (CPU_scour , 95, 20, 1) ; break; case 2 : // the CPU is winner // Call function to translate and print the CPU option CPU_Op_printer(CPU_option); // printing winner message Serial.println(" CPU win this round :( "); // printing winner message on Oled screen My_loop_Flash_text("CPU win this round" , 0,10,1); display.display(); // Light upp the Red digitalWrite(myPin11,HIGH); tone(myBuzzer,NOTE_CPU_Wins); delay(1000); noTone(myBuzzer); delay(2000); digitalWrite(myPin11,LOW); CPU_scour++; // increase the score by 1 to player // print the current result Serial.print(" The scoure is (YOU - CPU)"); Serial.print(Player_scour); Serial.print(" : "); Serial.println(CPU_scour); Serial.println("=========================================="); Serial.println(); // Print results on screen display.clearDisplay(); My_loop_Flash_text(" You : CPU " , 0,5,2); My_loop_Flash_Number_con (Player_scour , 30, 20, 1) ; My_loop_Flash_Number_con (CPU_scour , 95, 20, 1) ; break; case 3 : // it is Equal // Call function to translate and print the CPU option CPU_Op_printer(CPU_option); // printing winner message Serial.println(" Good just Equals "); // printing winner message on Oled screen My_loop_Flash_text(" Good just Equals " , 0,10,1); // Light the Yellow digitalWrite(myPin12,HIGH); tone(myBuzzer,NOTE_Equal); delay(1000); noTone(myBuzzer); delay(2000); digitalWrite(myPin12,LOW); // No increase we just get extra round! // print the current result Serial.print(" The scoure is (YOU - CPU)"); Serial.print(Player_scour); Serial.print(" : "); Serial.println(CPU_scour); Serial.println("=========================================="); Serial.println(); // Print results on screen display.clearDisplay(); My_loop_Flash_text(" You : CPU " , 0,5,2); My_loop_Flash_Number_con (Player_scour , 30, 20, 1) ; My_loop_Flash_Number_con (CPU_scour , 95, 20, 1) ; break; } } else { if (Player_scour >=10) // Player win and reach 10 points first { digitalWrite(myPin10,HIGH); delay(2000); digitalWrite(myPin10,LOW); Serial.println(" YOU ARE the winner ; Plz switch of the Device"); // Write Winner on screen display.clearDisplay(); My_loop_Flash_text("YOU ** WIN" , 24,8,2); } else if (CPU_scour >=10) // CPU win and reach 10 points first { digitalWrite(myPin11,HIGH); delay(2000); digitalWrite(myPin11,LOW); Serial.println(" CPU IS the winner :( Try Harder Plz switch of the Device"); // Write Winner on screen display.clearDisplay(); My_loop_Flash_text("CPU ** WIN" , 24,8,2); } } } int Rock_Scissors_Paper (int Player1,int Player2) { if ((Player1 >2) || (Player1<0)) return 4; // Player #1 withdraw if (Player1 == Player2) return 3; // Equals // P1 Vs P2 if ((Player1 ==0 ) && (Player2==1)) return 1; // Rock Vs Scissor => P1 if ((Player1 ==0 ) && (Player2==2)) return 2; // Rock Vs Paper => P2 if ((Player1 ==1 ) && (Player2==0)) return 2; // Scissors Vs Rock => P2 if ((Player1 ==1 ) && (Player2==2)) return 1; // Scissors Vs Paper=> P1 if ((Player1 ==2 ) && (Player2==0)) return 1; // Paper Vs Rock => P1 if ((Player1 ==2 ) && (Player2==1)) return 2; // Paper Vs Scissors=> P2 } void CPU_Op_printer(int result) { switch (result) { case 0: Serial.println(" CPU choose Rock @@@ \n"); break; case 1: Serial.println(" CPU Choose Scissors :x :x :x\n"); break; case 2: Serial.println(" CPU Choose Paper [] [] [] \n"); break; } } int Which_Button(void) { int buttonState_Rock = 0; int buttonState_Sci = 0; int buttonState_Paper = 0; Serial.println( "What do you choose Rock or Scissors or Paper : "); delay(200); while (1){ buttonState_Rock = digitalRead(myPin2); buttonState_Sci = digitalRead(myPin3); buttonState_Paper = digitalRead(myPin4); if ( buttonState_Rock == HIGH) {Serial.println("You Choosed the ROCK") ; return 0;} else if (buttonState_Sci ==HIGH) {Serial.println("You Choosed the Scissors") ; return 1;} else if (buttonState_Paper == HIGH ) {Serial.println("You Choosed the Paper ") ; return 2;} }} void My_loop_Flash_text(const char *Text , int posX, int posY, int Font_Size) { display.clearDisplay(); display.setTextSize(Font_Size); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(posX, posY); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.write(Text); display.display(); delay(2000); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); } void My_loop_Flash_Number(double My_Number , int posX, int posY, int Font_Size) { display.clearDisplay(); display.setTextSize(Font_Size); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(posX, posY); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.println(My_Number); display.display(); delay(2000); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); } void My_loop_Flash_Number(int My_Number , int posX, int posY, int Font_Size) { display.clearDisplay(); display.setTextSize(Font_Size); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(posX, posY); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.println(My_Number); display.display(); delay(2000); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); } void My_loop_Flash_Number_con (int My_Number , int posX, int posY, int Font_Size) { //display.clearDisplay(); display.setTextSize(Font_Size); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(posX, posY); // Start at top-left corner display.cp437(true); // Use full 256 char 'Code Page 437' font display.println(My_Number); display.display(); delay(2000); display.invertDisplay(true); delay(1000); display.invertDisplay(false); delay(1000); } ``` ### Step 3: Shring is caring! https://hackmd.io/@DSNA22/Rock_Scissors_Paper - YouTube Videos {%youtube 1E2P3bXKIeE%} # DSNA22 OTHER Projects : * [My Morse Code.](https://hackmd.io/@DSNA22/MorseCode) * [Traffic Lights Controler with IR Remote.](https://hackmd.io/@DSNA22/Italian_Job) * [Math Trainig with Arduino , LCD,and 4X4 Keypad.](https://hackmd.io/@DSNA22/Math_Trainig_with_Arduino) * [Rock,Scissors or Paper game.](https://hackmd.io/@DSNA22/Rock_Scissors_Paper) * [Connecting Quad Seven segment and DHT11 via WiFi and API.](https://hackmd.io/@DSNA22/4_Seven_segment_and_DHT11_via_WiFi) * YouTube Channel : [ Or you can visit my YouTube channel Here.](https://www.youtube.com/c/EngAmmarALNahas/videos) # Last but not least Thanks : Thanks for [Thomas Berggren](https://www.linkedin.com/in/tomas-berggren/) for all knowledge and experience that looks clearly in backscenes of this project!