Module: Square
- Defined in:
- lib/ttt/interface/limelight/players/square.rb
Class Method Summary collapse
- .advance_turn ⇒ Object
- .can_click?(position) ⇒ Boolean
- .check_for_end_of_game ⇒ Object
- .clicked(position) ⇒ Object
- .computer_takes_turn ⇒ Object
- .current_turn ⇒ Object
- .game ⇒ Object
- .mark(position) ⇒ Object
- .register(square) ⇒ Object
- .reset ⇒ Object
- .squares ⇒ Object
Instance Method Summary collapse
- #casted ⇒ Object
- #game_over ⇒ Object
- #mark(player) ⇒ Object
- #mark_winner ⇒ Object
- #marked? ⇒ Boolean
- #mouse_clicked(event) ⇒ Object
- #position ⇒ Object
- #unmark ⇒ Object
Class Method Details
.advance_turn ⇒ Object
63 64 65 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 63 def advance_turn @current_turn = (@current_turn % 2) + 1 end |
.can_click?(position) ⇒ Boolean
87 88 89 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 87 def can_click?(position) squares[position] && !squares[position].marked? && !game.over? end |
.check_for_end_of_game ⇒ Object
98 99 100 101 102 103 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 98 def check_for_end_of_game if game.over? squares.each_value { |square| square.game_over } game.winning_positions.each { |position| squares[position].mark_winner } end end |
.clicked(position) ⇒ Object
75 76 77 78 79 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 75 def clicked(position) return unless can_click? position mark position computer_takes_turn end |
.computer_takes_turn ⇒ Object
81 82 83 84 85 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 81 def computer_takes_turn position = TTT::ComputerPlayer.new(game).best_move return unless can_click? position mark position end |
.current_turn ⇒ Object
59 60 61 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 59 def current_turn @current_turn ||= 1 end |
.game ⇒ Object
55 56 57 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 55 def game @game ||= TTT::Game.new end |
.mark(position) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 91 def mark(position) game.mark position squares[position].mark current_turn advance_turn check_for_end_of_game end |
.register(square) ⇒ Object
71 72 73 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 71 def register(square) squares[square.position] = square end |
.reset ⇒ Object
50 51 52 53 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 50 def reset @game = @current_turn = nil squares.each_value { |square| square.unmark } end |
.squares ⇒ Object
67 68 69 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 67 def squares @squares ||= {} end |
Instance Method Details
#casted ⇒ Object
7 8 9 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 7 def casted Square.register self end |
#game_over ⇒ Object
37 38 39 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 37 def game_over self.style.text_color = :gray end |
#mark(player) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 19 def mark(player) if player == 1 self.text = 'X' self.style.text_color = :red else self.text = 'O' self.style.text_color = :blue end end |
#mark_winner ⇒ Object
41 42 43 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 41 def mark_winner self.style.text_color = :green end |
#marked? ⇒ Boolean
33 34 35 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 33 def marked? !text.empty? end |
#mouse_clicked(event) ⇒ Object
15 16 17 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 15 def mouse_clicked(event) Square.clicked(position) end |
#position ⇒ Object
11 12 13 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 11 def position id[/\d/].to_i end |
#unmark ⇒ Object
29 30 31 |
# File 'lib/ttt/interface/limelight/players/square.rb', line 29 def unmark self.text = String.new end |