Compare commits

..

No commits in common. "fcd1e62b5eafa9b742942d7d2a5e83a9b81c0511" and "afb384e96e5ed32eb2bbe9643b43cf873da5cffd" have entirely different histories.

3 changed files with 5 additions and 55 deletions

View File

@ -3,7 +3,6 @@ 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?
@ -117,4 +116,6 @@ 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

View File

@ -56,60 +56,9 @@ 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)
if @turn == color
@board[{x, y}] = color @board[{x, y}] = color
new_color = invert(color) @turn = @turn == Color::Black ? Color::White : Color::Black
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)

View File

@ -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 + ":" + PORT %>", 'url' : "<%= "ws://" + URL + ":" + Kemal.config.port.to_s %>",
'id' : "<%= id %>", 'id' : "<%= id %>",
'size' : <%= size %> 'size' : <%= size %>
}) })