lpst.pl - refactored moves to use a struct

This commit is contained in:
gashapwn 2021-04-04 23:22:20 +00:00
parent 9e89588b7a
commit 8dbc330d45

250
lpst.pl
View File

@ -5,6 +5,8 @@ use strict;
use utf8; use utf8;
my $ARRAY = "ARRAY";
my $X_BOX_CHR = chr(0x2573); my $X_BOX_CHR = chr(0x2573);
my $GRAY_BOX_CHR = chr(0x2591); my $GRAY_BOX_CHR = chr(0x2591);
my $ALT_US = chr(0x2017); my $ALT_US = chr(0x2017);
@ -91,6 +93,22 @@ my %AXIS_RV = map {
$AXIS{$_} => $_ $AXIS{$_} => $_
} keys(%AXIS); } keys(%AXIS);
package MoveStruct;
use warnings;
use strict;
sub new {
my $class = shift;
my $self = { @_ };
die "invalid MoveStruct {\"mov\"}" unless $self->{"mov"};
die "invalid MoveStruct ->{\"type\"}" unless $self->{"type"};
die "invalid MoveStruct type" unless ref($self->{"mov"}) eq $ARRAY;
return bless $self, $class;
}
package main;
# #
# /--\ +1 # /--\ +1
@ -110,81 +128,13 @@ my %AXIS_RV = map {
# [Row move, Diag move] # [Row move, Diag move]
my %MOVE = (
$MG_A => [
[-1, 1], [ 0, 1]
],
$MG_Q => [
[ 0, 1], [ 1, 0], [-1, 0], [ 0,-1],
],
$MG_I => [
[0,0]
],
$MG_IT => [
[ 0,-1], [ 1,-1]
],
$MG_N => [
[-1, 2],
[ 1,-2]
],
$MG_H => [
[-3, 2], [-1,-2], [ 1, 2], [ 3,-2]
],
$MG_RC => [
[-1, 0], [-2, 0], [ 1, 0], [ 2, 0]
],
$MG_S => [
[-1, 1], [-2, 2], [ 0, 1], [ 0, 2],
[ 0,-1], [ 0,-2], [ 1,-1], [ 2,-2],
],
$MG_HS => [
[-3, 1], [-2,-1], [ 2, 1], [ 3,-1]
],
$MG_RO => [
[-1, 1], [ 0, 1], [ 1, 1], [ 1,-2]
],
$MG_B => [
[-1, 1], [ 0, 1], [ 0,-2], [ 2,-2]
],
$MG_P => [
[-4, 2], [-2, 1], [-2,-2], [-1,-1],
[ 1, 1], [ 2,-1], [ 2, 2], [ 4,-2]
],
$MG_IM => [
[-2, 1], [-1,-1], [-1, 2], [ 1, 1],
[ 1,-1], [ 2,-1]
],
$MG_F => [
[-1, 0], [-1, 1], [ 0, 1], [ 0,-1],
[ 1, 0], [ 1,-1]
],
$MG_QS => [
[-3, 3], [-2, 2], [-2, 0], [-1, 0],
[-1, 1], [ 0, 1], [ 0, 2], [ 0, 3],
[ 0,-1], [ 0,-2], [ 0,-3], [ 1, 0],
[ 1,-1], [ 2, 0], [ 2,-2], [ 3,-3],
],
$MG_AU => [
[-1, 0], [-1, 1], [ 0, 1], [ 0,-1],
[ 1, 0], [ 1,-1]
],
$MG_SY => [
[-4, 4], [-3, 3], [-3, 2], [-1,-2],
[ 0,-4], [ 0,-3], [ 0, 3], [ 0, 4],
[ 1, 2], [ 3,-2], [ 3,-3], [ 4,-4]
]
);
my %BLOCK = (
);
# Special cells # Special cells
my $EMPTY_CELL = "_"; my $EMPTY_CELL = "_";
# Shade enum # Shade enum
my $SHADE_3_ENUM = 3; my $SHADE_3_ENUM = 0b011;
my $SHADE_2_ENUM = 2; my $SHADE_2_ENUM = 0b010;
my $SHADE_1_ENUM = 1; my $SHADE_1_ENUM = 0b001;
my $EMPTY_ENUM = 0; my $EMPTY_ENUM = 0;
my %SHADE = ( my %SHADE = (
@ -194,8 +144,143 @@ my %SHADE = (
$EMPTY_ENUM => [$HEX_1, $HEX_2, $HEX_3] $EMPTY_ENUM => [$HEX_1, $HEX_2, $HEX_3]
); );
my $MOVE_ENUM = $SHADE_1_ENUM;
my $BLOCK_ENUM = $SHADE_2_ENUM;
my $MOVBLOCK_ENUM = $SHADE_3_ENUM;
my $ERR_C_1 = -1; my $ERR_C_1 = -1;
my %MOVE = (
$MG_A => [
MoveStruct->new("mov" => [-1, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 1], "type" => $MOVE_ENUM)
],
$MG_Q => [
MoveStruct->new("mov" => [ 0, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-1, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-1], "type" => $MOVE_ENUM)
],
$MG_I => [
MoveStruct->new("mov" => [0,0], "type" => $MOVE_ENUM)
],
$MG_IT => [
MoveStruct->new("mov" => [ 0,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1,-1], "type" => $MOVE_ENUM)
],
$MG_N => [
MoveStruct->new("mov" => [-1, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1,-2], "type" => $MOVE_ENUM)
],
$MG_H => [
MoveStruct->new("mov" => [-3, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-1,-2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 3,-2], "type" => $MOVE_ENUM)
],
$MG_RC => [
MoveStruct->new("mov" => [-1, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-2, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 2, 0], "type" => $MOVE_ENUM)
],
$MG_S => [
MoveStruct->new("mov" => [-1, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-2, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 2,-2], "type" => $MOVE_ENUM),
],
$MG_HS => [
MoveStruct->new("mov" => [-3, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-2,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 2, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 3,-1], "type" => $MOVE_ENUM)
],
$MG_RO => [
MoveStruct->new("mov" => [-1, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1,-2], "type" => $MOVE_ENUM)
],
$MG_B => [
MoveStruct->new("mov" => [-1, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 2,-2], "type" => $MOVE_ENUM)
],
$MG_P => [
MoveStruct->new("mov" => [-4, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-2, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-2,-2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-1,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 2,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 2, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 4,-2], "type" => $MOVE_ENUM)
],
$MG_IM => [
MoveStruct->new("mov" => [-2, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-1,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-1, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 2,-1], "type" => $MOVE_ENUM)
],
$MG_F => [
MoveStruct->new("mov" => [-1, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-1, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1,-1], "type" => $MOVE_ENUM)
],
$MG_QS => [
MoveStruct->new("mov" => [-3, 3], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-2, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-2, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-1, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-1, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 3], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-3], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 2, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 2,-2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 3,-3], "type" => $MOVE_ENUM)
],
$MG_AU => [
MoveStruct->new("mov" => [-1, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-1, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-1], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1, 0], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1,-1], "type" => $MOVE_ENUM)
],
$MG_SY => [
MoveStruct->new("mov" => [-4, 4], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-3, 3], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-3, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [-1,-2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-4], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0,-3], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 3], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 0, 4], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 1, 2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 3,-2], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 3,-3], "type" => $MOVE_ENUM),
MoveStruct->new("mov" => [ 4,-4], "type" => $MOVE_ENUM)
]
);
my %sc = (); my %sc = ();
my %board; my %board;
@ -462,7 +547,6 @@ sub apply_shift($$){
$r0 += $my_shift->[0]; $r0 += $my_shift->[0];
$c0 += $my_shift->[1]; $c0 += $my_shift->[1];
# die "cell index [$r0, $c0]: out of bound exception" if $r0 > 11 or $c0 > 11 or $r0 < 1 or $c0 < 1;
return $ERR_C_1 if $r0 > 11 or $c0 > 11 or $r0 < 1 or $c0 < 1; return $ERR_C_1 if $r0 > 11 or $c0 > 11 or $r0 < 1 or $c0 < 1;
return cell_index_rev($r0, $c0); return cell_index_rev($r0, $c0);
} }
@ -501,23 +585,31 @@ sub calc_move($$){
sub shade_move($){ sub shade_move($$){
my $not = shift; my ($not, $en) = (shift, shift);
my $c1; my $c1;
my @mv1; my @mv1;
my @mv2; my @mv2;
$c1 = cell_pi($not); $c1 = cell_pi($not);
@mv1 = @{$MOVE{$c1}}; @mv1 = map {
$_
} grep {
($_->{"type"} && $en) eq $en
}@{$MOVE{$c1}};
@mv2 = grep { @mv2 = grep {
$_ ne $ERR_C_1 $_ if $_->[0] ne $ERR_C_1;
} map { } map {
calc_move($not, $_) [
calc_move($not, $_->{"mov"}),
$_->{"type"}
]
} @mv1; } @mv1;
for my $mv (@mv2){ for my $mv (@mv2){
shade_cell($mv, $SHADE_1_ENUM); shade_cell($mv->[0], $mv->[1]);
} }
} }
@ -541,7 +633,7 @@ my $m1 = "A1";
my $c1 = $MG_QS; my $c1 = $MG_QS;
$board{$m1} = $P1.$DIV.$c1; $board{$m1} = $P1.$DIV.$c1;
shade_move($m1); shade_move($m1, $MOVE_ENUM);
disp_board(); disp_board();