Created
July 11, 2013 14:10
-
-
Save ecomba/5975778 to your computer and use it in GitHub Desktop.
A little gist showing how you could bind an app with an adapter to a web controller.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Board | |
def add_ship ship | |
ship | |
end | |
def shoot(coordinates, player) | |
raise "Not your turn" if player == 2 | |
'BANG' | |
end | |
end | |
class GameInteractor | |
def initialize | |
@board = Board.new | |
end | |
def add_ship params, listener | |
ship = @board.add_ship params[:ship][:name] | |
listener.ship_added(ship) | |
end | |
def shoot coordinates, player, listener | |
begin | |
@board.shoot(coordinates, player) | |
listener.shot_at coordinates | |
rescue Exception => e | |
listener.not_his_turn player, e.message | |
end | |
end | |
end | |
class Sinatra | |
def initialize | |
@game = GameInteractor.new | |
end | |
def add_ship params | |
@game.add_ship(params, self) | |
end | |
def shoot coordinates, player | |
@game.shoot coordinates, player, self | |
end | |
def ship_added ship | |
"That's awesome, thank you!" | |
end | |
def shot_at coordinates | |
"You have shot at #{coordinates}" | |
end | |
def not_his_turn player, message | |
"Hey #{player}! #{message}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment