- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在用 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/
我正在上 C 编程入门类(class),我们被分配编写 Hangman程序。 在游戏中,计算机随机选择一个单词并显示它有多少个字母。用户必须通过输入他们认为可能存在于该单词中的字母来猜测该单词。此外,
我用 Java 创建了一个刽子手游戏。我不知道如何检查和更换它。一切正常,字符串字正确,游戏板也很好。因此,游戏板给我的单词长度为“_ _ _ _ _”,例如。 我的问题就是如何获取用户输入检查的字符
我正在尝试为我的大学制作一个 Hangman 程序,我需要一些帮助。经过一试,程序进展顺利。最后,您可以选择玩另一个游戏或停止程序,但它保持答案是"is",要求输入新单词。经过一番尝试后如何访问菜单。
我正在制作的 Hangman 游戏有以下 while 循环: while(amountOfGuessesLeft > 0 || !wordComplete){ System.out.p
我正在编写一个hangman 类,并且遇到了一个意外类型错误的小问题。此错误位于 this.hidden.charAt(i) = this.original.charAt(i); 中。任何帮助将不胜感
对于我的刽子手游戏,我希望有一堆错误消息来检查是否输入了多个字母、两次猜测同一个字母等。到目前为止我的完整代码: import java.awt.Color; import java.
我有一个 JAVA 作业,必须使用数组和循环创建一个 Hangman 程序。 用户1输入一个有效的单词(无数字或符号) 用户2可以尝试一次猜出整个单词,或者使用一个字母总共猜10次。最初,用户 2 必
我似乎有一个无法解决的问题,当用户为我的程序输入完整的单词时,它每次搜索字符时都会显示,而不是仅显示整个单词,表明他们猜对了。当用户输入整个单词时,如何让它只显示单词,而不是每次搜索字符时显示?感谢您
import java.util.*; public class Hangman { public static void main(String[] args) { Scanner
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我对编程和 java 非常陌生(如你所见),并且确实陷入了编程刽子手的一个基本问题上。我的代码不是我所知道的最干净的,但我真的很想就我在逻辑上出错的地方提供一些意见。我的麻烦是让程序在每次猜测时正确输
我正在构建一个刽子手/猜词游戏,它使用数组来检查用户输入是否与单词中的字母匹配(他们试图猜测)。尝试跟踪用户正确猜测的次数(使用发生变量),但每当代码运行时,它都会计数两次。 所以我们假设这个词是“a
这个问题已经有答案了: How do I compare strings in Java? (23 个回答) 已关闭 7 年前。 我正在尝试用 Java 制作一个 Hangman 游戏,但我在互联网上
我正在开发一个刽子手游戏,但我想从数据库中选择一个随机单词。 数据库连接: $db_host = "127.0.0.1"; $db_user
我是 Java 的初学者,需要为 Java 最终项目拼凑一个应用程序。我想做刽子手(当时看起来很容易)。这对我来说非常困难。到目前为止,这是我的代码。 (将大约 10 个开源刽子手游戏放在一起真是令人
如何让我的 python hangman 游戏将变量 blanks 中的下划线替换为正确的字母(如果猜到的话)。 我有正确的 if 语句,但我不确定如何替换字母。 这是我尝试过的: def main(
我最近完成了 Javascript、CSS、HTML 的基础知识,并开始使用 jQuery。我决定我所知道的足以制作一个简单的刽子手游戏,所以我决定这样做。我已经完成了大部分,但是如果他们猜对了字母,
这是一个简单的刽子手游戏,我通过按钮制作了一个字母键盘,所以 onclick 它应该调用函数 checkLetter 来检查在要猜测的单词中选择的字母是否不运行,以及如何在用户单击按钮时删除按钮上的字
我正在用 C++ 为 Uni 评估开发 Hangman 游戏,但在用户键入字母后我无法显示隐藏的单词。所以我得到的单词显示为“_ _ _ _ _ _ _”,但是当我键入一个字母时,它不会将下划线替换为
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我是一名优秀的程序员,十分优秀!