function addFood() {
  var count = 0;
  do {
    var x = Math.floor(Math.random() * (WIDTH - foodMargin * 2) + foodMargin);
    var y = Math.floor(Math.random() * (HEIGHT - foodMargin * 2) + foodMargin);
    var cell = getCell([x, y]);
    if (count++ > HEIGHT * WIDTH * 10) {
      // Can't find a place to place food.  End game.
      clearInterval(pid);
      isRunning = false;
      alert("You win!");
      showStart();
      return;
    }
  } while (cell.classList.contains('border') ||
           cell.classList.contains('snake') ||
           cell.classList.contains('food'));
  // Note: the game will crash if there are zero free squares.
  cell.classList.add('food');
}
// Add an new food square somewhere randomly on the board.
Add an new food square somewhere randomly on the board.
Add an new food square somewhere randomly on the board .
function addFood() {
  var count = 0;
  do {
    var x = Math.floor(Math.random() * (WIDTH - foodMargin * 2) + foodMargin);
    var y = Math.floor(Math.random() * (HEIGHT - foodMargin * 2) + foodMargin);
    var cell = getCell([x, y]);
    if (count++ > HEIGHT * WIDTH * 10) {
      clearInterval(pid);
      isRunning = false;
      alert("You win!");
      showStart();
      return;
    }
  } while (cell.classList.contains('border') ||
           cell.classList.contains('snake') ||
           cell.classList.contains('food'));
  cell.classList.add('food');
}
function addFood ( ) { var count = 0 ; do { var x = Math . floor ( Math . random ( ) * ( WIDTH - foodMargin * 2 ) + foodMargin ) ; var y = Math . floor ( Math . random ( ) * ( HEIGHT - foodMargin * 2 ) + foodMargin ) ; var cell = getCell ( [ x , y ] ) ; if ( count ++ > HEIGHT * WIDTH * 10 ) { clearInterval ( pid ) ; isRunning = false ; alert ( "You win!" ) ; showStart ( ) ; return ; } } while ( cell . classList . contains ( 'border' ) || cell . classList . contains ( 'snake' ) || cell . classList . contains ( 'food' ) ) ; cell . classList . add ( 'food' ) ; }
Add an new food square somewhere randomly on the board.
Add an new food square somewhere randomly on the board .
// Can't find a place to place food.  End game. // Note: the game will crash if there are zero free squares.