-
Notifications
You must be signed in to change notification settings - Fork 0
/
macro.vb
194 lines (178 loc) · 5.7 KB
/
macro.vb
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
Option Base 0
Type Color
Red As Integer
Green As Integer
Blue As Integer
End Type
Global playing As Boolean
Global generation1(100, 100) As Boolean
Global generation2(100, 100) As Boolean
Global aliveCells As Integer
Global deadCells As Integer
Global generation As Integer
Global aliveCellColor As Color
Global deadCellColor As Color
Global zoneDeJeu As Range
Global generationCell As Range
Global aliveCellsCell As Range
Global deadCellsCell As Range
Sub main()
Set zoneDeJeu = Range("F1:DA100")
Set generationCell = Range("D16")
Set aliveCellsCell = Range("D19")
Set deadCellsCell = Range("D21")
aliveCellColor.Red = 119
aliveCellColor.Green = 117
aliveCellColor.Blue = 255
deadCellColor.Red = 215
deadCellColor.Green = 214
deadCellColor.Blue = 255
If playing = True Then
Call changeGeneration
Call updateTable
End If
End Sub
Sub generateRandom()
Dim randomizedNumber As Integer
For y = 0 To UBound(generation1, 2)
For x = 0 To UBound(generation1, 1)
randomizedNumber = Int(5 * Rnd) + 1
If randomizedNumber = 1 Then
generation1(x, y) = True
generation2(x, y) = False
Else
generation1(x, y) = False
generation2(x, y) = False
End If
Next
Next
Call updateTable
End Sub
Sub generateEmpty()
For y = 0 To UBound(generation1, 2)
For x = 0 To UBound(generation1, 1)
generation1(x, y) = False
generation2(x, y) = False
Next
Next
Call updateTable
End Sub
Sub changeGeneration()
Dim coords(2) As Integer
For y = 0 To UBound(generation1, 2)
For x = 0 To UBound(generation1, 1)
coords(0) = x
coords(1) = y
generation2(x, y) = checkAlive(coords)
Next
Next
For y = 0 To UBound(generation1, 2)
For x = 0 To UBound(generation1, 1)
generation1(x, y) = generation2(x, y)
generation2(x, y) = False
Next
Next
generation = generation + 1
Call updateTable
End Sub
Function checkAlive(coords() As Integer) As Boolean
Dim alive As Boolean
Dim neighboursAlive As Integer
If coords(0) > 0 Then
If coords(1) > 0 Then
If generation1(coords(0) - 1, coords(1) - 1) = True Then
neighboursAlive = neighboursAlive + 1
End If
End If
If coords(1) < UBound(generation1, 2) - 1 Then
If generation1(coords(0) - 1, coords(1) + 1) = True Then
neighboursAlive = neighboursAlive + 1
End If
End If
If generation1(coords(0) - 1, coords(1)) = True Then
neighboursAlive = neighboursAlive + 1
End If
End If
If coords(0) < UBound(generation1, 1) - 1 Then
If coords(1) > 0 Then
If generation1(coords(0) + 1, coords(1) - 1) = True Then
neighboursAlive = neighboursAlive + 1
End If
End If
If coords(1) < UBound(generation1, 2) - 1 Then
If generation1(coords(0) + 1, coords(1) + 1) = True Then
neighboursAlive = neighboursAlive + 1
End If
End If
If generation1(coords(0) + 1, coords(1)) = True Then
neighboursAlive = neighboursAlive + 1
End If
End If
If coords(1) > 0 Then
If generation1(coords(0), coords(1) - 1) = True Then
neighboursAlive = neighboursAlive + 1
End If
End If
If coords(1) < UBound(generation1, 2) - 1 Then
If generation1(coords(0), coords(1) + 1) = True Then
neighboursAlive = neighboursAlive + 1
End If
End If
If neighboursAlive = 3 Then
alive = True
ElseIf neighboursAlive = 2 Then
alive = generation1(coords(0), coords(1))
ElseIf neighboursAlive < 2 Or neighboursAlive > 3 Then
alive = False
End If
checkAlive = alive
Exit Function
End Function
Sub updateTable()
aliveCells = 0
Set zoneDeJeu = Range("F1:DA100")
Set generationCell = Range("D10")
Set aliveCellsCell = Range("D13")
Set deadCellsCell = Range("D16")
aliveCellColor.Red = 119
aliveCellColor.Green = 117
aliveCellColor.Blue = 255
deadCellColor.Red = 215
deadCellColor.Green = 214
deadCellColor.Blue = 255
For Each c In zoneDeJeu
If generation1(c.Row, c.Column - 5) = True Then
c.Value = 1
'c.Select
'With selection.Interior
' .Color = RGB(aliveCellColor.Red, aliveCellColor.Green, aliveCellColor.Blue)
'End With
'With selection.Font
' .Color = RGB(255, 255, 255)
'End With
Else
c.Value = 0
'c.Select
'With selection.Interior
' .Color = RGB(deadCellColor.Red, deadCellColor.Green, deadCellColor.Blue)
'End With
'With selection.Font
' .Color = RGB(aliveCellColor.Red, aliveCellColor.Green, aliveCellColor.Blue)
'End With
End If
If generation1(c.Row, c.Column - 5) = True Then
aliveCells = aliveCells + 1
End If
Next c
generationCell.Value = generation
aliveCellsCell.Value = aliveCells
deadCellsCell.Value = 10000 - aliveCells
End Sub
Sub load()
Dim import As Range
Set import = Worksheets("Import").Range("F1:DA100")
For Each c In import
generation1(c.Row, c.Column - 5) = c.Value
Next c
Call updateTable
End Sub