Objectives:
- Build PHP pages
- Apply loops (For, While, & Do-While)
- Apply if, else if, and switch statements
- Apply functions
- Apply PHP string, numbers, and dates
- Learn about integrating HTML, PHP, and CSS together
- Dynamic coding
Reference
- Frosty Images: http://apollo.occc.edu/chyde/cs2623/images/SSP04_Frosty_Images.zip
- Array: Reference the "Loop Through an Indexed Array" at https://www.w3schools.com/php/php_arrays.asp. You can use a simple if statement like if($guess_letter == $secretWord_array[x]){} where the "x" is whatever for loop counter you're using.
- String & Date functions functions used in this assisngment.
- date()
- str_repeat()
- str_replace()
- str_split()
- strlen()
- strpos()
- strtoupper()
- substr()
-
SSP04 Session and Variables Session Variable secretWord $secretWord secretWord_hint $secretWord_hint secretWord_array $secretWord_array secretWord_len $secretWord_len guess_secretWord $guess_secretWord guess_count $guess_count guess_count_wrong $guess_count_wrong guess_tracked $guess_tracked guess_image $guess_image guess_letter $guess_letter game_lastGuess $game_lastGuess game_started $game_lastGuess
Overview
You are going to create a word guessing game called Frosty. You will select a word and a hint that helps the player, but doesn't help them so much they guess their word too quickly. Think of the hint as more of a category to chose from. Each time, the user will guess a character that might be in the secret word. They get unlimited right answers, but guessing a character that is not in the secret word is bad. For each wrong answer, the world will slowly go into another ice apocalypse symbolized by a giant snowman called Frosty. To be clear, frosty is bad....we do not want to see Frosty. With each wrong guess, Frosty will slowly form.
Specifics:
-
PHP Page: Create a new HTML page named "SSP04.php" in your root directory.
- Create a new HTML form
- Textbox: There will be one text box for the user to enter the letter. The name must be "frosty_letter_guess".
- Submit button: There will be one submit button for the form
- Method: Post
- Action: Just reload your ssp04.php page.
- Session: Start a new session
- Initialize the following sessions if they are not already set
- secretWord: Come up with a secret word that is at least seven characters long.
- secretWord_hint: Identify a hint for your secret word.
- secretWord_array: Set using the str_split function on secretWord session.
- secretWord_len: Set using the the strlen function on the secretWord session.
- guess_secretWord: Set using the str_repeat. You want the value to be underscores that are the same length as secretWord session.
- guess_count: Set equal to zero.
- guess_count_wrong: Set equal to zero.
- guess_tracked: Set equal to zero.
- guess_image: Set equal to zero.
- guess_letter: Set equal blank "" for now.
- game_lastGuess: Set equal to the current date and time.
- game_started: Set equal to the current date and time.
-
- Variables: Using the table "SSP04 Session and Variables" in the reference section. Intialize the variables using the appropriate session if the session is set.
- Create a new HTML form
- PHP Functions Page:
- functions_ssp04.php: Create a new PHP page in your functions folder called "functions_ssp04.php".
- ssp04.php: Add code to "ssp04.php" that loads "functions_ssp04.php" once.
- frosty_guessing: Create a function entitled "frosty_guessing".
- Paramaters:
- $secretWord
- $secretWord_hint
- $guess_secretWord
- $guess_letter
- $guess_tracked
- $guess_count
- $guess_count_wrong
- $game_started
- $game_lastGuess
- $secretWord_len
- $secretWord_array
- $guess_image
- Paramaters:
- $guess_found: Create a variable entitled "$guess_found" which will be used to indicate the guessed word was found. Intialize it to "N"
- $game_lastGuess: Create a variable entitled "$game_lastGuess" which will be used to indicate the last time a player made a guess. Set it equal to the current time and date.
- Conditional logic: Create an if statement that will handle three diferent conditions.
- If: Test if the guessed word (e.g. $guess_letter) has alread been guessed using the strpos() string function and $guess_tracked variable. If the word has been guesssed, increment the counter for the number of wrong guesses (e.g. $guess_count_wrong) by one. Also, increment the guess image number ($guess_image) by one.
- Else if: Test if the guessed word (e.g. $guess_letter) is the last one to win the game. If it does, display a congradulations message and special image. You can chose your own image for this.
-
Else if: Test if the guessed word (e.g. $guess_letter) is not in the secret word. If it is...
- $guess_tracked: Append the the guessed word (e.g. $guess_letter) to the string you're using to track all the guesses (e.g. $guess_tracked).
- $guess_count_wrong: Increment the counter tracking the number of wrong guesses (e.g. $guess_count_wrong) by one.
- $guess_image: Increment the guess image number variable (e.g. $guess_image)by one.
- Else: This will handle if conditions where the guessed character is in the secret word.
- For Loop: Create a for loop with the following settings: initialize counter to zero, and increment the counter by 1 while keeping it under the length of the secret word (e.g. $secretWord_len).
- if: While iterating through the array with the for loop counter, create an if statement that tests if the guessed word (e.g. $guess_letter). Check the reference section above for information about iterating through an array with a loop. If there
is a match than set $guess_found = "Y". Also, do the following...
- if: If the for loop counter is equal to zero. Update the guessed secret word status (e.g. $guess_secretWord). You will do this by concatenating the guessed letter (e.g. $guess_letter) with the existing $guess_secretWord using the string substr() function. I highly suggest pulling out a piece of paper and writing down the letter. I will give you a hint and tell you the position is 1. For the number of characters, I want you to try and figure this out.
- else: This will handle if the letter matches anything after the first character. Here you will be doing two substr() functions with the $guess_letter (i.e. substr($guess_secretWord,,).$guess_lettersubstr($guess_secretWord,,)). The first substr position is 0, and the second is the loop counter + 1. Again, I will leave it to you to find out the number of characters.
- if: While iterating through the array with the for loop counter, create an if statement that tests if the guessed word (e.g. $guess_letter). Check the reference section above for information about iterating through an array with a loop. If there
is a match than set $guess_found = "Y". Also, do the following...
- For Loop: Create a for loop with the following settings: initialize counter to zero, and increment the counter by 1 while keeping it under the length of the secret word (e.g. $secretWord_len).
- $guess_count: Increment the variable $guess_count by 1.
- Sessions: Update the sessions. Set each one equal to value of its variable.
- frosty_round: Create a function that will display the game output.
- Paramaters:
- $game_started
- $game_lastGuess
- $secretWord_len
- $secretWord_hint
- $guess_count
- $guess_count_wrong
- $guess_tracked
- $guess_image
- $guess_secretWord
- Date Information: Display when the game was started and the last time the player played
- Current Progress: Display the secret word and some information about the game. This should include:
- "Secret word hint" using the $secretWord_hint variable
- "Secret Word Length" using the $secretWord_len variable
- "Guess Count" using the $guess_count variable
- "Guess Count (Wrong Answers)" using the $guess_count_wrong variable
- "Guess Count (Right Answers)" using the $guess_secretWord variable. Use the strlen() and str_replace() to derive this number. You will be replacing the underscores with an empty string (e.g. "").
- "Guessed letters" using the $secretWord_hint variable
- "Frosty is currently at" display the current frosty pictue using the $guess_image variable to reference the appropriate frosty image dynamically.
- Call: Add a line to the end of the frosty_guessing() function to call the frosty_round() function.
- Paramaters:
Common Requirements for All Web Pages:
- Your code must adhere to HTML5 standards. W3C HTML5 Example: https://www.w3schools.com/html/html5_intro.asp
- All your pages must pass W3C validators
- HTML Validator: https://validator.w3.org/nu/
- CSS Validator: http://jigsaw.w3.org/css-validator/
- The web pages must display correctly in all of the following: FireFox, Chrome, Internet Explorer.
- The links between the existing pages with the Pacific Trails Resort website cannot be broken.
-
All HTML and CSS code must be well documented, properly indented, and easy to read.
http://css-tricks.com/examples/CleanCode/Beautiful-HTML.png
http://coding.smashingmagazine.com/2008/05/02/improving-code-readability-with-css-styleguides/