What Do We Have So Far?
Putting all this together, we have:
The method that we have just discussed is sometimes called
stepwise refinement
. You start with a specification
(which may be incomplete). Then you divide the proposed program into
routines, such that each routine will be easier to code than the
program
as a whole. Then you repeat the process for each of these routines
until you reach routines that you are sure you can code correctly
at the first attempt.
/*------------------------------------------------------*/
/* Main program */
/*------------------------------------------------------*/
do forever
call display
/*---------------------------------------------------*/
/* Mouse's turn */
/*---------------------------------------------------*/
...
if mouse = hole then leave /* reaches hole */
if mouse = cat then leave /* hits cat */
/*---------------------------------------------------*/
/* Cat's turn */
/*---------------------------------------------------*/
...
if cat = mouse then leave
end
/*------------------------------------------------------*/
/* Conclusion */
/*------------------------------------------------------*/
call display
if cat = mouse then say "Cat wins"
else say "Mouse wins"
exit
/*------------------------------------------------------*/
/* Subroutine to display the state of play */
/* */
/* Input: CAT and MOUSE */
/* */
/*------------------------------------------------------*/
display:
...
While you are doing this, keep asking yourself two questions:
- What data does this routine handle?
- Is the specification complete?