lpst.pl - calc_move() and apply_shift() allow us to calculate which cells pieces move to
This commit is contained in:
parent
d3cb948956
commit
15f96766da
75
lpst.pl
75
lpst.pl
@ -13,7 +13,8 @@ my $HEX_1 = '-/--\-';
|
||||
my $HEX_2 = '| __ |';
|
||||
my $HEX_3 = '| |';
|
||||
|
||||
my $SHADE_CHR = chr(0x2592);
|
||||
my $XBOX_CHR = chr(0x2573);
|
||||
my $SHADE_CHR = chr(0x2591);
|
||||
my $ALT_US = chr(0x2017);
|
||||
my $ALT_PIPE = chr(0x2016);
|
||||
|
||||
@ -82,7 +83,15 @@ my %AXIS_RV = map {
|
||||
# -1 -1
|
||||
#
|
||||
|
||||
# [Row mvoe, Diag move]
|
||||
# ODD
|
||||
# UP: 0, +1
|
||||
# DOWN: -1, -1
|
||||
|
||||
# EVEN
|
||||
# UP: +1, +1
|
||||
# DOWN: +0, -1
|
||||
|
||||
# [Row move, Diag move]
|
||||
|
||||
my %MOVE = (
|
||||
$MG_A => [
|
||||
@ -407,6 +416,16 @@ sub disp_board(){
|
||||
printf("%s", $b);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
sub cell_index_rev($$){
|
||||
return $AXIS_RV{$_[0]}.$_[1];
|
||||
}
|
||||
@ -416,6 +435,49 @@ sub cell_index($){
|
||||
return ($AXIS{$chr1}, $chr2);
|
||||
}
|
||||
|
||||
sub apply_shift($$){
|
||||
my ($not, $my_shift) = (shift, shift);
|
||||
my $r0;
|
||||
my $c0;
|
||||
|
||||
($r0, $c0) = cell_index($not);
|
||||
|
||||
$r0 += $my_shift->[0];
|
||||
$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 cell_index_rev($r0, $c0);
|
||||
}
|
||||
|
||||
sub calc_move($$){
|
||||
my ($not, $mov) = (shift, shift);
|
||||
my $r0;
|
||||
my $c0;
|
||||
|
||||
my $x_shift;
|
||||
my $y_shift;
|
||||
|
||||
my $n0;
|
||||
my $ni;
|
||||
|
||||
($r0, $c0) = cell_index($not);
|
||||
|
||||
# Calc y_shift
|
||||
$y_shift = $mov->[1];
|
||||
|
||||
# Calc x_shift
|
||||
$x_shift = $mov->[0];
|
||||
|
||||
# make adjustment
|
||||
$ni = ($y_shift / ($y_shift * -1));
|
||||
$ni *= -1;
|
||||
|
||||
$n0 = ($r0 + 1) % 2;
|
||||
|
||||
$x_shift += int(($y_shift + ($n0 * $ni)) / 2);
|
||||
|
||||
return apply_shift($not, [$x_shift, $y_shift]);
|
||||
}
|
||||
|
||||
|
||||
# Main starts here!
|
||||
binmode(STDOUT, ":utf8");
|
||||
@ -432,10 +494,13 @@ binmode(STDOUT, ":utf8");
|
||||
$AXIS_RV{$_}
|
||||
}(1...11);
|
||||
|
||||
$board{"F5"} = $P1.$DIV.$MG_QS;
|
||||
my $m1 = "F4";
|
||||
my $c1 = $MG_A;
|
||||
|
||||
$board{$m1} = $P1.$DIV.$c;
|
||||
|
||||
for my $mv in $MOVE{}
|
||||
printf("%s\n", calc_move($m1, [-2,-2]));
|
||||
# printf("%s\n", calc_move($m1, [-6,6]));
|
||||
disp_board();
|
||||
|
||||
# disp_0("C1");
|
||||
# printf(">>%s, %s\n", cell_index("C1"));
|
||||
|
Loading…
Reference in New Issue
Block a user