From 4d87b9001220ed76f0e6a31798fbe06678dcf4cf Mon Sep 17 00:00:00 2001 From: Austin White Date: Sun, 6 Mar 2022 00:17:00 -0700 Subject: [PATCH 1/5] perl batnum --- 08_Batnum/perl/batnum.pl | 93 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 08_Batnum/perl/batnum.pl diff --git a/08_Batnum/perl/batnum.pl b/08_Batnum/perl/batnum.pl new file mode 100644 index 00000000..8feba1ba --- /dev/null +++ b/08_Batnum/perl/batnum.pl @@ -0,0 +1,93 @@ +use strict; +use warnings; + +my %WIN_OPTIONS = ( + "UNDEFINED" => 0, + "TAKE_LAST" => 1, + "AVOID_LAST" => 2, +); + +my %START_OPTIONS = ( + "UNDEFINED" => 0, + "COMPUTER_FIRST" => 1, + "PLAYER_FIRST" => 2, +); + +sub run { + my $pile_size = undef; + my $min_select = undef; + my $max_select = undef; + my $win_option = undef; + my $start_option = undef; + + # while (1) { + write_intro(); + + ($pile_size, $min_select, $max_select, $win_option, $start_option) + = &get_user_input(); + + play($pile_size, $min_select, $max_select, $win_option, $start_option); + # } +} + +sub write_intro { + printf "%33s", "BATNUM\n"; + printf "%15s", "CREATIVE COMPUTING MORRISSTOWN, NEW JERSEY\n"; + printf "%s", "\n"; + printf "%s", "\n"; + printf "%s", "\n"; + printf "%s", "THIS PROGRAM IS A 'BATTLE OF NUMBERS' GAME, WHERE THE\n"; + printf "%s", "COMPUTER IS YOUR OPPONENT.\n"; + printf "%s", "\n"; + printf "%s", "THE GAME STARTS WITH AN ASSUMED PILE OF OBJECTS. YOU\n"; + printf "%s", "AND YOUR OPPONENT ALTERNATELY REMOVE OBJECTS FROM THE PILE.\n"; + printf "%s", "WINNING IS DEFINED IN ADVANCE AS TAKING THE LAST OBJECT OR\n"; + printf "%s", "NOT. YOU CAN ALSO SPECIFY SOME OTHER BEGINNING CONDITIONS.\n"; + printf "%s", "DON'T USE ZERO, HOWEVER, IN PLAYING THE GAME.\n"; + printf "%s", "ENTER A NEGATIVE NUMBER FOR NEW PILE SIZE TO STOP PLAYING.\n"; + printf "%s", "\n"; +} + +# TODO: check input +sub get_user_input { + my $pile_size = 0; + my $min_select = 0; + my $max_select = 0; + my $win_option = $WIN_OPTIONS{ "UNDEFINED" }; + my $start_option = $START_OPTIONS{ "UNDEFINED" }; + + while ($pile_size < 1) { + printf "%s", "ENTER PILE SIZE: "; + $pile_size = <>; + $pile_size *= 1; + } + + while ($win_option eq $WIN_OPTIONS{ "UNDEFINED" }) { + printf "%s", "ENTER WIN OPTION - 1 TO TAKE LAST, 2 TO AVOID LAST: "; + $win_option = <>; + $win_option *= 1; + } + + # get min select max select + + while ($start_option eq $START_OPTIONS{ "UNDEFINED" }) { + printf "%s", "ENTER START OPTION - 1 COMPUTER FIRST, 2 YOU FIRST: "; + $start_option = <>; + $start_option *= 1; + } + + return ($pile_size, $min_select, $max_select, $win_option, $start_option); +} + +sub play { + my $pile_size = shift; + my $min_select = shift; + my $max_select = shift; + my $win_option = shift; + my $start_option = shift; + + print("pile_size: $pile_size\n"); + print("win_option: $win_option\n"); +} + +run(); From 4b675ebb3bbb11444909587d969d91f015e0d979 Mon Sep 17 00:00:00 2001 From: Austin White Date: Sun, 6 Mar 2022 01:59:46 -0700 Subject: [PATCH 2/5] perl batnum --- 08_Batnum/perl/batnum.pl | 127 +++++++++++++++++++++++++++++++++++---- 1 file changed, 114 insertions(+), 13 deletions(-) diff --git a/08_Batnum/perl/batnum.pl b/08_Batnum/perl/batnum.pl index 8feba1ba..0d7b0fbc 100644 --- a/08_Batnum/perl/batnum.pl +++ b/08_Batnum/perl/batnum.pl @@ -13,6 +13,7 @@ my %START_OPTIONS = ( "PLAYER_FIRST" => 2, ); + sub run { my $pile_size = undef; my $min_select = undef; @@ -20,16 +21,19 @@ sub run { my $win_option = undef; my $start_option = undef; - # while (1) { + while (1) { write_intro(); ($pile_size, $min_select, $max_select, $win_option, $start_option) = &get_user_input(); play($pile_size, $min_select, $max_select, $win_option, $start_option); - # } + + printf "\n"; + } } + sub write_intro { printf "%33s", "BATNUM\n"; printf "%15s", "CREATIVE COMPUTING MORRISSTOWN, NEW JERSEY\n"; @@ -48,7 +52,7 @@ sub write_intro { printf "%s", "\n"; } -# TODO: check input + sub get_user_input { my $pile_size = 0; my $min_select = 0; @@ -58,27 +62,29 @@ sub get_user_input { while ($pile_size < 1) { printf "%s", "ENTER PILE SIZE: "; - $pile_size = <>; - $pile_size *= 1; + $pile_size = ; + } + + while ($min_select < 1 || $max_select < 1 || $min_select > $max_select) { + printf "%s", "ENTER MIN AND MAX: "; + my $raw_input = ; + ($min_select, $max_select) = split(' ', $raw_input); } while ($win_option eq $WIN_OPTIONS{ "UNDEFINED" }) { printf "%s", "ENTER WIN OPTION - 1 TO TAKE LAST, 2 TO AVOID LAST: "; - $win_option = <>; - $win_option *= 1; + $win_option = ; } - # get min select max select - while ($start_option eq $START_OPTIONS{ "UNDEFINED" }) { printf "%s", "ENTER START OPTION - 1 COMPUTER FIRST, 2 YOU FIRST: "; - $start_option = <>; - $start_option *= 1; + $start_option = ; } return ($pile_size, $min_select, $max_select, $win_option, $start_option); } + sub play { my $pile_size = shift; my $min_select = shift; @@ -86,8 +92,103 @@ sub play { my $win_option = shift; my $start_option = shift; - print("pile_size: $pile_size\n"); - print("win_option: $win_option\n"); + my $game_over = 0; + my $players_turn = $start_option eq $START_OPTIONS{ "PLAYER_FIRST" } ? 1 : 0; + + while (!$game_over) { + if ($players_turn) { + ($game_over, $pile_size) = players_move( + $pile_size, $min_select, $max_select, $win_option); + + $players_turn = 0; + + if ($game_over) { + return; + } + } else { + ($game_over, $pile_size) = computers_move( + $pile_size, $min_select, $max_select, $win_option); + + $players_turn = 1; + } + } + + return; } + +sub players_move { + my $pile_size = shift; + my $min_select = shift; + my $max_select = shift; + my $win_option = shift; + + my $finished = 0; + + while (!$finished) { + my $remove_amount = undef; + + printf "%s", "YOUR MOVE: "; + $remove_amount = <>; + + if ($remove_amount eq 0) { + printf "%s", "I TOLD YOU NOT TO USE ZERO! COMPUTER WINS BY FORFEIT."; + return (1, $pile_size); + } elsif ($remove_amount > $max_select || $remove_amount < $min_select) { + printf "%s", "ILLEGAL MOVE, TRY AGAIN.\n"; + next; + } else { + $pile_size -= $remove_amount; + $finished = 1; + } + + if ($pile_size <= 0) { + if ($win_option eq $WIN_OPTIONS{ "AVOID_LAST" }) { + printf "%s", "TOUGH LUCK, YOU LOSE.\n"; + } else { + printf "%s", "CONGRATULATIONS, YOU WIN.\n"; + } + return (1, $pile_size); + } + } + + return (0, $pile_size); +} + + +sub computers_move { + my $pile_size = shift; + my $min_select = shift; + my $max_select = shift; + my $win_option = shift; + + if ($win_option eq $WIN_OPTIONS{ "TAKE_LAST" } && $pile_size <= $max_select) { + printf "COMPUTER TAKES %d AND WINS.\n", $pile_size; + return (1, $pile_size); + } + + if ($win_option eq $WIN_OPTIONS{ "AVOID_LAST" } && $pile_size <= $min_select) { + printf "COMPUTER TAKES %d AND LOSES.\n", $pile_size; + return (1, $pile_size); + } + + my $remove_amount = get_computer_remove_amount($min_select, $max_select); + + $pile_size -= $remove_amount; + + printf "COMPUTER TAKES %d AND LEAVES %d.\n", $remove_amount, $pile_size; + + return (0, $pile_size); + +} + + +sub get_computer_remove_amount { + my $min_select = shift; + my $max_select = shift; + + return (int(rand($max_select - $min_select)) + $min_select); +} + + run(); From f3b5f54de7d66ef6cce26a503d424b94f31a60de Mon Sep 17 00:00:00 2001 From: Austin White Date: Sun, 6 Mar 2022 02:29:54 -0700 Subject: [PATCH 3/5] documentation --- 08_Batnum/perl/batnum.pl | 73 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/08_Batnum/perl/batnum.pl b/08_Batnum/perl/batnum.pl index 0d7b0fbc..a384b963 100644 --- a/08_Batnum/perl/batnum.pl +++ b/08_Batnum/perl/batnum.pl @@ -15,6 +15,14 @@ my %START_OPTIONS = ( sub run { + # input: no input perameters + # + # output: nothing returned + # + # description: This is the primary game loop. Once a game is concluded + # another will begin right away until the exeecution + # is terminated. + my $pile_size = undef; my $min_select = undef; my $max_select = undef; @@ -35,6 +43,12 @@ sub run { sub write_intro { + # input: no input perameters + # + # output: nothing returned + # + # description: This subroutine prints the intro and rules. + printf "%33s", "BATNUM\n"; printf "%15s", "CREATIVE COMPUTING MORRISSTOWN, NEW JERSEY\n"; printf "%s", "\n"; @@ -54,6 +68,22 @@ sub write_intro { sub get_user_input { + # input: no input perameters + # + # output: (int) pile_size + # (int) min_select + # (int) max_select + # (int) win_option + # (int) start_option + # + # description: This subroutine gets the necessary perametes from the player. + # + # pile_size (int > 0) + # min_select (int > 0) max_select (int > 0) + # -> min/max, space delimated + # win_option (int 1|2) + # start_option (int 1|2) + my $pile_size = 0; my $min_select = 0; my $max_select = 0; @@ -86,6 +116,17 @@ sub get_user_input { sub play { + # input: (int) pile_size + # (int) min_select + # (int) max_select + # (int) win_option + # (int) start_option + # + # output: nothing returned + # + # description: This is where the game logic lives. The player and computer + # both take turns until the current game is over. + my $pile_size = shift; my $min_select = shift; my $max_select = shift; @@ -118,6 +159,16 @@ sub play { sub players_move { + # input: (int) pile_size + # (int) min_select + # (int) max_select + # (int) win_option + # + # output: (boolean) game is over + # (int) new pile_size + # + # description: This subroutine handles the players move. + my $pile_size = shift; my $min_select = shift; my $max_select = shift; @@ -157,6 +208,16 @@ sub players_move { sub computers_move { + # input: (int) pile_size + # (int) min_select + # (int) max_select + # (int) win_option + # + # output: (boolean) game is over + # (int) new pile_size + # + # description: This subroutine handles the computers move. + my $pile_size = shift; my $min_select = shift; my $max_select = shift; @@ -179,16 +240,24 @@ sub computers_move { printf "COMPUTER TAKES %d AND LEAVES %d.\n", $remove_amount, $pile_size; return (0, $pile_size); - } sub get_computer_remove_amount { + # input: (int) min_select + # (int) max_select + # + # output: (int) random number (x) where, + # min_select <= x <= max_select + # + # description: This subroutine generates the amount of items the computer + # will remove. + my $min_select = shift; my $max_select = shift; return (int(rand($max_select - $min_select)) + $min_select); } - +# start the game run(); From de14f133e18bbae95bac0cac0cb88a5737f1ae42 Mon Sep 17 00:00:00 2001 From: Austin White Date: Sun, 6 Mar 2022 02:36:27 -0700 Subject: [PATCH 4/5] fix edge case: comptuer picks all remaining items --- 08_Batnum/perl/batnum.pl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/08_Batnum/perl/batnum.pl b/08_Batnum/perl/batnum.pl index a384b963..4280bf38 100644 --- a/08_Batnum/perl/batnum.pl +++ b/08_Batnum/perl/batnum.pl @@ -237,7 +237,12 @@ sub computers_move { $pile_size -= $remove_amount; - printf "COMPUTER TAKES %d AND LEAVES %d.\n", $remove_amount, $pile_size; + if ($pile_size <= 0) { + printf "COMPUTER TAKES %d AND WINS.\n", $remove_amount; + return (1, $pile_size); + } else { + printf "COMPUTER TAKES %d AND LEAVES %d.\n", $remove_amount, $pile_size; + } return (0, $pile_size); } From d9947349029645059df43e8d76d4805209c7eb3f Mon Sep 17 00:00:00 2001 From: Austin White Date: Sun, 6 Mar 2022 02:49:14 -0700 Subject: [PATCH 5/5] update README --- 08_Batnum/perl/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/08_Batnum/perl/README.md b/08_Batnum/perl/README.md index e69c8b81..8e05356a 100644 --- a/08_Batnum/perl/README.md +++ b/08_Batnum/perl/README.md @@ -1,3 +1,3 @@ Original source downloaded [from Vintage Basic](http://www.vintage-basic.net/games.html) -Conversion to [Perl](https://www.perl.org/) +Conversion to [Perl](https://www.perl.org/) by [Austin White](https://github.com/austinwhite)