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.

2. Include functional-fishing.php

Include functional-fishing.php inside index.php.

3. Create Game Data in Session

  1. In functional-fishing.php, start a new session
  2. In functional-fishing.php, create the createGameData() function. The function will create a game data array and store it in session and store it in session under the index functional_fishing. The game data array should contain the following:
    1. The response array
    2. The number of fish
    3. The number of wood
    4. The number of bait
    5. The status of fire

4. Send commands using the POST Method

In index.php, update the form to submit using the POST method.

5. Check for the $_POST array

In functional-fishing.php, check for the $_POST array for a player's command.

  1. If a player has entered a command, check If the command matches an existing function (HINT: Use function_exists()).
    1. If so, execute the function and pass the results to the updateResponse() function.
    2. If not, then add a response to the response array in session, using updateResponse(), telling the user that the command is not valid.

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.

  1. Create the fish() function will give the player a chance to catch fish. Each call to the fish() function will use one piece of bait and randomly determine if a fish has been caught. The requirements for fishing are as follows:
    1. The fire must not be going
    2. The player must have at least 1 piece of bait
  2. Create the eat() function will allow the player to eat a fish. Each call to the eat() function will use one fish. The requirements for eating are as follows:
    1. The fire must be going
    2. The player must have at least 1 fish
  3. Create the fire() function will allow the player to start or stop the fire and should have the following requirements:
    1. If the fire() function is called when the fire is going, it should be turned off the fire.
    2. 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.
  4. Create the bait() function will allow the player to search for bait as long as the fire is not going.
  5. Create the wood() function will allow the player to search for wood as long as the fire is not going.
  6. 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.
  7. 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.comopen in new window

Rubric

CriteriaPts
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
Total20