nikoli/views/application.coffee

151 lines
3.6 KiB
CoffeeScript
Raw Normal View History

2014-12-09 17:33:32 +01:00
window.Nikoli = {}
2014-12-09 12:30:29 +01:00
class Nikoli.Game
constructor: (@board, @name, @url) ->
2014-12-09 12:30:29 +01:00
@name = 'nikoli' unless @name?
@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
@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))
@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-09 12:30:29 +01:00
check: ->
errors = @errors()
if errors.length == 0
2014-12-11 22:29:46 +01:00
@board.querySelector('.congratulations').classList.add('show')
2014-12-09 12:30:29 +01:00
else
alert errors.map((el) -> el.message).join()
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: ->
2014-12-11 22:29:46 +01:00
@board.querySelector('.congratulations').classList.remove('show')
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-09 17:33:32 +01:00
class Nikoli.Cell
constructor: (@x, @y, @game) ->
@value = @game[@x][@y] if @valid()
toString: -> "#{@x};#{@y}"
2014-12-11 17:26:08 +01:00
getColumn: ->
column = []
column.push @game[i][@y] for i in [0...@game.length]
column
getRow: -> @game[@x]
2014-12-09 17:33:32 +01:00
adjacentCells: ->
[
new Cell(@x + 1, @y, @game),
new Cell(@x - 1, @y, @game),
new Cell(@x, @y + 1, @game),
new Cell(@x, @y - 1, @game)
]
2014-12-09 17:53:23 +01:00
isPool: ->
[
new Cell(@x, @y + 1, @game),
new Cell(@x + 1, @y, @game),
new Cell(@x + 1, @y + 1, @game),
].every (cell) => cell.valid(@value)
2014-12-09 17:33:32 +01:00
valid: (value) ->
0 <= @x < @game.length && 0 <= @y < @game[@x].length &&
(!value? || value < 0 && @game[@x][@y] < 0 || value >= 0 && @game[@x][@y] >= 0)
duplicatesIn: (array) ->
array.filter((cell) => cell == @value).length > 1
columnDuplicates: ->
2014-12-11 17:26:08 +01:00
@duplicatesIn @getColumn()
rowDuplicates: ->
2014-12-11 17:26:08 +01:00
@duplicatesIn @getRow()
squareDuplicates: (from, size) ->
square = []
for i in [from.x...(from.x + size)]
for j in [from.y...(from.y + size)]
square.push @game[i][j]
@duplicatesIn square
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'
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
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))