Exemplo n.º 1
0
 public function testGame()
 {
     //init
     $config = array("data_path" => PATH_TO_DICTIONARY);
     $game = new GhostGame($config);
     //load dictionary
     $game->loadData();
     if ($game->getError()) {
         echo $game->getErrorMsg();
     }
     $this->assertEquals(FALSE, $game->getError());
     //find possible words set
     $word_in = "nor";
     $words_arr = $game->findWords($word_in);
     $this->assertEquals(TRUE, count($words_arr) > 0);
     //find winning/longest word
     $result_word = $game->findWinWord($words_arr);
     $this->assertEquals(TRUE, !empty($result_word));
     //win word is the right size
     $this->assertEquals(TRUE, strlen($result_word) >= 4);
     //extract next letter
     $next_letter = $game->getNextLetter($word_in, $result_word);
     $this->assertEquals(1, strlen($next_letter));
     $word_in = "notegsist";
     $words_arr = $game->findWords($word_in);
     $this->assertEquals(FALSE, count($words_arr) > 0);
     //$game->readByLine();
     return 0;
 }
Exemplo n.º 2
0
 * */
if (empty($_GET['user']) && empty($_SESSION['user'])) {
    $output['error'] = 'no user registered';
} elseif (empty($_SESSION['user'])) {
    $_SESSION['user'] = $_GET['user'];
    $_SESSION['ghost'] = '';
    $_SESSION['ai_ghost'] = '';
    $output['user'] = $_SESSION['user'];
    $output['ghost'] = $_SESSION['ghost'];
    $output['ai_ghost'] = $_SESSION['ai_ghost'];
} elseif (isset($_GET['new'])) {
    $_SESSION['user'] = '';
    session_destroy();
    session_start();
} else {
    $output['user'] = $_SESSION['user'];
    $output['ghost'] = $_SESSION['ghost'];
    $output['ai_ghost'] = $_SESSION['ai_ghost'];
}
/*
 * main game
 * */
if (empty($output['error']) && !empty($_GET['word'])) {
    //word with next letter
    $word_in = $_GET['word'];
    $config = array("data_path" => PATH_TO_DICTIONARY);
    $game = new GhostGame($config);
    $word_out = $game->play($word_in);
    $output = array_merge($output, $word_out);
}
echo json_encode($output);