Class: Board
- Inherits:
-
Object
- Object
- Board
- Defined in:
- lib/ttt_malisa/board.rb
Instance Attribute Summary collapse
-
#squares ⇒ Object
readonly
Returns the value of attribute squares.
Instance Method Summary collapse
- #available_moves ⇒ Object
- #display ⇒ Object
- #full? ⇒ Boolean
-
#initialize(squares = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']) ⇒ Board
constructor
A new instance of Board.
- #mark(index, mark = 'X') ⇒ Object
- #position_taken?(index) ⇒ Boolean
- #valid_move?(index) ⇒ Boolean
Constructor Details
#initialize(squares = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']) ⇒ Board
Returns a new instance of Board.
4 5 6 |
# File 'lib/ttt_malisa/board.rb', line 4 def initialize(squares = [' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']) @squares = squares end |
Instance Attribute Details
#squares ⇒ Object (readonly)
Returns the value of attribute squares.
2 3 4 |
# File 'lib/ttt_malisa/board.rb', line 2 def squares @squares end |
Instance Method Details
#available_moves ⇒ Object
18 19 20 21 22 |
# File 'lib/ttt_malisa/board.rb', line 18 def available_moves open_squares = [] @squares.each_with_index {|value, index| open_squares << index if value == ' '} open_squares end |
#display ⇒ Object
8 9 10 11 12 13 14 15 16 |
# File 'lib/ttt_malisa/board.rb', line 8 def display <<-HEREDOC #{@squares[0]} | #{@squares[1]} | #{@squares[2]} ----------- #{@squares[3]} | #{@squares[4]} | #{@squares[5]} ----------- #{@squares[6]} | #{@squares[7]} | #{@squares[8]} HEREDOC end |
#full? ⇒ Boolean
36 37 38 39 40 |
# File 'lib/ttt_malisa/board.rb', line 36 def full? @squares.all? do |index| index != ' ' end end |
#mark(index, mark = 'X') ⇒ Object
32 33 34 |
# File 'lib/ttt_malisa/board.rb', line 32 def mark(index, mark = 'X') @squares[index] = mark end |
#position_taken?(index) ⇒ Boolean
24 25 26 |
# File 'lib/ttt_malisa/board.rb', line 24 def position_taken?(index) @squares[index] != ' ' end |
#valid_move?(index) ⇒ Boolean
28 29 30 |
# File 'lib/ttt_malisa/board.rb', line 28 def valid_move?(index) index.between?(0, 8) && !position_taken?(index) end |