gpt4 book ai didi

javascript - Hangman 更新 OO 值数组

转载 作者:行者123 更新时间:2023-11-28 07:45:23 24 4
gpt4 key购买 nike

我正在用 PHP 和 JS 编写一个刽子手游戏,但遇到了问题。我有两个数组,具有正确字母的答案数组,以及包含用户正确猜测的用户答案数组。

用户答案猜测数组以下划线开头,因为没有猜测正确 _ _ _

我想更新这个数组,并在用户猜对 _ A _ 时将正确的字母放入正确的字段中;

当我在 updateAnswer() 函数内进行 var_dump 时,用户答案数组已更改并添加了正确的字母,但是当我从 returnUserAnswer 中提取时,该数组并未更改。

PS。该库仅被调用一次,因此 __construct 不是问题。

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

}

}

最佳答案

由于 JavaScript 标记,我假设您正在编写一个使用 AJAX 请求的 Hangman 实现。如果这个假设是正确的,那么你的这个陈述就是错误的:

PS. the library is only being called once so __construct is not a problem.

PHP 是无状态的。这意味着,对于服务器收到的每个请求,您的类都会被实例化,并且当发送响应时,对所有实例的所有更改都会丢失。因此,ajax 驱动的流程如下所示:

[request 1] => new Word --> constructor initialize $usrAnswer to ['_','_','_']
initialize $answer to ['C', 'A', 'T']
=> destroy instances and resources
=> send response

[request 2] => guess a letter (send via AJAX)
=> new Word --> constructor is called, same as request 1
=> update $usrAnswer
=> destroy instance
=> send response

[request 3] => see [request 2]
[request N] => see [request 2]

那么,您需要做的是让 JavaScript 在每个请求上发送猜测答案的完整状态,而不仅仅是单个字母。像下面这样的对象就可以了:

var requestData = {
state: {
validGuesses: ['A'],
invalidGuesses: ['X', 'Q'] //<-- number of attempts, check if current guess is not a duplicate
},
guess: 'C'
};

然后,在服务器端(在 PHP 中)更改构造函数以接受参数(在本例中可以使用数组)。顺便说一句,下面的代码应用 coding standards ,我也强烈建议您遵循:

public function __construct(array $userState = array())
{
$this->answer = ['C', 'A', 'T'];
$this->usrAnswer = ['_','_','_'];
foreach ($userState as $value) {
$key = array_search($value, $this->answer);
if ($key === false) {
//handle invalid state, I'd suggest:
throw new InvalidArgumentException(
sprintf('%s is not a valid answer', $value)
);
}
$this->usrAnswer[$key] = $value;
}
}

现在,当您调用 guess 时,$usrAnswer 的值将是正确的。

更新:
为了澄清我最后的评论(关于向构造函数添加 $answer 参数),我会这样做:

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

就使用类而言,没有什么真正改变:

$cat = new Word();

除非 ajax 请求需要处理:

$cat = new Word('cat', $state);//where $state is the request data

另一个例子:

$iceCream = new Word('ice-cream', ['C', 'A']);
//sets usrAnswer to _C_-C__A_
$iceCream->guess('I');
//usrAnswer is now IC_-C__A_

关于javascript - Hangman 更新 OO 值数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27502862/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com