Class: Tictactoe::Board

Inherits:
Object
  • Object
show all
Includes:
SquaresContainer
Defined in:
lib/tictactoe/board.rb

Defined Under Namespace

Classes: Row

Constant Summary collapse

MOVES =
%w{a1    a2   a3   b1   b2   b3   c1   c2   c3}
INDICES =

Define constant INDICES

Hash.new { |h, k| h[k] = MOVES.find_index(k) }
HORIZONTALS =
[ [0, 1, 2], [3, 4, 5], [6, 7, 8] ]
COLUMNS =
[ [0, 3, 6], [1, 4, 7], [2, 5, 8] ]
DIAGONALS =
[ [0, 4, 8], [2, 4, 6] ]
ROWS =
HORIZONTALS + COLUMNS + DIAGONALS
BOARD =
<<EOS

  +---+---+---+
a | 0 | 1 | 2 |
  +---+---+---+
b | 3 | 4 | 5 |
  +---+---+---+
c | 6 | 7 | 8 |
  +---+---+---+
    1   2   3

EOS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SquaresContainer

#blanks, #os, #xs

Constructor Details

#initialize(squares) ⇒ Board

Returns a new instance of Board.



37
38
39
# File 'lib/tictactoe/board.rb', line 37

def initialize( squares )
  @squares = squares # An array of Strings: [ " ", " ", " ", " ", "X", " ", " ", " ", "O"]
end

Instance Attribute Details

#squaresObject (readonly)

Returns the value of attribute squares.



11
12
13
# File 'lib/tictactoe/board.rb', line 11

def squares
  @squares
end

Class Method Details

.index_to_name(index) ⇒ Object

Receives the index, like 4 and returns “b2”



33
34
35
# File 'lib/tictactoe/board.rb', line 33

def self.index_to_name( index ) # Receives the index, like 4 and returns "b2"
  MOVES[index]
end

.name_to_index(name) ⇒ Object

Receives “b2” and returns 4



29
30
31
# File 'lib/tictactoe/board.rb', line 29

def self.name_to_index( name )# Receives "b2" and returns 4
  INDICES[name]
end

Instance Method Details

#[](*indices) ⇒ Object



43
44
45
46
47
48
49
50
51
# File 'lib/tictactoe/board.rb', line 43

def []( *indices )
  if indices.size == 2                  # board[1,2] is @squares[7]
    super indices[0] + indices[1] * 3   # calls SquaresContainer [] method
  elsif indices[0].is_a? Fixnum         # board[7]
    super indices[0]
  else                                  # board["b2"]
    super Board.name_to_index(indices[0].to_s)
  end
end

#[]=(indice, value) ⇒ Object

board = “X”



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

def []=(indice, value)                  # board["b2"] = "X"
  m = Board.name_to_index(indice)
  @squares[m] = value
end

#each_rowObject



63
64
65
66
67
# File 'lib/tictactoe/board.rb', line 63

def each_row
  ROWS.each do |e|
    yield Row.new(@squares.values_at(*e), e)
  end
end

#movesObject



69
70
71
72
73
74
75
# File 'lib/tictactoe/board.rb', line 69

def moves
  moves = [ ]
  @squares.each_with_index do |s, i|
    moves << Board.index_to_name(i) if s == " "
  end
  moves # returns the set of feasible moves [ "b3", "c2", ... ]
end

#to_sObject



98
99
100
# File 'lib/tictactoe/board.rb', line 98

def to_s
  BOARD.gsub(/(\d)(?= \|)/) { |i| @squares[i.to_i] }
end

#won?Boolean

Returns:

  • (Boolean)


77
78
79
80
81
82
83
84
# File 'lib/tictactoe/board.rb', line 77

def won?
  each_row do |row|
    return "X" if row.xs == 3 # "X" wins
    return "O" if row.os == 3 # "O" wins
  end
  return " " if blanks == 0   # tie
  false
end