function distance( r1, c1, r2, c2 ) {
var a = Math.abs( r1 - r2 )
var b = Math.abs( c1 - c2 )
return Math.floor( Math.sqrt( ( a * a ) + ( b * b ) ) )
}
function isCellEmpty(col, row) {
// Return false if there is an enemy or player in cell!
return true;
}
function findPosition(playerPos) {
for (var ri = 0; ri < 8; ++ri)
for (var ci = 0; ci < 8; ++ci)
if (isCellEmpty(ci, ri) && distance(ri, ci, playerPos.r, playerPos.c) >= 4)
return { r: ri, c: ci};
// No places available!
return null;
}
function foo() {
var playerPos = { r: 2, c: 2 };
var enemyPos = findPosition(playerPos);
}
dist.jpg
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title> 8 x 8 </title>
</head>
<body>
<script type='text/javascript'>
function D1( r1, c1, r2, c2 ) {
var a = Math.abs( r1 - r2 )
var b = Math.abs( c1 - c2 )
return Math.floor( Math.sqrt( ( a * a ) + ( b * b ) ) )
}
function D2( r1, c1, r2, c2 ) {
return Math.max( Math.abs( r1 - r2 ), Math.abs( c1 - c2 ) )
}
function init() {
var total = diff = 0
for ( var r1 = 1; r1 < 9; r1++ ) {
for ( var c1 = 1; c1 < 9; c1++ ) {
for ( var r2 = 1; r2 < 9; r2++ ) {
for ( var c2 = 1; c2 < 9; c2++ ) {
if ( ( r1 != r2 ) || ( c1 != c2 ) ) {
var d1 = D1( r1, c1, r2, c2 )
var d2 = D2( r1, c1, r2, c2 )
if ( d1 != d2 ) {
document.write( r1 + ', ' + c1 + ' ' + r2 + ', ' + c2 + ' ' + d1 + ' ' + d2 + '<br>' )
diff++
}
total++
}
}
}
}
}
document.write( '<br><br>Total = ' + total + ' diff = ' + diff )
}
init()
</script>
</body>
</html>
If you are experiencing a similar issue, please ask a related question
Title | # Comments | Views | Activity |
---|---|---|---|
"Pause" execution of javascript function for x miliseconds | 2 | 29 | |
JavaScript let vs var | 5 | 27 | |
asp.net mvc5 | 6 | 17 | |
I want a tab to always be underlined when I open div then change | 1 | 17 |
Join the community of 500,000 technology professionals and ask your questions.