Class: Demos::TicTacToe::App

Inherits:
Hokusai::Block show all
Defined in:
ui/examples/tic_tac_toe.rb

Instance Attribute Summary collapse

Attributes inherited from Hokusai::Block

#node, #provides, #publisher

Instance Method Summary collapse

Methods inherited from Hokusai::Block

#children, #children?, compile, computed, computed!, #draw, #draw_with, #dump, #emit, inject, inject!, #method_missing, mount, #on_resize, provide, provides, style, styles_get, template, template_from_file, template_get, #update, use, uses

Constructor Details

#initialize(**args) ⇒ App

Returns a new instance of App.



142
143
144
145
146
147
148
# File 'ui/examples/tic_tac_toe.rb', line 142

def initialize(**args)
  @current_player = Player::X
  @count = 0
  super

  restart
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Hokusai::Block

Instance Attribute Details

#boardObject

Returns the value of attribute board.



140
141
142
# File 'ui/examples/tic_tac_toe.rb', line 140

def board
  @board
end

#countObject

Returns the value of attribute count.



140
141
142
# File 'ui/examples/tic_tac_toe.rb', line 140

def count
  @count
end

#current_playerObject

Returns the value of attribute current_player.



140
141
142
# File 'ui/examples/tic_tac_toe.rb', line 140

def current_player
  @current_player
end

Instance Method Details

#check_winObject



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'ui/examples/tic_tac_toe.rb', line 208

def check_win
  [Player::X, Player::O].each do |player|
    (0...3).step do |i|
      if board[i][0] == player && board[i][1] == player && board[i][2] == player
        return player
      end

      if board[0][i] == player && board[1][i] == player && board[2][i] == player
        return player
      end

      diagonal_left = board[0][0] == player && board[1][1] == player && board[2][2] == player
      diagonal_right = board[2][0] == player && board[1][1] == player && board[0][2] == player

      if diagonal_left || diagonal_right
        return player
      end
    end
  end

  nil
end

#has_winnerObject



185
186
187
# File 'ui/examples/tic_tac_toe.rb', line 185

def has_winner
  count >= 9 || !check_win.nil?
end

#key(index) ⇒ Object



159
160
161
# File 'ui/examples/tic_tac_toe.rb', line 159

def key(index)
  "row_#{index}"
end

#render(canvas) ⇒ Object



231
232
233
234
235
236
237
238
# File 'ui/examples/tic_tac_toe.rb', line 231

def render(canvas)
  if Hokusai.can_render(canvas)

    emit("width", canvas.width)

    yield canvas
  end
end

#restart(*args) ⇒ Object



150
151
152
153
154
155
156
157
# File 'ui/examples/tic_tac_toe.rb', line 150

def restart(*args)
  @board = [
    [Player::None, Player::None, Player::None],
    [Player::None, Player::None, Player::None],
    [Player::None, Player::None, Player::None]
  ]
  self.count = 0
end

#update_game(position) ⇒ Object



193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'ui/examples/tic_tac_toe.rb', line 193

def update_game(position)
  row, col = position

  self.count += 1

  case current_player
  when Player::X
    board[row][col] = Player::X unless board[row][col] != Player::None
    self.current_player = Player::O
  when Player::O
    board[row][col] = Player::O unless board[row][col] != Player::None
    self.current_player = Player::X
  end
end

#winnerObject



189
190
191
# File 'ui/examples/tic_tac_toe.rb', line 189

def winner
  check_win
end

#winner_imageObject



163
164
165
166
167
168
169
170
171
172
# File 'ui/examples/tic_tac_toe.rb', line 163

def winner_image
  case winner
  when Player::X
    return "#{__dir__}/assets/science-troll.png"
  when Player::O
    return "#{__dir__}/assets/football-troll.png"
  else
    return "#{__dir__}/assets/baby_sean.png"
  end
end

#winner_textObject



174
175
176
177
178
179
180
181
182
183
# File 'ui/examples/tic_tac_toe.rb', line 174

def winner_text
  case winner
  when Player::X
    "Player X won!"
  when Player::O
    "Player O won!"
  else
    "No winner"
  end
end