![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
master
Dependencies: mbed
Fork of ESE350-Whack-a-Mole by
Diff: WhackAMole.cpp
- Revision:
- 3:3c0c008bacb3
- Parent:
- 2:4c79ab84c285
--- a/WhackAMole.cpp Wed Oct 21 01:30:31 2015 +0000 +++ b/WhackAMole.cpp Thu Oct 22 02:16:41 2015 +0000 @@ -88,24 +88,114 @@ int action; int round_counter; int score_counter; + int best_score=0; + int level; + int number_of_nodes=2; char input; + bool confirmed; //Start the timer timer.start(); - + if (number_of_nodes>=3){ + confirmed=false; + strcpy(txBuffer, "check_3"); + rf_send(txBuffer, strlen(txBuffer)+1); + pc.printf("Sent: %s\r\n", txBuffer); + timer.reset(); + while(timer.read_ms()<2000){ + rxLen = rf_receive(rxBuffer, 128); + if (rxLen>0){ + pc.printf("Received: %s\r\n", rxBuffer); + //strcpy(rxBuffer,""); + if(!strcmp("confirm_3", rxBuffer)) { + confirmed=true; + pc.printf("Node 3 alive\r\n"); + break; + } + } + } + if (!confirmed){ + pc.printf("Node 3 is dead. Repair your hardware!!!"); + return 0; + } + } + if (number_of_nodes>=2){ + confirmed=false; + strcpy(txBuffer, "check_2"); + rf_send(txBuffer, strlen(txBuffer)+1); + pc.printf("Sent: %s\r\n", txBuffer); + timer.reset(); + while(timer.read_ms()<2000){ + rxLen = rf_receive(rxBuffer, 128); + if (rxLen>0){ + pc.printf("Received: %s\r\n", rxBuffer); + //strcpy(rxBuffer,""); + if(!strcmp("confirm_2", rxBuffer)) { + confirmed=true; + pc.printf("Node 2 alive\r\n"); + break; + } + } + } + if (!confirmed){ + pc.printf("Node 2 is dead. Repair your hardware!!!"); + return 0; + } + } + if (number_of_nodes>=1){ + confirmed=false; + strcpy(txBuffer, "check_1"); + rf_send(txBuffer, strlen(txBuffer)+1); + pc.printf("Sent: %s\r\n", txBuffer); + timer.reset(); + while(timer.read_ms()<2000){ + rxLen = rf_receive(rxBuffer, 128); + if (rxLen>0){ + pc.printf("Received: %s\r\n", rxBuffer); + //strcpy(rxBuffer,""); + if(!strcmp("confirm_1", rxBuffer)) { + confirmed=true; + pc.printf("Node 1 alive\r\n"); + break; + } + } + } + if (!confirmed){ + pc.printf("Node 1 is dead. Repair your hardware!!!"); + return 0; + } + } + while (true){ + pc.printf("Enter Level (1-3) \r\n"); + input=pc.getc(); + if (input=='1'){ + level=1; + break; + } + else if (input=='2'){ + level=2; + break; + } + else if (input=='3'){ + level=3; + break; + } + } + while (true){ + pc.printf("Press 's' to start \r\n"); + input=pc.getc(); + if (input=='s'){ + break; + } + } while(true) { - while (true){ - input=pc.getc(); - if (input=='s'){ - break; - } - } + pc.printf("start!"); round_counter=0; score_counter=0; while(round_counter<10){ - action = rand()%3; + action = rand()%(number_of_nodes+1); if(action==0){ - wait(1); + wait_ms(1000/level); } else if (action==1){ timer.reset(); @@ -113,15 +203,22 @@ rf_send(txBuffer, strlen(txBuffer)+1); pc.printf("Sent: %s\r\n", txBuffer); while(true){ - rxLen = rf_receive(rxBuffer, 128); - if (rxLen>0){ + rxLen = rf_receive(rxBuffer, 128); + if (timer.read_ms()>((1000+(500*(10-round_counter)))/level)){ + strcpy(txBuffer, "time_out_1"); + rf_send(txBuffer, strlen(txBuffer)+1); + pc.printf("Sent: %s\r\n", txBuffer); + score_counter+=((500*(10-round_counter))/level)+2000; + break; + } + else if (rxLen>0){ pc.printf("Received: %s\r\n", rxBuffer); if(!strcmp("finished_1", rxBuffer)) { + score_counter+=timer.read_ms(); break; } } } - score_counter+=timer.read_ms(); round_counter++; } else if (action==2){ @@ -130,34 +227,64 @@ rf_send(txBuffer, strlen(txBuffer)+1); pc.printf("Sent: %s\r\n", txBuffer); while(true){ - rxLen = rf_receive(rxBuffer, 128); - if (rxLen>0){ - pc.printf("Received: %s\r\n", rxBuffer); - if(!strcmp("finished_2", rxBuffer)) { + rxLen = rf_receive(rxBuffer, 128); + if (timer.read_ms()>((1000+(500*(10-round_counter)))/level)){ + strcpy(txBuffer, "time_out_2"); + rf_send(txBuffer, strlen(txBuffer)+1); + pc.printf("Sent: %s\r\n", txBuffer); + score_counter+=((500*(10-round_counter))/level)+2000; + break; + } + else if (rxLen>0){ + pc.printf("Received: %s\r\n", rxBuffer); + if(!strcmp("finished_2", rxBuffer)) { + score_counter+=timer.read_ms(); break; } } } - score_counter+=timer.read_ms(); round_counter++; } else if (action ==3){ timer.reset(); strcpy(txBuffer, "activate_3"); rf_send(txBuffer, strlen(txBuffer)+1); + pc.printf("Sent: %s\r\n", txBuffer); while(true){ - rxLen = rf_receive(rxBuffer, 128); - if (rxLen>0){ - pc.printf("Received: %s\r\n", rxBuffer); - if(!strcmp("finished_3", rxBuffer)) { + rxLen = rf_receive(rxBuffer, 128); + if (timer.read_ms()>((1000+(500*(10-round_counter)))/level)){ + strcpy(txBuffer, "time_out_3"); + rf_send(txBuffer, strlen(txBuffer)+1); + pc.printf("Sent: %s\r\n", txBuffer); + score_counter+=((500*(10-round_counter))/level)+2000; + break; + } + else if (rxLen>0){ + pc.printf("Received: %s\r\n", rxBuffer); + if(!strcmp("finished_3", rxBuffer)) { + score_counter+=timer.read_ms(); break; } } } - score_counter+=timer.read_ms(); round_counter++; } } - pc.printf("Score: %d", score_counter); + pc.printf("Score: %d \r\n", score_counter); + if (score_counter<best_score){ + best_score=score_counter; + } + while (true){ + pc.printf("New game? (y/n)\r\n"); + input=pc.getc(); + if (input=='y'|| input=='n'){ + break; + } + } + if (input=='n'){ + pc.printf("Best Score %d", best_score); + break; + } + } }