Module: Square

Defined in:
lib/ttt/interface/limelight/players/square.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.advance_turnObject



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

Returns:

  • (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_gameObject



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_turnObject



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_turnObject



59
60
61
# File 'lib/ttt/interface/limelight/players/square.rb', line 59

def current_turn
  @current_turn ||= 1
end

.gameObject



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

.resetObject



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

.squaresObject



67
68
69
# File 'lib/ttt/interface/limelight/players/square.rb', line 67

def squares
  @squares ||= {}
end

Instance Method Details

#castedObject



7
8
9
# File 'lib/ttt/interface/limelight/players/square.rb', line 7

def casted
  Square.register self
end

#game_overObject



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_winnerObject



41
42
43
# File 'lib/ttt/interface/limelight/players/square.rb', line 41

def mark_winner
  self.style.text_color = :green
end

#marked?Boolean

Returns:

  • (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

#positionObject



11
12
13
# File 'lib/ttt/interface/limelight/players/square.rb', line 11

def position
  id[/\d/].to_i
end

#unmarkObject



29
30
31
# File 'lib/ttt/interface/limelight/players/square.rb', line 29

def unmark
  self.text = String.new
end