0

I am working on a simple "wheel of fortune" style algorithm. The correct answer is being stored in $phrase, and the user can submit a letter to guess a part of the phrase.

My problem is that each time the user submits their guess, my session array gets blown away and doesn't dynamically update like I would like.

So ideally if the answer is "boat", and the user's first guess is "o", the following displays: "#o##

Second guess is "t", the following displays: "#o#t"

Any suggestions or comments are greatly appreciated.

<!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>
4
  • What is the exact problem? Commented Sep 13, 2014 at 22:56
  • I just made some small edits as I realized it wasn't displaying correctly. The problem is that each time the user clicks the "submit" button, my session array which is displaying the answer below is being lost. I need it to keep track and display any characters the user guesses correctly. Commented Sep 13, 2014 at 22:59
  • I still don't know what the problem is. Commented Sep 13, 2014 at 23:00
  • Tell us how it should be. Commented Sep 13, 2014 at 23:01

2 Answers 2

1

I was able to figure it out this morning with some simplification of my code. I was running myself in circles by continually overwriting all the contents of my session array instead of just matching characters. My next step will be to link image files to each character so it displays graphics instead of just plain 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++;
            }
        }
    }
?>
Sign up to request clarification or add additional context in comments.

Comments

0

You should store everything and check against the stored values.

// 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));

Here is a class version (just for kicks)

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.