       console.log direction
 
       # try moving
-      move(@board, direction)
-      # check move validity
+      newBoard = move(@board, direction)
+      printArray newBoard
+      if moveIsValid(@board, newBoard)
+        console.log "Valid"
+        @board = newBoard
+        generateTile(@board)
+        showBoard(@board)
+      else
+        console.log "Invalid"
+
+
+      # check move validity, by comparing the original and new board
 
 
 

      row.push 0
  row

# console.log collapseCells [0,4,4,0], 'right'

moveIsValid = (originalBoard, newBoard) ->
  for row in [0..3]
    for col in [0..3]
      if originalBoard[row][col] isnt newBoard[row][col]
        return true
  false

showBoard = (board) ->
  for row in [0..3]
    for col in [0..3]
      $(".r#{row}.c#{col} > div").html(board[row][col])

printArray = (array) ->
  console.log "--- Start ---"
  for row in array
    console.log row
  console.log "--- End ---"


$ ->
