2014-12-08 17:33:04 +01:00
|
|
|
window.Nikoli = Nikoli = {}
|
|
|
|
|
2014-12-09 12:30:29 +01:00
|
|
|
class Nikoli.Game
|
2014-12-09 12:47:40 +01:00
|
|
|
constructor: (@board, @name, @url) ->
|
2014-12-09 12:30:29 +01:00
|
|
|
@name = 'nikoli' unless @name?
|
2014-12-09 12:47:40 +01:00
|
|
|
@url = "/data/#{@name}" unless @url?
|
|
|
|
|
2014-12-09 12:30:29 +01:00
|
|
|
@board.classList.add @name
|
|
|
|
|
|
|
|
@grid = document.createElement 'div'
|
|
|
|
@grid.classList.add 'game-container'
|
|
|
|
@board.appendChild @grid
|
|
|
|
|
2014-12-09 12:47:40 +01:00
|
|
|
@getFiles()
|
|
|
|
|
2014-12-09 12:30:29 +01:00
|
|
|
buttons_div = document.createElement 'div'
|
|
|
|
buttons = {check: 'Check', reset: 'Reset', newgame: 'New game', help: '?'}
|
|
|
|
|
|
|
|
for k,v of buttons
|
|
|
|
button = document.createElement 'button'
|
|
|
|
button.innerHTML = v
|
|
|
|
button.classList.add k
|
|
|
|
|
|
|
|
buttons_div.appendChild button
|
|
|
|
|
|
|
|
@board.appendChild buttons_div
|
|
|
|
|
|
|
|
@board.querySelector('.check').addEventListener('click', @check.bind(this))
|
|
|
|
@board.querySelector('.reset').addEventListener('click', @reset.bind(this))
|
2014-12-09 12:47:40 +01:00
|
|
|
@board.querySelector('.newgame').addEventListener('click', @newgame.bind(this))
|
2014-12-09 12:30:29 +01:00
|
|
|
|
|
|
|
check: ->
|
|
|
|
errors = @errors()
|
|
|
|
|
|
|
|
if errors.length == 0
|
|
|
|
alert 'Congratulations!'
|
|
|
|
else
|
|
|
|
alert errors.map((el) -> el.message).join()
|
|
|
|
|
2014-12-09 12:47:40 +01:00
|
|
|
getFiles: ->
|
|
|
|
xmlhttp = new XMLHttpRequest()
|
|
|
|
xmlhttp.open("GET", "#{@url}.json")
|
|
|
|
|
|
|
|
xmlhttp.addEventListener('load', (evt) =>
|
|
|
|
@setFiles JSON.parse(evt.target.responseText))
|
|
|
|
xmlhttp.send()
|
|
|
|
|
|
|
|
setFiles: (files) ->
|
|
|
|
@files = files
|
|
|
|
@file = @files[0]
|
|
|
|
|
|
|
|
@newgame() unless @game?
|
|
|
|
|
|
|
|
newgame: ->
|
|
|
|
xmlhttp = new XMLHttpRequest()
|
|
|
|
xmlhttp.open("GET", "#{@url}/#{@file}.json")
|
|
|
|
|
|
|
|
xmlhttp.addEventListener('load', (evt) =>
|
|
|
|
@generate JSON.parse(evt.target.responseText))
|
|
|
|
xmlhttp.send()
|
|
|
|
|
2014-12-09 12:30:29 +01:00
|
|
|
reset: ->
|
|
|
|
@generate()
|
|
|
|
|
2014-12-08 17:33:04 +01:00
|
|
|
class Nikoli.Stream
|
|
|
|
constructor: (@game) ->
|
|
|
|
@cells = []
|
|
|
|
|
|
|
|
calculate: (cell) ->
|
|
|
|
value = @game[cell.x][cell.y]
|
|
|
|
@cells = []
|
|
|
|
@type = if value < 0 then 'black' else 'white'
|
|
|
|
|
|
|
|
cell = {x: cell.x, y: cell.y, value: value}
|
|
|
|
cells_to_process = [cell]
|
|
|
|
|
|
|
|
while cells_to_process.length > 0
|
|
|
|
cell = cells_to_process.pop()
|
|
|
|
cells_to_process = cells_to_process.concat @process(cell) unless @include(cell)
|
|
|
|
|
|
|
|
|
|
|
|
@cells
|
|
|
|
|
|
|
|
checkCell: (cell, value) ->
|
|
|
|
0 <= cell.x < @game.length && 0 <= cell.y < @game[cell.x].length &&
|
|
|
|
(value < 0 && @game[cell.x][cell.y] < 0 || value >= 0 && @game[cell.x][cell.y] >= 0)
|
|
|
|
|
|
|
|
empty: ->
|
|
|
|
@cells.length == 0
|
|
|
|
|
|
|
|
getCell: (cell, value) ->
|
|
|
|
{x: cell.x, y: cell.y, value: @game[cell.x][cell.y]} if @checkCell(cell, value)
|
|
|
|
|
|
|
|
include: (cell) ->
|
|
|
|
@cells.indexOf("#{cell.x};#{cell.y}") >= 0
|
|
|
|
|
|
|
|
length: ->
|
|
|
|
@cells.length
|
|
|
|
|
|
|
|
process: (cell) ->
|
|
|
|
@cells.push("#{cell.x};#{cell.y}")
|
|
|
|
|
|
|
|
x = cell.x
|
|
|
|
y = cell.y
|
|
|
|
value = cell.value
|
|
|
|
|
|
|
|
cells_to_add = []
|
|
|
|
tmp_cell = @getCell({x: x+1, y: y}, value)
|
|
|
|
cells_to_add.push tmp_cell if tmp_cell?
|
|
|
|
tmp_cell = @getCell({x: x-1, y: y}, value)
|
|
|
|
cells_to_add.push tmp_cell if tmp_cell?
|
|
|
|
tmp_cell = @getCell({x: x, y: y+1}, value)
|
|
|
|
cells_to_add.push tmp_cell if tmp_cell?
|
|
|
|
tmp_cell = @getCell({x: x, y: y-1}, value)
|
|
|
|
cells_to_add.push tmp_cell if tmp_cell?
|
|
|
|
|
|
|
|
cells_to_add
|