Link to home
Start Free TrialLog in
Avatar of edvinson
edvinsonFlag for United States of America

asked on

JS Matrix question

I am making a simple game. It is a grid 8x8 , the good guy occupies on square, the bad guy another.

I need to make sure when the good guy chooses his initial square that my script does not put the bad guy within 4 squares of him.

I am stumped on how to do this!

Can anyone give me an example????
Avatar of HonorGod
HonorGod
Flag of United States of America image

Given row, and column values (each from 1 to 8) you can use the following to determine the distance.


  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 ) ) )
  }

Open in new window

Just in case you are wondering how HonorGod came up with that function:

The function uses the "distance" formula which is a derivative of Pythagoras' Theorm (http://en.wikipedia.org/wiki/Pythagorean_theorem).
You are on a grid, I think you want this measure of distance

 function distance( x1, y1, x2, y2 ) {
    var dx = Math.abs( x1 - x2 )
    var dy = Math.abs(y1 - y2 )

    if ( a > b) return a + Math.abs( a - b )
        else     return b + Math.abs( a - b )

  }
gah!!  sorry I have it the wrong way round, it should read

 function distance( x1, y1, x2, y2 ) {
    var dx = Math.abs( x1 - x2 )
    var dy = Math.abs(y1 - y2 )

    if ( a < b) return a + Math.abs( a - b )
        else     return b + Math.abs( a - b )

  }
ASKER CERTIFIED SOLUTION
Avatar of GwynforWeb
GwynforWeb
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Gwen, you're right. This is correct

function D2( r1, c1, r2, c2 ) {
  return Math.max( Math.abs( r1 - r2 ), Math.abs( c1 - c2 ) )
}


<!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>

Open in new window

If movement is between grid cells and diagonal movement is allowed then numberkruncher's diagram  seems wrong. And my solution of the max of the difference in the 2 coords is correct.
GwynforWeb:  Yes, your 3rd response for maximum difference is correct; your earlier two responses didn't make a lot of sense, but you corrected those whilst I was writing my response.

Both approaches work, but GwynforWeb's approach uses less processing power.

I cannot see any problems with the diagram...
Avatar of edvinson

ASKER

as i am fairly new to game prog, can anyone tell me which solution would be best for just passing the good guy coords and the bad guys?
I am biased, and would vote for http://#a24127605 with partial credit to
http:#//a24127680 and http://#a24127663 (for the nice graphics)
Drat, wrong format for links...  How about:
http:#a24127605 with partial credit to http:#a24127680 and http:#a24127663 (for the nice graphics)