lunes, 9 de marzo de 2009

Juego de la vida de Conway con Small Basic

Small Basic es una version reducida del Basic .NET de Microsoft, está enfocado a entornos educativos y su meta fundamental es convertir la programación en algo divertido y sencillo.

Desde aquí se puede bajar el Small Basic totalmente gratis.

La interfaz es muy intuitiva:



A continuacion se plasma el código para SmallBasic de la simulación del juego de la vida de Conway:

'ThePirat 2009
GraphicsWindow.PenColor = "Red"
GraphicsWindow.BrushColor = "Blue"
GraphicsWindow.BackgroundColor="white"
GraphicsWindow.MouseDown = OnMouseDown
GraphicsWindow.KeyDown = OnKeyDown
GraphicsWindow.Show()

SIZE = 30
Randomize()

RunLoop:
Draw()
Paso()
Program.Delay(1)
Goto RunLoop

Sub Randomize
For i = 0 To (SIZE * SIZE - 1)
If (Math.GetRandomNumber(5) > 3) Then
Array.SetValue("arr", i, 1)
Else
Array.SetValue("arr", i, 0)
EndIf
Endfor
EndSub

Sub Draw
GraphicsWindow.Clear()
w = GraphicsWindow.Width
h = GraphicsWindow.Height
For i = 0 To (SIZE * SIZE - 1)
val = Array.GetValue("arr", i)
If (val = 1) Then
GraphicsWindow.FillEllipse(w / SIZE * Math.Remainder(i+1, SIZE), h / SIZE * (Math.Floor(i / SIZE)), w / SIZE, h / SIZE)
'GraphicsWindow.SetPixel(Math.Remainder(i+1, SIZE), Math.Floor(i / SIZE), "Blue")
EndIf
EndFor
EndSub


Sub Paso
'Col = 1 if first column, Col = 2 if last column, Col = 0 otherwise
'Row = 1 if first row, Row = 2 if last row, Row = 0 otherwise
For i = 0 To (SIZE * SIZE - 1)
Alive = 0
COL = 0
ROW = 0
If (Math.Remainder(i, SIZE) = 0) Then
COL = 1 'First col
Endif
If (Math.Remainder(i + 1, SIZE) = 0) Then
COL = 2 'Last col
EndIf
If (i<SIZE) Then
ROW = 1 'First row
EndIf
If (i>=(SIZE*SIZE-SIZE)) Then
ROW = 2 'Last row
EndIf

If (COL <> 1 And ROW <> 1) Then 'not 1st row, not 1st col
Alive = Alive + Array.GetValue("arr", i - (SIZE + 1)) 'Up-Left
Endif
If (ROW <> 1) Then 'Not 1st row
Alive = Alive + Array.GetValue("arr", i - SIZE) 'Up
EndIf
If (COL <> 2 And ROW <> 1) Then 'Not mast col, not 1st row
Alive = Alive + Array.GetValue("arr", i - (SIZE - 1)) 'Up-Right
EndIf
If (COL <> 1) Then 'Not 1st col
Alive = Alive + Array.GetValue("arr", i - 1) 'Left
EndIf
If (COL <> 2) Then 'Not last col
Alive = Alive + Array.GetValue("arr", i + 1) 'Right
EndIf
If (COL <> 1 And ROW <> 2) Then 'Not 1st col, not last row
Alive = Alive + Array.GetValue("arr", i + (SIZE - 1)) 'Down-Left
EndIf
If (ROW <> 2) Then 'Not last row
Alive = Alive + Array.GetValue("arr", i + SIZE) 'Down
EndIf
If (COL <> 2 And ROW <> 2) Then 'Not last col, not last row
Alive = Alive + Array.GetValue("arr", i + (SIZE + 1)) 'Down-Right
EndIf

actual = Array.GetValue("arr", i)
newValue = actual
If (actual = 0 And Alive = 3) Then
newValue = 1
EndIf
If (actual = 1 And Alive <> 2 And Alive <> 3) Then
newValue = 0
EndIf

Array.SetValue("arrTmp", i, newValue)
Endfor

'Copy "arrTmp" into "arr"
For k = 0 To (SIZE * SIZE - 1)
Array.SetValue("arr", k, Array.GetValue("arrTmp", k))
EndFor
EndSub

Sub OnMouseDown
Randomize()
EndSub
Sub OnKeyDown
Randomize()
EndSub


Aquí se muestra la captura del programa corriendo:





Esta versión es muy simple y sólo permite configurar el tamaño de la grilla cambiando el valor de la variable SIZE.

Existen versiones más completas del juego de la vida para SmallBasic en este foro

Suerte !!!

Datos personales