我选择在Perl 6中重新设计我的先前代码的一部分,在本例中为棋盘。前两节课进展顺利(或者至少有效,我知道的很少,我无法与之交谈)他们的正确性),但我坚持使用第三个。这是代码:
#!/home/hsmyers/rakudo741/bin/perl6
# board.p6 - Beginnings of a PGN toolset. And place to start learning
# Perl 6/Raku.
use v6d;
#!___________________________________________________________
constant $size = 4;
class Piece {
my Str @namesOfPieces[$size] = <
white-rook white-knight white-bishop white-queen
>;
my Str @abrevsOfPieces[$size] = <
R N B Q K B N R
>;
my Str @symbolsOfPieces[$size] = <
♖ ♘ ♗ ♕ ♔ ♗ ♘ ♖
>;
my Str @codeptsOfPieces[$size] = (
"\x2656", "\x2658", "\x2657", "\x2655",
);
has Str $.name;
has Str $.abrev;
has Str $.symbol;
has Uni $.codept;
submethod BUILD( :$i ) {
$!name = @namesOfPieces[$i];
$!abrev = @abrevsOfPieces[$i];
$!symbol = @symbolsOfPieces[$i];
$!codept = @codeptsOfPieces[$i].NFC;
}
}
class Square {
my Int @colors[$size] = <
1 0 1 0 1 0 1 0
>;
my Str @names[$size] = <
a1 b1 c1 d1 e1 f1 g1 h1
>;
has Int $.color;
has Int $.index;
has Str $.name;
has Piece $.piece;
submethod BUILD( :$i ) {
$!color = @colors[$i];
$!index = $i;
$!name = @names[$i];
$!piece = Piece.new(:i($i));
}
}
class Board is Array {
}
my $p = Piece.new(:i(0));
$p.say;
my $s = Square.new(:i(0));
$s.say;
#!___________________________________________________________
my @b := Board.new(
Square.new(:i(0)),
Square.new(:i(1)),
Square.new(:i(2))
);
say @b;
say @b.WHAT;
在cli上运行时,结果为:
Piece.new(name => "white-rook", abrev => "R", symbol => "♖", codept => Uni.new(0x2656).NFC)
Square.new(color => IntStr.new(1, "1"), index => 0, name => "a1", piece => Piece.new(name => "white-
rook", abrev => "R", symbol => "♖", codept => Uni.new(0x2656).NFC))
[Square.new(color => IntStr.new(1, "1"), index => 0, name => "a1", piece => Piece.new(name =>
"white-rook", abrev => "R", symbol => "♖", codept => Uni.new(0x2656).NFC)) Square.new(color =>
IntStr.new(0, "0"), index => 1, name => "b1", piece => Piece.new(name => "white-knight", abrev =>
"N", symbol => "♘", codept => Uni.new(0x2658).NFC)) Square.new(color => IntStr.new(1, "1"), index =>
2, name => "c1", piece => Piece.new(name => "white-bishop", abrev => "B", symbol => "♗", codept =>
Uni.new(0x2657).NFC))]
(Board)
Board类(实际上是空的)是到目前为止我尝试中剩下的全部。令人惊讶的是(至少对我而言),它提供了一定程度的可操作性。它具有不同的“新”和“已构建”,均未提供有效的解决方案。考虑到实际计数是64而不是4,因此当前的方法行不通。
我目前的想法是我需要构建一个由64个Square组成的数组,这将反过来将创建必要的片段。我试图增加自我,却无济于事。有建议吗?
答案 0 :(得分:8)
从Array
继承可能不是这里的最佳设计选择。它揭示并承诺了Board
的基本表示形式,随着代码的发展,这将带来重构挑战。相反,我建议Board
具有Array
的{{1}},并用Square
个对象进行初始化。
假设董事会打算将Square
平方成正方形,那么您可以执行以下操作:
$size
也就是说,取0到但不包括class Board {
has @.squares[$size ** 2];
method TWEAK() {
@!squares = map { Square.new(i => $_ % $size) }, ^($size ** 2);
}
}
平方的范围,然后将每个值映射到$size
实例中。 (我们对索引取模,以避免索引超出其他类之一的范围。)
2D数组可能更合适:
Square
在这里,我们再次class Board {
has @.squares[$size;$size];
method TWEAK() {
@!squares = (map -> $i { Square.new(:$i) }, ^$size) xx $size;
}
}
,但是这一次,因为我们只是在做一维,所以我们放弃了模。使用命名的map
参数意味着我们可以使用$i
的便利,这是:$i
的简称(在您发布的代码中也有这样做的可能)。然后,我们使该表达式产生一行,并使用:i($i)
运行xx
次,以获取每一列的数据。
最终,它可能不会像这样简单。也许$size
应该使用两个构造函数参数(一个数字和一个字母)来形成其名称。最好将Square
中的map
做为。此外,map
实例的初始化可能也想在Piece
中进行;自从我上一次下象棋已经25年了,但我很确定在比赛开始时并不是每个方块都有一块棋子。