Code for Player 2 of Rock Paper Scissors
Dependencies: 4DGL-uLCD-SE PinDetect SDFileSystem mbed wave_player
Diff: main.cpp
- Revision:
- 3:fb5a647123cb
- Parent:
- 2:54fafe349aa7
- Child:
- 4:0be9cd917c17
diff -r 54fafe349aa7 -r fb5a647123cb main.cpp --- a/main.cpp Wed Oct 21 23:41:47 2015 +0000 +++ b/main.cpp Thu Oct 22 01:15:48 2015 +0000 @@ -9,6 +9,8 @@ DigitalOut myled(LED1); DigitalOut myled2(LED2); +DigitalOut myled3(LED3); + SDFileSystem sd(p5, p6, p7, p8, "sd"); //AnalogOut DACout(p18); //Beep DACout(p18); @@ -55,16 +57,14 @@ latch=0; } - -// Serial -Serial player2(p9, p10); - +//Serial +Serial player2(p9,p10); int main() { - + //Serial player2.baud(9600); - + spi.format(16,0); spi.frequency(500000); enable=0; @@ -112,15 +112,19 @@ while(1){ //If push button 1 is pressed, start the game! if(pb1 != 1){ + //Play button select + /* FILE *wave_file; printf("\n\n\nHello, wave world!\n"); - wave_file=fopen("/sd/mydir/select.wav","r"); + wave_file=fopen("/sd/select.wav","r"); waver.play(wave_file); wait(1); printf("ok!!"); fclose(wave_file); - //L3: + */ + + L3: lcd.cls(); //Clear screen lcd.rectangle(0,0,125,125,WHITE); //Draw white border //Tell player 1 that she/he has 5 seconds to chose between rock,paper and scissors @@ -136,7 +140,7 @@ //Start timer for 5 seconds - int i = 5; + int i = 5;; int red = 1; //for(i = 5; i > -1 ; i--){ while(i > -1){ @@ -167,6 +171,7 @@ wait(1); printf("ok!!"); fclose(wave_file); + lcd.circle(60, 60, 30,WHITE); wait(3); lcd.cls(); @@ -221,8 +226,6 @@ i--; } - - RGB_LED(0,0,0); // Reset shitbrite color //------------------------------------------------------------------------------------------------------------------------------------- @@ -250,26 +253,12 @@ } //-------------------------------------------------------------------------------------------------------------------------------------- - - //data = 'u'; - //data = btserial.getc(); - - player2.printf("%c",choice2); - myled = 1; - wait(1); - - while(1){ - if(player2.readable()){ - choice1 = player2.getc(); - myled2 = 1; - break; - } - } // lcd.cls(); //Clear screen // wait(2); + /* //-------------------------------------------------------------------------------------------------------------------------------- //------------------------------- PLAYER 2 -------------------------------------------------------------------------------------- @@ -413,21 +402,35 @@ goto L1; } RGB_LED(0,0,0); - */ - - + + */ + + player2.printf("%c",choice2); + myled2 = 1; + wait(1); + + while(1){ + if(player2.readable()){ + choice1 = player2.getc(); + myled = 1; + break; + } + } + + //------------------------------------------------------------------------------------------------------------------------------------- //Check to see who won - +//-------------------------------------------------------------------------------------------------------------------------- //Case 1 : Both players choose same option if(choice1 == 'R' && choice2 == 'R' || - choice1== 'P' && choice2 == 'P'|| + choice1== 'P' && choice2 == 'P' || choice1 == 'S' && choice2 == 'S' ) { lcd.cls(); lcd.rectangle(0,0,125,125,WHITE); //Draw white border lcd.locate(1,2); lcd.printf(" It's a draw!"); wait(4); + /* lcd.cls(); lcd.locate(1,4); lcd.rectangle(0,0,125,125,WHITE); //Draw white border @@ -442,7 +445,7 @@ goto L3; } else if(pb2 != 1) goto L2; - } + }*/ } //Case 2: //Player 1 : Rock @@ -460,6 +463,7 @@ lcd.filled_rectangle(50, 60, 80,90,WHITE); p2++; wait(4); + /* lcd.cls(); lcd.locate(1,4); lcd.rectangle(0,0,125,125,WHITE); //Draw white border @@ -474,7 +478,7 @@ goto L3; } else if(pb2 != 1) goto L2; - } + }*/ } //Case 3: //Player 1 : Paper @@ -491,6 +495,7 @@ lcd.filled_rectangle(50, 60, 80,90,WHITE); p1++; wait(5); + /* lcd.cls(); lcd.locate(1,4); lcd.rectangle(0,0,125,125,WHITE); //Draw white border @@ -506,6 +511,7 @@ } else if(pb2 != 1) goto L2; } + */ } //Case 4: @@ -526,6 +532,7 @@ lcd.line(62, 61, 90, 85, WHITE); lcd.line(62, 81, 90, 55, WHITE); wait(5); + /* lcd.cls(); lcd.locate(1,4); lcd.rectangle(0,0,125,125,WHITE); //Draw white border @@ -541,6 +548,7 @@ } else if(pb2 != 1) goto L2; } + */ } //Case 5: //Player 1 : Scissor @@ -559,6 +567,7 @@ lcd.line(62, 61, 90, 85, WHITE); lcd.line(62, 81, 90, 55, WHITE); wait(5); + /* lcd.cls(); lcd.locate(1,4); lcd.rectangle(0,0,125,125,WHITE); //Draw white border @@ -574,6 +583,7 @@ } else if(pb2 != 1) goto L2; } + */ } //Case 6: //Player 1 : Rock @@ -589,6 +599,7 @@ p1++; lcd.circle(60, 70, 30,WHITE); wait(5); + /* lcd.cls(); lcd.locate(1,4); lcd.rectangle(0,0,125,125,WHITE); //Draw white border @@ -603,7 +614,7 @@ goto L3; } else if(pb2 != 1) goto L2; - } + }*/ } //Case 7: //Player 1 : Scissors @@ -620,6 +631,7 @@ p2++; lcd.circle(60, 70, 30,WHITE); wait(5); + /* lcd.cls(); lcd.locate(1,4); lcd.rectangle(0,0,125,125,WHITE); //Draw white border @@ -634,26 +646,75 @@ goto L3; } else if(pb2 != 1) goto L2; + }*/ + } + + lcd.cls(); + lcd.locate(1,4); + lcd.rectangle(0,0,125,125,WHITE); //Draw white border + lcd.printf(" P1 Score : %d\n",p1); + lcd.printf(" P2 Score : %d\n\n\n\n",p2); + lcd.printf(" 1 to continue\n\n"); + lcd.printf(" 2 to end\n\n"); + + char continue1 = 'N'; + char continue2 = 'N'; + //char cont; + + + while(1){ + if(pb1 != 1) { + lcd.cls(); + continue2 = 'Y'; + break; + //goto L3; + } + else if(pb2 != 1) { + lcd.cls(); + continue2 = 'N'; + break; + } } - } + + + player2.printf("%c",continue2); + myled3 = 1; + wait(1); + + while(1){ + if(player2.readable()){ + continue1 = player2.getc(); + myled = 1; + break; + } + } + + //If both say continue then you need to go back to L3 + if(continue1 == 'Y' && continue2 == 'Y') goto L3; + else goto L2; } - - + + } // Code to play a wave file - FILE *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); + */ + - + + + + //If game ends : L2: lcd.cls(); @@ -723,8 +784,6 @@ x2=(int)fx2; y2=(int)fy; } -lcd.cls(); - +lcd.cls(); - -} +} \ No newline at end of file