Designing a program – IBM SC34-5764-01 User Manual

Page 114

Advertising
background image

Good job! Now, take a while to put your new skills into action, or continue reading.

Designing a Program

Still thinking about method, which is just as important as language, let us take another look at
CATMOUSE EXEC.

The program is about a cat and a mouse and their positions in a corridor. At some stage their positions will
have to be pictured on the screen. The whole thing is too complicated to think about all at once; the first
step is to break it down into:
v

Main program: calculate their positions

v

Display subroutine: display their positions.

Now let us look at main program. The user (who plays the mouse) will want to see where everybody is
before making a move. The cat will not. The next step is to break the main program down further, into:

then mouse = cat
otherwise

/* moves

*/

mouse = mouse + move
end
if mouse = hole then leave

/* reaches hole */

if mouse = cat then leave

/* hits cat

*/

/*---------------------------------------------------*/
/* Cat turn

*/

/*---------------------------------------------------*/
jump = random(1,spring)
if cat > mouse then do

/* cat tries to jump left */

Temp = cat - jump
if Temp < 1 then nop

/* hits wall

*/

else cat = Temp
end

else do

/* cat tries to jump right */

if cat + jump > len then nop

/* hits wall

*/

else cat = cat + jump
end
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

*/

/*

*/

/* Design note:

each position in the corridor occupies */

/* three character positions on the screen.

*/

/*------------------------------------------------------*/

corridor = copies(" ",3*len)

/* corridor

*/

corridor = overlay("O",corridor,3*hole-1)

/* hole

*/

if mouse ¬= len

/* mouse in hole? */

then corridor = overlay("@",corridor,3*mouse-1)/* mouse */

corridor = overlay("(",corridor,3*cat-2)

/* cat

*/

corridor = overlay(")",corridor,3*cat)
say "

|"corridor"|"

return

Figure 46. CATMOUSE EXEC 2/2

Programming Style and Techniques

92

CICS TS for VSE/ESA: REXX Guide

Advertising