commit 4f0a8be35308a047a6fe8376510f5e7a78eeb51f
parent 382b15e831c01a672b257c9e0388f48a412bab4b
Author: Samir Parikh <noreply@samirparikh.com>
Date: Fri, 18 Feb 2022 02:47:55 +0000
get basic game play to work
Diffstat:
M | guess_number.cgi | | | 122 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------- |
1 file changed, 102 insertions(+), 20 deletions(-)
diff --git a/guess_number.cgi b/guess_number.cgi
@@ -7,6 +7,8 @@ use CGI;
use constant TRIES => 5;
my $game = CGI->new;
+# state maintenance subroutines
+
sub get_state {
return undef unless $game->param();
my $state = {};
@@ -27,6 +29,19 @@ sub initialize {
return $state;
}
+sub save_state {
+ my $state = shift;
+ foreach ( qw[ NUMBER GAMENO GUESSES_LEFT WON GUESSED ] ) {
+ print $game->hidden(
+ -name => $_,
+ -value => $state->{ $_ },
+ -override => 1
+ );
+ }
+}
+
+# game play logic
+
sub process_guess {
my ( $guess, $state ) = @_;
@@ -34,7 +49,7 @@ sub process_guess {
my %guessed = map { $_ => 1 } split /,/, $state->{GUESSED};
# do nothing if user did not enter a guess
- return ("Did not enter a guess", 'continue') unless $guess;
+ return ('', 'continue') unless $guess;
# ensure guess is a valid number between 1 and 100
return ("Not a valid guess.", 'error')
@@ -46,27 +61,47 @@ sub process_guess {
# we have a valid guess. update list of guesses
$guessed{ $guess } = 1;
- $state->{GUESSED} = join ',' sort keys %guessed;
+ $state->{GUESSED} = join ',', sort keys %guessed;
$state->{GUESSES_LEFT}--;
- # incorrect guess and out of tries
+ # the order of the checks is important here. At this point, it's
+ # possible that the player has used their last guess. Therefore,
+ # we first check whether the guess is correct
+
+ # guess is correct!
+ return ("Congratulations! You guessed that the correct number
+ was $state->{NUMBER}!", 'won')
+ if ( $guess == $state->{NUMBER} );
+
+ # if the guess is not correct AND we are out of guesses...
return ("Sorry, you ran out of turns. The number was $state->{NUMBER}.",
'lost') unless ($state->{GUESSES_LEFT} && $guess != $state->{NUMBER});
+ # otherwise...
# guess too low
return ("Your guess is too low.", 'continue')
if ( $guess < $state->{NUMBER} );
+ # ...or
# guess is too high
return ("Your guess is too high.", 'continue')
if ( $guess > $state->{NUMBER} );
- # guess is correct!
- return ("Congratulations! You guessed that the correct number
- was $state->{NUMBER}!", 'won')
- if ( $guess == $state->{NUMBER} );
+}
+# html generation subroutines
+
+sub print_header {
+ print $game->header(
+ -type => "text/html",
+ -charset => 'utf-8',
+ );
+ print $game->start_html(
+ -title => "Guess a Secret Number!",
+ -onLoad => 'if (document.gf) document.gf.guess.focus()'
+ );
+ print $game->h1( 'Guess a Secret Number Interactive Game' );
}
sub print_status {
@@ -74,17 +109,62 @@ sub print_status {
# draw table
print
- $game->table( { -border=>"1", -width=>"100%" },
- $game->tr(
+ $game->table( { -border=>"0", -width=>"50%" },
+ $game->Tr(
$game->td( "Game #: ", $state->{GAMENO} ),
$game->td( "Wins: ", $state->{WON} )
),
- $game->tr(
+ $game->Tr(
$game->td( "Guesses Left: ", $state->{GUESSES_LEFT} ),
$game->td( "Numbers Guessed: ", $state->{GUESSED} )
)
);
- print $game->h2( font( {-color => 'red'}, $message )) if $message;
+# print $game->p( "Game #: ", $state->{GAMENO} );
+# print $game->p( "Wins: ", $state->{WON} );
+# print $game->p( "Guesses Left: ", $state->{GUESSES_LEFT} );
+# print $game->p( "Numbers Guessed: ", $state->{GUESSED} );
+ print $game->h2( $game->font( {-color => 'red'}, $message ))
+ if $message;
+}
+
+sub show_restart_form {
+ my $state = shift;
+ print $game->start_form();
+ print "Do you want to play again?";
+ print $game->submit (
+ -name => 'restart',
+ -value => 'Play again'
+ );
+ save_state( $state );
+ print $game->end_form();
+}
+
+sub show_guess_form {
+ my $state = shift;
+ print $game->start_form( -name => 'gf' );
+ print "Your guess: ";
+ print $game->textfield (
+ -name => 'guess',
+ -value => '',
+ -override => 1
+ );
+ print $game->submit (
+ -name => '_guess',
+ -value => 'Guess'
+ );
+ save_state( $state );
+ print $game->end_form();
+}
+
+sub print_footer {
+ print $game->hr;
+ print $game->p(
+ #{ -style => "fontsize: 10pt" },
+ 'inspired by Hangman game from ',
+ 'Writing Apache Modules with Perl and C',
+ ' by Lincoln Stein and Doug MacEachern.'
+ );
+ print $game->end_html();
}
# begin main program
@@ -103,12 +183,14 @@ my ( $message, $status ) =
process_guess( $game->param( 'guess' ) || '', $state );
# generate the HTML page
-print $game->header(
- -type => "text/html",
- -charset => 'utf-8',
-);
-print $game->start_html(
- -title => "Guess a Secret Number!",
- -onLoad => 'if (document.gf) document.gf.guess.focus()',
-);
-print $game->h1( 'Guess a Secret Number Interactive Game' );
+print_header();
+print_status( $message, $state );
+
+# prompt user
+if ( $status =~ /^(won|lost)$/ ) {
+ show_restart_form( $state );
+} else {
+ show_guess_form( $state );
+}
+
+print_footer();