For this exercise you will practice using Functions, Includes, and the $_POST
and $_SESSION
variables to create a interactive, text-based blacksmith game.
The player is a Blacksmith who must make a living making and selling items. To do this 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 in order for the task to be performed. When a condition is not correct, the game must inform the player what they should do in order to perform the desired task. After an action has been performed, a status message should be present to the player explain what happened.
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 need to buy resources in order make items. The player can buy a specified resource using the buy
command followed by an item, as long as the player has enough gold. The required gold can be found below. The fire must be put out before buying any resources
Resource | Gold |
---|---|
Wood | 2 |
Ore | 4 |
The player can make a specified item by using the make
command followed by an item, as long as the player has enough resources for that item. The items requirements are below. The fire must be going before making items.
Item | Wood | Ore |
---|---|---|
Sword | 1 | 3 |
Axe | 2 | 1 |
Staff | 2 | 0 |
The player can sell a specified item or resource using the sell
command followed by an item, as long as the player has the item or resource in inventory. The amount of Gold received from an item will be randomly determined based on a range. The ranges for each item or resource can be found below. The fire must be put out before selling items or resources.
Item / Resource | Sell Min | Sell Max |
---|---|---|
Wood | 1 | 1 |
Ore | 2 | 2 |
Sword | 12 | 20 |
Axe | 7 | 12 |
Staff | 4 | 7 |
The player can check the inventory status using the inventory
command. The inventory status should return a list of how many items the player currently has.
The player can display the instructions on how to play the game using the help
command.
The player can clear the current game data by restarting the game using the restart
command. All inventory should be set back to zero.
In this repository you should find the following files:
- The
index.php
file is the main file for the game, and contains all the HTML needed. It already links toblacksmith.css
andblacksmith.js
. - The
settings.php
file will hold the an array of settings for the game. - The
blacksmith.php
file will be where all the functions and logic for the Blacksmith game.
The following requirement must be met in order to complete the exercise successfully:
In index.php
, use the require
statement to include the settings.php
and blacksmith.php
files.
- In the
blacksmith.php
file, start a new session. - In
blacksmith.php
create thecreateGameData()
function. The function will create a game data array and store it in session under the indexblacksmith
. The game data array should contain the following:- The
response
array - The number of
gold
- The number of
wood
- The number of
ore
- The number of
swords
- The number of
axes
- The number of
staffs
- The status of
fire
- The
In index.php
, update the form to submit using the POST
method.
In blacksmith.php
check for the $_POST
array for a command from the player.
- If a player has entered a command, complete the following actions:
- Use the
explode()
function to split the command on space. This will help separate command from an option. - Use the
function_exists()
function to check if the command matches a existing function- If so, execute the function passing the results to the the
updateResponse()
function. - If not, then add a response to response array in session, using
updateResponse()
, telling the user that the command is not valid.
- If so, execute the function passing the results to the the
- Use the
Each action or command (see above), will require a function. Create the following functions with the necessary requirements:
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.
The buy()
function will allow the player to buy a resource and should have the following requirements:
- The function should take 1 parameter which will be the resource the player wishes to buy.
- The function will add 1 resource to the player's inventory and remove the appropriate amount of gold from the players inventory if all of the following conditions are met:
- The fire is NOT burning
- An resource is provided
- The resource provided is valid
- If the player has enough gold to buy the resource
- A message should be returned indicating that a transaction took place or a requirement was not met.
The make()
function will allow the player to make an item and should have the following requirements:
- The function should take 1 parameter which will be the item the player wishes to make.
- The function will add 1 item to the players inventory and remove the appropriate amount of wood and / or ore from the players inventory if all of the following conditions are met:
- The fire is burning
- An item is provided
- The item provided is valid
- If the player has enough wood and / or ore to make the item
- A message should be returned indicating that an item was made or a requirement was not met.
The sell()
function will allow the player to sell an item or resource and should have the following requirements:
- The function should take 1 parameter which will be the item or resource the player wishes to sell.
- The function will remove 1 item or resource from the player's inventory and add a random amount of gold to the players inventory based on the item's or resource's sell range and if all of the following conditions are met:
- The fire is NOT burning
- An item / resource is provided
- The item / resource provided is valid
- If the player has the item / resource they are trying to sell
- A message should be returned indicating that an item was made or a requirement was not met.
The inventory()
function will display the players current inventory. It should list the amount of wood, ore, swords, axes, and staffs the player has as well as if the fire is going.
The reset()
function will clear the game data and start the game over.