116 lines
1.9 KiB
Perl
116 lines
1.9 KiB
Perl
#!/usr/bin/perl
|
|
|
|
package MNU;
|
|
|
|
use warnings;
|
|
use strict;
|
|
|
|
use lib "./";
|
|
use NTVL;
|
|
use LPST;
|
|
|
|
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);
|
|
$b->disp_board();
|
|
} 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 3 if($in =~ /^show$/i);
|
|
return 0;
|
|
}
|
|
|
|
sub show($$){
|
|
my ($b, $game_str) = (shift, shift);
|
|
$b->shade_all_p1_mvmt();
|
|
disp_board($b);
|
|
return prompt($b, $game_str);
|
|
}
|
|
|
|
|
|
sub prompt($$){
|
|
my ($b, $game_str) = (shift, shift);
|
|
|
|
printf("> ");
|
|
my $in;
|
|
$in = <STDIN>;
|
|
chomp $in;
|
|
|
|
return "QUIT" if(parse_cmd($in) == -1);
|
|
return save($game_str) if parse_cmd($in) == 1;
|
|
return load() if parse_cmd($in) == 2;
|
|
return show($b, $game_str) if parse_cmd($in) == 3;
|
|
|
|
$game_str .= $in."\n";
|
|
return $game_str;
|
|
}
|
|
|
|
|
|
sub loop($$){
|
|
my ($class, $game_str) = (shift, shift);
|
|
my $in;
|
|
my $b;
|
|
|
|
$b = LPST->new();
|
|
$game_str = eval_game_str($b, $game_str);
|
|
printf("%s\n", $game_str);
|
|
|
|
$game_str = prompt($b, $game_str);
|
|
return $game_str;
|
|
}
|
|
|
|
return 1;
|