Jump to content
Sign in to follow this  
mv2112

Another Conway's Game of Life

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 ~/

Share this post


Link to post
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
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By SOCCERMOMMY
      Hello all,
      First time posting; please excuse my noobness as I am self-taught over a short period with very little programming experience.  However, I am trying to create a simple program that retrieves college basketball scores from the web, puts them into an array and writes to excel.  Ultimately I want to create an additional variable that would be a date range so the program can cycle through many pages and retrieve years worth of information at a time.  For now, I am working on a single day.  Here is where I am stuck:  I want to use _StringBetween to isolate team names while considering whether they are "winners" or "losers".  Here is a snippet from the source code i am dealing with:
      <tr class="winner">   <td><a href="/teams/MIN/2020.html">Minnesota</a></td>   <td class="right">118</td>   <td class="right gamelink">   <a href="/boxscores/202001050CLE.html">Final</a>       </td>   </tr>   <tr class="loser">   <td><a href="/teams/CLE/2020.html">Cleveland</a></td>   <td class="right">103</td>   <td class="right">&nbsp;   </td>   </tr>   </tbody>   </table>
       
      My thought was to use _Stringbetween( '<tr class ="loser> <td>' & @CRLF & '<td><a href="/teams/', '</a></td>')  
      However, i believe I am using the @CRLF out of context.  Is there another way to identify page breaks within the stringbetween function?  Complete script is attached for reference.  Appreciate the help and patience as I try to piece it all together   
      Muchos gracias.
       
       
      basketball scores x2.au3
×
×
  • Create New...