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 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
Join the community of 500,000 technology professionals and ask your questions.
Connect with top rated Experts
16 Experts available now in Live!