title: Algorithm to find an empty space on a grid
body: <p>So I'm creating a snake game, which you can see here: <a href="http://jaminweb.com/snake_TEST_PHP.php" rel="nofollow">http://jaminweb.com/snake_TEST_PHP.php</a></p>
  
  <p>When the snake's head hits a piece of food, the piece of food reappears somewhere else on the board, somewhere that is not on the snake. My function for handling this is simply to try random locations on the board until . But I realize this could involve a lot of computations when the snake has nearly taken up the entire board.</p>
  
  <p>Here's my function, which should be self-explanatory:</p>
  
  <pre><code>this.moveFood = function(bbf) {
      /*
          bbf: bounding box for the food
      */
      var tx = randInt(0, C_w / this.linkSize - 1) * this.linkSize - bbf.x;
      var ty = randInt(0, C_h / this.linkSize - 1) * this.linkSize - bbf.y;
      var fcopy = this.food;
      fcopy.translate(tx, ty);
      if (!this.hitSnake(fcopy)) {
          this.food.translate(tx, ty);
      } else {
          this.moveFood(bbf);
      }
  }
  </code></pre>
  
  <p>Does anyone have a better idea, or can anyone direct me to a resource where I can learn about whatever types of algorithms I'm going to need to improve on this?</p>