gpt4 book ai didi

perl - Perl 中的生命游戏——从 C++ 翻译代码时出现问题

转载 作者:行者123 更新时间:2023-12-02 07:46:10 26 4
gpt4 key购买 nike

我最近开始尝试将我编写的 Conway 生命游戏的 C++ 代码复制到 Perl 中,而且我几乎是逐字逐句地复制它。但是,C++ 代码的输出与 Perl 代码的输出有很大不同。 C++ 代码运行完美,但 Perl 代码出现奇怪的行为。任何看过生命游戏的人都应该看到以下游戏状态很奇怪:

  [][]  []  []  []  [][][]  []  []              []    []  []  []    [][]      []
[] [] [] [] [] [] [] [][] [] [] [][] [] [] []
[] [][] [] [] [] [] [] [][][][] [] [] [][][][][] [] []
[] [][] [] [] [] [][][][] [] [] [] [] [][] []
[] [][] [] [] [][][] [] [] [] [] [][] [] []
[] [] [][][][][] [] [] [][] [][] [] [][] [] [][][] []
[] [] [] [][][] [][] [][][] [][][] [] []
[] [] [][][][][][][][][][] [][][][][] [] []
[][] [] [][][][][][][] [][][] [][] [][] []
[] [] [][][][] [] [] [] [] [][][] []
[][] [] [] [] [] [] [][] [] [][]
[] [] [][] [] [][][][] [][] [] []
[][] [] [][][][][] [][][] [][][][][][][][] [][]
[] [] [] [] [] [][][][] [][]
[][] [] [] [] [] [] [] [][][] [][][][][][][][]
[][] [] [] [] [] [] [] [][][][] [] [][] [][]
[] [][] [] [][] [] [] [][][][] [][][]
[] [][][][] [] [] [] [] [] [] [] [] [] [] [] []
[][][] [][][] [] [][] [][] [] [] [] []
[] [][][] [] [][] [][] [] [] [] [][] []
[][] [] [] [][][][][][] [][] [] [] []
[] [] [][][][][][][][][] [] [] [] [] [][][]
[][] [][][] [] [] [][] [] [] [] [][] [] [] []
[] [] [] [][] [][][][][][][] [] [][] [] [] [] []

每个 [] 代表一个活细胞,而任意两个空格代表一个死细胞。奇怪的部分是在多次运行代码的尝试中出现的水平线和垂直线。我还没有看到预期的行为(滑翔机、振荡器等)。

我的代码如下;我将不胜感激任何帮助/澄清。提前致谢!

#!/usr/bin/perl

use warnings;
use strict;
use Curses;
use Time::HiRes 'usleep';

my $iterations = 100;
$iterations = $ARGV[0] if @ARGV;

initscr;
getmaxyx(my $rows, my $columns);
$columns = int($columns / 2);
my ($i, $j);
my @initial_state;
foreach $i (0 .. $rows) {
foreach $j (0 .. $columns) {
$initial_state[$i][$j] = int rand(2);
}
}

my @current_state = @initial_state;
my @next_state;
my $iteration;
my ($up, $down, $right, $left);
my $adjacent_cells;

foreach $iteration (0 .. $iterations) {
foreach $i (0 .. $rows) {
foreach $j (0 .. $columns) {
$up = ($i + 1) % $rows;
$down = ($i - 1) % $rows;
$right = ($j + 1) % $columns;
$left = ($j - 1) % $columns;
$adjacent_cells = $current_state[$i][$right]
+ $current_state[$i][$left]
+ $current_state[$up][$right]
+ $current_state[$up][$left]
+ $current_state[$down][$right]
+ $current_state[$down][$left]
+ $current_state[$up][$j]
+ $current_state[$down][$j];
if ( $current_state[$i][$j] ) {
$next_state[$i][$j] = $adjacent_cells == 2 || $adjacent_cells == 3 ? 1 : 0;
addstr($i, 2*$j, '[]');
}
else {
$next_state[$i][$j] = $adjacent_cells == 3 ? 1 : 0;
addstr($i, 2*$j, ' ');
}
}
}
@current_state = @next_state unless $iteration == $iterations;
usleep 10000;
refresh();
}

getch();
endwin();

最佳答案

啊哈,找到了。

在 Perl 中,二维数组实际上是对数组的引用的数组。当您执行 @current_state = @next_state 时,它只会生成外部数组的浅拷贝,因此两个数组最终都包含对相同行数组的引用。

对于您的情况,解决方法很简单:只需在循环开头设置 @next_state = () 即可。这样,perl 将为您自动生成一组新的行数组。

(此外,您可能想要修复循环,以便从 0 迭代到 $rows-1$columns-1 而不是 $rows$columns。)

关于perl - Perl 中的生命游戏——从 C++ 翻译代码时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6856075/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com