From f01c891cecd2edecbc41698842b43267eb7cd823 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Fri, 25 May 2018 15:03:04 -0700 Subject: [PATCH] Move game into separate source file and add encode method. --- src/Go.cr | 69 +++--------------------------------------- src/Go/Game.cr | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 65 deletions(-) create mode 100644 src/Go/Game.cr diff --git a/src/Go.cr b/src/Go.cr index 35dfc5c..2d0483e 100644 --- a/src/Go.cr +++ b/src/Go.cr @@ -2,71 +2,10 @@ require "./Go/*" require "kemal" require "json" -enum Color - Black - White -end - -enum Size - Small = 9, - Medium = 13, - Large = 19 -end - -alias Board = Hash(Tuple(Int8, Int8), Color) - - -def cell_json(index, color, json) - json.object do - json.field "index" do - json.object do - json.field "x", index[0] - json.field "y", index[1] - end - end - json.field "color", color.to_s - end -end - -def board_json(board, json) - json.array do - board.each do |key, value| - cell_json(key, value, json) - end - end -end - -class Game - property size : Size - property board : Board - property turn : Color - property sockets : Array(HTTP::WebSocket) - - def initialize(size : Size) - @size = size - @board = Board.new - @turn = Color::Black - @sockets = [] of HTTP::WebSocket - end - - def to_string - JSON.build do |json| - json.object do - json.field "turn", @turn.to_s - json.field "board" { board_json(@board, json) } - end - end - end - - def update(x, y, color) - @board[{x, y}] = color - end -end - URL = "localhost" -GAME_CACHE = {} of Int64 => Game +GAME_CACHE = {} of Int64 => Go::Game -def lookup_game(cache, id) : Game? +def lookup_game(cache, id) : Go::Game? return nil end @@ -76,7 +15,7 @@ def handle_message(id, game, socket, message) if command == "place" x = split_command[1].to_i8 y = split_command[2].to_i8 - color = split_command[3] == "Black" ? Color::Black : Color::White + 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 } @@ -123,6 +62,6 @@ ws "/game/:id" do |socket, env| end end -GAME_CACHE[1_i64] = Game.new(Size::Small) +GAME_CACHE[1_i64] = Go::Game.new(Go::Size::Small) Kemal.run diff --git a/src/Go/Game.cr b/src/Go/Game.cr new file mode 100644 index 0000000..b6025f2 --- /dev/null +++ b/src/Go/Game.cr @@ -0,0 +1,81 @@ +module Go + enum Color + Black + White + end + + enum Size + Small = 9, + Medium = 13, + Large = 19 + end + + alias Board = Hash(Tuple(Int8, Int8), Color) + + class Game + property size : Size + property board : Board + property turn : Color + property sockets : Array(HTTP::WebSocket) + + def initialize(size : Size) + @size = size + @board = Board.new + @turn = Color::Black + @sockets = [] of HTTP::WebSocket + end + + private def cell_json(index, color, json) + json.object do + json.field "index" do + json.object do + json.field "x", index[0] + json.field "y", index[1] + end + end + json.field "color", color.to_s + end + end + + private def board_json(board, json) + json.array do + board.each do |key, value| + cell_json(key, value, json) + end + end + end + + def to_string + JSON.build do |json| + json.object do + json.field "turn", @turn.to_s + json.field "board" { board_json(@board, json) } + end + end + end + + def update(x, y, color) + @board[{x, y}] = color + end + + private def color_char(color) + color == Color::Black ? 'B' : 'W' + end + + private def board_string(board) + String.build do |str| + (0...@size.value).each do |x| + (0...@size.value).each do |y| + color = @board[{x.to_i8, y.to_i8}]? + char = color ? color_char(color) : 'E' + str << char + end + end + end + end + + def encode + { @turn.to_s, @size.value, board_string(@board) } + end + end +end