
previous

page 6
next
To get a feel for the process, let's write a function that places the 4-square horizontal randomly on the grid.
We start by choosing the upper left corner cell of the enclosing rectangle.
Study page 4 and convince yourself that the starting row can be any of 0 to 7 and the starting column any of 0 to 4.
Recall that Random(n) starts at 1. But I have included Random0(n) in the library and it starts at zero.
Once chosen, we can set the approriate a values.
So there's another idea for a little function set_a(row,col).
Add it to your functions.js.
function set_a(row,col,a) {
var id=rc2id(row,col);
var el=document.getElementById(id);
el.setAttribute('a',a);
}
var id=rc2id(row,col);
var el=document.getElementById(id);
el.setAttribute('a',a);
}
I'm going to call the function that places the ship on the grid, seed().
See if you can follow this:
function seed() {
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);
}
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);
}
Catch is: how do we know whether it worked or not?
previous
next