This is the Mexican Standoff prototype made by Francisco Martin and Andrew Smith. Please refer to the following link for instructions on hardware hookup: https://developer.mbed.org/users/fomartin/notebook/mexican-standoff-reaction-game/

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

Revision:
1:4976bbb3376f
Parent:
0:75716bd37804
Child:
2:3c1a5079243d
--- a/States/Gameplay.cpp	Mon Mar 14 03:04:08 2016 +0000
+++ b/States/Gameplay.cpp	Mon Mar 14 16:19:47 2016 +0000
@@ -2,7 +2,7 @@
 #include "mbed.h"
 
 ////////////////////////////////////////////////////////////////
-// CONSTRUCTORS
+// CONSTRUCTOR
 ////////////////////////////////////////////////////////////////
 Gameplay::Gameplay(uLCD_4DGL &uLCD, int numberOfPlayers, PinDetect &P1_Left, PinDetect &P1_Right, PinDetect &P2_Left, PinDetect &P2_Right)
 {   
@@ -15,6 +15,7 @@
     points = new int[2];
     CPU cpu;
     
+    //LED's used to indicate which player wins each round
     DigitalOut led1(LED1);
     DigitalOut led4(LED4);
 
@@ -27,22 +28,28 @@
     resetPoints(uLCD);
     
     //it takes 5-6 seconds for guns to get drawn, but we only draw
-    //them once at the beginning of the game
+    //them once at the beginning of the game-- so that's okay
     drawGun(uLCD, 10, 30, true);
     drawGun(uLCD, 10, 92, true);
     
     drawGun(uLCD, 127 - 10 - 42, 30, false);
     drawGun(uLCD, 127 - 10 - 42, 92, false);
     
+    //loop until someone runs out of points
     while(getPoints(1) != 0 && getPoints(2) != 0)
     {
         //gameplay here
-        //basic structure should be this:
+        
+        //generate which prompt will be given
         Prompt prompt = (Prompt)(rand() % 4);
         
-        countdown(uLCD);        
+        //display countdown
+        countdown(uLCD);   
+        
+        //clear the countdown     
         clearCountdown(uLCD);
         
+        //show the prompt, and listen for inputs
         if(prompt == HoldFire)
         {
             //hold fire
@@ -51,7 +58,9 @@
             Timer timer;
             timer.start();
             
-            //hold fire for 2 seconds
+            //hold fire lasts for 2 seconds
+            //if a player fires during this 2 seconds
+            //they lose a point
             while(timer.read_ms() < 2000)
             {
                 if(!P1_Left || !P1_Right)
@@ -61,7 +70,7 @@
                     break;
                 }
                 
-                //AI will never mis-fire
+                //AI will never mis-fire on hold-fire... just for simplicity's sake
                 if(numPlayers == 1 && (!P2_Left || !P2_Right))
                 {
                     setPoints(2, getPoints(2) - 1, uLCD);
@@ -92,11 +101,17 @@
                 
             Timer timer;
             timer.start();
+            
+            //these variables act as a layer of indirection between the button being pressed
+            //and the value being read by the program. This allows us to "inject" fake
+            //button presses for the AI without having to actually have a button signal
+            //be sent by the hardware
             bool p1l = false, p1r = false, p2l = false, p2r = false;
             
             //wait for button to be pressed
             while(true)
             {
+                //if the predetermined ai wait time has elapsed, input the AI's button press
                 if(numPlayers == 1 && timer.read_ms() > aiShootTime)
                 {
                     bool aiCorrect = cpu.shootAnswer(prompt);
@@ -129,7 +144,7 @@
                     }
                     else
                     {
-                        //prompt = either, just press left
+                        //prompt = either, just press left since either one works for this prompt
                         p2l = true;
                         p2r = false;
                     }
@@ -155,6 +170,7 @@
                     break;
                 }
                 
+                //only read p2 buttons if its a 2 player game
                 if(numPlayers == 2 && !P2_Left)
                 {
                     p1l = false;
@@ -174,6 +190,8 @@
                 }
             }
             
+            
+            //check the button presses against the prompt given. decrement points accordingly
             if(p1l)
             {
                 if(prompt == Either || prompt == Up)
@@ -228,12 +246,15 @@
             }
         }
         
+        //remove the displayed prompt. Wait 2 seconds to give the players some
+        //amount of rest time between rounds
         clearPrompt(uLCD);
         Thread::wait(2000);
         led1 = 0;
         led4 = 0;
     }
     
+    //store the winning player in a variable so the main method can create the correct GameOver screen
     if(getPoints(1) == 0)
         winningPlayer = 2;
     else
@@ -275,6 +296,7 @@
 
 void Gameplay::drawGun(uLCD_4DGL &uLCD, int x, int y, bool facingRight)
 {
+    //image arrays generated using a separate Java program (for ease of operating with images)
     if(facingRight)
     {
         int colors[] = {12566463, 12566463, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -5023488, -5023488, -5023488, -5023488, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -5023488, -5023488, -5023488, -5023488, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -5023488, -5023488, -8372224, -8372224, -8372224, -8372224, -8372224, -8372224, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, -16777216, -16777216, 12566463, 12566463, -16777216, -16777216, -5023488, -5023488, -8372224, -8372224, -8372224, -8372224, -8372224, -8372224, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -16777216, -16777216, -8372224, -8372224, -8372224, -8372224, -8372224, -8372224, -8372224, -8372224, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -16777216, -16777216, -8372224, -8372224, -8372224, -8372224, -8372224, -8372224, -8372224, -8372224, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -16777216, -16777216, -8372224, -8372224, -13158601, -13158601, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -16777216, -16777216, -8372224, -8372224, -13158601, -13158601, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, 12566463, 12566463, -16777216, -16777216, -13158601, -13158601, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, 12566463, 12566463, -16777216, -16777216, -13158601, -13158601, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, 12566463, 12566463, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, 12566463, 12566463, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -13158601, -13158601, -13158601, -13158601, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -13158601, -13158601, -13158601, -13158601, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -13158601, -13158601, -13158601, -13158601, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -13158601, -13158601, -13158601, -13158601, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -13158601, -13158601, -13158601, -13158601, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -13158601, -13158601, -13158601, -13158601, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -11382190, -11382190, -11382190, -11382190, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -11382190, -11382190, -11382190, -11382190, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -11382190, -11382190, -13158601, -13158601, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -13158601, -13158601, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, -13158601, -13158601, -16777216, -16777216, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, 12566463, -16777216, -16777216, 12566463, 12566463, 12566463, 12566463};
@@ -342,7 +364,7 @@
     uLCD.line(54, 108, 74, 88, LGREY);
 }
 
-
+//Displays 3..2..1.. on screen
 void Gameplay::countdown(uLCD_4DGL &uLCD)
 {
     uLCD.locate(5, 8);
@@ -366,11 +388,13 @@
     Thread::wait(333);
 }
 
+//clears the 3..2..1.. display
 void Gameplay::clearCountdown(uLCD_4DGL &uLCD)
 {
     uLCD.filled_rectangle(30, 50, 98, 78, LGREY);
 }
 
+//re-draws the score on the screen
 void Gameplay::renderScore(uLCD_4DGL &uLCD)
 {    
     uLCD.locate(3, 0);