Class: WorldState
- Inherits:
-
String
- Object
- String
- WorldState
- Defined in:
- lib/world_state.rb
Overview
class WorldState
Defined Under Namespace
Classes: InvalidDimension, InvalidPosition, InvalidState
Instance Attribute Summary collapse
-
#height ⇒ Integer
readonly
The heigh of the world.
-
#width ⇒ Integer
readonly
The width of the world.
Instance Method Summary collapse
-
#dead?(pos) ⇒ Boolean
true if the cell at position is dead and false if it is live.
-
#e(pos) ⇒ Integer
(also: #east)
The position east (right) of the position.
-
#initialize(width:, height:, init: nil) ⇒ WorldState
constructor
A new instance of WorldState.
-
#live?(pos) ⇒ Boolean
true if the cell at position is live and false if it is dead.
-
#n(pos) ⇒ Integer
(also: #north)
the position north (up) of the position.
-
#ne(pos) ⇒ Integer
(also: #northeast)
The position northest (up, right) of the position.
-
#neighborhood(pos) ⇒ Array<String>
The player chars or emptiness around the position (includes position).
-
#next(pos) ⇒ String
The player char of this position in the next state; space if dead.
-
#nw(pos) ⇒ Integer
(also: #northwest)
the position northwest (up, left) of the position.
-
#p(pos) ⇒ String
(also: #player)
the player character for a live cell, or space if dead cell.
-
#s(pos) ⇒ Integer
(also: #south)
The position south (down) of the position.
-
#se(pos) ⇒ Integer
(also: #southeast)
The position southeast (down, left) of the position.
-
#sw(pos) ⇒ Integer
(also: #southwest)
The position southeast (down, left) of the position.
-
#w(pos) ⇒ Integer
(also: #west)
The position west (left) of the position.
Constructor Details
#initialize(width:, height:, init: nil) ⇒ WorldState
Returns a new instance of WorldState
20 21 22 23 24 25 26 27 |
# File 'lib/world_state.rb', line 20 def initialize(width:, height:, init: nil) raise InvalidDimension if width < 1 || height < 1 raise InvalidState if init && width * height != init.length init ? super(init) : super(' ' * (width * height)) @width = width @height = height end |
Instance Attribute Details
#height ⇒ Integer (readonly)
Returns the heigh of the world
8 9 10 |
# File 'lib/world_state.rb', line 8 def height @height end |
#width ⇒ Integer (readonly)
Returns the width of the world
6 7 8 |
# File 'lib/world_state.rb', line 6 def width @width end |
Instance Method Details
#dead?(pos) ⇒ Boolean
true if the cell at position is dead and false if it is live
40 41 42 43 |
# File 'lib/world_state.rb', line 40 def dead?(pos) valid_position?(pos) self[pos] && self[pos] == ' ' end |
#e(pos) ⇒ Integer Also known as: east
The position east (right) of the position
96 97 98 99 100 |
# File 'lib/world_state.rb', line 96 def e(pos) valid_position?(pos) return pos + 1 - @width if (pos % @width) == @width - 1 # right edge case pos + 1 end |
#live?(pos) ⇒ Boolean
true if the cell at position is live and false if it is dead
32 33 34 35 |
# File 'lib/world_state.rb', line 32 def live?(pos) valid_position?(pos) self[pos] && self[pos] != ' ' end |
#n(pos) ⇒ Integer Also known as: north
the position north (up) of the position
67 68 69 70 71 |
# File 'lib/world_state.rb', line 67 def n(pos) valid_position?(pos) return pos + @width * (@height - 1) if (pos / @width).zero? # top edge case pos - @width end |
#ne(pos) ⇒ Integer Also known as: northeast
The position northest (up, right) of the position
77 78 79 80 |
# File 'lib/world_state.rb', line 77 def ne(pos) valid_position?(pos) e(n(pos)) end |
#neighborhood(pos) ⇒ Array<String>
The player chars or emptiness around the position (includes position)
139 140 141 142 143 144 145 146 147 |
# File 'lib/world_state.rb', line 139 def neighborhood(pos) valid_position?(pos) [ self[nw(pos)], self[n(pos)], self[ne(pos)], self[w(pos)], self[pos], self[e(pos)], self[sw(pos)], self[s(pos)], self[se(pos)] ] end |
#next(pos) ⇒ String
The player char of this position in the next state; space if dead
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/world_state.rb', line 153 def next(pos) valid_position?(pos) nhood = neighborhood(pos) return handle_dead(nhood) if p(pos) == ' ' # overpopulation return ' ' if nhood.count(' ') < 5 # underpopulation return ' ' if nhood.count(' ') > 6 return handle_two_neighbors(nhood) if nhood.count(' ') == 6 handle_three_neighbors(nhood) end |
#nw(pos) ⇒ Integer Also known as: northwest
the position northwest (up, left) of the position
58 59 60 61 |
# File 'lib/world_state.rb', line 58 def nw(pos) valid_position?(pos) w(n(pos)) end |
#p(pos) ⇒ String Also known as: player
the player character for a live cell, or space if dead cell
48 49 50 51 52 |
# File 'lib/world_state.rb', line 48 def p(pos) valid_position?(pos) return if self[pos] == ' ' self[pos] end |
#s(pos) ⇒ Integer Also known as: south
The position south (down) of the position
115 116 117 118 119 |
# File 'lib/world_state.rb', line 115 def s(pos) valid_position?(pos) return pos % @width if (pos / @width) == @height - 1 # bottom edge case pos + @width end |
#se(pos) ⇒ Integer Also known as: southeast
The position southeast (down, left) of the position
125 126 127 128 |
# File 'lib/world_state.rb', line 125 def se(pos) valid_position?(pos) e(s(pos)) end |
#sw(pos) ⇒ Integer Also known as: southwest
The position southeast (down, left) of the position
106 107 108 109 |
# File 'lib/world_state.rb', line 106 def sw(pos) valid_position?(pos) w(s(pos)) end |
#w(pos) ⇒ Integer Also known as: west
The position west (left) of the position
86 87 88 89 90 |
# File 'lib/world_state.rb', line 86 def w(pos) valid_position?(pos) return pos - 1 + @width if (pos % @width).zero? # left edge case pos - 1 end |