previous

page 8

next

I have rewritten the code that produced a horizontal 4 square ship as hor4() and I used it as a guide to writing ver4() which produced a vertical 4 square ship. Try this out:

function seed() {
ver4();
hor4();
}
function ver4() {
var r0=Random0(4);
var c0=Random0(7);
set_a(r0+1,c0+1,5);
set_a(r0+2,c0+1,2);
set_a(r0+3,c0+1,2);
set_a(r0+4,c0+1,6);
}
function hor4() {
var r0=Random0(7);
var c0=Random0(4);
set_a(r0+1,c0+1,3);
set_a(r0+1,c0+2,2);
set_a(r0+1,c0+3,2);
set_a(r0+1,c0+4,4);
}

If you tried it several times, you might have found that it works fine except that sometimes the two ships clash.
We need a way to check if our random choice (r0,c0) is acceptable.

And then the bigger question is ... what do we do if the choice is unacceptable?
One idea is to keep trying until we get an acceptable pair but it is considered poor practice to put a program in a loop that may never end so I prefer to just try lots of times and jump ship (pun intended) if I'm unsuccessful.

previous
next