Sei sulla pagina 1di 10

Contoh Subrutin

Subrutin Pengolahan Data




Robot Posisi & Arah

Posisi pada lantai ditentukan dengan koordinat X,Y dengan nilai positif integer.
* Posisi Robot :
Pada progra nantin!a, posisi robot ditentukan oleh " kun#i, $Robot dan
!Robot. $Robot dan !Robot eiliki nilai diana Robot berada.

* Arah Robot :
Arah robot ditentukan oleh d$Robot dan d!Robot. %ilain!a diabil dari &ariasi
$Robot dan !Robot ketika robot bergerak ke lantai berikutn!a:
' if robot enghadap kanan : d$Robot ( ) dan d!Robot ( *
' if robot enghadap kiri : d$Robot ( ') dan d!Robot ( *
' if robot enghadap depan : d$Robot ( * dan d!Robot ( )
' if robot enghadap belakang : d$Robot ( * dan d!Robot ( ')

+urn+o,ards-round.eft subroutine
flo,#hart :
A subroutine flo,#hart is built the
sae ,a! as the ain progra
flo,#hart. /t ust #ontain one and
onl! one begin blo#k and at least
one end blo#k.
0ere, the flo,#hart #ontains the
ke!,ord d$Robot. 1hen the robot
is fa#ing left, d$Robot &alue is ').
/f d$Robot is different fro ' ), ,e
rotate the robot b! 2*3 then ,e
redo the test.

Subroutine e$e#ution : a subroutine #all blo#k
e$e#ution #auses the e$e#ution of the
subroutine begin blo#k, then the
subroutine is e$e#uted until its end blo#k,
then the e$e#ution #ontinues at the blo#k
follo,ing the subroutine #all blo#k.
' /nitiali4e the progra 5#hoose 6$e#ution 7
/nitiali4ation enu or #li#k /%/+ button8
' Run the progra.
' After e$e#ution, #li#k the robot to #hange its
initial dire#tion, run again the progra : in
an! #ase, the robot fa#es left of the ground
,hen the progra end

9uild and test a ne, subroutine naed
+urn+o,ards-roundRight
+he robot goes to the ground #enter

* +he ground #enter is at the interse#tion
of the #entral #olun and the #entral
ro, 5see the ground8.
/n order that the robot goes to the ground
#enter, it has to go to a tile in #entral
#olun, then to a tile in #entral ro,. +o
a#hie&e that purpose, ,e need t,o
subroutines.
* -o+oCentralColun subroutine :
' Create a ne, subroutine ,ith the nae
-o+oCentralColun
' 9uild follo,ing flo,#hart :

* -o+oCentralRo, subroutine :

+his subroutine ethod is siilar to the pre&ious
one, e$#ept ,e ha&e to test the robot ! position,
and ,e o&e it to,ards front or rear of the
ground.
' 9uild ne, subroutines naed
+urn+o,ards-roundRear and
+urn+o,ards-round:ront.
' 9uild ne, subroutine naed -o+oCentralRo,
' 9uild the ain progra :

.engkapi flo,#hart Robot diatas;

Potrebbero piacerti anche