Class: Text2048::Board
- Inherits:
-
Object
- Object
- Text2048::Board
- Defined in:
- lib/text2048/board.rb
Overview
2048 game board
Instance Attribute Summary collapse
-
#score ⇒ Number
readonly
Returns the current score.
Move collapse
-
#down ⇒ Board
Move the tiles to the down.
-
#left ⇒ Board
Move the tiles to the left.
-
#right ⇒ Board
Move the tiles to the right.
-
#up ⇒ Board
Move the tiles to the up.
Tiles collapse
-
#generate ⇒ Board
Generates a new tile.
-
#generate?(other) ⇒ Boolean
Need to generate a new tile?.
-
#generated_tiles ⇒ Array
The list of [row, col] of the newly generated tiles.
-
#merged_tiles ⇒ Array
The list of [row, col] of the merged tiles.
-
#tiles ⇒ Array<Tile>
The list of tiles.
Win/Lose collapse
Conversion collapse
-
#to_a ⇒ Array
A 2D array of tiles.
Instance Method Summary collapse
-
#initialize(tiles = Array.new(4, Array.new(4)), score = 0) ⇒ Board
constructor
A new instance of Board.
Constructor Details
Instance Attribute Details
#score ⇒ Number (readonly)
Returns the current score
12 13 14 |
# File 'lib/text2048/board.rb', line 12 def score @score end |
Instance Method Details
#down ⇒ Board
Move the tiles to the down.
45 46 47 |
# File 'lib/text2048/board.rb', line 45 def down transpose { right } end |
#generate ⇒ Board
Generates a new tile
77 78 79 80 81 |
# File 'lib/text2048/board.rb', line 77 def generate tiles = @all_tiles.dup tiles[sample_empty_tile] = Tile.new(rand < 0.9 ? 2 : 4, :generated) new_board(tiles, @score) end |
#generate?(other) ⇒ Boolean
Need to generate a new tile?
71 72 73 |
# File 'lib/text2048/board.rb', line 71 def generate?(other) to_a != other.to_a end |
#generated_tiles ⇒ Array
Returns the list of [row, col] of the newly generated tiles.
64 65 66 |
# File 'lib/text2048/board.rb', line 64 def generated_tiles find_tiles :generated end |
#left ⇒ Board
Move the tiles to the left.
35 36 37 |
# File 'lib/text2048/board.rb', line 35 def left flip_horizontal { right } end |
#lose? ⇒ Boolean
91 92 93 |
# File 'lib/text2048/board.rb', line 91 def lose? right.left.up.down.tiles.size == 4 * 4 end |
#merged_tiles ⇒ Array
Returns the list of [row, col] of the merged tiles.
59 60 61 |
# File 'lib/text2048/board.rb', line 59 def merged_tiles find_tiles :merged end |
#right ⇒ Board
Move the tiles to the right.
26 27 28 29 30 31 32 |
# File 'lib/text2048/board.rb', line 26 def right board, score = to_a.reduce([[], @score]) do |(rows, sc), each| row, row_sc = each.right [rows << row, sc + row_sc] end new_board(board, score) end |
#tiles ⇒ Array<Tile>
Returns the list of tiles.
54 55 56 |
# File 'lib/text2048/board.rb', line 54 def tiles @all_tiles.select { |_key, each| each.to_i > 0 } end |
#to_a ⇒ Array
Returns a 2D array of tiles.
100 101 102 |
# File 'lib/text2048/board.rb', line 100 def to_a [0, 1, 2, 3].map { |each| row(each) } end |
#up ⇒ Board
Move the tiles to the up.
40 41 42 |
# File 'lib/text2048/board.rb', line 40 def up transpose { left } end |
#win? ⇒ Boolean
87 88 89 |
# File 'lib/text2048/board.rb', line 87 def win? @all_tiles.any? { |_key, value| value.to_i >= 2048 } end |