-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLifeGame_working.rb
151 lines (139 loc) · 3.58 KB
/
LifeGame_working.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
def ArrayDuplicator(array)
newarray = Array.new(array.length) {Array.new(array[0].length) {"*"}}
arraylength = array.length
(0...arraylength).each do |x|
(0...arraylength).each do |y|
newarray[x][y] = array[x][y]
end
end
return newarray
end
#Welcome system
system 'cls'
File.foreach("./LifeAscii.txt") { |line| puts line }
puts
puts "Welcome, how many iterations would you like to see?"
runtime = gets.chomp.to_i
puts "What shape would you like to see? \n
(a) Blinker
(b) r-pentimino
(c) Figure 8
(d) Surprise me"
shape = gets.chomp.downcase
#initiate process
system 'cls'
puts
# create grid
dead = 1
live = 0
dead_display = ". "
live_display = "0 "
grid = Array.new(20) {Array.new(20){dead}}
live_count = 0
display_size = grid.length-2
if shape == "a"
#blinker
grid[3][4] = live
grid[3][5] = live
grid[3][6] = live
elsif shape == "b"
#r-pentimino
grid[8][10] = live
grid[9][8] = live
grid[8][9] = live
grid[9][9] = live
grid[10][9] = live
elsif shape == "c"
#figure 8
grid[8][8] = live
grid[8][9] = live
grid[8][10] = live
grid[9][8] = live
grid[9][9] = live
grid[9][10] = live
grid[10][8] = live
grid[10][9] = live
grid[10][10] = live
grid[11][11] = live
grid[11][12] = live
grid[11][13] = live
grid[12][11] = live
grid[12][12] = live
grid[12][13] = live
grid[13][11] = live
grid[13][12] = live
grid[13][13] = live
elsif shape == "d"
#randomizer
(1..display_size).each do |x|
(1..display_size).each do |y|
grid[x][y] = live if rand(2) == 1
end
end
end
#set up duplicate grid for processing
working_grid = ArrayDuplicator(grid)
#Display initial positions
(1..display_size).each do |x|
(1..display_size).each do |y|
print live_display if grid[x][y] == live
print dead_display if grid[x][y] == dead
end
puts
end
#cycle through cells, determine what to do with them
#while loop includes whole game process and display
while runtime >= 0
system 'cls'
#for each cell, take count of how many live cells are in a 3x3 square centered on it
(1..display_size).each do |x|
live_count = 0
(1..display_size).each do |y|
live_count = 0
(x-1..x+1).each do |x_check|
(y-1..y+1).each do |y_check|
if (grid[x_check][y_check] == live)
live_count += 1
end
end
end
#Use count to determine what cell state should be in next iteration
if grid[x][y] == live
if (((live_count - 1) == 2) || ((live_count-1) == 3))
working_grid[x][y] = live
else
working_grid[x][y] = dead
end
elsif (grid[x][y] == dead)
if (live_count == 3)
working_grid[x][y] = live
else
working_grid[x][y] = dead
end
end
end
end
#print new iteration
(1..display_size).each do |x|
(1..display_size).each do |y|
print live_display if grid[x][y] == live
print dead_display if grid[x][y] == dead
end
puts
end
#Switch grid state to working_grid state
grid = ArrayDuplicator(working_grid)
puts
#pause for viewing
sleep 0.25
runtime -= 1
end
system("cls")
puts "Thank you for playing"
puts
puts "Brought to you by:
Gret
Justice
David
Yousef
James"