Example #1
0
<?php

require __DIR__ . '/../autoload.php';
use Hangman\Word;
use Hangman\WordList;
use Hangman\GameContext;
use Hangman\Loader\TextFileLoader;
use Hangman\Loader\XmlFileLoader;
use Hangman\Storage\SessionStorage;
$letters = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
$list = new WordList();
$list->load(new TextFileLoader(__DIR__ . '/../data/words.txt'));
$list->load(new XmlFileLoader(__DIR__ . '/../data/words.xml'));
$list->addWord('programming');
$context = new GameContext(new SessionStorage('hangman'));
// New Game?
if (isset($_GET['new'])) {
    $context->reset();
}
// Restore Game?
if (!($game = $context->loadGame())) {
    $game = $context->newGame(new Word($list->getRandomWord(8)));
}
if (!empty($_GET['letter'])) {
    $game->tryLetter($_GET['letter']);
} else {
    if (!empty($_POST['word'])) {
        $game->tryWord($_POST['word']);
    }
}
$context->save($game);
Example #2
0
<?php

require __DIR__ . '/../vendor/autoload.php';
use Hangman\Word;
use Hangman\WordList;
use Hangman\GameContext;
use Hangman\Loader\JsonGameFileLoader;
use Hangman\Loader\JsonConfigurationFileLoader;
use Hangman\Storage\SessionStorage;
$charsMap = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
// load the list of the words used in the game
$list = new WordList();
$list->load(new JsonGameFileLoader(__DIR__ . '/../data/words.json'));
// load configuration parameters
$configuration = new JsonConfigurationFileLoader(__DIR__ . '/../data/configuration.json');
$configuration->load();
$maxAttempts = $configuration->maxAttempts;
$context = new GameContext(new SessionStorage('play_hangman'));
// Wanna play again / reset current game?
if (array_key_exists('new', $_GET) && strcmp($_GET['new'], 'true') === 0) {
    $context->reset();
}
// Restores the last unfinished game
// getRandomWord() accepts an int as a parameter; 0 for a random length
if (!($game = $context->loadGame($maxAttempts))) {
    $game = $context->newGame(new Word($list->getRandomWord(0)), $maxAttempts);
}
// Checks if the letter / word chosen by the user is valid
if (!empty($_GET['letter'])) {
    try {
        $game->tryLetter($_GET['letter']);