Hangman App Issues

I’m trying to make a fairly simple Hangman app, and I’ve somehow hung myself up on it. I was hoping someone would be willing to help me get it sorted. I can’t figure out how to make it properly interact with the page, and my execution flow is getting all gummed up by my poor brain planning. I’m using this site for guidance but the implementations are different enough that it’s doing more harm than good:

I was trying to do it TDD style. That failed, obviously. Help? Thoughts? Thanks!

index.php
[php]<?php

//include the required files
require_once(‘HangmanGame.php’);
require_once(‘WordList.php’);

//this will keep the game data as they refresh the page
session_start();

//load a game if one hasn’t started yet
/if (!isset($_SESSION[‘game’][‘hangman’])) {
$_SESSION[‘game’][‘hangman’] = new HangmanGame();
}
/
?>

Hangman Demo
Hangman!
[/php]

HangmanGame.php
[php]<?php

namespace Hangman;

class HangmanGame
{
public $word;
public $maskedWord = array();
private $guesses = 6;
private $letters = array();
private $gameOver = false;

public function __construct($word = null) {
    $this->newGame($word);
}

public function newGame($word = null) {
    // can be passed a word to start, or get a random one from the word list
    if (is_null($word)) {
        $wordList = new WordList;
        $this->word = $wordList->getRandomWord();
    } else {
        $this->word = $word;
    }
    // set the masked word
    $this->maskedWord = str_split(str_repeat("_",strlen($this->word)));

}

public function nextRound() {
    if (($this->guesses == 0) || ($this->word == implode($this->maskedWord))) {
        $this->gameOver();
    } else {

    }
}

public function guess($letter) {
    $hit = 0;
    for ($i=0; $i<strlen($this->word); $i++) {
        if ($this->word{$i} == $letter) {
            $hit++;
            $this->maskedWord[$i] = $letter;
        }
    }
    if ($hit == 0) {
        if ($this->guesses == 0) {
            $this->gameOver = true;
        } else {
            array_push($this->letters, $letter);
            $this->guesses -= 1;
        }
    }
    echo $this->word;
    echo "\n" . implode($this->maskedWord) . "\n";
}

public function hasWon() {
    return $this->word == implode($this->maskedWord);
}

}[/php]

WordList.php
[php]<?php

namespace Hangman;

class WordList
{
const DEFAULT_WORDLIST = array(“word”, “list”, “missing”);
// const DEFAULT_PATH = ‘data/words.txt’;
private $path = ‘data/words.txt’;
private $wordList = null;

public function __construct($wordListPath = null) {
    $this->wordList = (is_null($wordListPath)) ? file($this->path) : file($wordListPath);
}

public function getRandomWord() {
    $randomKey = array_rand($this->wordList, 1);
    return $this->wordList[$randomKey];
}

}[/php]

I built a Hangman/Trivia game about 4 or 5 years ago in Flash Actionscript 3.0, I’ll take a look at your script(s) and see what help I can give you.

Though here is the basic setup I would do.

  1. Assign words or phrase to an array including the spaces if it’s a phrase or sentence.
  2. Create array with * or _ plus space between words if it’s a phrase or sentence.
  3. Then when the user picks a letter it is checked against the array with the word or phrase, if it’s a letter in the word or phrase then those spots with the * or _ are replaced with the corresponding letter.
  4. Wrong guesses are obviously the hangman portion, if you don’t use graphics then just have a simple counter that subtracts one from it every time there is an incorrect guess.

Once you get those basics done you can add extra features such as a timer or a trivia question, but first thing is to get the hangman portion working.

Sponsor our Newsletter | Privacy Policy | Terms of Service