2014-12-09 17:33:32 +01:00
|
|
|
window.Nikoli = {}
|
2014-12-08 17:33:04 +01:00
|
|
|
|
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
|
|
|
|
|
2014-12-15 12:44:53 +01:00
|
|
|
files_select = document.createElement 'select'
|
|
|
|
files_select.classList.add 'files'
|
|
|
|
files_select.addEventListener('change', (evt) =>
|
|
|
|
@file = evt.target.value
|
|
|
|
@newgame())
|
|
|
|
|
|
|
|
buttons_div.appendChild files_select
|
|
|
|
|
2014-12-09 12:30:29 +01:00
|
|
|
@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
|
|
|
|
2014-12-11 22:29:46 +01:00
|
|
|
congratulations = document.createElement 'div'
|
|
|
|
congratulations.innerHTML = 'Congratulations!'
|
|
|
|
congratulations.classList.add 'congratulations'
|
|
|
|
congratulations.classList.add 'hide'
|
|
|
|
@board.appendChild congratulations
|
|
|
|
|
2014-12-12 12:57:51 +01:00
|
|
|
errors = document.createElement 'ul'
|
|
|
|
errors.classList.add 'errors'
|
|
|
|
errors.classList.add 'hide'
|
|
|
|
@board.appendChild errors
|
|
|
|
|
2014-12-09 12:30:29 +01:00
|
|
|
check: ->
|
|
|
|
errors = @errors()
|
|
|
|
|
|
|
|
if errors.length == 0
|
2014-12-12 12:57:51 +01:00
|
|
|
[].forEach.call @board.querySelectorAll('.error'), (cell) ->
|
|
|
|
cell.classList.remove 'error'
|
|
|
|
@board.querySelector('.errors').classList.remove('show')
|
2014-12-11 22:29:46 +01:00
|
|
|
@board.querySelector('.congratulations').classList.add('show')
|
2014-12-09 12:30:29 +01:00
|
|
|
else
|
2014-12-12 12:57:51 +01:00
|
|
|
errors_elem = @board.querySelector('.errors')
|
|
|
|
errors_elem.innerHTML = ''
|
|
|
|
|
|
|
|
errors.forEach (error) =>
|
|
|
|
error_cell = @board.querySelector("[data-row=\"#{error.row}\"][data-column=\"#{error.column}\"]")
|
|
|
|
error_cell.classList.add 'error'
|
|
|
|
|
|
|
|
li = document.createElement('li')
|
|
|
|
li.innerHTML = error.message
|
|
|
|
|
|
|
|
errors_elem.appendChild li
|
|
|
|
|
|
|
|
@board.querySelector('.congratulations').classList.remove('show')
|
|
|
|
errors_elem.classList.add('show')
|
2014-12-09 12:30:29 +01:00
|
|
|
|
2014-12-12 12:18:22 +01:00
|
|
|
generate: (game, solution = false, cell_class = Nikoli.Cell) ->
|
|
|
|
@game = game if game?
|
|
|
|
|
2014-12-12 12:57:51 +01:00
|
|
|
@board.querySelector('.congratulations').classList.remove('show')
|
|
|
|
@board.querySelector('.errors').classList.remove('show')
|
|
|
|
|
2014-12-12 12:18:22 +01:00
|
|
|
@grid.innerHTML = ''
|
|
|
|
@game.forEach((row, i) =>
|
|
|
|
row_elem = new Nikoli.Row().create()
|
|
|
|
row.forEach((cell, j) ->
|
|
|
|
row_elem.appendChild new cell_class(i, j).create(cell))
|
|
|
|
|
|
|
|
@grid.appendChild row_elem)
|
|
|
|
|
|
|
|
return
|
|
|
|
|
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]
|
|
|
|
|
2014-12-15 12:44:53 +01:00
|
|
|
select = @board.querySelector('select')
|
|
|
|
select.innerHTML = ''
|
|
|
|
@files.forEach((file) ->
|
|
|
|
option = document.createElement('option')
|
|
|
|
option.text = file
|
|
|
|
select.appendChild option
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2014-12-09 12:47:40 +01:00
|
|
|
@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-12 12:18:22 +01:00
|
|
|
class Nikoli.Row
|
|
|
|
create: ->
|
|
|
|
row = document.createElement 'div'
|
|
|
|
row.classList.add 'grid-row'
|
|
|
|
|
|
|
|
row
|
|
|
|
|
2014-12-09 17:33:32 +01:00
|
|
|
class Nikoli.Cell
|
2014-12-14 22:12:27 +01:00
|
|
|
constructor: (@row, @column, @game) ->
|
|
|
|
@value = @game[@row][@column] if @game? && @valid()
|
2014-12-12 12:18:22 +01:00
|
|
|
|
|
|
|
create: (value) ->
|
|
|
|
cell = document.createElement 'div'
|
2014-12-14 22:12:27 +01:00
|
|
|
cell.dataset.row = @row
|
|
|
|
cell.dataset.column = @column
|
2014-12-12 12:18:22 +01:00
|
|
|
|
|
|
|
cell.classList.add 'grid-cell'
|
|
|
|
|
|
|
|
cell.innerHTML = ' '
|
|
|
|
|
|
|
|
cell
|
2014-12-09 17:33:32 +01:00
|
|
|
|
2014-12-14 22:12:27 +01:00
|
|
|
toString: -> "#{@row};#{@column}"
|
2014-12-09 17:33:32 +01:00
|
|
|
|
2014-12-11 17:26:08 +01:00
|
|
|
getColumn: ->
|
|
|
|
column = []
|
2014-12-14 22:12:27 +01:00
|
|
|
column.push @game[i][@column] for i in [0...@game.length]
|
2014-12-11 17:26:08 +01:00
|
|
|
column
|
|
|
|
|
2014-12-14 22:12:27 +01:00
|
|
|
getRow: -> @game[@row]
|
2014-12-11 17:26:08 +01:00
|
|
|
|
2014-12-09 17:33:32 +01:00
|
|
|
adjacentCells: ->
|
2014-12-12 12:23:25 +01:00
|
|
|
constructor = Object.getPrototypeOf(this).constructor
|
2014-12-09 17:33:32 +01:00
|
|
|
[
|
2014-12-14 22:12:27 +01:00
|
|
|
new constructor(@row + 1, @column, @game),
|
|
|
|
new constructor(@row - 1, @column, @game),
|
|
|
|
new constructor(@row, @column + 1, @game),
|
|
|
|
new constructor(@row, @column - 1, @game)
|
2014-12-09 17:33:32 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
valid: (value) ->
|
2014-12-14 22:12:27 +01:00
|
|
|
0 <= @row < @game.length && 0 <= @column < @game[@row].length &&
|
|
|
|
(!value? || value < 0 && @game[@row][@column] < 0 || value >= 0 && @game[@row][@column] >= 0)
|
2014-12-09 17:33:32 +01:00
|
|
|
|
2014-12-10 12:52:00 +01:00
|
|
|
duplicatesIn: (array) ->
|
|
|
|
array.filter((cell) => cell == @value).length > 1
|
|
|
|
|
|
|
|
columnDuplicates: ->
|
2014-12-11 17:26:08 +01:00
|
|
|
@duplicatesIn @getColumn()
|
2014-12-10 12:52:00 +01:00
|
|
|
|
|
|
|
rowDuplicates: ->
|
2014-12-11 17:26:08 +01:00
|
|
|
@duplicatesIn @getRow()
|
2014-12-10 12:52:00 +01:00
|
|
|
|
2014-12-10 12:58:02 +01:00
|
|
|
squareDuplicates: (from, size) ->
|
|
|
|
square = []
|
2014-12-14 22:12:27 +01:00
|
|
|
for i in [from.row...(from.row + size)]
|
|
|
|
for j in [from.column...(from.column + size)]
|
2014-12-10 12:58:02 +01:00
|
|
|
square.push @game[i][j]
|
|
|
|
|
|
|
|
@duplicatesIn square
|
|
|
|
|
2014-12-08 17:33:04 +01:00
|
|
|
class Nikoli.Stream
|
|
|
|
constructor: (@game) ->
|
|
|
|
@cells = []
|
|
|
|
|
|
|
|
calculate: (cell) ->
|
|
|
|
@cells = []
|
2014-12-09 17:33:32 +01:00
|
|
|
@type = if cell.value < 0 then 'black' else 'white'
|
2014-12-08 17:33:04 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
empty: ->
|
|
|
|
@cells.length == 0
|
|
|
|
|
|
|
|
include: (cell) ->
|
2014-12-09 17:33:32 +01:00
|
|
|
@cells.indexOf(cell.toString()) >= 0
|
2014-12-08 17:33:04 +01:00
|
|
|
|
|
|
|
length: ->
|
|
|
|
@cells.length
|
|
|
|
|
|
|
|
process: (cell) ->
|
2014-12-09 17:33:32 +01:00
|
|
|
@cells.push(cell.toString())
|
|
|
|
|
|
|
|
cell.adjacentCells().filter((adj_cell) -> adj_cell.valid(cell.value))
|