我正在研究一种简单的“财富之轮”式算法。正确答案存储在$ phrase中,用户可以提交一封信来猜测短语的一部分。
我的问题是,每次用户提交他们的猜测时,我的会话数组都会被吹走,并且不会像我想的那样动态更新。
理想情况下,如果答案是“船”,并且用户的第一个猜测是“o”,则显示以下内容:“#o ##
第二个猜测是“t”,以下显示:“#o#t”
非常感谢任何建议或意见。
<!doctype html>
<?php session_start();?>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
<body>
<!-- Form for user guess input -->
<form method="post" action="wof2.php">
Input Letter: <input type="text" name="usr_guess">
<input type="submit" value="submit">
</form><br/><br/><br/>
<?php
// Set the phrase
$phrase = "The Terminator";
// Dump phrase into an array
$split = str_split($phrase);
// Counter used in the comparrison
$count = 0;
if (!isset($_SESSION['answer'])) {
$_SESSION['answer'] = array();
foreach ($split as $char) {
array_push($_SESSION['answer'], $char);
}
}
if (isset($_POST['usr_guess'])) {
// User's guess
$usr_guess = $_POST['usr_guess'];
foreach ($split as $char) {
// Compares user guess to the answer and sets the answer in count position
if ($usr_guess == $char) {
$_SESSION['answer'][$count] = $usr_guess;
$count++;
}
else { // Checks for breaks in the word, used in accurately displaying spaces between words or # if it is a character
if ($split[$count] == " ") {
$_SESSION['answer'][$count] = " ";
$count++;
}
else {
$_SESSION['answer'][$count] = "#";
$count++;
}
}
}
}
?>
</body>
</html>
答案 0 :(得分:1)
我今天早上能够通过一些简化代码来弄明白。我通过不断覆盖会话数组的所有内容而不是仅仅匹配字符来自行运行。我的下一步是将图像文件链接到每个字符,以便显示图形而不仅仅是纯HTML。
<?php
// ***************************************** Game Initialization ***************************************
// Set the phrase
$phrase = "The Terminator";
// Dump phrase into an array
$split = str_split($phrase);
// If Session array doesn't already exist, create it and dump each character of the phrase into it
if (!isset($_SESSION['answer'])) {
$_SESSION['answer'] = array();
foreach ($split as $char) {
array_push($_SESSION['answer'], $char);
}
$counter = 0; // Counter used to assign hidden characters to the phrase, change array contents " " if a space, "*" if a character
foreach ($_SESSION['answer'] as $char) {
if ($char == " ") {
$_SESSION['answer'][$counter] = " ";
$counter++;
}
else {
$_SESSION['answer'][$counter] = "*";
$counter++;
}
}
}
// ********************************************** Comparing Algorithm ***************************
// Counter used in the comparrison algorithm
$count = 0;
// Check if user has submitted a guess
if (isset($_POST['usr_guess'])) {
// Dump user's guess into a variable
$usr_guess = $_POST['usr_guess'];
foreach ($split as $char) {
// Compares user guess to the answer and sets the answer in count position
if ($usr_guess == $char) {
$_SESSION['answer'][$count] = $usr_guess;
echo $_SESSION['answer'][$count];
$count++;
}
else {
echo $_SESSION['answer'][$count];
$count++;
}
}
}
?>
答案 1 :(得分:0)
您应该存储所有内容并检查存储的值。
// Set the phrase
$phrase = "The Terminator";
// Dump phrase into an array
$split = str_split($phrase);
// Set container if not set
if(!isset($_SESSION['phrase'])) {
// Save phrase
$_SESSION['phrase']['full'] = $phrase;
// Split phrase for checking
$_SESSION['phrase']['split'] = $split;
}
// If guess set, attempt process
if (isset($_POST['usr_guess'])) {
// If not already in array, process
if(!in_array($_POST['usr_guess'],$_SESSION['guess']))
// Store each guess
$_SESSION['guess'][] = strip_tags($_POST['usr_guess']);
}
// Set a container array for letter to phrase storage
// For each letter check if there are spaces
foreach($_SESSION['phrase']['split'] as $_letter) {
if(preg_match("/\\s/",$_letter))
$_comp[] = " ";
// No spaces, check if instance of upper or lowercase letter in phrase
else
$_comp[] = (preg_grep("/".$_letter."/i",$_SESSION['guess']))? $_letter: '#';
}
// If no instances of # are in comp, juse echo saved phrase
if(!in_array("#",$_comp))
echo $_SESSION['phrase']['full'];
// If # in comp, implode the current comp array
else
echo ucwords(implode("",$_comp));
这是一个班级版本(仅适用于踢腿)
class VannaWhite
{
public static function Play($phrase) {
// Dump phrase into an array
$split = str_split($phrase);
// Set container if not set
if(!isset($_SESSION['phrase'])) {
// Save phrase
$_SESSION['phrase']['full'] = $phrase;
// Split phrase for checking
$_SESSION['phrase']['split'] = $split;
}
// If guess set, attempt process
if (isset($_POST['usr_guess'])) {
// If not already in array, process
if(!in_array($_POST['usr_guess'],$_SESSION['guess']))
// Store each guess
$_SESSION['guess'][] = strip_tags($_POST['usr_guess']);
}
// Set a container array for letter to phrase storage
// For each letter check if there are spaces
foreach($_SESSION['phrase']['split'] as $_letter) {
if(preg_match("/\\s/",$_letter))
$_comp[] = " ";
// No spaces, check if instance of upper or lowercase letter in phrase
else
$_comp[] = (preg_grep("/".$_letter."/i",$_SESSION['guess']))? $_letter: '#';
}
// If no instances of # are in comp, juse echo saved phrase
if(!in_array("#",$_comp)) {
echo $_SESSION['phrase']['full'];
// Unset the session arrays ready for new word/phrase
self::Clear();
}
// If # in comp, implode the current comp array
else
echo ucwords(implode("",$_comp));
}
protected static function Clear()
{
unset($_SESSION['phrase'],$_SESSION['guess'],$_SESSION['answer']);
}
}
// Create Instance
VannaWhite::Play("The Terminator");