
function Grid(width, height)
{
  this.width = width;
  this.height = height;
  this.bombs = 0;
  this.flags = 0;
  
  this.cells = [];
  
  for (var x = 0; x < width; x++)
  {
    this.cells[x] = [];
    for (var y = 0; y < width; y++)
    {
      this.cells[x][y] = new Case(0,x,y);
    }
  }
}

Grid.prototype.isValidCoordinate = function (x, y)
{
  return x >= 0 && x < this.width && y >= 0 && y < this.height;
};

Grid.prototype.addBomb = function (x, y)
{
  this.cells[x][y].value = 'B';
  
  for (var i = -1 ; i <= 1 ; i++)
  {
    for (var j = -1 ; j <= 1 ; j++)
    {
      var x2 = x + i;
      var y2 = y + j;
      
      if (!this.isValidCoordinate(x2, y2))
      {
        continue;
      }
      
      if (!this.cells[x2][y2].isBomb())
      {
        this.cells[x2][y2].value++;
      }
    }
  }
};

Grid.prototype.addBombs = function (nbBombs)
{
  this.bombs = nbBombs;
  this.flags = nbBombs;

  for (var i = 0; i < nbBombs; i++)
  {
    var x = Math.floor((Math.random() * this.width));
    var y = Math.floor((Math.random() * this.height));
    
    while (this.cells[x][y].isBomb())
    {
      x = Math.floor((Math.random() * this.width));
      y = Math.floor((Math.random() * this.height));
    }
    
    this.addBomb(x, y);
  }
};

Grid.prototype.removeCases = function () {
  for (var x = 0; x < width; x++) {
    for (var y = 0; y < width; y++) {
      this.cells[x][y] = null;
    }
  }

};

Grid.prototype.reveal = function (x,y)
{
  if (this.cells[x][y].shown || this.cells[x][y].flagged)
  {
    return [];
  }
  
  this.cells[x][y].shown = true;
  
  var listCaseChanged = [this.cells[x][y]];
  
  if (this.cells[x][y].value === 0)
  {
    for (var i = -1 ; i <= 1 ; i++)
    {
      for (var j = -1 ; j <= 1 ; j++)
      {
        var x2 = x + i;
        var y2 = y + j;

        if (!this.isValidCoordinate(x2, y2))
        {
          continue;
        }

        listCaseChanged = listCaseChanged.concat(this.reveal(x2, y2));
      }
    }
  }
  
  return listCaseChanged;
};

Grid.prototype.getNbFlagsAround = function (x, y)
{
  var nbFlagsAround = 0;
  
  for (var i = -1 ; i <= 1 ; i++)
  {
    for (var j = -1 ; j <= 1 ; j++)
    {
      var x2 = x + i;
      var y2 = y + j;

      if (!this.isValidCoordinate(x2, y2))
      {
        continue;
      }

      if (this.cells[x2][y2].flagged)
      {
        nbFlagsAround++;
      }
    }
  }
  
  return nbFlagsAround;
};

Grid.prototype.quickReveal = function (x, y)
{
  if (this.cells[x][y].isBomb())
  {
    return {cases: [], lost: true};
  }
  
  if (this.getNbFlagsAround(x, y) != this.cells[x][y].value)
  {
      return {cases: [], lost: false};
  }
  
  var lost = false;
  var listCaseChanged = [];
  
  for (var i = -1 ; i <= 1 ; i++)
  {
    for (var j = -1 ; j <= 1 ; j++)
    {
      var x2 = x + i;
      var y2 = y + j;

      if (!this.isValidCoordinate(x2, y2) || this.cells[x2][y2].flagged)
      {
        if (!this.cells[x2][y2].isBomb())
        {
          listCaseChanged.push(this.cells[x2][y2]);
        }
        
        continue;
      }
      
      if (this.cells[x2][y2].isBomb())
      {
        lost = true;
      }
      
      listCaseChanged = listCaseChanged.concat(this.reveal(x2, y2));
    }
  }
  
  return {cases: listCaseChanged, lost: lost};
};

@@ -871,16 +871,17 @@
 s.bombs 
++
 = nbBomb
@@ -896,16 +896,17 @@
 s.flags 
++
 = nbBomb
@@ -2820,32 +2820,33 @@
 ar y2 = y + j;%0A%0A
+%0A
       if (!this.
@@ -2870,20 +2870,69 @@
 (x2, y2)
- %7C%7C 
+)%0A      %7B%0A        continue;%0A      %7D%0A      %0A      if (
 this.cel
@@ -2953,30 +2953,12 @@
 gged
-)%0A      %7B%0A        if (
+ && 
 !thi
@@ -2981,34 +2981,32 @@
 isBomb())%0A      
-  
 %7B%0A          list
@@ -2995,26 +2995,24 @@
   %7B%0A        
- 
- 
 listCaseChan
@@ -3045,27 +3045,8 @@
 %5D);%0A
-        %7D%0A        %0A
     
@@ -3285,12 +3285,201 @@
 : lost%7D;%0A%7D;%0A
+%0AGrid.prototype.getBombs = function ()%0A%7B%0A  var bombList = %5B%5D;%0A  %0A  for (var cell in this.cells) %7B%0A    if (cell.isBomb())%0A    %7B%0A      bombList.push(cell);%0A    %7D%0A  %7D%0A  %0A  return bombList;%0A%7D;%0A

fix double click and add Grid.getBombs