-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConway.bas
165 lines (142 loc) · 3.47 KB
/
Conway.bas
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
REM ***** BASIC *****
'''''''''''''''''''''''Declaracion de constantes'''''''''''''''''''''''''
Const COLUMNA_INICIAL = 1
Const FILA_INICIAL = 1
Const ANCHO_TABLERO = 20
Const ALTO_TABLERO = 20
Const COLUMNA_FINAL = COLUMNA_INICIAL + ANCHO_TABLERO - 1
Const FILA_FINAL = FILA_INICIAL + ALTO_TABLERO - 1
Sub Main
Conway
End Sub
'Pinta el tablero de gris con los valores de cada celda en 1
Function inicializarTablero(hoja)
Dim i As Integer
Dim j as Integer
For i = COLUMNA_INICIAL To COLUMNA_FINAL
For j = FILA_INICIAL To FILA_FINAL
celda = hoja.getCellByPosition(i, j)
If celda.Value = 1 Then
celda.CellBackColor = RGB(0,0,0)
Else
If celda.Value = 0 Then
celda.CellBackColor = RGB(250,250,250)
Else
celda.CellBackColor = RGB(200,200,200)
End If
End If
Next j
Next i
End Function
'Hace que todas las celdas del mapa sean cero
Function inicializarMapa(mapa)
Dim i As Integer
Dim j as Integer
For i = 0 To ANCHO_TABLERO - 1
For j = 0 To ALTO_TABLERO - 1
mapa(i , j) = 0
Next j
Next i
End Function
'Se le pasa la hoja y las coordenadas, devuelve cuantas celulas vivas vecinas tiene
Function getNumeroDeVecinos(hoja, columna, fila)
Dim vecinos As Integer
vecinos = 0
Dim i
Dim j
For i = -1 To 1
For j = -1 to 1
If (i <> 0) OR (j <> 0) Then
celdaRondante = hoja.getCellByPosition(columna + i, fila + j)
If( celdaRondante.Value = 1 ) Then
vecinos = vecinos + 1
End If
End If
Next j
Next i
devolverNumeroDeVecinos = vecinos
End Function
Function mapearTablero(hoja, mapa)
inicializarMapa(mapa)
Dim circundantes As Integer
Dim estaVivo As Boolean
Dim esCelula As Boolean
Dim estado
Dim mapeo
Dim iMapa As Integer
Dim jMapa As Integer
Dim i
Dim j
For i = 0 To ANCHO_TABLERO - 1
For j = 0 to ALTO_TABLERO - 1
iHoja = i + COLUMNA_INICIAL
jHoja = j + FILA_INICIAL
estado = hoja.getCellByPosition(iHoja,jHoja).Value
If estado = 1 Then
estaVivo = True
esCelula = True
Else
If estado = 0 Then
estaVivo = False
esCelula = True
Else
esCelula = False
End If
End If
circundantes = devolverNumeroDeVecinos(hoja, iHoja, jHoja)
If esCelula Then
If estaVivo Then
If circundantes < 2 OR circundantes > 3 Then
mapa(i, j) = -1
End If
Else
If circundantes = 3 Then
mapa(i, j) = 1
End If
End If
End If
Next j
Next i
End Function
Function actualizarTablero(hoja, mapa)
Dim i
Dim j
Dim mapeo
For i = 0 To ANCHO_TABLERO - 1
For j = 0 to ALTO_TABLERO - 1
mapeo = mapa(i,j)
celda = hoja.getCellByPosition(i + COLUMNA_INICIAL,j + FILA_INICIAL)
If mapeo = -1 Then
celda.Value = 0
celda.CellBackColor = RGB(250,250,250)
Else
If mapeo = 1 Then
celda.Value = 1
celda.CellBackColor = RGB(0,0,0)
End If
End If
Next j
Next i
End Function
'Juego en si
Sub Conway
'necesario para poder acceder a las celdas
esteDocumento = ThisComponent
hojas = esteDocumento.getSheets()
hoja = hojas.getByIndex(0)
'
inicializarTablero(hoja)
'mapa es una matriz de int que pueden ser -1, 1 o 0.
' 0 representa que no hace falta cambiar nada en la celda representada.
' 1 representa que se debe encender.
' -1 que se debe apagar
Dim mapa(ANCHO_TABLERO - 1, ALTO_TABLERO - 1) As Integer
'Ciclo ppal, corre el juego dentro de esto
Dim ciclos As Integer
ciclos = 0
Do While ciclos < 450
mapearTablero(hoja, mapa)
actualizarTablero(hoja, mapa)
ciclos = ciclos + 1
Loop
End Sub