} else { if (count($this->incorrectLetters) === 6) { // Completely replace the first message $messages['msg'] = 'Game over!'; } } // The number of incorrect guesses made (for displaying the gallows) $messages['gallows'] = $this->getGallows(); $messages['fact'] = $this->getFact(); return $messages; } } // Only react on POST requests if ($_SERVER['REQUEST_METHOD'] == 'POST') { $hangMan = new Hangman(); // Start a new game if (isset($_POST['pageLoad'])) { echo json_encode(array('artNum' => $hangMan->getArtNum(), 'hint' => $hangMan->getHint(), 'word' => $hangMan->displayWordStructure(), 'header' => $hangMan->getHeader(), 'gallows' => $hangMan->getGallows())); // Game is in progress } else { if (isset($_POST['guess'])) { $hangMan->setGuess($_POST['guess']); $result = $hangMan->processGuess(); // A new game should be started if (stristr($result['msg'], 'win') || stristr($result['msg'], 'over')) { $hangMan->newGame(); } echo json_encode($result); } } }