Skip to content
Advertisement

How can I make buttons where I can choose a word from an array for a hangman game

I’m trying to create a hangman game for a school project and I am stuck. So I have an array of words and I want the player to pick one of these words so a the other can try to guess the letters/word.

Something like this: Example

This is the words array:

public $words = [
      'apple',
      'tree',
      'car',
      'school',
      'table',
      'laptop',
      'house',
      'summer', ];

I don’t know if this might help but this is the rest of my code:

<?php

 class game {

     public $letters = [];
     public $chosenword;
     public $words = [
          'apple',
          'tree',
          'car',
          'school',
          'table',
          'laptop',
          'house',
          'summer',
        ];

    function randomWord() {

        $this->chosenword = $this->words[rand ( 0 , count($this->words) -1)];
        return $this->chosenword;
    }

    function addLetter($letter){
        array_push($this->letters, $letter);
    }

    function ShowWord(){
        $pattern = '/[^' . implode('', $this->letters) . ']/';
        return preg_replace($pattern, '-', $this->chosenword);
    }

    function isWord($woord, $randomRandom){
        if ($woord == $randomRandom) {
            return "Found";
        }
        return "Not Found";
    } 
}


$game = new game ();
$randomRandom = $game ->randomWord();
echo $randomRandom;
$game->addLetter('a');
echo "<br>";
echo $game->ShowWord();
echo "<br>";
echo $game->isWord('apple', $randomRandom);
?>

I thought of making buttons in a different PHP file but really can’t think of where I should start. Does anyone have tips for me or could show me how I can continue?

Thanks in advance!

Advertisement

Answer

One method is using a form and post your chosenword to the server. In this example first the form is shown. When it is submitted, the game will init:

// your game class
class game {

     public $letters = [];
     public $chosenword;
     public $words = [
          'apple',
          'tree',
          'car',
          'school',
          'table',
          'laptop',
          'house',
          'summer',
        ];

    function randomWord() {

        $this->chosenword = $this->words[rand ( 0 , count($this->words) -1)];
        return $this->chosenword;
    }

    function addLetter($letter){
        array_push($this->letters, $letter);
    }

    function ShowWord(){
        $pattern = '/[^' . implode('', $this->letters) . ']/';
        return preg_replace($pattern, '-', $this->chosenword);
    }

    function isWord($woord, $randomRandom){
        if ($woord == $randomRandom) {
            return "Found";
        }
        return "Not Found";
    }
}

// init a new game
$game = new game();

// if the form is not sent before, show the form
if( !isset( $_POST['chosenword'] ) )  { ?>

<form action="" method="post">
  <select name="chosenword">
    <?php foreach( $game->words as $word ) { ?>
      <option value="<?= $word; ?>"><?= $word; ?></option>
    <?php } ?>
  </select>
  <button type="submit">Submit</button>
</form>

<?php } else {
  // this will be executed if the form was sent before

  // set the chosenword from the post data
  $game->chosenword = $_POST['chosenword'];

  // do the rest of your code
  $randomRandom = $game->randomWord();
  echo $randomRandom;
  $game->addLetter('a');
  echo "<br>";
  echo $game->ShowWord();
  echo "<br>";
  echo $game->isWord('apple', $randomRandom);
}
?>
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement