Class: Text2048::App

Inherits:
Object
  • Object
show all
Defined in:
lib/text2048/app.rb

Overview

Controller class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view = CursesView.new, board = Board.new, high_score = HighScore.new) ⇒ App

Returns a new instance of App.



13
14
15
16
17
18
19
# File 'lib/text2048/app.rb', line 13

def initialize(view = CursesView.new,
               board = Board.new,
               high_score = HighScore.new)
  @view = view
  @board = board
  @high_score = high_score
end

Instance Attribute Details

#boardObject (readonly)

Returns the value of attribute board.



10
11
12
# File 'lib/text2048/app.rb', line 10

def board
  @board
end

#viewObject (readonly)

Returns the value of attribute view.



11
12
13
# File 'lib/text2048/app.rb', line 11

def view
  @view
end

Instance Method Details

#demoObject



54
55
56
# File 'lib/text2048/app.rb', line 54

def demo
  input [:left, :right, :up, :down][rand(5)]
end

#generate(num_tiles = 1) ⇒ Object



32
33
34
35
36
# File 'lib/text2048/app.rb', line 32

def generate(num_tiles = 1)
  num_tiles.times { @board = @board.generate }
  @view.update(@board)
  @view.zoom_tiles(@board.generated_tiles)
end

#show_titleObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/text2048/app.rb', line 21

def show_title
  @view.high_score(@high_score)
  @view.message = 'PRESS ANY KEY TO START'
  @board = Board.new([[nil, nil, nil, nil],
                      [2, 0, 4, 8],
                      [nil, nil, nil, nil],
                      [nil, nil, nil, nil]])
  @view.update(@board)
  @view.wait_any_key
end

#stepObject



38
39
40
41
42
43
# File 'lib/text2048/app.rb', line 38

def step
  @view.win if @board.win?
  @view.game_over if @board.lose?
  input @view.command
  @view.high_score(@high_score)
end

#wait_any_key(seconds) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/text2048/app.rb', line 45

def wait_any_key(seconds)
  begin
    timeout(seconds) { @view.wait_any_key }
  rescue Timeout::Error
    return false
  end
  true
end