[Win32::Console]Perl终端版生命游戏

环境,WinXP/Win7  Perl 5.16

默认循环1000次,按ESC提前退出

[Win32::Console]Perl终端版生命游戏

 use strict;
use Term::ReadKey;
use Win32::Console;
use Time::HiRes 'sleep';
use IO::Handle;
STDOUT->autoflush();
system("mode con lines=40 cols=100"); our $OUT=Win32::Console->new(STD_OUTPUT_HANDLE);
$OUT->Cursor(, , , ); #hide cursor my ($i, $j);
our ($rows, $cols) = (, );
our ($mxrow, $mxcol) = ($rows-, $cols-); # init
our @coord;
my (@h, @n);
my $y = ; foreach (<DATA>) {
s/\r?\n$//;
tr/\*\.//;
@{$h[$y++]} = ( split("", $_) );
} foreach $i ( .. $mxrow) {
foreach $j ( .. $mxcol) {
$coord[$i][$j]{'x'} = $j*;
$coord[$i][$j]{'y'} = $i;
$h[$i][$j] = unless (defined $h[$i][$j]);
$n[$i][$j] = ;
}
} &Draw(\@n, \@h);
foreach (..) {
sleep 0.05;
@n = ();
&NextBuffer(\@h, \@n);
&Draw(\@h, \@n);
@h = (@n); &KeyFunc();
} sub NextBuffer {
my ($ra, $rb) = (shift, shift);
my ($i, $j, $sum);
my ($L, $R, $U, $D);
foreach $i ( .. $mxrow) {
$U = ($i-) < ? $mxrow : ($i-);
$D = ($i+) > $mxrow ? : ($i+);
foreach $j ( .. $mxcol) {
$L = ($j-) < ? $mxcol : ($j-);
$R = ($j+) > $mxcol ? : ($j+);
$sum = $ra->[$U][$L] + $ra->[$U][$j] + $ra->[$U][$R] +
$ra->[$i][$L] + + $ra->[$i][$R] +
$ra->[$D][$L] + $ra->[$D][$j] + $ra->[$D][$R]; if ($sum == ) {
$rb->[$i][$j] = ;
} elsif ($sum == ) {
$rb->[$i][$j] = $ra->[$i][$j];
} else {
$rb->[$i][$j] = ;
}
}
}
} sub Draw {
my ($ra, $rb) = (shift, shift);
foreach $i ( .. $mxrow) {
foreach $j ( .. $mxcol) {
if ($rb->[$i][$j] != $ra->[$i][$j]) {
&Point(
$coord[$i][$j]{'x'},
$coord[$i][$j]{'y'},
$rb->[$i][$j],
);
}
}
}
} sub Point {
my ($mx, $my, $light) = (shift, shift, shift);
my $color;
if ($light == ) {
$color = $FG_WHITE|$BG_GRAY;
} else {
$color = $FG_WHITE|$BG_BLACK;
}
$OUT->Cursor($mx, $my);
$OUT->FillAttr($color, , $mx, $my);
} sub KeyFunc {
my $key;
$key = ReadKey(-);
return if (not defined $key);
if ( ord($key) == ) {
exit;
}
} __DATA__
......................**...............
......................**...............
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.........*..........**...**............
.......*.*............***..............
......*.*............*...*.............
**...*..*.............*.*..............
**....*.*..............*...............
.......*.*......*.*....................
.........*......**.....................
.................*...*.................
.....................**......*.........
....................*.*......*.*.......
...............*..............*.*....**
..............*.*.............*..*...**
.............*...*............*.*......
..............***............*.*.......
............**...**..........*.........
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
...............**......................
...............**......................
上一篇:[UOJ#127][BZOJ4195][NOI2015]程序自动分析


下一篇:Win32 Console Application、Win32 Application、MFC三者之间的联系和区别