0

I am creating a JavaScript game which is very similar to classic arcade Snake game. For every piece of food snake eats it gets +1 on its size starting with size 5. It is contained in canvas of 450X450 px with the size of one cell 10x10 px (picture).

enter image description here

The original game is taken and modified from another source, but my goal is to create autopilot.

This is the function that generates food

    function create_food() {

    food = {
        x: Math.round(Math.random() * (w - cw) / cw),
        y: Math.round(Math.random() * (h - cw) / cw),
    };
}

Basically it is random number from 0 to width_of_canvas - snake_cell / snake_cell ((450-10)/10). So food has coordinate x and coordinate y. On very similar principle are the snake cells but it is an array with all the snake cells.

    function create_snake() {
    snake_array = []; //Empty array to start with
    length = 5;
    for (var i = length - 1; i >= 0; i--) {
        //This will create a horizontal snake starting from the top left
        snake_array.push({
            x: i,
            y: 0
        });
    }
}

Only difference is there are multiple fields in snake array while there is only one in food array.

PROBLEM: I don't want food spawning INSIDE my snake, so I need to check if the coordinates of the food would be same as one of the fields in snake array.

I was thinking of inArray() function but I don't know how to use it in this case.

EXAMPLE: food[0].x = 31; food[0].y = 22

  • snake_array[0].x = 28; snake_array[0].y = 22
  • snake_array[1].x = 29; snake_array[1].y = 22
  • snake_array[2].x = 30; snake_array[2].y = 22
  • snake_array[3].x = 31; snake_array[3].y = 22 collision
  • snake_array[4].x = 32; snake_array[4].y = 22

EDIT: thanks @Siguza

CountGradsky
  • 268
  • 2
  • 4
  • 15
  • Would't it be enough to create first the snake with complete freedom, and then create the food pieces taking care not to be included in the snake array? – Little Santi Sep 06 '15 at 18:51
  • @LittleSanti yes, thats actually what I'm doing, but again food needs to check if its coordinates collide with the snake, so again I need this method. – CountGradsky Sep 06 '15 at 19:05
  • @Siguza this question explains comparison of 2 objects. But I need to check one object vs array of objects. EDIT: I could do this with for loop? Standby.... – CountGradsky Sep 06 '15 at 19:07
  • 1
    @user3188464 Yes, so you'd like to **compare an object** with every object in an array. I trust you know enough JS to add a loop yourself. – Siguza Sep 06 '15 at 19:10
  • @user3188464 For what it's worth, here's a one-liner: `snake_array.map(function(e){return JSON.stringify(e)}).indexOf(JSON.stringify({x:31,y:22}))>-1` – Siguza Sep 06 '15 at 19:17

1 Answers1

0

If you can use lodash there's already a function to find if two objects are same:

_.isEqual(object, other);

And in your function, you can check for the in_array(); equivalent in JavaScript.

function in_array(needle, haystack, argStrict) {
  //  discuss at: http://phpjs.org/functions/in_array/
  // original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
  // improved by: vlado houba
  // improved by: Jonas Sciangula Street (Joni2Back)
  //    input by: Billy
  // bugfixed by: Brett Zamir (http://brett-zamir.me)
  //   example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
  //   returns 1: true
  //   example 2: in_array('vlado', {0: 'Kevin', vlado: 'van', 1: 'Zonneveld'});
  //   returns 2: false
  //   example 3: in_array(1, ['1', '2', '3']);
  //   example 3: in_array(1, ['1', '2', '3'], false);
  //   returns 3: true
  //   returns 3: true
  //   example 4: in_array(1, ['1', '2', '3'], true);
  //   returns 4: false

  var key = '',
    strict = !! argStrict;

  //we prevent the double check (strict && arr[key] === ndl) || (!strict && arr[key] == ndl)
  //in just one for, in order to improve the performance 
  //deciding wich type of comparation will do before walk array
  if (strict) {
    for (key in haystack) {
      if (haystack[key] === needle) {
        return true;
      }
    }
  } else {
    for (key in haystack) {
      if (haystack[key] == needle) {
        return true;
      }
    }
  }

  return false;
}

You can change the function where it says:

haystack[key] == needle

With the following:

_.isEqual(haystack[key], needle)
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252