     game.board.cases[casePosition].g.remove()
     game.board.cases[casePosition] = new TextTile(@ctx, @x, @y, nbMinesAround)
 
-    if ++game.safe is game.board.cases.length - game.nbMines
-      console.log "WIN"
-      return
+    game.safe++
 
     if nbMinesAround is 0
       for [x, y] in game.neighborCoord @x, @y


class @Game
  constructor: (id) ->
    @states = Empty: 0, Playing: 1, GameOver: 2
    @state = @states.Empty
    @entities = Empty: 0, Mine: 1, Flag: 2
    @nbMines = 50
    @flags = 0
    @safe = 0
    @data = []
    @board = new Board id
    @board.createBoard()


  initGame: (x, y) ->
    @state = @states.Playing
    @generateBoard x, y





  gameOver: (deadPosition) =>
    @state.set StateManager.GameOver
    @showMines(deadPosition)


  win: () ->
    @state.set StateManager.Win
    background = @board.board.rect 0, 0, 42*19, 42*13
    background.attr fill: 'rgba(255, 255, 255, 0.7)'
    winLbl = @board.board.text 42*19/2, 42*13/2, 'Win'
    winLbl.attr
      fontSize: 40, fontFamily: 'Arial', fill: 'green',
      alignmentBaseline: 'central', textAnchor: 'middle'


  reset: () ->
    @safe = 0
    @flags = 0
    @data = []
    @willDie = false
    @board.reset()


class StateManager
  constructor: () ->
    @delay = 0
    @state = StateManager.Empty


  is: (state) ->
    @state is state


  set: (state) ->
    @state = state


  @Empty = 0
  @Playing = 1
  @GameOver = 2
  @Win = 3


class Board
