// board is double array
// snake is a list w/head and tail

snake = [[0, 0]]; // x, y
dir = 'right';
var board = [];     // top left is 0, 0, bottom right is height-1, width-1
var food = [-1, -1];

// initialize board and snake, and start snake movement
function initSnake() {
  console.log(width+" "+height);
  board.length = width; // board[x][y]
  for (var i = 0; i < width; i++) {
    board[i] = [];
    board[i].length = height;
  }
  window.setInterval(aiLevel0, 500);
  aiLevel0();
}

function aiLevel0() {
  moveHead(dir);
  if (dir == 'right' || dir == 'left') {
    if (snake[0][0] == food[0]) {   // is the snake above or below the food?
      if (snake[0][1] < food[1]) {
        dir = 'down';
      } else {
        dir = 'up';
      }
    } else if ((snake[0][0] == width - 1) && dir == 'right') { // is the snake at the right wall
		dir = 'down';
	} else if ((snake[0][0] == 0) && dir == 'left') {  // is the snake at the left wall?
		dir = 'up';
	}
  } else if (dir == 'up' || dir == 'down') {
    if (snake[0][1] == food[1]) {   // is the snake to the right or left of the food?
      if (snake[0][0] < food[0]) {
        dir = 'right';
      } else {
        dir = 'left';
      }
    } else if ((snake[0][1] == 0) && dir == 'up') { // is the snake at the top wall
		dir = 'right'; 
	} else if (snake[0][1] == height - 1 && dir == 'down') {  // is the snake at the bottom wall?
		dir = 'left';
	} // else dir is unchanged
  }
}

function moveHead(dir) {
  // move snake's head in given direction
  // 'right', 'left', 'up', and 'down'
  var newHead = snake[0];
  if (dir == 'up') {  // add new head above current one
    newHead[1]--;  // changes y-coordinate by -1 (going UP)
  } else if (dir == 'down') {
    newHead[1]++;
  } else if (dir == 'right') {
    newHead[0]++;
  } else if (dir == 'left') {
    newHead[0]--;
  }
//  if (newHead[0] < 0 || newHead[0] >= width || newHead[1] < 0 || newHead[1] >= height) {

//  }
  snake.unshift(newHead); // add newHead to the beginning of the list
  console.log(snake);
  updateBoard(snake);
  if (snake[0] == food) { // if snake's head is on the food
    food = [-1, -1];
  } else {
    delTail();
  }
}

function delTail() {  // simple function to remove the tail
  console.log(snake.pop());
  updateBoard(snake);
}

function placeFood(x, y) {
	if (food != [-1, -1]) { // already food
		console.log("Invalid food placement: There is already food");
		return false;
	} else if (x >= width || y >= height || x < 0 || y < 0) { // off of board
		console.log("Invalid food placement: Food is off board");
		return false;
	} else if (snake.includes([x, y])) {	 // new food collides with snake
		console.log("Invalid food placement: Food collides with snake");
		return false;
	} else {
		food = [x, y];
		console.log("Food placed at "+x+" "+y);
		return true;
	}
}

@@ -275,41 +275,8 @@
 ) %7B%0A
-  console.log(width+%22 %22+height);%0A
   bo
@@ -310,16 +310,16 @@
 d%5Bx%5D%5By%5D%0A
+
   for (v
@@ -846,24 +846,25 @@
 ake%5B0%5D%5B0%5D ==
+=
  0) && dir =
@@ -1191,16 +1191,17 @@
 0%5D%5B1%5D ==
+=
  0) && d
@@ -1266,11 +1266,11 @@
 ht';
- %0A%09
+%0A  
 %7D el
@@ -2242,24 +2242,24 @@
 (snake);%0A%7D%0A%0A
-
 function pla
@@ -2273,16 +2273,138 @@
 x, y) %7B%0A
+  for (var s=0; s%3Csnake.length; s++) %7B%0A%09%09if (x == snake%5Bs%5D%5B0%5D && y == snake%5Bs%5D%5B1%5D) %7B%0A%09%09%09isSnakeCell = true;%0A%09%09%09break;%0A%09%09%7D%0A
 %09if (foo

use === for 0, so that you don't get null values passing the check.