Compare commits
3 Commits
afb384e96e
...
fcd1e62b5e
Author | SHA1 | Date |
---|---|---|
Danila Fedorin | fcd1e62b5e | |
Danila Fedorin | 0145903e70 | |
Danila Fedorin | a6260deefd |
|
@ -3,6 +3,7 @@ require "kemal"
|
||||||
require "json"
|
require "json"
|
||||||
|
|
||||||
URL = "localhost"
|
URL = "localhost"
|
||||||
|
PORT = "3000"
|
||||||
GAME_CACHE = {} of String => Go::Game
|
GAME_CACHE = {} of String => Go::Game
|
||||||
|
|
||||||
def query_game(db, id) : Go::Game?
|
def query_game(db, id) : Go::Game?
|
||||||
|
@ -116,6 +117,4 @@ ws "/game/:id" do |socket, env|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
GAME_CACHE["debug"] = Go::Game.new(Go::Size::Small, "black", "white")
|
|
||||||
|
|
||||||
Kemal.run
|
Kemal.run
|
||||||
|
|
|
@ -56,9 +56,60 @@ module Go
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def count_neighbors(x, y, color, visited)
|
||||||
|
if visited.includes?({x, y}) || (x < 0 || x >= @size.value || y < 0 || y >= @size.value)
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
visited.push({x, y})
|
||||||
|
case @board[{x, y}]?
|
||||||
|
when color
|
||||||
|
return count_neighbors(x - 1, y, color, visited) +
|
||||||
|
count_neighbors(x + 1, y, color, visited) +
|
||||||
|
count_neighbors(x, y - 1, color, visited) +
|
||||||
|
count_neighbors(x, y + 1, color, visited)
|
||||||
|
when nil
|
||||||
|
return 1
|
||||||
|
else
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return 0
|
||||||
|
end
|
||||||
|
|
||||||
|
private def remove_color(x, y, color)
|
||||||
|
if !(x < 0 || x >= @size.value || y < 0 || y >= @size.value) && @board[{x, y}]? == color
|
||||||
|
@board.delete({x, y})
|
||||||
|
remove_color(x - 1, y, color)
|
||||||
|
remove_color(x + 1, y, color)
|
||||||
|
remove_color(x, y - 1, color)
|
||||||
|
remove_color(x, y + 1, color)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private def try_remove_branch(x, y, color)
|
||||||
|
if @board[{x, y}]? == color
|
||||||
|
neighbor_count = count_neighbors(x, y, color, [] of Tuple(Int8, Int8))
|
||||||
|
if neighbor_count == 0
|
||||||
|
remove_color(x, y, color)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def invert(color)
|
||||||
|
color == Color::Black ? Color::White : Color::Black
|
||||||
|
end
|
||||||
|
|
||||||
def update(x, y, color)
|
def update(x, y, color)
|
||||||
@board[{x, y}] = color
|
if @turn == color
|
||||||
@turn = @turn == Color::Black ? Color::White : Color::Black
|
@board[{x, y}] = color
|
||||||
|
new_color = invert(color)
|
||||||
|
try_remove_branch(x - 1, y, new_color)
|
||||||
|
try_remove_branch(x + 1, y, new_color)
|
||||||
|
try_remove_branch(x, y - 1, new_color)
|
||||||
|
try_remove_branch(x, y + 1, new_color)
|
||||||
|
try_remove_branch(x, y, color)
|
||||||
|
@turn = new_color
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private def color_char(color)
|
private def color_char(color)
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
var node = document.getElementById('elm-root')
|
var node = document.getElementById('elm-root')
|
||||||
var app = Elm.Main.embed(node, {
|
var app = Elm.Main.embed(node, {
|
||||||
'black' : <%= black %>,
|
'black' : <%= black %>,
|
||||||
'url' : "<%= "ws://" + URL + ":" + Kemal.config.port.to_s %>",
|
'url' : "<%= "ws://" + URL + ":" + PORT %>",
|
||||||
'id' : "<%= id %>",
|
'id' : "<%= id %>",
|
||||||
'size' : <%= size %>
|
'size' : <%= size %>
|
||||||
})
|
})
|
||||||
|
|
Loading…
Reference in New Issue