Code for Player 2 of Rock Paper Scissors
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed wave_player
Diff: main.cpp
- Revision:
- 1:5841ff349aae
- Parent:
- 0:75276caf1a37
- Child:
- 2:54fafe349aa7
diff -r 75276caf1a37 -r 5841ff349aae main.cpp --- a/main.cpp Mon Oct 19 07:43:06 2015 +0000 +++ b/main.cpp Mon Oct 19 07:58:09 2015 +0000 @@ -29,16 +29,14 @@ //Choice = 1 for rock //Choice = 2 for paper //Choice = 3 for scissors -int choice1 = 0; -int choice2 = 0; +int choice1 = 0; //Player 1's choice +int choice2 = 0; //Player 2's choice //Points -int p1 = 0; -int p2 = 0; +int p1 = 0; //Player 1's points +int p2 = 0; //Player 2's points - - - +//RGB function for Shiftbrite void RGB_LED(int red, int green, int blue); void RGB_LED(int red, int green, int blue) { @@ -70,26 +68,22 @@ 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 push button 1 is pressed, start the game! if(pb1 != 1){ L3: lcd.cls(); //Clear screen + //Tell player 1 that she/he has 5 seconds to chose between rock,paper and scissors lcd.locate(0,4); lcd.printf(" Player 1 \n\n\n"); lcd.printf("You have 5 seconds"); @@ -98,29 +92,20 @@ wait(2); - + //Start timer for 5 seconds 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); + lcd.locate(4,4); //Place text in the center of the screen + lcd.text_width(8); //Set tet width + lcd.text_height(8); //Set text height + lcd.text_bold(ON); //Set text style to 'bold' + RGB_LED(red*25,0,0);//Increase intensity of light on shiftbrite as time decreases red = red + 5; - //green = green + 5; - //blue = blue + 5; - //wait(2); - //red = red + 15; - //RGB_LED(red, green,blue); - lcd.printf("%d",i); + lcd.printf("%d",i); //Display time left on LCD wait(1); + //Press pushbutton 1 to choose rock if(pb1 != 1) { lcd.cls(); choice1 = 1; @@ -130,6 +115,7 @@ lcd.cls(); break; } + //Press pushbutton 2 to choose paper if(pb2 != 1) { lcd.cls(); choice1 = 2; @@ -139,6 +125,7 @@ lcd.cls(); break; } + //Press pushbutton 3 to choose scissor if(pb3 != 1) { lcd.cls(); choice1 = 3; @@ -152,18 +139,23 @@ break; } } - RGB_LED(0,0,0); - if(i == -1){ + RGB_LED(0,0,0); // Reset shitbrite color + + //If user doesn't choose an option, show "Time Out" message and restart game + if(i == -1){ lcd.cls(); lcd.printf("Your time is up"); wait(5); - goto L1; + goto L1; //Starts the game again } lcd.cls(); //Clear screen wait(2); + //Check to see if Player 2 is ready to play lcd.printf("Press 1 if P2 is ready"); + + //Start timer for player 2 if player 2 presses pushbutton 1 while(1){ if(pb1 != 1) break; } @@ -190,6 +182,8 @@ blue = blue + 5; lcd.printf("%d",j); wait(1); + + //Rock if(pb1 != 1) { lcd.cls(); choice2 = 1; @@ -199,6 +193,7 @@ lcd.cls(); break; } + //Paper if(pb2 != 1) { lcd.cls(); choice2 = 2; @@ -208,6 +203,7 @@ lcd.cls(); break; } + //Scissors if(pb3 != 1) { lcd.cls(); choice2 = 3; @@ -221,6 +217,8 @@ break; } } + + //Time out message if(j == -1){ lcd.cls(); lcd.printf("Your time is up"); @@ -230,8 +228,9 @@ RGB_LED(0,0,0); - - + //Check to see who won + + //Case 1 : Both players choose same option if(choice1 == 1 && choice2 == 1 || choice1== 2 && choice2 == 2 || choice1 == 3 && choice2 == 3 ) { @@ -250,7 +249,9 @@ else if(pb2 != 1) goto L2; } } - + //Case 2: + //Player 1 : Rock + //Player 2 : Paper else if(choice1 == 1 && choice2 == 2){ lcd.cls(); @@ -272,11 +273,11 @@ } else if(pb2 != 1) goto L2; } - } - - } - - else if(choice2== 1 && choice1== 2){ + }} + //Case 3: + //Player 1 : Paper + //Player 2 : Rock + else if(choice2== 1 && choice1== 2){ lcd.cls(); lcd.printf("P1 chose Paper\n"); @@ -300,9 +301,11 @@ } } - + //Case 4: + //Player 1 : Paper + //Player 2 : Scissor - else if(choice1 == 2 && choice2 == 3){ + else if(choice1 == 2 && choice2 == 3){ lcd.cls(); lcd.printf("P1 chose Paper\n"); @@ -329,7 +332,9 @@ else if(pb2 != 1) goto L2; } } - + //Case 5: + //Player 1 : Scissor + //Player 2 : Paper else if(choice1 == 3 && choice2 == 2){ lcd.cls(); @@ -358,7 +363,9 @@ else if(pb2 != 1) goto L2; } } - + //Case 6: + //Player 1 : Rock + //Player 2 : Scissors else if(choice1 == 1 && choice2 == 3){ lcd.cls(); @@ -383,9 +390,11 @@ else if(pb2 != 1) goto L2; } } - + //Case 7: + //Player 1 : Scissors + //Player 2 : Rock - else if(choice1 == 3 && choice2 == 1){ + else if(choice1 == 3 && choice2 == 1){ lcd.cls(); lcd.printf("P1 chose Scissors\n"); @@ -415,14 +424,6 @@ - /* - while(1) { - myled = 1; - wait(0.2); - myled = 0; - wait(0.2); - } - */ // Code to play a wave file /* FILE *wave_file; @@ -434,18 +435,8 @@ 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); - } - */ - - + +//If game ends : L2: lcd.cls(); @@ -454,23 +445,17 @@ 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 Player 1's score is higher than Player 2's score if(p1 > p2) //lcd.printf("Player 1 wins!"); lcd.text_string("Player 1 Wins!", 2, 4, FONT_7X8, WHITE); - + //If Player 1's score is equal to Player 2's score else if(p1 == p2) //lcd.printf("It's a draw!"); lcd.text_string("It's a draw!", 2, 4, FONT_7X8, WHITE); - + //If Player 2's score is higher than Player 1's score else if(p2 > p1) //lcd.printf("Player 2 wins!"); lcd.text_string("Player 2 Wins!", 2, 4, FONT_7X8, WHITE);