Skip to main content
Added language specifier to colour the code
Source Link
int playerXWon = 0; // No player won yet, 1 = player 1 wins, 2 = player 2 wins, 3 = draw

void loop() 
{ 
   // Delay for a random time.
   P1_Time = random(2000, 5000);
   delay(P1_Time);
 
   CheatCheck();

   if (playerXWon == 0)
   {
      // Start
      digitalWrite(P1_Pin, HIGH); 

      checkFirstPress(); 
   }
}

void CheatCheck()
{
   // Check if a player did not press its button too early. 
   // (Btw, it does not matter if the player presses and release its button before the LED goes on.
   S1_State = digitalRead(S1_Pin);
   S2_State = digitalRead(S2_Pin);

   if (S1_State == HIGH)
   {
      else if (S2_State == HIGH) 
      {
         PlayerWin(3);
      }
      else 
      {
         PlayerWin(2);
      }
   }
   else 
   {
      if (S2_State == HIGH) 
      {
         PlayerWin(1);
      }
   }
}

void checkFirstPress() 
{
   // Whoever presses first wins.
   while (playerXWon == 0) 
   {
      S1_State = digitalRead(S1_Pin);
      S2_State = digitalRead(S2_Pin);  

      if (S1_State == HIGH) 
      {
         if (S2_State == HIGH) 
         {
            PlayerWin(3);
         }
         else if (S1_State == HIGH) 
         {
            PlayerWin(1);
         }
      } 
      else if (S2_State == HIGH)
      {
         PlayerWin(2);
      }
   }
}

void playerWins(int player)
{
   switch (player) 
   {
   case 1:
      Serial.print("Player 1 wins\n");
      break;

   case 2:
      Serial.print("Player 2 wins\n");
      break;
 
   case 3:
      Serial.print("Both players win\n");
      break;

    default:
       assert(NULL); // Programming error
       break;
   }

   playerXWon = player;

   digitalWrite(P1_Pin, LOW);
}
    int playerXWon = 0; // No player won yet, 1 = player 1 wins, 2 = player 2 wins, 3 = draw
    
    void loop() 
    { 
       // Delay for a random time.
       P1_Time = random(2000, 5000);
       delay(P1_Time);
     
       CheatCheck();
    
       if (playerXWon == 0)
       {
          // Start
          digitalWrite(P1_Pin, HIGH); 
    
          checkFirstPress(); 
       }
    }
    
    void CheatCheck()
    {
       // Check if a player did not press its button too early. 
       // (Btw, it does not matter if the player presses and release its button before the LED goes on.
       S1_State = digitalRead(S1_Pin);
       S2_State = digitalRead(S2_Pin);
    
       if (S1_State == HIGH)
       {
          else if (S2_State == HIGH) 
          {
             PlayerWin(3);
          }
          else 
          {
             PlayerWin(2);
          }
       }
       else 
       {
          if (S2_State == HIGH) 
          {
             PlayerWin(1);
          }
       }
    }
    
    void checkFirstPress() 
    {
       // Whoever presses first wins.
       while (playerXWon == 0) 
       {
          S1_State = digitalRead(S1_Pin);
          S2_State = digitalRead(S2_Pin);  
    
          if (S1_State == HIGH) 
          {
             if (S2_State == HIGH) 
             {
                PlayerWin(3);
             }
             else if (S1_State == HIGH) 
             {
                PlayerWin(1);
             }
          } 
          else if (S2_State == HIGH)
          {
             PlayerWin(2);
          }
       }
    }
    
    void playerWins(int player)
    {
       switch (player) 
       {
       case 1:
          Serial.print("Player 1 wins\n");
          break;
    
       case 2:
          Serial.print("Player 2 wins\n");
          break;
     
       case 3:
          Serial.print("Both players win\n");
          break;
    
        default:
           assert(NULL); // Programming error
           break;
       }
    
       playerXWon = player;
    
       digitalWrite(P1_Pin, LOW);
    }
int playerXWon = 0; // No player won yet, 1 = player 1 wins, 2 = player 2 wins, 3 = draw

void loop() 
{ 
   // Delay for a random time.
   P1_Time = random(2000, 5000);
   delay(P1_Time);
 
   CheatCheck();

   if (playerXWon == 0)
   {
      // Start
      digitalWrite(P1_Pin, HIGH); 

      checkFirstPress(); 
   }
}

void CheatCheck()
{
   // Check if a player did not press its button too early. 
   // (Btw, it does not matter if the player presses and release its button before the LED goes on.
   S1_State = digitalRead(S1_Pin);
   S2_State = digitalRead(S2_Pin);

   if (S1_State == HIGH)
   {
      else if (S2_State == HIGH) 
      {
         PlayerWin(3);
      }
      else 
      {
         PlayerWin(2);
      }
   }
   else 
   {
      if (S2_State == HIGH) 
      {
         PlayerWin(1);
      }
   }
}

void checkFirstPress() 
{
   // Whoever presses first wins.
   while (playerXWon == 0) 
   {
      S1_State = digitalRead(S1_Pin);
      S2_State = digitalRead(S2_Pin);  

      if (S1_State == HIGH) 
      {
         if (S2_State == HIGH) 
         {
            PlayerWin(3);
         }
         else if (S1_State == HIGH) 
         {
            PlayerWin(1);
         }
      } 
      else if (S2_State == HIGH)
      {
         PlayerWin(2);
      }
   }
}

void playerWins(int player)
{
   switch (player) 
   {
   case 1:
      Serial.print("Player 1 wins\n");
      break;

   case 2:
      Serial.print("Player 2 wins\n");
      break;
 
   case 3:
      Serial.print("Both players win\n");
      break;

    default:
       assert(NULL); // Programming error
       break;
   }

   playerXWon = player;

   digitalWrite(P1_Pin, LOW);
}
    int playerXWon = 0; // No player won yet, 1 = player 1 wins, 2 = player 2 wins, 3 = draw
    
    void loop() 
    { 
       // Delay for a random time.
       P1_Time = random(2000, 5000);
       delay(P1_Time);
     
       CheatCheck();
    
       if (playerXWon == 0)
       {
          // Start
          digitalWrite(P1_Pin, HIGH); 
    
          checkFirstPress(); 
       }
    }
    
    void CheatCheck()
    {
       // Check if a player did not press its button too early. 
       // (Btw, it does not matter if the player presses and release its button before the LED goes on.
       S1_State = digitalRead(S1_Pin);
       S2_State = digitalRead(S2_Pin);
    
       if (S1_State == HIGH)
       {
          else if (S2_State == HIGH) 
          {
             PlayerWin(3);
          }
          else 
          {
             PlayerWin(2);
          }
       }
       else 
       {
          if (S2_State == HIGH) 
          {
             PlayerWin(1);
          }
       }
    }
    
    void checkFirstPress() 
    {
       // Whoever presses first wins.
       while (playerXWon == 0) 
       {
          S1_State = digitalRead(S1_Pin);
          S2_State = digitalRead(S2_Pin);  
    
          if (S1_State == HIGH) 
          {
             if (S2_State == HIGH) 
             {
                PlayerWin(3);
             }
             else if (S1_State == HIGH) 
             {
                PlayerWin(1);
             }
          } 
          else if (S2_State == HIGH)
          {
             PlayerWin(2);
          }
       }
    }
    
    void playerWins(int player)
    {
       switch (player) 
       {
       case 1:
          Serial.print("Player 1 wins\n");
          break;
    
       case 2:
          Serial.print("Player 2 wins\n");
          break;
     
       case 3:
          Serial.print("Both players win\n");
          break;
    
        default:
           assert(NULL); // Programming error
           break;
       }
    
       playerXWon = player;
    
       digitalWrite(P1_Pin, LOW);
    }
added 510 characters in body
Source Link
Michel Keijzers
  • 13k
  • 7
  • 43
  • 59
int playerXWon = 0; // No player won yet, 1 = player 1 wins, 2 = player 2 wins, 3 = draw

void loop()  
{ 
   // Delay for a random time.
   P1_Time = random(2000, 5000);
   delay(P1_Time);
 
   CheatCheck();

   if (playerXWon == 0)
   {
      // Start
      digitalWrite(P1_Pin, HIGH); 

      checkFirstPress(); 
   }
}

void CheatCheck()
{
   // Check if a player did not press its button too early. 
   // (Btw, it does not matter if the player presses and release its button before the LED goes on.
   S1_State = digitalRead(S1_Pin);
   S2_State = digitalRead(S2_Pin);
 
   if (S1_State == HIGH)
   {
      else if (S2_State == HIGH) 
      {
         PlayerWin(3);
      }
      else 
      {
         PlayerWin(2);
      }
   }
   else 
   {
      if (S2_State == HIGH) {
        PlayerWin(1);{
     }
     else PlayerWin(1);
      }
   }
}
  // Start
 void digitalWritecheckFirstPress(P1_Pin, HIGH);  
  {
   // Whoever presses first wins.
   while (playerXWon == 0) 
   {
      S1_State = digitalRead(S1_Pin);
      S2_State = digitalRead(S2_Pin);  

      if (S1_State == HIGH) 
      {
         if (S2_State == HIGH) 
         {
            PlayerWin(3);
         }
         else if (S1_State == HIGH) 
         {
            PlayerWin(1);
         }
      } 
      else if (S2_State == HIGH)
      {
         PlayerWin(2);
      }
   }
}

void playerWins(int player) // -1 is both
{
    ifswitch (player)  
   {
  (player ==case 1):
 {     Serial.print("Player 1 wins\n");
    else if break;

  (player ==case 2):
 {     Serial.print("Player 2 wins\n");
    else if break;
 (player 
 ==  case 3):
 {     Serial.print("Both players win\n");
      break;

    default:
       assert(NULL); // Programming error
       break;
   }

   playerXWon = player;
  
   digitalWrite(P1_Pin, LOW);
}
int playerXWon = 0; // No player won yet, 1 = player 1 wins, 2 = player 2 wins, 3 = draw

void loop() { 
  // Delay for a random time.
  P1_Time = random(2000, 5000);
  delay(P1_Time);
  
  // Check if a player did not press its button too early. 
  // (Btw, it does not matter if the player presses and release its button before the LED goes on.
  S1_State = digitalRead(S1_Pin);
  S2_State = digitalRead(S2_Pin);
 
  if (S1_State == HIGH)
  {
     else if (S2_State == HIGH) {
        PlayerWin(3);
     }
     else {
        PlayerWin(2);
     }
  else {
     if (S2_State == HIGH) {
        PlayerWin(1);
     }
     else 
     }
  }

  // Start
  digitalWrite(P1_Pin, HIGH);  
  
  // Whoever presses first wins.
  while (playerXWon == 0) {
     S1_State = digitalRead(S1_Pin);
     S2_State = digitalRead(S2_Pin);

     if (S1_State == HIGH) {
        if (S2_State == HIGH) {
           PlayerWin(3);
        }
        else if (S1_State == HIGH) {
           PlayerWin(1);
        }
     } 
     else if (S2_State == HIGH)
     {
        PlayerWin(2);
     }
 }


void playerWins(int player) // -1 is both
{
    if       (player == 1) { Serial.print("Player 1 wins\n");
    else if  (player == 2) { Serial.print("Player 2 wins\n");
    else if  (player == 3) { Serial.print("Both players win\n");

    playerXWon = player;
    digitalWrite(P1_Pin, LOW);
}
int playerXWon = 0; // No player won yet, 1 = player 1 wins, 2 = player 2 wins, 3 = draw

void loop()  
{ 
   // Delay for a random time.
   P1_Time = random(2000, 5000);
   delay(P1_Time);
 
   CheatCheck();

   if (playerXWon == 0)
   {
      // Start
      digitalWrite(P1_Pin, HIGH); 

      checkFirstPress(); 
   }
}

void CheatCheck()
{
   // Check if a player did not press its button too early. 
   // (Btw, it does not matter if the player presses and release its button before the LED goes on.
   S1_State = digitalRead(S1_Pin);
   S2_State = digitalRead(S2_Pin);

   if (S1_State == HIGH)
   {
      else if (S2_State == HIGH) 
      {
         PlayerWin(3);
      }
      else 
      {
         PlayerWin(2);
      }
   }
   else 
   {
      if (S2_State == HIGH) 
      {
         PlayerWin(1);
      }
   }
}

void checkFirstPress() 
{
   // Whoever presses first wins.
   while (playerXWon == 0) 
   {
      S1_State = digitalRead(S1_Pin);
      S2_State = digitalRead(S2_Pin);  

      if (S1_State == HIGH) 
      {
         if (S2_State == HIGH) 
         {
            PlayerWin(3);
         }
         else if (S1_State == HIGH) 
         {
            PlayerWin(1);
         }
      } 
      else if (S2_State == HIGH)
      {
         PlayerWin(2);
      }
   }
}

void playerWins(int player)
{
   switch (player)  
   {
   case 1:
      Serial.print("Player 1 wins\n");
      break;

   case 2:
      Serial.print("Player 2 wins\n");
      break;
  
   case 3:
      Serial.print("Both players win\n");
      break;

    default:
       assert(NULL); // Programming error
       break;
   }

   playerXWon = player;
 
   digitalWrite(P1_Pin, LOW);
}
added 1643 characters in body
Source Link
Michel Keijzers
  • 13k
  • 7
  • 43
  • 59

Untested proposal:

int playerXWon = 0; // No player won yet, 1 = player 1 wins, 2 = player 2 wins, 3 = draw

void loop() { 
  // Delay for a random time.
  P1_Time = random(2000, 5000);
  delay(P1_Time);
  
  // Check if a player did not press its button too early. 
  // (Btw, it does not matter if the player presses and release its button before the LED goes on.
  S1_State = digitalRead(S1_Pin);
  S2_State = digitalRead(S2_Pin);
 
  if (S1_State == HIGH)
  {
     else if (S2_State == HIGH) {
        PlayerWin(3);
     }
     else {
        PlayerWin(2);
     }
  else {
     if (S2_State == HIGH) {
        PlayerWin(1);
     }
     else 
     }
  }

  // Start
  digitalWrite(P1_Pin, HIGH);  
  
  // Whoever presses first wins.
  while (playerXWon == 0) {
     S1_State = digitalRead(S1_Pin);
     S2_State = digitalRead(S2_Pin);

     if (S1_State == HIGH) {
        if (S2_State == HIGH) {
           PlayerWin(3);
        }
        else if (S1_State == HIGH) {
           PlayerWin(1);
        }
     } 
     else if (S2_State == HIGH)
     {
        PlayerWin(2);
     }
 }


void playerWins(int player) // -1 is both
{
    if       (player == 1) { Serial.print("Player 1 wins\n");
    else if  (player == 2) { Serial.print("Player 2 wins\n");
    else if  (player == 3) { Serial.print("Both players win\n");

    playerXWon = player;
    digitalWrite(P1_Pin, LOW);
}

Untested proposal:

int playerXWon = 0; // No player won yet, 1 = player 1 wins, 2 = player 2 wins, 3 = draw

void loop() { 
  // Delay for a random time.
  P1_Time = random(2000, 5000);
  delay(P1_Time);
  
  // Check if a player did not press its button too early. 
  // (Btw, it does not matter if the player presses and release its button before the LED goes on.
  S1_State = digitalRead(S1_Pin);
  S2_State = digitalRead(S2_Pin);
 
  if (S1_State == HIGH)
  {
     else if (S2_State == HIGH) {
        PlayerWin(3);
     }
     else {
        PlayerWin(2);
     }
  else {
     if (S2_State == HIGH) {
        PlayerWin(1);
     }
     else 
     }
  }

  // Start
  digitalWrite(P1_Pin, HIGH);  
  
  // Whoever presses first wins.
  while (playerXWon == 0) {
     S1_State = digitalRead(S1_Pin);
     S2_State = digitalRead(S2_Pin);

     if (S1_State == HIGH) {
        if (S2_State == HIGH) {
           PlayerWin(3);
        }
        else if (S1_State == HIGH) {
           PlayerWin(1);
        }
     } 
     else if (S2_State == HIGH)
     {
        PlayerWin(2);
     }
 }


void playerWins(int player) // -1 is both
{
    if       (player == 1) { Serial.print("Player 1 wins\n");
    else if  (player == 2) { Serial.print("Player 2 wins\n");
    else if  (player == 3) { Serial.print("Both players win\n");

    playerXWon = player;
    digitalWrite(P1_Pin, LOW);
}
Source Link
Michel Keijzers
  • 13k
  • 7
  • 43
  • 59
Loading