如何制作可以从数组中为刽子手游戏选择单词的按钮

我正在尝试为学校项目创建一个刽子手游戏,但我被卡住了。所以我有一个单词数组,我希望玩家选择其中一个单词,以便另一个可以尝试猜测字母/单词。

像这样的东西:示例

这是单词数组:

public $words = [

      'apple',

      'tree',

      'car',

      'school',

      'table',

      'laptop',

      'house',

      'summer', ];

我不知道这是否有帮助,但这是我的其余代码:


<?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);

?>

我想在不同的 PHP 文件中制作按钮,但真的想不出我应该从哪里开始。有没有人给我提示或可以告诉我如何继续?


有只小跳蛙
浏览 98回答 1
1回答

守着一只汪

一种方法是使用表单并将您的表单发布chosenword到服务器。在此示例中,首先显示表单。提交后,游戏将启动:// your game classclass game {&nbsp; &nbsp; &nbsp;public $letters = [];&nbsp; &nbsp; &nbsp;public $chosenword;&nbsp; &nbsp; &nbsp;public $words = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'apple',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'tree',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'car',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'school',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'table',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'laptop',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'house',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'summer',&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; function randomWord() {&nbsp; &nbsp; &nbsp; &nbsp; $this->chosenword = $this->words[rand ( 0 , count($this->words) -1)];&nbsp; &nbsp; &nbsp; &nbsp; return $this->chosenword;&nbsp; &nbsp; }&nbsp; &nbsp; function addLetter($letter){&nbsp; &nbsp; &nbsp; &nbsp; array_push($this->letters, $letter);&nbsp; &nbsp; }&nbsp; &nbsp; function ShowWord(){&nbsp; &nbsp; &nbsp; &nbsp; $pattern = '/[^' . implode('', $this->letters) . ']/';&nbsp; &nbsp; &nbsp; &nbsp; return preg_replace($pattern, '-', $this->chosenword);&nbsp; &nbsp; }&nbsp; &nbsp; function isWord($woord, $randomRandom){&nbsp; &nbsp; &nbsp; &nbsp; if ($woord == $randomRandom) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return "Found";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return "Not Found";&nbsp; &nbsp; }}// init a new game$game = new game();// if the form is not sent before, show the formif( !isset( $_POST['chosenword'] ) )&nbsp; { ?><form action="" method="post">&nbsp; <select name="chosenword">&nbsp; &nbsp; <?php foreach( $game->words as $word ) { ?>&nbsp; &nbsp; &nbsp; <option value="<?= $word; ?>"><?= $word; ?></option>&nbsp; &nbsp; <?php } ?>&nbsp; </select>&nbsp; <button type="submit">Submit</button></form><?php } else {&nbsp; // this will be executed if the form was sent before&nbsp; // set the chosenword from the post data&nbsp; $game->chosenword = $_POST['chosenword'];&nbsp; // do the rest of your code&nbsp; $randomRandom = $game->randomWord();&nbsp; echo $randomRandom;&nbsp; $game->addLetter('a');&nbsp; echo "<br>";&nbsp; echo $game->ShowWord();&nbsp; echo "<br>";&nbsp; echo $game->isWord('apple', $randomRandom);}?>
打开App,查看更多内容
随时随地看视频慕课网APP