Class: Text2048::Board

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

Overview

2048 game board

Instance Attribute Summary collapse

Move collapse

Tiles collapse

Win/Lose collapse

Conversion collapse

Instance Method Summary collapse

Constructor Details

#initialize(tiles = Array.new(4, Array.new(4)), score = 0) ⇒ Board

Returns a new instance of Board.



14
15
16
17
# File 'lib/text2048/board.rb', line 14

def initialize(tiles = Array.new(4, Array.new(4)), score = 0)
  @all_tiles = tiles.hashinize
  @score = score
end

Instance Attribute Details

#scoreNumber (readonly)

Returns the current score

Returns:

  • (Number)

    returns the current score



12
13
14
# File 'lib/text2048/board.rb', line 12

def score
  @score
end

Instance Method Details

#downBoard

Move the tiles to the down.

Returns:

  • (Board)

    returns a new board



45
46
47
# File 'lib/text2048/board.rb', line 45

def down
  transpose { right }
end

#generateBoard

Generates a new tile

Returns:

  • (Board)

    a new board



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?

Parameters:

Returns:

  • (Boolean)

    generate a new tile?



71
72
73
# File 'lib/text2048/board.rb', line 71

def generate?(other)
  to_a != other.to_a
end

#generated_tilesArray

Returns the list of [row, col] of the newly generated tiles.

Returns:

  • (Array)

    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

#leftBoard

Move the tiles to the left.

Returns:

  • (Board)

    returns a new board



35
36
37
# File 'lib/text2048/board.rb', line 35

def left
  flip_horizontal { right }
end

#lose?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/text2048/board.rb', line 91

def lose?
  right.left.up.down.tiles.size == 4 * 4
end

#merged_tilesArray

Returns the list of [row, col] of the merged tiles.

Returns:

  • (Array)

    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

#rightBoard

Move the tiles to the right.

Returns:

  • (Board)

    returns a new board



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

#tilesArray<Tile>

Returns the list of tiles.

Returns:



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

def tiles
  @all_tiles.select { |_key, each| each.to_i > 0 }
end

#to_aArray

Returns a 2D array of tiles.

Returns:

  • (Array)

    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

#upBoard

Move the tiles to the up.

Returns:

  • (Board)

    returns a new board



40
41
42
# File 'lib/text2048/board.rb', line 40

def up
  transpose { left }
end

#win?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/text2048/board.rb', line 87

def win?
  @all_tiles.any? { |_key, value| value.to_i >= 2048 }
end