Created
March 24, 2011 22:40
-
-
Save ianbishop/886042 to your computer and use it in GitHub Desktop.
Revised GameGUI to show new send/respond_to? support
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 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