Example #1
0
 public function testGuessShouldReturnWonStatusWhenWordIsComplete()
 {
     $hangman = new Hangman('dafiti');
     $hangman->guessLetter('d');
     $hangman->guessLetter('a');
     $hangman->guessLetter('f');
     $hangman->guessLetter('i');
     $hangman->guessLetter('t');
     $this->assertEquals(Hangman::WON, $hangman->getStatus());
 }
Example #2
0
        $this->updateAttempts($string);
        print_r($string . "\n");
    }
    public function updateAttempts($string)
    {
        if (count($this->bad_letters) < self::MAX_ATTEMPTS) {
            print_r("You have " . (self::MAX_ATTEMPTS - count($this->bad_letters)) . " guesses left\n\n");
        } else {
            print_r("You have 0 attempts left. Game over. The word was " . $this->word . ".\n");
            exit(0);
        }
    }
    public function showString()
    {
        $string = '';
        foreach ($this->letters as $letter) {
            if (in_array($letter, $this->good_letters)) {
                $string .= $letter;
            } else {
                $string .= "_";
            }
        }
        if (strpos($string, "_") === false) {
            print_r("You have won hangman with word. " . $this->word . ". Game over.\n");
            exit(0);
        }
        return $string;
    }
}
$hangman = new Hangman();
$hangman->readInput();
Example #3
0
            } else {
                $left_to_guess++;
                print_r("_ ");
            }
        }
        print_r("\n");
        if (count($this->bad_letters) >= self::ATTEMPTS_LIMIT) {
            print_r("Womp womp\n");
            print_r("It was " . $this->word . "\n");
            exit(0);
        }
        if ($left_to_guess == 0) {
            print_r("Well done!\n");
            exit(0);
        }
        print_r("\n");
    }
    public function play()
    {
        $this->printState();
        print_r("Please type a letter: ");
        $input = readline();
        print_r("\n");
        $this->updateState($input);
        if ($input) {
            $this->play();
        }
    }
}
$hangman = new Hangman();
$hangman->play();
            $messages['msg'] = 'You win!';
        } 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);
        }
    }
Example #5
0
        $this->addField($param);
        $letter = new A_Controller_Input_Field('letter');
        $letter->addFilter($letter_rule);
        $this->addField($letter);
        $this->processRequest($request);
        $game = new HangmanGame();
        $locator->set('Game', $game);
        $game->word = $request->get('word');
        $game->guesses = $request->get('guesses');
        $game->misses = $request->get('misses');
        $game->level = $request->get('level');
        $game->guess($letter->value);
        $this->addState(new A_Controller_App_State('start', array(new StartView(), 'render')));
        $this->addState(new A_Controller_App_State('game', array(new GameView(), 'render')));
        $this->addState(new A_Controller_App_State('win', array(new WinView(), 'render')));
        $this->addState(new A_Controller_App_State('lose', array(new LoseView(), 'render')));
        $this->addTransition(new A_Controller_App_Transition('start', 'game', new A_Rule_Notnull('level', '')));
        $this->addTransition(new A_Controller_App_Transition('start', 'lose', new A_Rule_Notnull('giveup', '')));
        $this->addTransition(new A_Controller_App_Transition('game', 'lose', new LoseRule($game)));
        $this->addTransition(new A_Controller_App_Transition('game', 'win', new WinRule($game)));
        parent::run($locator);
    }
}
//-----------------------------------------------------------------------------
$Request = new A_Http_Request();
$Response = new A_Http_Response($Locator);
$Locator->set('Request', $Request);
$Locator->set('Response', $Response);
$controller = new Hangman($Locator, 'start');
$controller->run($Locator);
echo $Response->render();