Thinking Procedurally

Lesson Objectives
Aims
You should be able to:
1. Identify the components of a problem
2. Identify the components of a solution to a
problem
3. Determine the order of the steps needed to
solve a problem
4. Identify the sub-procedures necessary to
solve a problem
Note
• Before we start:
• This lesson makes use of a task
described in the “learn VB” course. You
can get a LOT of extra help/hints in that
document if you’ve not already read it.
Procedure
• Procedure:
– A block of code which performs a specific
task or sub task
– It may allow data to be sent in and may
give data out again when complete
(passing variables)
– It is re-usable
– It can be called from anywhere in your
code
Thinking procedurally
• Thinking procedurally is simply the ability
to:
– Break a problem down into chunks
– Decide which could be a re-usable block
of code
– Work out what inputs the procedure will
need to do the task
– Work out what outputs the procedure
should produce
– Make those procedures
Task
• The only way to learn this is practise, applying it to
programming tasks.
• You are going to make a program which simulates a
really simple slot machine type game. It will do the
following things:
– Give the user £1 starting money and take 20p off per go
– Spin the reels
– Display the result of the spin – win or loss depending on some
simple rules
– Allow the user to select if they want another go or to take
their winnings.
Method
1.
Decompose the problem – what are the main tasks this
program should perform
2.
Abstract – Make sure you are only focussing on what is strictly
necessary to complete the task
3.
Decide what variables your program will need
4.
Algorithms – what will your code need to
calculate/manipulate/produce
5.
Outline algorithm – what will the basic structure of your code
be?
6.
Decide what procedures you should produce.
• Have a go, then I’ll walk you through
my thinking.
Decompose/Abstract
• As I see it the program will need to:
– Spin the reels
– Compare the reels to check the outcome
– Calculate profit/loss
– It will need to do all of these things whilst
the player has enough money to play
again (or quits)
Variables
• If we read the task, we obviously need
to store the following information:
–
–
–
–
Money
Reel 1 value
Reel 2 value
Reel 3 value
Algorithms
Loop while money is > 0 and user wants to play again
Reduce money by 20p
Do spin
Show outcome of spin
Update money based on outcome
Exit loop if no money left
Ask user if they want to proceed
Exit loop if they don’t want to
End loop
Procedures
• Clearly the following things happen
repeatedly:
• Spinning the reels
• Calculating the outcome of the spin
• Updating the money the user has
Code outline
• This would lead us to this initial structure
in VB:
• Next steps…
• The outline algorithm goes in the MAIN()
sub and controls our program – it should
be trivial to convert to code
• Final step – outline what each sub will
do.
Main()
Create the game variables
Loop round, allowing the user to play until no more money or no more turns required
Call DoSpin()
Call CompareReels()
Display outcomes
Call UpdateMoney()
Ask if the player wants another turn.
DoSpin() – Inputs (none), Outputs (reel values)
Generate three random numbers – one for each reel
Return these numbers
CompareReels() – inputs (reel values), outputs (winnings/losses)
Compare the three random numbers to the rules:
Return any winnings or losses this combination will cause
UpdateMoney() – inputs (player money, winnings/losses), outputs (updated player money)
Reduce the player’s available money by 0.2
Increase or decrease the player’s money according to any winnings they may have earned or
losses they have made
Display the players current balance
Next step
• Code it!
Review/Success Criteria
You should know:
 The