Function Fishing
Objective
For this assignment, you will be demonstrating your understanding of using functions, conditional statements, HTTP variables, and PHP includes by creating a simple interactive fishing game.
Game Play
The player will perform actions by typing in the commands into the form text box. The response for each command will be displayed in the response box. The old responses will remain on the page even as new ones come in.
Each action may have certain conditions that must be met for the task to be performed. When a condition is not correct, the game must inform the player what they should do to perform the desired task. After an action has been performed, a status message should be present to the player to explain what happened.
Actions
Fishing
When the player goes fishing, they have a chance of catching a fish. But the player may only go fishing if:
- The player has at least 1 piece of bait
- The fire has been stopped
Starting / Stopping a fire
The player can start or stop a fire. The player will start the fire only if:
- The fire has been stopped
- The player has at least 1 piece of wood
The player will stop the fire only if:
- The fire has been started
Searching for bait
The player can search for bait, which will increase the player's bait inventory by one. The player can go searching for bait if:
- The fire has been stopped
Searching for wood
The player can search for wood, which will increase the player's wood inventory by one. The player can go searching for wood if:
- The fire has been stopped
Eating
The player can eat fish. Each fish will decrease the player's fish inventory by one. The player can only eat if:
- The fire has been started
- The player has at least 1 fish
Checking inventory
The player can do a check on the inventory status. The inventory status should return a list of how many items the player currently has.
Help
The player can display the instructions on how to play the game.
Restarting the Game
The player can clear the current game data by restarting the game. All inventory should be set back to zero.
Game Data
The game data should be stored and retrieved from the session. Each time the player enters a command, the game data should be retrieved from and updated to the session.
Commands
The commands received by the player should be sent using the POST method.
Project Setup
1. Clone the Repository
Clone this repository from GitHub and use the provided file to complete the assignment. Warning: The provided file will throw an error if run as is. The error will be fixed after step 3 is completed.
functional-fishing.php
2. Include Include functional-fishing.php
inside index.php
.
3. Create Game Data in Session
- In
functional-fishing.php
, start a new session - In
functional-fishing.php
, create thecreateGameData()
function. The function will create a game data array and store it in session and store it in session under the indexfunctional_fishing
. The game data array should contain the following:- The
response
array - The number of
fish
- The number of
wood
- The number of
bait
- The status of
fire
- The
4. Send commands using the POST Method
In index.php
, update the form to submit using the POST
method.
$_POST
array
5. Check for the In functional-fishing.php
, check for the $_POST
array for a player's command.
- If a player has entered a command, check If the command matches an existing function (HINT: Use
function_exists()
).- If so, execute the function and pass the results to the
updateResponse()
function. - If not, then add a response to the response array in session, using
updateResponse()
, telling the user that the command is not valid.
- If so, execute the function and pass the results to the
Requirements
The following requirement must be met to complete the assignment successfully:
Warning
The web application must be functional and without errors. A non-functional game due to syntax errors or missing functionality will result in a failing grade.
- Create the
fish()
function will give the player a chance to catch fish. Each call to thefish()
function will use one piece of bait and randomly determine if a fish has been caught. The requirements for fishing are as follows:- The fire must not be going
- The player must have at least 1 piece of bait
- Create the
eat()
function will allow the player to eat a fish. Each call to theeat()
function will use one fish. The requirements for eating are as follows:- The fire must be going
- The player must have at least 1 fish
- Create the
fire()
function will allow the player to start or stop the fire and should have the following requirements:- If the
fire()
function is called when the fire is going, it should be turned off the fire. - If the
fire()
function is called when the fire is NOT going AND the player has at least 1 piece of wood, the fire should be turned on.
- If the
- Create the
bait()
function will allow the player to search for bait as long as the fire is not going. - Create the
wood()
function will allow the player to search for wood as long as the fire is not going. - Create the
inventory()
function will display the player's current inventory. It should list the amount of fish, bait, wood the player has, and if the fire is going. - Create the
restart()
function will allow the player to clear the current game data and restart the game.
Example
A completed version of the assignment can be found at the following:
https://mtm6405-functional-fishing.herokuapp.com
Rubric
Criteria | Pts |
---|---|
Create a fish() function that meets all project requirements. | 3 |
Create a eat() function that meets all project requirements. | 3 |
Create a fire() function that meets all project requirements. | 3 |
Create a bait() function that meets all project requirements. | 3 |
Create a wood() function that meets all project requirements. | 3 |
Create an inventory() function that meets all project requirements. | 3 |
Create a restart() function that meets all project requirements. | 2 |
Total | 20 |