Class: XO::AI::GeometricGrid

Inherits:
Grid
  • Object
show all
Defined in:
lib/xo/ai/geometric_grid.rb

Overview

A geometric grid is a Tic-tac-toe grid (Grid) with the added benefit that various geometric transformations (rotation and reflection) can be applied. It defines a concept of equivalence under these transformations. Geometric grids can be checked for equality and they define a hash function that allows them to be used as keys within a Hash.

Constant Summary

Constants inherited from Grid

Grid::COLS, Grid::EMPTY, Grid::N, Grid::O, Grid::ROWS, Grid::X

Instance Method Summary collapse

Methods inherited from Grid

#[], #[]=, #clear, contains?, #each, #each_open, #empty?, #full?, #initialize, #initialize_copy, #inspect, is_token?, #open?, other_token, #to_s

Constructor Details

This class inherits a constructor from XO::Grid

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Redefines equality for a geometric grid.

Two geometric grids are equal iff they are equivalent.

Returns:

  • (Boolean)


76
77
78
# File 'lib/xo/ai/geometric_grid.rb', line 76

def ==(other)
  equivalent?(other)
end

#equivalent?(other) ⇒ Boolean

Determines whether or not this geometric grid is equivalent to the given geometric grid.

Two geometric grids are considered equivalent iff one is a rotation or reflection of the other.

Parameters:

Returns:

  • (Boolean)


65
66
67
68
69
# File 'lib/xo/ai/geometric_grid.rb', line 65

def equivalent?(other)
  return false unless other.instance_of?(self.class)

  transformations.any? { |grid| other.same?(grid) }
end

#hashInteger

Required if you want to be able to use a geometric grid as a key in a Hash.

Equivalent grids must have the same hash.

Returns:

  • (Integer)


86
87
88
# File 'lib/xo/ai/geometric_grid.rb', line 86

def hash
  transformations.map(&:inspect).sort.uniq.join.hash
end

#reflectGeometricGrid

Reflect the geometric grid in its vertical axis.

 0 | 1 | 2          2 | 1 | 0
---+---+---        ---+---+---
 3 | 4 | 5    =>    5 | 4 | 3
---+---+---        ---+---+---
 6 | 7 | 8          8 | 7 | 6

Returns:



40
41
42
43
44
45
46
# File 'lib/xo/ai/geometric_grid.rb', line 40

def reflect
  GeometricGrid.new(
    "#{self[1, 3]}#{self[1, 2]}#{self[1, 1]}" +
    "#{self[2, 3]}#{self[2, 2]}#{self[2, 1]}" +
    "#{self[3, 3]}#{self[3, 2]}#{self[3, 1]}"
  )
end

#rotateGeometricGrid

Rotate the geometric grid clockwise by 90 degrees.

 0 | 1 | 2          6 | 3 | 0
---+---+---        ---+---+---
 3 | 4 | 5    =>    7 | 4 | 1
---+---+---        ---+---+---
 6 | 7 | 8          8 | 5 | 2

Returns:



23
24
25
26
27
28
29
# File 'lib/xo/ai/geometric_grid.rb', line 23

def rotate
  GeometricGrid.new(
    "#{self[3, 1]}#{self[2, 1]}#{self[1, 1]}" +
    "#{self[3, 2]}#{self[2, 2]}#{self[1, 2]}" +
    "#{self[3, 3]}#{self[2, 3]}#{self[1, 3]}"
  )
end

#same?(other) ⇒ Boolean

Determines whether or not this geometric grid has the same occupied positions as the given geometric grid.

Parameters:

Returns:

  • (Boolean)


53
54
55
# File 'lib/xo/ai/geometric_grid.rb', line 53

def same?(other)
  self.inspect == other.inspect
end