Clean up code.

This commit is contained in:
Danila Fedorin 2018-05-26 13:21:53 -07:00
parent fcd1e62b5e
commit 228612168c
4 changed files with 43 additions and 38 deletions

2
external/GoUI vendored

@ -1 +1 @@
Subproject commit 237630054b0d2fcab327cd271573d769c03f593b Subproject commit b68222dab7a766ede897ba9bdc68f262e2194e3d

View File

@ -9326,22 +9326,6 @@ var _user$project$Go_Util$zip = F2(
return {ctor: '[]'}; return {ctor: '[]'};
} }
}); });
var _user$project$Go_Util$allIndices = function (n) {
var pairs = F2(
function (xs, i) {
return A2(
_elm_lang$core$List$map,
function (x) {
return {ctor: '_Tuple2', _0: i, _1: x};
},
xs);
});
var vals = A2(_elm_lang$core$List$range, 0, n - 1);
return A2(
_elm_lang$core$List$concatMap,
pairs(vals),
vals);
};
var _user$project$Go_Util$lookup = F2( var _user$project$Go_Util$lookup = F2(
function (val, list) { function (val, list) {
return A2( return A2(
@ -9351,8 +9335,10 @@ var _user$project$Go_Util$lookup = F2(
A2( A2(
_elm_lang$core$List$filter, _elm_lang$core$List$filter,
function (_p1) { function (_p1) {
var _p2 = _p1; return function (a) {
return _elm_lang$core$Native_Utils.eq(_p2._0, val); return _elm_lang$core$Native_Utils.eq(a, val);
}(
_elm_lang$core$Tuple$first(_p1));
}, },
list))); list)));
}); });
@ -9360,6 +9346,20 @@ var _user$project$Go_Util$pair = F2(
function (a1, a2) { function (a1, a2) {
return {ctor: '_Tuple2', _0: a1, _1: a2}; return {ctor: '_Tuple2', _0: a1, _1: a2};
}); });
var _user$project$Go_Util$allIndices = function (n) {
var pairs = F2(
function (xs, i) {
return A2(
_elm_lang$core$List$map,
_user$project$Go_Util$pair(i),
xs);
});
var vals = A2(_elm_lang$core$List$range, 0, n - 1);
return A2(
_elm_lang$core$List$concatMap,
pairs(vals),
vals);
};
var _user$project$Go_Decoders$decodeColor = function () { var _user$project$Go_Decoders$decodeColor = function () {
var tryDecode = function (s) { var tryDecode = function (s) {
@ -9522,10 +9522,11 @@ var _user$project$Go_View$renderBoard = F2(
var cells = A2( var cells = A2(
_elm_lang$core$List$map, _elm_lang$core$List$map,
function (i) { function (i) {
return A2( return {
_user$project$Go_Util$pair, ctor: '_Tuple2',
i, _0: i,
A2(_user$project$Go_Util$lookup, i, board)); _1: A2(_user$project$Go_Util$lookup, i, board)
};
}, },
_user$project$Go_Util$allIndices(size)); _user$project$Go_Util$allIndices(size));
return A2( return A2(

View File

@ -33,7 +33,7 @@ def handle_message(id, game, socket, message)
color = split_command[3] == "Black" ? Go::Color::Black : Go::Color::White color = split_command[3] == "Black" ? Go::Color::Black : Go::Color::White
game.update(x, y, color) game.update(x, y, color)
game.sockets.each { |socket| socket.send game.to_string } game.sockets.each { |socket| socket.send game.to_json }
end end
end end
@ -51,9 +51,9 @@ post "/game" do |env|
size = game.size.value size = game.size.value
black = nil black = nil
if game_password == game.blackPass if game_password == game.black_pass
black = true black = true
elsif game_password == game.whitePass elsif game_password == game.white_pass
black = false black = false
end end
@ -102,7 +102,7 @@ end
ws "/game/:id" do |socket, env| ws "/game/:id" do |socket, env|
game_id = env.params.url["id"] game_id = env.params.url["id"]
if game = lookup_game(nil, GAME_CACHE, game_id) if game = lookup_game(nil, GAME_CACHE, game_id)
socket.send game.to_string socket.send game.to_json
game.sockets << socket game.sockets << socket
socket.on_message do |message| socket.on_message do |message|

View File

@ -14,13 +14,13 @@ module Go
class Game class Game
property size : Size property size : Size
property blackPass : String property black_pass : String
property whitePass : String property white_pass : String
property board : Board property board : Board
property turn : Color property turn : Color
property sockets : Array(HTTP::WebSocket) property sockets : Array(HTTP::WebSocket)
def initialize(size : Size, @blackPass, @whitePass) def initialize(size : Size, @black_pass, @white_pass)
@size = size @size = size
@board = Board.new @board = Board.new
@turn = Color::Black @turn = Color::Black
@ -47,7 +47,7 @@ module Go
end end
end end
def to_string def to_json
JSON.build do |json| JSON.build do |json|
json.object do json.object do
json.field "turn", @turn.to_s json.field "turn", @turn.to_s
@ -57,11 +57,12 @@ module Go
end end
private def count_neighbors(x, y, color, visited) private def count_neighbors(x, y, color, visited)
if visited.includes?({x, y}) || (x < 0 || x >= @size.value || y < 0 || y >= @size.value) coord = {x, y}
if visited.includes?(coord) || (x < 0 || x >= @size.value || y < 0 || y >= @size.value)
return 0 return 0
else else
visited.push({x, y}) visited.push(coord)
case @board[{x, y}]? case @board[coord]?
when color when color
return count_neighbors(x - 1, y, color, visited) + return count_neighbors(x - 1, y, color, visited) +
count_neighbors(x + 1, y, color, visited) + count_neighbors(x + 1, y, color, visited) +
@ -77,8 +78,9 @@ module Go
end end
private def remove_color(x, y, color) private def remove_color(x, y, color)
if !(x < 0 || x >= @size.value || y < 0 || y >= @size.value) && @board[{x, y}]? == color coord = {x, y}
@board.delete({x, y}) if !(x < 0 || x >= @size.value || y < 0 || y >= @size.value) && @board[coord]? == color
@board.delete(coord)
remove_color(x - 1, y, color) remove_color(x - 1, y, color)
remove_color(x + 1, y, color) remove_color(x + 1, y, color)
remove_color(x, y - 1, color) remove_color(x, y - 1, color)
@ -87,7 +89,8 @@ module Go
end end
private def try_remove_branch(x, y, color) private def try_remove_branch(x, y, color)
if @board[{x, y}]? == color coord = {x, y}
if @board[coord]? == color
neighbor_count = count_neighbors(x, y, color, [] of Tuple(Int8, Int8)) neighbor_count = count_neighbors(x, y, color, [] of Tuple(Int8, Int8))
if neighbor_count == 0 if neighbor_count == 0
remove_color(x, y, color) remove_color(x, y, color)
@ -100,8 +103,9 @@ module Go
end end
def update(x, y, color) def update(x, y, color)
coord = {x, y}
if @turn == color if @turn == color
@board[{x, y}] = color @board[coord] = color
new_color = invert(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 + 1, y, new_color) try_remove_branch(x + 1, y, new_color)