Class: Board

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

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#squaresObject (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_movesObject



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

#displayObject



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

Returns:

  • (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

Returns:

  • (Boolean)


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

def position_taken?(index)
  @squares[index] != ' '
end

#valid_move?(index) ⇒ Boolean

Returns:

  • (Boolean)


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

def valid_move?(index)
  index.between?(0, 8) && !position_taken?(index)
end