<commit_before>var canvas = document.getElementById("the-game");

Movement = {
  UP: 1,
  DOWN: 2,
  LEFT: 3,
  RIGHT: 4,
  CONTINUE: -1
}

//heuristic 1
function lineDistance(a, b)
{
  var xs = 0,
      ys = 0;

  xs = b.x - a.x;
  xs = xs * xs;

  ys = b.y - a.y;
  ys = ys * ys;

  return Math.sqrt( xs + ys );
}

//heuristic 2

//avg heuristic 1 and 2
function heuristicAverage(a, b)
{ }//return (lineDistance(a, b) + otherheuristic(a, b))/ 2; }

ai = {
  moveQueue: [],
  optionalMoves: [],
  inSearch: false,

  comparePosition: function(a, b) {
    if( a.pos === undefined || b.pos === undefined )
      return false;
    else
      return a.pos === b.pos;
  },

  init: function(){
    this.inSearch = false;
    this.moveQueue = [];
    this.optionalMoves = [];
  },

  addMove: function(move){
    this.moveQueue.unshift(move);
  },

  AStar: function() {
    var open = new PriorityQueue({ comparator: function(a, b){
      if (a.cost !== undefined && b.cost !== undefined)
        return a.cost - b.cost;
      return 0;
    }}),
       closed = [];

    var foodPos = food.getPosition(),
        snakePos = [Math.ceil(Math.floor(snake.x)/snake.size),
          Math.ceil(Math.floor(snake.y)/snake.size)];

    var pathLength = 0;

    open.queue({
      pos: snakePos[0] + " " + snakePos[1],
      body: snake.getPosition(),
      parent: undefined,
      cost: pathLength + lineDistance({x: foodPos[0], y: foodPos[1]},
        {x: snakePos[0], y: snakePos[1]}),
      move: snake.direction,
      path: pathLength});
    this.inSearch = true;

    while (open.length > 0)
    {
      var current = open.dequeue();

      if( this.atFood(current) )
      {
        console.log("success");
        var curParent = current.parent;
        while (curParent)
        {
          if ( curParent.parent !== undefined )
            this.addMove(curParent.move);
          curParent = curParent.parent;
        }

        var co = current.parent.pos.split(" ");
        var x = co[0],
            y = co[1];
        var fo = food.getPosition();

        if( !(x === fo[0] || y === fo[1]) )
        {
          if( x < 19 && current.move !== "right" )
            this.moveQueue.push(current.move);
        }

        return true;
      }
      else
      {
        ++pathLength;
        //get children
        this.getOptionalMoves(current);
        var nxtMoves = this.computeNextMoveCoordinates(current);
        //foreach child DO<commit_msg>Broke A* :(<commit_after>var canvas = document.getElementById("the-game");

Movement = {
  UP: 1,
  DOWN: 2,
  LEFT: 3,
  RIGHT: 4,
  CONTINUE: -1
}

//heuristic 1
function lineDistance(a, b)
{
  var xs = 0,
      ys = 0;

  xs = b.x - a.x;
  xs = xs * xs;

  ys = b.y - a.y;
  ys = ys * ys;

  return Math.sqrt( xs + ys );
}

//heuristic 2

//avg heuristic 1 and 2
function heuristicAverage(a, b)
{ }//return (lineDistance(a, b) + otherheuristic(a, b))/ 2; }

ai = {
  moveQueue: [],
  optionalMoves: [],
  inSearch: false,

  comparePosition: function(a, b) {
    if( a.pos === undefined || b.pos === undefined )
      return false;
    else
      return a.pos === b.pos;
  },

  init: function(){
    this.inSearch = false;
    this.moveQueue = [];
    this.optionalMoves = [];
  },

  addMove: function(move){
    this.moveQueue.unshift(move);
  },

  AStar: function() {
    var open = new PriorityQueue({ comparator: function(a, b){
      if (a.cost !== undefined && b.cost !== undefined)
        return a.cost - b.cost;
      return 0;
    }}),
       closed = [];

    var foodPos = food.getPosition(),
        snakePos = [Math.ceil(Math.floor(snake.x)/snake.size),
          Math.ceil(Math.floor(snake.y)/snake.size)];

    var pathLength = 0;

    open.queue({
      pos: snakePos[0] + " " + snakePos[1],
      body: snake.getPosition(),
      parent: undefined,
      cost: pathLength + lineDistance({x: foodPos[0], y: foodPos[1]},
        {x: snakePos[0], y: snakePos[1]}),
      move: snake.direction,
      path: pathLength});
    this.inSearch = true;

    while (open.length > 0)
    {
      var current = open.dequeue();

      if( this.atFood(current) )
      {
        console.log("success");
        var curParent = current.parent;
        while (curParent)
        {
          if ( curParent.parent !== undefined )
            this.addMove(curParent.move);
          curParent = curParent.parent;
        }

        var co = current.parent.pos.split(" ");
        var x = co[0],
            y = co[1];
        var fo = food.getPosition();

        if( !(x === fo[0] || y === fo[1]) && x < 19 )
          this.moveQueue.push(current.move);

        return true;
      }
      else
      {
        ++pathLength;
        //get children
        this.getOptionalMoves(current);
        var nxtMoves = this.computeNextMoveCoordinates(current);
        //foreach child DO
{'fraction_non_alphanumeric': 0.15904860392967943, 'fraction_numerical': 0.011168562564632886, 'mean_word_length': 2.039597737272156, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2386514', 'n_tokens_mistral': 1740, 'n_tokens_neox': 1628, 'n_words': 382}