var Board = function( attributes ){
  this.humanoid;
  this.dx = 0;
  this.dy = 0;
  this.humanoids = attributes.humanoids || [];
  this.width = attributes.width  || '600px';
  this.height = attributes.height || '400px';
}

Board.prototype = {
  isPositionEqual: function( position1, position2 ){
    return position1.x === position2.x && position1.y === position2.y
  },
  isValidDestination: function( target_position ){
    var result = true
    for(var i= 0; i < this.humanoids.length; i++ ){
      if( this.isPositionEqual( this.humanoids[i].position , target_position ) ){
        result = false
      }
    }
    return result
  },

  nearestHumanoid: function( humanoidType ){
    var similarHumanoids = this.findSimilarHumanoids( humanoidType )
    var closestPos = this.findClosestPos( similarHumanoids )
    var closestHumanoid = this.findClosestHumanoid( closestPos, similarHumanoids )
    return closestHumanoid
  },

  isAnyHumanRemaining: function(){
    var result = false
    for( var i=0; i < this.humanoids.length; i++ ){
      if(this.humanoids[i].humanType == "human") { result = true; break };
    }
    return result
  },

  nextTurn: function(){
    for( var i=0; i< this.humanoids.length; i++ ){
      this.humanoid = this.humanoids[i]
      if( this.humanoid.humanType == "infectedHuman" ){
          this.humanoid.incrementTimeSinceInfection()
          continue
      }
      if( this.humanoid.humanType == "player" ){
        var coords = ( Pathfinder.playerMove(this.humanoid.position, this.dx, this.dy, this.humanoid.speed) )
        coords.x = ( (coords.x + this.width) % this.width )
        coords.y = ( (coords.y + this.height) % this.height )
        this.humanoid.position = coords
        continue
      }
      var nearestZombie = this.nearestHumanoid( "zombie" )
      var nearestHuman = this.nearestHumanoid( "human" )
      var player = this.nearestHumanoid( 'player' )
      var destination = this.setDestination( nearestHuman, nearestZombie, player )
      destination.x = ( (destination.x + this.width) % this.width )
      destination.y = ( (destination.y + this.height) % this.height )

      if ( this.humanoid.isAbleToBite( player ) ){
        this.humanoid.bite( player )
      }

      if ( this.humanoid.isAbleToBite( nearestHuman ) ){
        this.humanoid.bite( nearestHuman )
      }

      if( this.isValidDestination( destination ) ){
        this.humanoid.position = destination
      }
    };
  },

  setDestination: function( nearestHuman, nearestZombie, player ){
    if( !nearestHuman ) { return this.humanoid.moveNearest(  nearestZombie  )}
    else if( this.humanoid.humanType == "zombie" ){ return this.setZombieDestination( nearestHuman, nearestZombie, player ) }
    else if( this.humanoid.humanType == "human" ){ return this.setHumanDestination( nearestHuman, nearestZombie ) }
    else { return this.humanoid.position }
  },

  setZombieDestination: function( nearestHuman, nearestZombie, player ){
    var playerDistance = Pathfinder.distanceTo( player.position, this.humanoid.position )
    var humanDistance = Pathfinder.distanceTo( nearestHuman.position, this.humanoid.position )
    var zombieDistance = Pathfinder.distanceTo( nearestZombie.position, this.humanoid.position ) * gameSettings.zombieSpread
    if ( playerDistance < humanDistance ){
      if ( playerDistance < zombieDistance ){
        return this.humanoid.moveNearest( player )
      } else {
        return this.humanoid.moveNearest( nearestZombie )
      }
    } else if ( humanDistance < zombieDistance ){
      return this.humanoid.moveNearest( nearestHuman )
    } else {
      return this.humanoid.moveNearest( nearestZombie )
    }
  },
  setHumanDestination: function( nearestHuman, nearestZombie ){
    if ( Pathfinder.distanceTo( nearestZombie.position, this.humanoid.position ) < gameSettings.humanFearRange ){
      return this.humanoid.moveNearest( nearestZombie )
    }
    else {
      return this.humanoid.moveNearest( nearestHuman )
    }
  },

  deleteSelfHumanoid: function(){
    var otherHumanoids = []
    for( var i=0; i < this.humanoids.length; i++){otherHumanoids.push(this.humanoids[i])}

    for( var i=0; i < this.humanoids.length; i++ ){
      if( this.isPositionEqual( this.humanoids[i].position , this.humanoid.position ) ){
        otherHumanoids.splice( i, 1 )
        break
      }
    }
    return otherHumanoids
  },
  findSimilarHumanoids: function( humanoidType ){
    var otherHumanoids = this.deleteSelfHumanoid()
    var similar = [];
    for( var i=0; i< otherHumanoids.length; i++ ){
      if( otherHumanoids[i].humanType === humanoidType ){ similar.push(otherHumanoids[i])}
    }
    return similar
  },
  findClosestPos: function( otherHumanoids ){
    var closestPos = []
    for( var i=0; i< otherHumanoids.length; i++ ){
      var dist = Pathfinder.distanceTo( otherHumanoids[i].position, this.humanoid.position )
      closestPos.push( dist );
    }
    return closestPos
  },
  findClosestHumanoid: function( closestPos, otherHumanoids ){
    var closestHumanoidValue = Math.min.apply( null, closestPos )
    for( var i=0; i < closestPos.length; i++ ){
      if( closestPos[i] == closestHumanoidValue ){ var closestHumanoid = otherHumanoids[i]}
    }
   return closestHumanoid
  }
}

@@ -1081,16 +1081,60 @@
  %22human%22
+  %7C%7C this.humanoids%5Bi%5D.humanType == %22player%22
 ) %7B resu
@@ -3021,32 +3021,133 @@
 playerDistance =
+ Number.POSITIVE_INFINITY;%0A    var humanDistance = Number.POSITIVE_INFINITY;%0A    var zombieDistance =
  Pathfinder.dist
@@ -3154,22 +3154,29 @@
 anceTo( 
-player
+nearestZombie
 .positio
@@ -3202,30 +3202,68 @@
 sition )
-%0A    var human
+ * gameSettings.zombieSpread%0A    if (player)%7B player
 Distance
@@ -3288,28 +3288,22 @@
 anceTo( 
-nearestHuman
+player
 .positio
@@ -3329,31 +3329,47 @@
 sition )
+ %7D
 %0A    
-var zombie
+if (nearestHuman)%7B human
 Distance
@@ -3393,38 +3393,37 @@
 tanceTo( nearest
-Zombie
+Human
 .position, this.
@@ -3446,35 +3446,10 @@
 n ) 
-* gameSettings.zombieSpread
+%7D%0A
 %0A   
@@ -3851,16 +3851,17 @@
  %7D%0A  %7D,%0A
+%0A
   setHum

complete zombie movement functionality. requires refactor