Code for Player 1 of Rock Paper Scissors Game

Dependencies:   4DGL-uLCD-SE PinDetect SDFileSystem mbed wave_player

Revision:
0:75276caf1a37
Child:
1:5841ff349aae
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Oct 19 07:43:06 2015 +0000
@@ -0,0 +1,519 @@
+//ECE 4180 Mini Project
+//Prachi Kulkarni and Kendra Dodson
+
+#include "mbed.h"
+#include "uLCD_4DGL.h"
+#include "SDFileSystem.h"
+#include "wave_player.h"
+ 
+SDFileSystem sd(p5, p6, p7, p8, "sd");
+AnalogOut DACout(p18);
+wave_player waver(&DACout); // Wave Player
+
+//DigitalOut myled(LED1);
+uLCD_4DGL lcd(p28, p27, p29); //uLCD
+DigitalIn pb1(p17); //Pushbutton 1 
+DigitalIn pb2(p19); //Pushbutton 2
+DigitalIn pb3(p20); //Pushbutton 3
+
+PwmOut led1(p21); //led 1
+PwmOut led2(p22); //led2
+PwmOut led3(p23); //led3
+
+//shiftbrite
+DigitalOut latch(p15);
+DigitalOut enable(p16);
+SPI spi(p11, p12, p13);
+
+//Choice
+//Choice = 1 for rock
+//Choice = 2 for paper
+//Choice = 3 for scissors
+int choice1 = 0;
+int choice2 = 0;
+
+//Points
+int p1 = 0;
+int p2 = 0;
+
+
+
+
+void RGB_LED(int red, int green, int blue);
+void RGB_LED(int red, int green, int blue)
+{
+    unsigned int low_color=0;
+    unsigned int high_color=0;
+    high_color=(blue<<4)|((red&0x3C0)>>6);
+    low_color=(((red&0x3F)<<10)|(green));
+    spi.write(high_color);
+    spi.write(low_color);
+    latch=1;
+    latch=0;
+}
+
+
+int main() {
+    L1:
+    spi.format(16,0);
+    spi.frequency(500000);
+    enable=0;
+    latch=0;
+    pb1.mode(PullUp);
+    wait(0.001);
+    pb2.mode(PullUp);
+    wait(0.001);
+    pb3.mode(PullUp);
+    wait(0.001);
+    
+    //Display 
+    lcd.locate(0,0);
+    //lcd_mutex.lock();
+    lcd.printf("Rock Paper Scissor");
+    
+    lcd.locate(0,4);
+    lcd.printf("Button 1 for Rocks");
+    
+    lcd.locate(0,6);
+    lcd.printf("Button 2 for Paper");
+    
+    lcd.locate(0,8);
+    lcd.printf("Button 3 for Scissors\n\n");
+    
+    lcd.printf("Press 1 to continue");
+    
+    wait(2);
+    
+    while(1){
+    
+    if(pb1 != 1){
+    L3:
+    lcd.cls(); //Clear screen
+    
+    lcd.locate(0,4);
+    lcd.printf("     Player 1 \n\n\n");
+    lcd.printf("You have 5 seconds");
+    lcd.printf("    to choose\n");
+    lcd.printf("    an option!\n");
+    wait(2);
+    
+
+    
+    int i;
+    int red = 1;
+
+    for(i = 5; i > -1 ; i--){
+        lcd.cls();
+        lcd.locate(4,4);
+        lcd.set_font(FONT_12X16);
+        //lcd.color(BLUE); // Setting Font color
+        //lcd.text_mode(TRANSPARENT);
+        lcd.text_width(8);
+        lcd.text_height(8);
+        
+        lcd.text_bold(ON);
+        RGB_LED(red*25,0,0);
+        red = red + 5;
+        //green = green + 5;
+        //blue = blue + 5;
+        //wait(2);
+        //red = red + 15;
+        //RGB_LED(red, green,blue);
+        lcd.printf("%d",i);
+        wait(1);
+        if(pb1 != 1) {
+            lcd.cls();
+            choice1 = 1;
+            lcd.printf("You chose rock!");
+            lcd.circle(60, 60, 30,WHITE);
+            wait(2);
+            lcd.cls();
+            break;
+        }
+         if(pb2 != 1) {
+            lcd.cls();
+            choice1 = 2;
+            lcd.printf("You chose paper!");
+            lcd.rectangle(40, 60, 80,100,WHITE);
+            wait(2);
+            lcd.cls();
+            break;
+        }
+           if(pb3 != 1) {
+            lcd.cls();
+            choice1 = 3;
+            lcd.printf("You chose scissors!");
+            lcd.circle(50, 60, 10,WHITE);
+            lcd.circle(50, 80, 10,WHITE);
+            lcd.line(62, 61, 90, 85, WHITE);
+            lcd.line(62, 81, 90, 55, WHITE);
+            wait(2);
+            lcd.cls();
+            break;
+        }
+        }
+        RGB_LED(0,0,0);
+          if(i == -1){
+            lcd.cls();
+            lcd.printf("Your time is up"); 
+            wait(5);
+            goto L1;
+            }  
+            
+    lcd.cls(); //Clear screen
+    wait(2);
+    
+    lcd.printf("Press 1 if P2 is  ready");
+    while(1){
+        if(pb1 != 1) break;
+        }
+    lcd.cls();
+    wait(1);
+    
+    lcd.locate(0,4);
+    lcd.printf("     Player 2 \n\n\n");
+    lcd.printf("You have 5 seconds");
+    lcd.printf("    to choose\n");
+    lcd.printf("    an option!\n");
+    wait(5);
+    
+
+    
+    int j;
+    int blue = 1;
+    for(j = 5; j > -1 ; j--){
+        lcd.cls();
+        lcd.locate(4,4);
+        lcd.text_width(8);
+        lcd.text_height(8);
+        RGB_LED(0,0,blue*25);
+        blue = blue + 5;
+        lcd.printf("%d",j);
+        wait(1);
+        if(pb1 != 1) {
+            lcd.cls();
+            choice2 = 1;
+            lcd.printf("You chose rock!");
+            lcd.circle(60, 60, 30,WHITE);
+            wait(2);
+            lcd.cls();
+            break;
+        }
+         if(pb2 != 1) {
+            lcd.cls();
+            choice2 = 2;
+            lcd.printf("You chose paper!");
+            lcd.rectangle(40, 60, 80,100,WHITE);
+            wait(2);
+            lcd.cls();
+            break;
+        }
+           if(pb3 != 1) {
+            lcd.cls();
+            choice2 = 3;
+            lcd.printf("You chose scissors!");
+            lcd.circle(50, 60, 10,WHITE);
+            lcd.circle(50, 80, 10,WHITE);
+            lcd.line(62, 61, 90, 85, WHITE);
+            lcd.line(62, 81, 90, 55, WHITE);
+            wait(2);
+            lcd.cls();
+            break;
+        }
+        }   
+        if(j == -1){
+            lcd.cls();
+            lcd.printf("Your time is up"); 
+            wait(5);
+            goto L1;
+            }  
+         RGB_LED(0,0,0);   
+            
+    
+            
+            
+        if(choice1 == 1 && choice2 == 1 ||
+            choice1== 2 && choice2 == 2 || 
+            choice1 == 3 && choice2 == 3 ) {
+                lcd.cls();
+                lcd.printf("It's a draw!");
+                wait(4);
+                lcd.cls();
+                lcd.printf("Press 1 to continue\n\n");
+                lcd.printf("Press 2 to end\n\n");
+                
+                while(1){
+                    if(pb1 != 1) {
+                        lcd.cls();
+                        goto L3;
+                    }
+                    else if(pb2 != 1) goto L2;
+                }
+                }
+                
+          else if(choice1 == 1 && choice2 == 2){
+              
+                lcd.cls();
+                lcd.printf("Player 1 chose Rock\n");
+                lcd.printf("Player 2 chose Paper\n\n");
+                
+                lcd.printf("Player 2 wins!");
+                lcd.rectangle(40, 60, 80,100,WHITE);
+                p2++;
+                wait(4);
+                lcd.cls();
+                lcd.printf("Press 1 to continue\n\n");
+                lcd.printf("Press 2 to end\n\n");
+                
+                while(1){
+                    if(pb1 != 1) {
+                        lcd.cls();
+                        goto L3;
+                    }
+                    else if(pb2 != 1) goto L2;
+                }
+               }     
+    
+    }
+    
+                  else if(choice2== 1 && choice1== 2){
+              
+                lcd.cls();
+                lcd.printf("P1 chose Paper\n");
+                lcd.printf("P2 chose Rock\n\n");
+                lcd.printf("P1 wins!");
+                lcd.rectangle(40, 60, 80,100,WHITE);
+                p1++;
+                wait(5);
+                lcd.cls();
+                lcd.printf("P1 Score : %d\n",p1);
+                lcd.printf("P2 Score : %d\n\n",p2);
+                lcd.printf("Press 1 to continue\n\n");
+                lcd.printf("Press 2 to end\n\n");
+                
+                while(1){
+                    if(pb1 != 1) {
+                        lcd.cls();
+                        goto L3;
+                    }
+                    else if(pb2 != 1) goto L2;
+                }
+               }     
+    
+    
+    
+    else if(choice1 == 2 && choice2 == 3){
+              
+                lcd.cls();
+                lcd.printf("P1 chose Paper\n");
+                lcd.printf("P2 chose Scissors\n\n");
+                
+                lcd.printf("Player 2 wins!");
+                p2++;
+                lcd.circle(50, 60, 10,WHITE);
+                lcd.circle(50, 80, 10,WHITE);
+                lcd.line(62, 61, 90, 85, WHITE);
+                lcd.line(62, 81, 90, 55, WHITE);
+                wait(5);
+                lcd.cls();
+                lcd.printf("P1 Score : %d\n",p1);
+                lcd.printf("P2 Score : %d\n\n",p2);
+                lcd.printf("Press 1 to continue\n\n");
+                lcd.printf("Press 2 to end\n\n");
+                
+                while(1){
+                    if(pb1 != 1) {
+                        lcd.cls();
+                        goto L3;
+                    }
+                    else if(pb2 != 1) goto L2;
+                }
+               }     
+    
+    else if(choice1 == 3 && choice2 == 2){
+              
+                lcd.cls();
+            
+                lcd.printf("P1 chose Scissors\n");
+                lcd.printf("P2 chose Paper\n\n");
+                
+                lcd.printf("P1 wins!");
+                p1++;
+                lcd.circle(50, 60, 10,WHITE);
+                lcd.circle(50, 80, 10,WHITE);
+                lcd.line(62, 61, 90, 85, WHITE);
+                lcd.line(62, 81, 90, 55, WHITE);
+                wait(5);
+                lcd.cls();
+                lcd.printf("P1 Score : %d\n",p1);
+                lcd.printf("P2 Score : %d\n\n",p2);
+                lcd.printf("Press 1 to continue\n\n");
+                lcd.printf("Press 2 to end\n\n");
+                
+                while(1){
+                    if(pb1 != 1) {
+                        lcd.cls();
+                        goto L3;
+                    }
+                    else if(pb2 != 1) goto L2;
+                }
+               }     
+    
+    else if(choice1 == 1 && choice2 == 3){
+              
+                lcd.cls();
+                lcd.printf("P1 chose Rock\n");
+                lcd.printf("P2 chose Scissors\n\n");
+                
+                lcd.printf("P1 wins!");
+                p1++;
+                lcd.circle(60, 60, 30,WHITE);
+                wait(5);
+                lcd.cls();
+                lcd.printf("P1 Score : %d\n",p1);
+                lcd.printf("P2 Score : %d\n\n",p2);
+                lcd.printf("Press 1 to continue\n\n");
+                lcd.printf("Press 2 to end\n\n");
+                
+                while(1){
+                    if(pb1 != 1) {
+                        lcd.cls();
+                        goto L3;
+                    }
+                    else if(pb2 != 1) goto L2;
+                }
+               }   
+               
+               
+               else if(choice1 == 3 && choice2 == 1){
+              
+                lcd.cls();
+                lcd.printf("P1 chose Scissors\n");
+                lcd.printf("P2 chose Rock\n\n");
+                
+                lcd.printf("P2 wins!");
+                p2++;
+                lcd.circle(60, 60, 30,WHITE);
+                wait(5);
+                lcd.cls();
+                lcd.printf("P1 Score : %d\n",p1);
+                lcd.printf("P2 Score : %d\n\n",p2);
+                lcd.printf("Press 1 to continue\n\n");
+                lcd.printf("Press 2 to end\n\n");
+                
+                while(1){
+                    if(pb1 != 1) {
+                        lcd.cls();
+                        goto L3;
+                    }
+                    else if(pb2 != 1) goto L2;
+                }
+               }     
+    
+    }  
+    
+    
+            
+    
+    /*
+    while(1) {
+        myled = 1;
+        wait(0.2);
+        myled = 0;
+        wait(0.2);
+    }
+    */
+    
+    // Code to play a wave file
+    /* FILE *wave_file;
+    printf("\n\n\nHello, wave world!\n");
+    wave_file=fopen("/sd/mydir/HG.wav","r");
+    waver.play(wave_file);
+    wait(5);
+    printf("ok!!");
+    fclose(wave_file);
+    */
+    
+    //Code to use Shiftbrite
+   /* for(int i=1; i <= 10; i++) {
+            //if(lightSensor > 0.5) {
+                RGB_LED(0, 0, i*10);
+                wait(3);
+                if(i%5){
+                RGB_LED(250, 253,8);
+                wait(1);
+                }
+    */
+
+
+L2:
+lcd.cls();
+
+
+                        //Create animation of two bouncing balls
+                        float fx=50.0,fy=21.0,vx=5.0,vy=0.4,vx2=5,fx2=40.0;
+                        int x=50,y=21,radius=4;
+                        int x2 = 30, y2 = 10;
+                        //lcd.background_color(BLACK);
+                        //lcd.cls();
+                        //uLCD.text_string("Highest Score!!", 2, 4, FONT_7X8, WHITE);
+                       // playSound("/sd/wavfiles/BUZZER.wav");
+                        //draw walls
+                        wait(2);
+                        
+                        //lcd.text_width(2);
+                        //lcd.text_height(2);
+                        if(p1 > p2) 
+                        //lcd.printf("Player 1 wins!");
+                        lcd.text_string("Player 1 Wins!", 2, 4, FONT_7X8, WHITE);
+                        
+                        else if(p1 == p2) 
+                        //lcd.printf("It's a draw!");
+                        lcd.text_string("It's a draw!", 2, 4, FONT_7X8, WHITE);
+                                
+                        else if(p2 > p1) 
+                        //lcd.printf("Player 2 wins!");
+                       lcd.text_string("Player 2 Wins!", 2, 4, FONT_7X8, WHITE);
+                       
+                       
+                            for (int i=0; i<100; i++) {
+                                //draw ball
+                                //lcd.cls();
+                        if(p1 > p2) 
+                        //lcd.printf("Player 1 wins!");
+                        lcd.text_string("Player 1 Wins!", 2, 4, FONT_7X8, WHITE);
+                        
+                        else if(p1 == p2) 
+                        //lcd.printf("It's a draw!");
+                      lcd.text_string("It's a draw!", 2, 4, FONT_7X8, WHITE);
+                                
+                        else if(p2 > p1) 
+                        //lcd.printf("Player 2 wins!");
+                        lcd.text_string("Player 2 Wins!", 2, 4, FONT_7X8, WHITE);
+                                
+                                lcd.filled_circle(x, y, radius, RED);
+                                lcd.filled_circle(x2,y2,radius,BLUE);
+                                RGB_LED(x,y,x);
+                                //bounce off edge walls and slow down a bit
+                                if ((x<=radius+1) || (x>=126-radius)) vx = -.90*vx;
+                                if ((y<=radius+1) || (y>=126-radius)) vy = -.90*vy;
+                                if ((x2<=radius+1) || (x2>=126-radius)) vx2 = -.90*vx2;
+                                if ((y2<=radius+1) || (y2>=126-radius)) vy = -.90*vy;
+                                //erase old ball location
+                                lcd.filled_circle(x, y, radius, BLACK);
+                                lcd.filled_circle(x2, y2, radius, BLACK);
+                                //move ball
+                                fx=fx+vx;
+                                fx2=fx2+vx2;
+                                fy=fy+vy;
+                                x=(int)fx;
+                                y=(int)fy;
+                                x2=(int)fx2;
+                                y2=(int)fy;
+                                }
+wait(3);
+lcd.cls();
+lcd.printf("End!");
+wait(3);
+lcd.cls();
+}
\ No newline at end of file