-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.rb
108 lines (79 loc) · 2.15 KB
/
game.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
require_relative 'cell.rb'
require_relative 'grid.rb'
class Game
attr_reader :grid, :rows, :columns
def initialize(rows, columns)
@rows = rows
@columns = columns
@grid = Grid.new(@rows, @columns)
populateGrid()
end
# Puts a cell in each grid space
def populateGrid
@grid.matrix.each_with_index do | row, i |
row.each_with_index do | col, j |
@grid.matrix[i][j] = Cell.new(i, j, false)
end
end
end
# Define which cells in Grid begin the game alive
# Param pattern is an array of arrays
def loadPattern(pattern)
pattern.each do | coordinate |
# Wrap out-of-bounds coordinates
x = @grid.toroidalRow(coordinate[0])
y = @grid.toroidalColumn(coordinate[1])
cell = getCell(x, y)
cell.state = true
end
end
# Return the cell at the specified row and column coordinates
def getCell(row, column)
return @grid.getElement(row, column)
end
# Iterate over each cell in grid and set to alive or dead conforming
# to game rules
def turn()
newGrid = Grid.new(grid.rows, grid.columns)
@grid.iterate do | cell |
nextState = deadOrAlive(cell)
newGrid.matrix[cell.row][cell.column] = Cell.new(cell.row, cell.column, nextState)
end
@grid = newGrid
end
# Set cell state based on number of alive neighbours
def deadOrAlive(cell)
livingNeighbours = 0
# Count number of living neighbours
neighbours(cell).each do | neighbour |
if neighbour.state
livingNeighbours += 1
end
end
# Dead cells come alive when exactly three adjacent neighbours are alive
# Alive cells die without two or three living adjacent neighbours
if cell.state == false && livingNeighbours == 3
return true
elsif cell.state == true && livingNeighbours.between?(2, 3)
return true
else
return false
end
end
# Return array of all neighbouring cells
def neighbours(cell)
row = cell.row
column = cell.column
neighbours = []
# Subsample eight cells adjacent to given cell
for i in ((row - 1)..(row + 1))
for j in ((column - 1)..(column + 1))
# Do not include given cell in sample
if !(i == row && j == column)
neighbours << getCell(i, j)
end
end
end
return neighbours
end
end