Skip to content

Instantly share code, notes, and snippets.

@ianbishop
Created March 24, 2011 22:40
Show Gist options
  • Select an option

  • Save ianbishop/886042 to your computer and use it in GitHub Desktop.

Select an option

Save ianbishop/886042 to your computer and use it in GitHub Desktop.
Revised GameGUI to show new send/respond_to? support
class GameGUI
def initialize(repeater)
@repeater = repeater
@repeater.subscribe(self)
end
# When a player (color) clicks on board location
# x,y we attempt a move by broadcasting it
def on_click(color, x, y)
# Our new messages have options hashes that allow us to include
# any information for a given command. Also, note that commands are now
# symbolic rather than strings.
message = {
command: :attempt_move,
options: {
color: color,
x: x,
y: y
}
}
@repeater.broadcast(message)
end
# Each message that the class responds to is simply defined
# as a method which contains the block to handle that event
def accept_move(opts)
draw_move(opts[:color], opts[:x], opts[:y])
end
def illegal_move(opts)
alert_player("Illegal move", opts[:color])
end
# This is where the real draw code would go!
def draw_move(color, x, y)
puts "#{self.class} drew #{color} at (#{x},#{y})"
end
def alert_player(message, color)
puts "#{self.class} ALERT #{color}: #{message}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment