Class: Upwords::Board

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = 10, max_height = 5) ⇒ Board

creates a 10 x 10 board



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/upwords/board.rb', line 7

def initialize(size=10, max_height=5)
  if !size.positive?
    raise ArgumentError, "Board size must be greater than zero!"
  else
    @size = size
    @max_height = max_height
    @min_word_length = 2
    @grid = Hash.new do |h, (row, col)|
      if row < 0 || col < 0 || num_rows <= row || num_columns <= col
        raise IllegalMove, "#{row}, #{col} is out of bounds!"
      else
        h[[row, col]] = []    # Initialize with empty array
      end
    end
  end
end

Instance Attribute Details

#max_heightObject

Returns the value of attribute max_height.



4
5
6
# File 'lib/upwords/board.rb', line 4

def max_height
  @max_height
end

#min_word_lengthObject

Returns the value of attribute min_word_length.



4
5
6
# File 'lib/upwords/board.rb', line 4

def min_word_length
  @min_word_length
end

#sizeObject

Returns the value of attribute size.



4
5
6
# File 'lib/upwords/board.rb', line 4

def size
  @size
end

Class Method Details

.build(moves, size = 10, max_height = 5) ⇒ Object



100
101
102
103
104
# File 'lib/upwords/board.rb', line 100

def self.build(moves, size=10, max_height=5)
  moves.reduce(Board.new(size, max_height)) do |board, move|
    board.play_move(move)
  end
end

Instance Method Details

#can_play_letter?(letter, row, col, raise_exception = false) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
69
70
71
# File 'lib/upwords/board.rb', line 61

def can_play_letter?(letter, row, col, raise_exception = false)
  if stack_height(row, col) == max_height
    raise IllegalMove, "You cannot stack any more letters on this space" if raise_exception
  elsif top_letter(row, col) == letter
    raise IllegalMove, "You cannot stack a letter on the same letter!" if raise_exception
  else 
    return true
  end

  return false
end

#coordinatesObject



96
97
98
# File 'lib/upwords/board.rb', line 96

def coordinates
  (0...num_rows).to_a.product((0...num_columns).to_a)
end

#empty?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/upwords/board.rb', line 24

def empty?
  @grid.empty? || @grid.each_key.all? {|k| @grid[k].empty?}
end

#middle_squareObject

Defines a 2x2 square in the middle of the board (in the case of the 10 x 10 board) The top left corner of the square is the initial cursor position The square itself defines the region where at least one of the first letters must be placed



43
44
45
46
47
# File 'lib/upwords/board.rb', line 43

def middle_square
  [1, 0].product([1, 0]).map do |r, c|
    [(num_rows) / 2 - r, (num_columns) / 2 - c]
  end
end

#nonempty_space?(row, col) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/upwords/board.rb', line 28

def nonempty_space?(row, col)
  @grid.key?([row, col]) && stack_height(row, col) > 0
end

#nonempty_spacesObject



92
93
94
# File 'lib/upwords/board.rb', line 92

def nonempty_spaces
  coordinates.select {|row, col| nonempty_space?(row, col)}.to_set
end

#num_columnsObject



36
37
38
# File 'lib/upwords/board.rb', line 36

def num_columns
  @size
end

#num_rowsObject



32
33
34
# File 'lib/upwords/board.rb', line 32

def num_rows
  @size
end

#play_letter(letter, row, col) ⇒ Object



73
74
75
76
77
78
# File 'lib/upwords/board.rb', line 73

def play_letter(letter, row, col)
  if can_play_letter?(letter, row, col, raise_exception = true)
    @grid[[row, col]] << letter
    return [[row, col], letter] # Return position after successfully playing a move
  end  
end

#play_move(move) ⇒ Object



53
54
55
# File 'lib/upwords/board.rb', line 53

def play_move(move)
  move.play(self)
end

#remove_top_letter(row, col) ⇒ Object



80
81
82
# File 'lib/upwords/board.rb', line 80

def remove_top_letter(row, col)
  @grid[[row, col]].pop
end

#stack_height(row, col) ⇒ Object



49
50
51
# File 'lib/upwords/board.rb', line 49

def stack_height(row, col)
  @grid[[row, col]].size
end

#top_letter(row, col) ⇒ Object



84
85
86
# File 'lib/upwords/board.rb', line 84

def top_letter(row, col)
  @grid[[row, col]].last
end

#undo_move(move) ⇒ Object



57
58
59
# File 'lib/upwords/board.rb', line 57

def undo_move(move)
  move.remove_from(self)
end

#word_positionsObject



88
89
90
# File 'lib/upwords/board.rb', line 88

def word_positions
  row_word_posns + column_word_posns
end