Class: XO::Grid
- Inherits:
-
Object
- Object
- XO::Grid
- Defined in:
- lib/xo/grid.rb
Overview
Direct Known Subclasses
Constant Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
- #[](r, c) ⇒ Object
- #[]=(r, c, k) ⇒ Object
- #clear ⇒ Object
-
#each ⇒ Object
Iterates over all the positions of this grid from left to right and top to bottom.
-
#each_open ⇒ Object
Iterates over all the open positions of this grid from left to right and top to bottom.
- #empty? ⇒ Boolean
- #full? ⇒ Boolean
-
#initialize(g = '') ⇒ Grid
constructor
A new instance of Grid.
- #initialize_copy(orig) ⇒ Object
-
#inspect ⇒ Object
Returns a string representation of this grid which can be useful for debugging.
- #open?(r, c) ⇒ Boolean
-
#to_s ⇒ Object
Returns a string representation of this grid which can be useful for display.
Constructor Details
#initialize(g = '') ⇒ Grid
Returns a new instance of Grid.
39 40 41 |
# File 'lib/xo/grid.rb', line 39 def initialize(g = '') @grid = from_string(g) end |
Class Method Details
.contains?(r, c) ⇒ Boolean
27 28 29 |
# File 'lib/xo/grid.rb', line 27 def self.contains?(r, c) r.between?(1, ROWS) && c.between?(1, COLS) end |
Instance Method Details
#[](r, c) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/xo/grid.rb', line 63 def [](r, c) if self.class.contains?(r, c) grid[idx(r, c)] else raise IndexError, "position (#{r}, #{c}) is off the grid" end end |
#[]=(r, c, k) ⇒ Object
55 56 57 58 59 60 61 |
# File 'lib/xo/grid.rb', line 55 def []=(r, c, k) if self.class.contains?(r, c) grid[idx(r, c)] = normalize(k) else raise IndexError, "position (#{r}, #{c}) is off the grid" end end |
#clear ⇒ Object
75 76 77 |
# File 'lib/xo/grid.rb', line 75 def clear grid.fill(EMPTY) end |
#each ⇒ Object
Iterates over all the positions of this grid from left to right and top to bottom.
86 87 88 89 90 91 92 |
# File 'lib/xo/grid.rb', line 86 def each (1..ROWS).each do |r| (1..COLS).each do |c| yield(r, c, self[r, c]) end end end |
#each_open ⇒ Object
Iterates over all the open positions of this grid from left to right and top to bottom.
105 106 107 |
# File 'lib/xo/grid.rb', line 105 def each_open self.each { |r, c, _| yield(r, c) if open?(r, c) } end |
#empty? ⇒ Boolean
47 48 49 |
# File 'lib/xo/grid.rb', line 47 def empty? grid.all? { |k| !self.class.is_token?(k) } end |
#full? ⇒ Boolean
51 52 53 |
# File 'lib/xo/grid.rb', line 51 def full? grid.all? { |k| self.class.is_token?(k) } end |
#initialize_copy(orig) ⇒ Object
43 44 45 |
# File 'lib/xo/grid.rb', line 43 def initialize_copy(orig) @grid = orig.instance_variable_get(:@grid).dup end |
#inspect ⇒ Object
Returns a string representation of this grid which can be useful for debugging.
110 111 112 |
# File 'lib/xo/grid.rb', line 110 def inspect grid.map { |k| t(k) }.join end |
#open?(r, c) ⇒ Boolean
71 72 73 |
# File 'lib/xo/grid.rb', line 71 def open?(r, c) !self.class.is_token?(self[r, c]) end |
#to_s ⇒ Object
Returns a string representation of this grid which can be useful for display.
115 116 117 118 119 120 121 122 123 |
# File 'lib/xo/grid.rb', line 115 def to_s g = grid.map { |k| t(k) } [" #{g[0]} | #{g[1]} | #{g[2]} ", "---+---+---", " #{g[3]} | #{g[4]} | #{g[5]} ", "---+---+---", " #{g[6]} | #{g[7]} | #{g[8]} "].join("\n") end |