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: '[]'};
}
});
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(
function (val, list) {
return A2(
@ -9351,8 +9335,10 @@ var _user$project$Go_Util$lookup = F2(
A2(
_elm_lang$core$List$filter,
function (_p1) {
var _p2 = _p1;
return _elm_lang$core$Native_Utils.eq(_p2._0, val);
return function (a) {
return _elm_lang$core$Native_Utils.eq(a, val);
}(
_elm_lang$core$Tuple$first(_p1));
},
list)));
});
@ -9360,6 +9346,20 @@ var _user$project$Go_Util$pair = F2(
function (a1, 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 tryDecode = function (s) {
@ -9522,10 +9522,11 @@ var _user$project$Go_View$renderBoard = F2(
var cells = A2(
_elm_lang$core$List$map,
function (i) {
return A2(
_user$project$Go_Util$pair,
i,
A2(_user$project$Go_Util$lookup, i, board));
return {
ctor: '_Tuple2',
_0: i,
_1: A2(_user$project$Go_Util$lookup, i, board)
};
},
_user$project$Go_Util$allIndices(size));
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
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
@ -51,9 +51,9 @@ post "/game" do |env|
size = game.size.value
black = nil
if game_password == game.blackPass
if game_password == game.black_pass
black = true
elsif game_password == game.whitePass
elsif game_password == game.white_pass
black = false
end
@ -102,7 +102,7 @@ end
ws "/game/:id" do |socket, env|
game_id = env.params.url["id"]
if game = lookup_game(nil, GAME_CACHE, game_id)
socket.send game.to_string
socket.send game.to_json
game.sockets << socket
socket.on_message do |message|

View File

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