Class: Tictactoe::Board

Inherits:
Object
  • Object
show all
Includes:
SquaresContainer
Defined in:
lib/tictactoe/tictactoe.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

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.



43
44
45
# File 'lib/tictactoe/tictactoe.rb', line 43

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

Class Method Details

.index_to_name(index) ⇒ Object

Receives the index, like 4 and returns “b2”



39
40
41
# File 'lib/tictactoe/tictactoe.rb', line 39

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



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

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

Instance Method Details

#[](*indices) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/tictactoe/tictactoe.rb', line 49

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”



59
60
61
62
# File 'lib/tictactoe/tictactoe.rb', line 59

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

#each_rowObject



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

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

#movesObject



75
76
77
78
79
80
81
# File 'lib/tictactoe/tictactoe.rb', line 75

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



104
105
106
# File 'lib/tictactoe/tictactoe.rb', line 104

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

#won?Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
# File 'lib/tictactoe/tictactoe.rb', line 83

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