Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have to get a better grasp on how the input works in php.</p> <p>You can't have the user to input data into the middle of a script. When the php script start to execute all the user input is already done and you have just to process it.</p> <p>Try to split your method into two to simulate the intended behaviour</p> <pre><code>class GamePlay { $playerChoice=""; $gameOver="false"; public function pendingInput() { // check for input into $_GET or $_POST and return data or false // something like (this function maybe a lot more complicate (or splitted // into smaller ones): if (isset($_POST['submit']) { // always check user input for forgeries // you have to write the sanitize function obviously $input=sanitize($_POST['submit']); return $input; } else { return false; } } public function play(){ if (!$this-&gt;gameOver) { $input = $this-&gt;pendingInput(); if($input) { $this-&gt;applyUserMove($input); $this-&gt;doComputerMove(); } $this-&gt;getPlayerInput(); } else { $this-&gt;gameEnds(); } } public function getPlayerInput(){ echo '&lt;form name="playerForm" "method="post"&gt;'; echo '&lt;input type="submit" name="submit" value="do this" /&gt;'; echo '&lt;input type="submit" name="submit" value="do that" /&gt;'; echo '&lt;/form&gt;'; } public function gameEnds() { echo 'The game is over. The winner is blah blah ... '; echo 'play another game?'; echo 'link to start game...'; } } </code></pre> <p>You have to think on a single run of the while loop you designed in your question. Use sessions (as http is stateless) and let the server/client take care of the loop.</p> <p><strong>Notice:</strong> This is just an approach to the problem, there are others but this is similar to your intended solutions.</p> <p><strong>Some additional hints:</strong> You can think of a php game as a <a href="http://www.eti.pg.gda.pl/katedry/kiw/pracownicy/Jan.Daciuk/personal/thesis/node12.html" rel="nofollow">Finite State Automata</a>:</p> <ul> <li>you get input from outside</li> <li>change your internal status</li> <li>output for inform user of your new status</li> <li>eventually wait for input</li> </ul> <p>You can use a dispatcher to select the status and write a method for every state or just do a kitchen sink method like applyUserMove and operate the switch internally. Either way the work (as program) you have to do is the same.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload