javascript - Hangman updating OO value array -


i writing hangman game in php , js , having problems. have 2 arrays, answer array has correct letters, , user answer array contains users correct guesses.

the user answer guesses array starts out underscores no guesses have been correct _ _ _

i update array , put correct letter correct field when user guesses correctly _ _;

when var_dump inside updateanswer() function user answer array has changed , added correct letter, when pull returnuseranswer array has not.

ps. library being called once __construct not problem.

<?php if ( ! defined('basepath')) exit('no direct script access allowed');   class word {  private $answer; public $usranswer;  function __construct(){ $this->answer = array('c','a','t'); $this->usranswer = array('_','_','_'); } function returnuseranswer(){ return $this->usranswer; }  function updateanswer($letter,$try){     $change = array($try => $letter);     $this->usranswer = array_replace($this->usranswer,$change);  }  function guess($letter){      $try = array_search($letter,$this->answer);      if($try === false){      return false;     }else{      $this->updateanswer($letter,$try);return $try;           }  }  } 

because of javascript tag, i'm assuming you're writing hangman implementation uses ajax requests. if assumption correct statement of yours false can be:

ps. library being called once __construct not problem.

php stateless. means that, every request server receives, class instantiated, , when response sent, of changes instances lost. result, here's ajax-driven flow looks like:

[request 1] => new word --> constructor initialize $usranswer ['_','_','_']                                         initialize $answer ['c', 'a', 't']             => destroy instances , resources             => send response  [request 2] => guess letter (send via ajax)             => new word --> constructor called, same request 1             => update $usranswer             => destroy instance             => send response  [request 3] => see [request 2] [request n] => see [request 2] 

what need do, then, have javascript send full state of guessed answer on every request, not single letter. following object do:

var requestdata = {     state: {         validguesses: ['a'],         invalidguesses: ['x', 'q'] //<-- number of attempts, check if current guess not duplicate     },     guess: 'c' }; 

then, server-side (in php), change constructor take arguments (an array in case do). aside, code below applies coding standards, recommend follow, too:

public function __construct(array $userstate = array()) {     $this->answer = ['c', 'a', 't'];     $this->usranswer = ['_','_','_'];     foreach ($userstate $value) {         $key = array_search($value, $this->answer);         if ($key === false) {             //handle invalid state, i'd suggest:             throw new invalidargumentexception(                 sprintf('%s not valid answer', $value)             );         }         $this->usranswer[$key] = $value;     } } 

now, when call guess, value of $usranswer correct.

update:
clarify last comment (about adding $answer argument constructor), here's i'd do:

public function __construct($answer = 'cat', array $state = []) {     $this->answer = str_split(strtoupper($answer));//ensure upper-case, create array     //create array containing correct amount of _ chars     $this->usranswer = array_fill(0, strlen($answer), '_');     foreach ($state $value) {         $key = array_search($value, $this->answer);         if ($key === false) {             throw new invalidargumentexception(                 sprintf('%s not valid answer', $value)             );         }         $this->usranswer[$key] = $value;     }     //replace _ dash, if answer contains dashes (eg ice-cream)     $pos = -1;     while (($pos = strpos($answer, '-', $pos +1)) !== false) {         $this->usranswer[$pos] = '-';     } } 

as far using class concerned, nothing changes:

$cat = new word(); 

except when ajax request needs processing:

$cat = new word('cat', $state);//where $state request data 

another example:

$icecream = new word('ice-cream', ['c', 'a']); //sets usranswer _c_-c__a_ $icecream->guess('i'); //usranswer ic_-c__a_ 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -