MNU.pm - added save, load and parse

This commit is contained in:
gashapwn 2021-04-12 00:36:01 +00:00
parent 11a2806dbd
commit b0ec1b2d42
6 changed files with 135 additions and 30 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ p[0-9].pl
p[0-9][0-9].pl p[0-9][0-9].pl
notes.txt notes.txt
#* #*
save.txt

View File

@ -1,5 +1,7 @@
#!/usr/bin/perl #!/usr/bin/perl
package LPST;
use warnings; use warnings;
use strict; use strict;

91
MNU.pm
View File

@ -1,5 +1,7 @@
#!/usr/bin/perl #!/usr/bin/perl
package MNU;
use warnings; use warnings;
use strict; use strict;
@ -7,23 +9,96 @@ use lib "./";
use NTVL; use NTVL;
use LPST; use LPST;
package MNU; use Try::Tiny;
my $SAVE_FN = "save.txt";
sub roll_back($){
my @arr;
my $game_str = shift;
@arr = split(/\n/, $game_str);
if(scalar @arr == 1){
return "";
}
return join("\n", splice(@arr, 0, -1))."\n";
}
sub eval_game_str($$){
my ($b, $game_str) = (shift, shift);
try {
NTVL->f1($b, $game_str);
disp_board($b);
} catch {
warn "invalid move: $_";
$game_str = roll_back($game_str);
};
return $game_str;
}
sub disp_board($){
my $b = shift;
$b->disp_board();
}
sub load(){
my $game_str;
$game_str = "";
open FILE, "<".$SAVE_FN or die "couldn't open save file";
while(<FILE>){
$game_str .= $_;
}
close FILE;
return $game_str;
}
sub save($){
my $game_str = shift;
open FILE, ">".$SAVE_FN or die "couldn't open save file";
printf FILE "%s", $game_str;
close FILE;
return $game_str;
}
sub parse_cmd($){
my $in = shift;
return -1 if $in =~ /^q(uit)?$/i;
return 1 if($in =~ /^save$/i);
return 2 if($in =~ /^load$/i);
return 0;
}
sub prompt($){
my $game_str = shift;
sub prompt(){
printf("> "); printf("> ");
my $in; my $in;
$in = <STDIN>; $in = <STDIN>;
chomp $in; chomp $in;
return $in; return "QUIT" if(parse_cmd($in) == -1);
return save($game_str) if parse_cmd($in) == 1;
return load() if parse_cmd($in) == 2;
$game_str .= $in."\n";
return $game_str;
} }
sub loop(){
my $in = prompt();
return 0 unless $in; sub loop($$){
return 0 if $in eq "quit"; my ($class, $game_str) = (shift, shift);
return 1; my $in;
$b = LPST->new();
$game_str = eval_game_str($b, $game_str);
printf("%s\n", $game_str);
$game_str = prompt($game_str);
return $game_str;
} }
return 1; return 1;

View File

@ -1,6 +1,6 @@
#!/usr/bin/perl #!/usr/bin/perl
package main; package NTVL;
use warnings; use warnings;
use strict; use strict;
@ -17,7 +17,6 @@ my $SACRIFICE = 0b00001000;
my $DRAW = 0b00010000; my $DRAW = 0b00010000;
my $FT = 0b00100001; my $FT = 0b00100001;
package NTVL;
sub is_summon($){ sub is_summon($){
return $_[0] =~ /\*/ ? $SUMMON : 0; return $_[0] =~ /\*/ ? $SUMMON : 0;

6
drv.pl
View File

@ -6,7 +6,11 @@ use strict;
use lib "./"; use lib "./";
use MNU; use MNU;
while(MNU->loop()){ my $game_str;
$game_str = "";
while(($game_str = MNU->loop($game_str)) ne "QUIT"){
# printf("XXXX%s\n", $game_str);
} }

58
test.pl
View File

@ -9,25 +9,49 @@ use LPST;
my $ns0; my $ns0;
# $ns0 = "";
# $ns0 .= "A I Im Au H\n";
# $ns0 .= "A S Im Rc It\n";
# $ns0 .= "Aa1*, Ab1*, Ac1*, Ad1*, Ae1*, Qf1*, Ag1*, Ah1*, Ai1*, Aj1*, Ak1*, Ad2*, Ai2*, Aj2*, Ak2*, Ad3*\n";
# $ns0 .= "Aa11*, Ab11*, Ac11*, Ad11*, Ae11*, Qf11*\n";
# $ns0 .= "Aa1a2\n";
# $ns0 .= "Qf11g11\n";
# $ns0 .= "Aa2a3\n";
# $ns0 .= "Qg11f10\n";
# $ns0 .= "Aa3a4\n";
# $ns0 .= "Qf10e10\n";
# $ns0 .= "Aa4a5\n";
# $ns0 .= "Qe10d10\n";
# $ns0 .= "Aa5a6\n";
# # $ns0 .= "Qd10e9\n";
$ns0 = ""; $ns0 = "";
$ns0 .= "A I Im Au H\n"; $ns0 .= "A I H It N\n";
$ns0 .= "A S Im Rc It\n"; $ns0 .= "A I H It N\n";
$ns0 .= "Aa1*, Ab1*, Ac1*, Ad1*, Ae1*, Qf1*, Ag1*, Ah1*, Ai1*, Aj1*, Ak1*, Ad2*, Ai2*, Aj2*, Ak2*, Ad3*\n"; $ns0 .= "Ac3*, Qd3*\n";
$ns0 .= "Aa11*, Ab11*, Ac11*, Ad11*, Ae11*, Qf11*\n"; $ns0 .= "Ac11*, Qd11*\n";
$ns0 .= "Aa1a2\n";
$ns0 .= "Qf11g11\n";
$ns0 .= "Aa2a3\n";
$ns0 .= "Qg11f10\n"; sub roll_back($){
$ns0 .= "Aa3a4\n"; my @arr;
$ns0 .= "Qf10e10\n"; my $game_str = shift;
$ns0 .= "Aa4a5\n";
$ns0 .= "Qe10d10\n"; @arr = split(/\n/, $game_str);
$ns0 .= "Aa5a6\n";
# $ns0 .= "Qd10e9\n"; if(scalar @arr == 1){
return "";
}
return join("\n", splice(@arr, 0, -1))
}
# printf("%s\n", $ns0);
# $ns0 = roll_back($ns0);
# printf("%s\n", $ns0);
$b = LPST->new(); $b = LPST->new();
NTVL->f1($b, $ns0); NTVL->f1($b, $ns0);
$b->shade_all_p2_mvmt(); # $b->shade_all_p2_mvmt();
$b->disp_board(); # $b->disp_board();