Jump to content

Another Conway's Game of Life


mv2112
 Share

Recommended Posts

This is the first script iv posted here. A very slow Conways Game of Life

I Think ill just leave this here...

#cs
Author: mv2112
Based on Conway's Game of Life
 
Do with this code as you please!
#ce
#include <GUIConstantsEx.au3>
Global $DEAD = 0x000000
Global $ALIVE = 0x00FF00
_Main()
Func _Main()
$gui = GUICreate("mv2112's Take on Conway's Game of Life", 375, 374)
Global $aMap[25][25][2]
Global $temp[25][25][2]
For $x = 0 To UBound($aMap) - 1
  For $y = 0 To UBound($aMap, 2) - 1
   $char = $DEAD
   $num = Int(Random(0, 1000))
   If Mod($num, 5) = 0 Then
    $char = $ALIVE
   EndIf
   $aMap[$x][$y][0] = GUICtrlCreateLabel("", $x * 15, $y * 15, 15, 15)
   $aMap[$x][$y][1] = $char
   $temp[$x][$y][1] = $char
   GUICtrlSetFont($aMap[$x][$y][0], 20)
   GUICtrlSetBkColor($aMap[$x][$y][0], $char)
  Next
Next
GUISetState()
While True
  $msg = GUIGetMsg()
  If $msg = $GUI_EVENT_CLOSE Then
   Exit
  EndIf
  For $x = 0 To UBound($aMap) - 1
   For $y = 0 To UBound($aMap, 2) - 1
    Life($x, $y)
   Next
  Next
  For $x = 0 To UBound($aMap) - 1
   For $y = 0 To UBound($aMap, 2) - 1
    $aMap[$x][$y][1] = $temp[$x][$y][1]
    GUICtrlSetBkColor($aMap[$x][$y][0], $aMap[$x][$y][1])
   Next
  Next
WEnd
EndFunc   ;==>_Main
#cs
1. Fewer than 2 neighbors = DEATH
2. 2-3 Neighbors = LIVE
3. 4+ Neighbors = DEATH
4. Dead Cell with 3 Neighboors = LIFE
#ce
Func Life($x, $y)
If _getAlive($x, $y) Then
  If willDie($x, $y) = True Then
   _Kill($x, $y)
  EndIf
Else
  If canSpawn($x, $y) = True Then
   _Spawn($x, $y)
  EndIf
EndIf
EndFunc   ;==>Life
Func _getAlive($x, $y)
$_x = _xWrap($x)
$_y = _yWrap($y)
Return ($aMap[$_x][$_y][1] = $ALIVE)
EndFunc   ;==>_getAlive
Func _Kill($x, $y)
$_x = _xWrap($x)
$_y = _yWrap($y)
$temp[$_x][$_y][1] = $DEAD
EndFunc   ;==>_Kill
Func _Spawn($x, $y)
$_x = _xWrap($x)
$_y = _yWrap($y)
$temp[$_x][$_y][1] = $ALIVE
EndFunc   ;==>_Spawn
Func _xWrap($x)
If $x < 0 Then
  Return UBound($aMap) - 1
ElseIf $x >= UBound($aMap) Then
  Return 0
Else
  Return $x
EndIf
EndFunc   ;==>_xWrap
Func _yWrap($y)
If $y < 0 Then
  Return UBound($aMap, 2) - 1
ElseIf $y >= UBound($aMap, 2) Then
  Return 0
Else
  Return $y
EndIf
EndFunc   ;==>_yWrap
Func _numNeighbors($x, $y)
$vNCount = 0
;TOP
If $aMap[_xWrap($x)][_yWrap($y - 1)][1] = $ALIVE Then
  $vNCount += 1
EndIf
;DOWN
If $aMap[_xWrap($x)][_yWrap($y + 1)][1] = $ALIVE Then
  $vNCount += 1
EndIf
;LEFT
If $aMap[_xWrap($x - 1)][_yWrap($y)][1] = $ALIVE Then
  $vNCount += 1
EndIf
;RIGHT
If $aMap[_xWrap($x + 1)][_yWrap($y)][1] = $ALIVE Then
  $vNCount += 1
EndIf
;DOWN RIGHT
If $aMap[_xWrap($x + 1)][_yWrap($y + 1)][1] = $ALIVE Then
  $vNCount += 1
EndIf
;UP RIGHT
If $aMap[_xWrap($x + 1)][_yWrap($y - 1)][1] = $ALIVE Then
  $vNCount += 1
EndIf
;LEFT UP
If $aMap[_xWrap($x - 1)][_yWrap($y - 1)][1] = $ALIVE Then
  $vNCount += 1
EndIf
;LEFT DOWN
If $aMap[_xWrap($x - 1)][_yWrap($y + 1)][1] = $ALIVE Then
  $vNCount += 1
EndIf
Return $vNCount
EndFunc   ;==>_numNeighbors
Func willDie($x, $y)
$vNeighbors = _numNeighbors($x, $y)
Return (($vNeighbors < 2) Or ($vNeighbors > 3))
EndFunc   ;==>willDie
Func canSpawn($x, $y)
$vNeighbors = _numNeighbors($x, $y)
Return ($vNeighbors = 3)
EndFunc   ;==>canSpawn

There's no place like ~/

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...