Жизнь Конвея | Powershell

Я работал над созданием базового симулятора игры жизни Конвея в Powershell, чтобы лучше познакомиться с языком. Но мой текущий код неправильно подсчитывает количество соседних ячеек, поэтому игра не работает. Я считаю, что моя упаковка верна, насколько я могу судить, но, похоже, что-то не так с моим соседом.

Вот код:

function next-gen{
    param([System.Array]$origM)

    $tmpM = $origM

    For($x=0; $x -lt $tmpM.GetUpperBound(0); $x++ ){
        For($y=0; $y -lt $tmpM.GetUpperBound(1); $y++){
            $neighborCount = getNeighbors $tmpM $x $y
            if($neighborCount -lt 2 -OR $neighborCount -gt 3){
                $tmpM[$x,$y] = 0
            }
            elseif($neighborCount -eq 3){
                $tmpM[$x, $y] = 1
            }
        } 
    }
    $Global:origM = $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($g[$(wrap $x $newX),$(wrap $y $newY)]){
                $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 ""
}
#board is always a square, size represents both x and y
$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 ""
}

С настройкой платы в приведенной выше демонстрации, результат должен быть миганием:

Gen0

0 0 0 0 0
0 0 0 0 0
0 1 1 1 0
0 0 0 0 0
0 0 0 0 0

Gen1

0 0 0 0 0
0 0 1 0 0
0 0 1 0 0
0 0 1 0 0
0 0 0 0 0

Для тех, кто незнаком, правила можно найти здесь: Википедия - Игра жизни Конвея

3 ответа

Решение

Я добавил некоторый отладочный код (с многословной записью) и нашел, где были ошибки.

Во-первых, вы проверяли соседей в $tmpm массив (который обновлялся), а не текущее поколение). Во-вторых, вы устанавливали $global:OrigM когда ты имел ввиду $Global:M в конце функции следующего поколения, но это на самом деле должно быть $script:Mпотому что переменная существует в области действия сценария, а не в глобальной.

Также, 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 ""
}
#board is always a square, size represents both x and y
$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 ""
}

PS Проверка ваших границ в циклах в следующем поколении была отключена на 1, и я заставил ее начать с чистой доски, а не повторно использовать последний ген (так как вы явно устанавливаете каждую позицию, это не имеет значения).

Полезный ответ Майка Шепарда уже дает рабочее решение и объяснение проблем с кодом в вопросе.

Позвольте мне дополнить его переработанной версией, которая:

  • обновляет переменную платы на месте через [ref] (по ссылке) переменная, чтобы последовательные вызовы работали как нужно.

  • имеет гибкую функцию отображения, которая позволяет указывать количество отображаемых поколений, обновлять ли отображение на месте и как долго делать паузу между поколениями.

    • Если вы запустите код "как есть", 5 поколений будут показаны, обновлены на месте с паузой в 1 секунду. между поколениями.
  • использует имена функций более в соответствии с соглашениями об именах PowerShell.

  • делает код более надежным, например:

    • полностью избегая использования переменных уровня сценария (или глобальных переменных).
      • Кроме того: глобальные переменные являются сессионными и остаются в области действия после выхода из скрипта; если нужны переменные "script-global", используйте $script: объем; эта область действия скрипта (не глобальная) является значением по умолчанию для переменных, созданных без явной области видимости на верхнем уровне вашего сценария.
    • используя строго типизированный [int[,]] параметры для параметров получения платы.
    • предотвращение ссылок на неинициализированные переменные.

Примечание. Рабочую реализацию полноценной игры с интерактивными функциями можно найти в этом Gist; работает как в Windows PowerShell v3+, так и в PowerShell Core.


$ErrorActionPreference = 'Stop' # Abort on all unhandled errors.
Set-StrictMode -version 1 # Prevent use of uninitialized variables.

# Given a board as $m_ref, calculates the next generation and assigns it
# back to $m_ref.
function update-generation {
    param(
      [ref] [int[,]]$m_ref  # the by-reference board variable (matrix)
    )

    # Create a new, all-zero clone of the current board (matrix) to 
    # receive the new generation.
    $m_new = New-Object 'int[,]' ($m_ref.Value.GetLength(0), $m_ref.Value.GetLength(1))

    For($x=0; $x -le $m_new.GetUpperBound(0); $x++ ){
        For($y=0; $y -le $m_new.GetUpperBound(1); $y++){
            # Get the count of live neighbors.
            # Note that the *original* matrix must be used to:
            #  - determine the live neighbors
            #  - inspect the current state
            # because the game rules must be applied *simultaneously*.
            $neighborCount = get-LiveNeighborCount $m_ref.Value $x $y
            if ($m_ref.Value[$x,$y]) { # currently LIVE cell
              # A live cell with 2 or 3 neighbors lives, all others die.
              $m_new[$x,$y] = [int] ($neighborCount -eq 2 -or $neighborCount -eq 3)
            } else { # curently DEAD cell
              # A currently dead cell is resurrected if it has 3 live neighbors.
              $m_new[$x,$y] = [int] ($neighborCount -eq 3)
            }
            $null = $m_new[$x,$y]
        } 
    }

    # Assign the new generation to the by-reference board variable.
    $m_ref.Value = $m_new
}

# Get the count of live neighbors for board position $x, $y.
function get-LiveNeighborCount{
  param(
      [int[,]]$m, # the board (matrix)
      [Int]$x,
      [Int]$y
  )

  $xLength = $m.GetLength(0)
  $yLength = $m.GetLength(1)

  $count = 0
  for($xOffset = -1; $xOffset -le 1; $xOffset++) {
    for($yOffset = -1; $yOffset -le 1; $yOffset++) {
      if (-not ($xOffset -eq 0 -and $yOffset -eq 0)) { # skip the position at hand itself
        if($m[(get-wrappedIndex $xLength ($x + $xOffset)),(get-wrappedIndex $yLength ($y + $yOffset))]) {
          $count++
        }
      }
    }
  }
  # Output the count.
  $count
}

# Given a potentially out-of-bounds index along a dimension of a given length, 
# return the wrapped-around-the-edges value.
function get-wrappedIndex{
  param(
      [Int]$length,
      [Int]$index
  )

  If($index -lt 0){
    $index += $length
  }
  ElseIf($index -ge $length){
    $index -= $length
  }
  # Output the potentially wrapped index.
  $index
}

# Print a single generation's board.
function show-board {
  param(
    [int[,]] $m # the board (matrix)
  )
  0..$m.GetUpperBound(0) |
    ForEach-Object { 
      $dim1=$_
      (0..$m.GetUpperBound(1) | ForEach-Object { $m[$dim1, $_] }) -join ' ' 
    }
}

# Show successive generations.
function show-generations {

  param(
    [int[,]] $Board,
    [uint32] $Count = [uint32]::MaxValue,
    [switch] $InPlace,
    [int] $MilliSecsToPause
  )

  # Print the initial board (the 1st generation).
  Clear-Host
  show-board $Board

  # Print the specified number of generations or 
  # indefinitely, until Ctrl+C is pressed.
  [uint32] $i = 1
  while (++$i -le $Count -or $Count -eq [uint32]::MaxValue) {

    # Calculate the next generation.
    update-generation ([ref] $Board)

    if ($MilliSecsToPause) {
      Start-Sleep -Milliseconds $MilliSecsToPause
    }
    if ($InPlace) {
      Clear-Host
    } else {
      '' # Output empty line before new board is printed.
    }

    # Print this generation.
    show-board $Board

  } 

}

# Board is always a square, $size represents both x and y.
$size = 5
$board = New-Object 'int[,]' ($size, $size)

# Seed the board.
$board[2,1] = 1
$board[2,2] = 1
$board[2,3] = 1

# Determine how many generations to show and how to show them.
$htDisplayParams = @{
  Count = 5         # How many generations to show (1 means: just the initial state);
                    # omit this entry to keep going indefinitely.
  InPlace = $True   # Whether to print subsequent generations in-place.
  MilliSecsToPause = 1000 # To slow down updates.
}

# Start showing the generations.
show-generations -Board $board @htDisplayParams

ВНИМАНИЕ: То, что вы увидите, чрезвычайно хакерское и... графическое по своей природе. Нет, правда, этот код небрежен даже для новичка, поэтому я прошу прощения.

Вот версия, которая выводит ч / б на консоль. Он также случайным образом генерирует количество семян для вас, если хотите. Это не красиво, и я на работе, так что нет времени тратить на это в настоящее время:) в надежде на github на этих выходных версию, которая будет опрятной.

$ErrorActionPreference = 'Stop' # Abort on all unhandled errors.
Set-StrictMode -version 1 # Prevent use of uninitialized variables.

# Given a board as $m_ref, calculates the next generation and assigns it
# back to $m_ref.
function update-generation {
    param(
      [ref] [int[,]]$m_ref  # the by-reference board variable (matrix)
    )

    # Create a new, all-zero clone of the current board (matrix) to 
    # receive the new generation.
    $m_new = New-Object 'int[,]' ($m_ref.Value.GetLength(0), $m_ref.Value.GetLength(1))

    For($x=0; $x -le $m_new.GetUpperBound(0); $x++ ){
        For($y=0; $y -le $m_new.GetUpperBound(1); $y++){
            # Get the count of live neighbors.
            # Note that the *original* matrix must be used to:
            #  - determine the live neighbors
            #  - inspect the current state
            # because the game rules must be applied *simultaneously*.
            $neighborCount = get-LiveNeighborCount $m_ref.Value $x $y
            if ($m_ref.Value[$x,$y]) { # currently LIVE cell
              # A live cell with 2 or 3 neighbors lives, all others die.
              $m_new[$x,$y] = [int] ($neighborCount -eq 2 -or $neighborCount -eq 3)
            } else { # curently DEAD cell
              # A currently dead cell is resurrected if it has 3 live neighbors.
              $m_new[$x,$y] = [int] ($neighborCount -eq 3)
            }
            $null = $m_new[$x,$y]
        } 
    }

    # Assign the new generation to the by-reference board variable.
    $m_ref.Value = $m_new
}

# Get the count of live neighbors for board position $x, $y.
function get-LiveNeighborCount{
  param(
      [int[,]]$m, # the board (matrix)
      [Int]$x,
      [Int]$y
  )

  $xLength = $m.GetLength(0)
  $yLength = $m.GetLength(1)

  $count = 0
  for($xOffset = -1; $xOffset -le 1; $xOffset++) {
    for($yOffset = -1; $yOffset -le 1; $yOffset++) {
      if (-not ($xOffset -eq 0 -and $yOffset -eq 0)) { # skip the position at hand itself
        if($m[(get-wrappedIndex $xLength ($x + $xOffset)),(get-wrappedIndex $yLength ($y + $yOffset))]) {
          $count++
        }
      }
    }
  }
  # Output the count.
  $count
}

# Given a potentially out-of-bounds index along a dimension of a given length, 
# return the wrapped-around-the-edges value.
function get-wrappedIndex{
  param(
      [Int]$length,
      [Int]$index
  )

  If($index -lt 0){
    $index += $length
  }
  ElseIf($index -ge $length){
    $index -= $length
  }
  # Output the potentially wrapped index.
  $index
}

# Print a single generation's board.
function show-board {
  param(
    [int[,]] $m # the board (matrix)
  )
  0..$m.GetUpperBound(0) |
    ForEach-Object { 
      $dim1=$_
      (0..$m.GetUpperBound(1) | ForEach-Object { $m[$dim1, $_] }) -join ' ' 
    }
}

# Show successive generations.
function show-generations {

  param(
    [int[,]] $Board,
    [uint32] $Count = [uint32]::MaxValue,
    [switch] $InPlace,
    [int] $MilliSecsToPause
  )

  # Print the initial board (the 1st generation).


  Clear-Host
  #show-board $Board
  drawIt $Board
  # Print the specified number of generations or 
  # indefinitely, until Ctrl+C is pressed.
  [uint32] $i = 1
  while (++$i -le $Count -or $Count -eq [uint32]::MaxValue) {

    # Calculate the next generation.
    update-generation ([ref] $Board)

    if ($MilliSecsToPause) {
      Start-Sleep -Milliseconds $MilliSecsToPause
    }
    if ($InPlace) {
      Clear-Host
    } else {
      '' # Output empty line before new board is printed.
    }

    # Print this generation.
    #show-board $Board
    drawIt $Board
  } 

}

function drawIt{
    param([int[,]]$m_ref)


     For($x=0; $x -le $m_ref.GetUpperBound(0); $x++ ){
        For($y=0; $y -le $m_ref.GetUpperBound(1); $y++){
            $val = $m_ref[$x,$y]
           If($val -eq 1){
               $cellColor = 'White'
           }
           Else{
               $cellColor = 'Black'
           }
           #write-host $cellColor.GetType()
           Write-Host " " -NoNewline -BackgroundColor $CellColor
           Write-Host " " -NoNewline
        }
        Write-Host '' #start a new line
    }

}

# Board is always a square, $size represents both x and y.
$size = 10 #change this to change size of the board
$board = New-Object 'int[,]' ($size, $size)
$seedCount = 17 #change this to change # of alive cells to start with
# Seed the board.
for($seed = 0; $seed -lt $seedCount ; $seed ++){
    $board[$(Get-random -Maximum ($size -1)),$(Get-random -Maximum ($size -1))] = 1
}

# Determine how many generations to show and how to show them.
$htDisplayParams = @{
  Count = 25         # how many generations to show (1 means: just the initial state)
                    # omit this entry to keep going indefinitely
  InPlace = $True   # whether to print subsequent generations in-place
  MilliSecsToPause = 1000 # To slow down updates
}

# Start showing the generations.
show-generations -Board $board @htDisplayParams

Большое спасибо mklement0 и Майку Шепарду за то, что они нашли время, чтобы помочь и предложить рекомендации и предложения.

Другие вопросы по тегам