diff --git a/.gitignore b/.gitignore index dc11994..b3380b9 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ p[0-9].pl p[0-9][0-9].pl notes.txt -#* \ No newline at end of file +#* +save.txt \ No newline at end of file diff --git a/LPST.pm b/LPST.pm index d77325c..681ee2b 100644 --- a/LPST.pm +++ b/LPST.pm @@ -1,5 +1,7 @@ #!/usr/bin/perl +package LPST; + use warnings; use strict; diff --git a/MNU.pm b/MNU.pm index 87f4393..04aaf82 100644 --- a/MNU.pm +++ b/MNU.pm @@ -1,5 +1,7 @@ #!/usr/bin/perl +package MNU; + use warnings; use strict; @@ -7,23 +9,96 @@ use lib "./"; use NTVL; use LPST; -package MNU; +use Try::Tiny; -sub prompt(){ +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(){ + $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; + printf("> "); my $in; $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; - return 0 if $in eq "quit"; - return 1; +sub loop($$){ + my ($class, $game_str) = (shift, shift); + 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; diff --git a/NTVL.pm b/NTVL.pm index 0a7a44a..28f727a 100644 --- a/NTVL.pm +++ b/NTVL.pm @@ -1,6 +1,6 @@ #!/usr/bin/perl -package main; +package NTVL; use warnings; use strict; @@ -17,7 +17,6 @@ my $SACRIFICE = 0b00001000; my $DRAW = 0b00010000; my $FT = 0b00100001; -package NTVL; sub is_summon($){ return $_[0] =~ /\*/ ? $SUMMON : 0; diff --git a/drv.pl b/drv.pl index 66c0644..b0c71b9 100644 --- a/drv.pl +++ b/drv.pl @@ -6,7 +6,11 @@ use strict; use lib "./"; 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); } diff --git a/test.pl b/test.pl index 387015e..0e1ab53 100644 --- a/test.pl +++ b/test.pl @@ -9,25 +9,49 @@ use LPST; 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 .= "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 .= "A I H It N\n"; +$ns0 .= "A I H It N\n"; +$ns0 .= "Ac3*, Qd3*\n"; +$ns0 .= "Ac11*, Qd11*\n"; + + + +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)) +} + +# printf("%s\n", $ns0); +# $ns0 = roll_back($ns0); +# printf("%s\n", $ns0); $b = LPST->new(); - NTVL->f1($b, $ns0); -$b->shade_all_p2_mvmt(); -$b->disp_board(); +# $b->shade_all_p2_mvmt(); +# $b->disp_board();