
previous

page 9
next

The top left corner is (r0,c0) so we have to check from row r0 to r0+2 and from column c0 to c0+5 checking that every square is free.
break allows us to leave a for loop early.
function hor_clash_check(r0,c0) {
var ok=true;
for (r=r0;r<(r0+3);r++) {
for (c=c0;c<(c0+6);c++) {
var a=get_a(r,c);
if (a!=7) {ok=false; break;}
}
if (!ok) break;
}
return ok;
}
var ok=true;
for (r=r0;r<(r0+3);r++) {
for (c=c0;c<(c0+6);c++) {
var a=get_a(r,c);
if (a!=7) {ok=false; break;}
}
if (!ok) break;
}
return ok;
}
function hor4() {
for (var k=0;k<500;k++) {
var r0=Random0(7);
var c0=Random0(4);
var ok=hor_clash_check(r0,c0);
if (ok) {
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);
break;
}
}
return ok;
}
for (var k=0;k<500;k++) {
var r0=Random0(7);
var c0=Random0(4);
var ok=hor_clash_check(r0,c0);
if (ok) {
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);
break;
}
}
return ok;
}
previous
next