Class: Text2048::App
- Inherits:
-
Object
- Object
- Text2048::App
- Defined in:
- lib/text2048/app.rb
Overview
Controller class.
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
-
#view ⇒ Object
readonly
Returns the value of attribute view.
Instance Method Summary collapse
- #demo ⇒ Object
- #generate(num_tiles = 1) ⇒ Object
-
#initialize(view = CursesView.new, board = Board.new, high_score = HighScore.new) ⇒ App
constructor
A new instance of App.
- #show_title ⇒ Object
- #step ⇒ Object
- #wait_any_key(seconds) ⇒ Object
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
#board ⇒ Object (readonly)
Returns the value of attribute board.
10 11 12 |
# File 'lib/text2048/app.rb', line 10 def board @board end |
#view ⇒ Object (readonly)
Returns the value of attribute view.
11 12 13 |
# File 'lib/text2048/app.rb', line 11 def view @view end |
Instance Method Details
#demo ⇒ Object
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_title ⇒ Object
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. = '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 |
#step ⇒ Object
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 |