I added the debug code (using write-verbose) and found where the errors were.
$tmpm ( ), ).
-, $global:OrigM, $Global:M next-gen, $script:M, script, .
, getNeighbors , 8 .
function next-gen{
param([System.Array]$origM)
$size=1+$origM.GetUpperBound(0)
$tmpM = New-Object 'int[,]' ($size, $size)
For($x=0; $x -lt $size; $x++ ){
For($y=0; $y -lt $size; $y++){
$neighborCount = getNeighbors $origm $x $y
if($neighborCount -lt 2 -OR $neighborCount -gt 3){
$tmpM[$x,$y] = 0
write-verbose "Clearing $x,$y"
}
elseif($neighborCount -eq 3 -or $OrigM[$x,$y] -eq 1){
$tmpM[$x, $y] = 1
write-verbose "Setting $x,$y"
}
}
}
$script:M = $tmpM
}
function getNeighbors{
param(
[System.Array]$g,
[Int]$x,
[Int]$y
)
$newX=0
$newY=0
$count=0
for($newX = -1; $newX -le 1; $newX++){
for($newY = -1; $newY -le 1; $newY++){
if($newX -ne 0 -or $newY -ne 0){
$neighborx=wrap $x $newx
$neighborY=wrap $y $newY
write-verbose "x=$x y=$y Nx=$neighborx Ny=$neighborY"
if($g[$neighborx,$neighborY] -eq 1){
write-verbose "Neighbor at $neighborx, $neighborY is Set!"
$count++
}
}
}
}
write-verbose "x=$x y=$y Neighbor count = $count"
return $count
}
function wrap{
param(
[Int]$z,
[Int]$zEdge
)
$z+=$zEdge
If($z -lt 0){
$z += $size
}
ElseIf($z -ge $size){
$z -= $size
}
return $z
}
function printBoard{
0..$m.GetUpperBound(0) |
% { $dim1=$_; (0..$m.GetUpperBound(1) | % { $m[$dim1, $_] }) -join ' ' }
write-host ""
}
$size = 5
$m = New-Object 'int[,]' ($size, $size)
$m[2,1] = 1
$m[2,2] = 1
$m[2,3] = 1
clear
printBoard
For($x=0; $x -lt 1; $x++){
next-gen $m
printBoard
write-host ""
}
P.S. 1, , ( , ).