Pseudocode
When we think about programming, we often focus on languages, syntax, and the code. But these are just tools that used to complete the purpose of a program, which is to solve a problem. Problem solving is a crucial part of the programming process, and can be a pain point for some developers. We may know what we want to do, but what are the steps to get it done. Well, one way to overcome this hurdle, is to use pseudocode.
What is Pseudocode
Pseudocode is an informal high-level description of a program, written in plain English. Pseudocode is much like creating an outline before writing a paper. It serves as a guideline for how to write the program.
There is no standards for writing pseudocode, but well written pseudocode should be understood by any one with even a basic understanding of programming. By writing pseudocode, we can ignore all the restrictions of the code and focus only on the problem. We can walk through the program defining the structure and identifying problems without ever writing a single line of code.
Writing Pseudocode
Let's assume we are creating a blacksmith game where the player will use wood and ore to make weapons, which in turn can be sold for gold. The player will be able to execute a buy
action to buy wood and ore, and a make
action to make different weapons. However, the player must meet certain requirements to successfully complete these actions. For example, the player must have enough gold to buy wood or enough ore to make the sword.
So, we can see that even this relatively simple game is beginning to have a lot of requirements, and we can use pseudocode to map it all out. So, we will start by laying out the basic structure of the problem.
create inventory object
create requirements object
create buy action
create make action
Now we can list what items will be in inventory and what requirements need to be tracked.
create inventory object
gold
wood
ore
axes
swords
create requirements object
gold for wood
gold for ore
wood for axes
ore for axes
wood for swords
ore for swords
create buy action
create make action
Next we will add the logic for the buy
action. The player will be able to provide which item they wish to buy, from there we must check if the player has enough gold and provide the appropriate response.
create inventory object
gold
wood
ore
axes
swords
create requirements object
gold for wood
gold for ore
wood for axes
ore for axes
wood for swords
ore for swords
create buy action (item)
if gold in inventory is greater than the item's requirement of gold
subtract the item's requirement of gold from the gold in inventory
increase the number of item in the inventory
respond that the player has bought the item
else
respond that the player does not have enough gold
create make action
Now let's do the same for the make
action.
create inventory object
gold
wood
ore
axes
swords
create requirements object
gold for wood
gold for ore
wood for axes
ore for axes
wood for swords
ore for swords
create buy action (item)
if gold in inventory is greater than the item's requirement of gold
subtract the item's requirement of gold from the gold in inventory
increase the number of item in the inventory
respond that the player has bought the item
else
respond that the player does not have enough gold
create make action (item)
if wood in inventory is greater than the item's requirement of wood AND
ore in inventory is greater than the item's requirement of ore
subtract the item's requirement of wood from the wood in inventory
subtract the item's requirement of ore from the ore in inventory
increase the number of item in the inventory
respond that the player has made the item
else
respond that player does not have enough resources to make the item
At this point, we may have noticed a problem with our actions. For example, the player may try to buy a sword or make wood. This is a logic problem in our structure, but we can still fix it in pseudocode.
create inventory object
gold
wood
ore
axes
swords
create requirements object
gold for wood
gold for ore
wood for axes
ore for axes
wood for swords
ore for swords
create buy action (item)
if item is an item that can be bought
if gold in inventory is greater than the item's requirement of gold
subtract the item's requirement of gold from the gold in inventory
increase the number of item in the inventory
respond that the player has bought the item
else
respond that the player does not have enough gold
else
respond that the player cannot buy that item
create make action (item)
if item is an item that can be made
if wood in inventory is greater than the item's requirement of wood AND
ore in inventory is greater than the item's requirement of ore
subtract the item's requirement of wood from the wood in inventory
subtract the item's requirement of ore from the ore in inventory
increase the number of item in the inventory
respond that the player has made the item
else
respond that player does not have enough resources to make the item
else
respond that the player cannot make that item
Now, we have a blue print to logical structure of our game, and we can start programming.