Guess the String (v1.6.1)

Enter a string and hit the guess button.

Here is the code

The code has been obfuscated very slightly, just to provide a bit more fun.

<?php
if ($form->isSubmitted() && $form->isValid()) {
  $win   = 'Successful guess';
  $lose  = 'Unsuccessful guess';
  $arrGuess = str_split($form->getData()['guess']);
  $arrSolved = array();
  if (count($arrGuess) == 16) {
    for ($i = 0; $i < 16; $i++) {
      $toSolve = ($i < 10) ? $i+48 : $i+55;
      if ($toSolve == ord($arrGuess[$i])) {
        $arrSolved[] = true;
      } else {
        break;
      }
    }
  }
  $output = (count($arrSolved) == 16) ? $win : $lose;
  $this->addFlash('success', $output);
}
?>

Update notes

1.1.0 - Removed unneccessary loop.

1.2.0 - Replaced foreach loop with for loop.

1.3.0 - Refactored success/fail response to catch all fails.

1.4.0 - Refactored so no longer displays 'Unsuccessful guess' on page load.

1.4.1 - Removed unneccessary addslashes() function call.

1.5.0 - Removed guess string variable and refactored into guess array.

1.5.1 - Added break from loop on first incorrect value.

1.5.2 - Fixed the code so that it works, a number of issues were present which frankly, were embarrasing, it was so bad that the change introduced in 1.5.1 was rolled back it simply couldn't work like that. Then it got fixed, and it appears that the 1.5.1 update is still in the game. However, it should be noted that I have recently thought of a better way of doing this, but I haven't the time or energy to spend on it now.

1.5.3 - Added isset() test to squish undefined index warning.

1.6.0 - Updated to use Symfony 6 FormType.

1.6.1 - Moved 'break' statement rather than testing later, added arrSolved initialisation, removed excessive variables.

Last Modified Date: 2023/08/24

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.