#!/usr/bin/perl # Perl Hangman # Terrence Ma # Modified from Perl Black Book # V1.1 08/22/2001, V1.0 08/25/2000 use CGI::Pretty; $co = new CGI; if ($co->param('newgame') eq "yes" || !$co->param('newgame')) { newgame(); } else { if($co->param('newgameyesno') eq "yes"){ newgame(); } else { $theanswer = $co->param('answer'); $theguess = getguess(); if($theguess eq "-"){ $thehits = $co->param('hits'); $themisses = $co->param('misses'); displayresult(); } else { $thehits = gethits(); if (index($thehits, "-") eq -1){ youwin(); } else { $themisses = getmisses(); if(length($themisses) >= 9){ youlose(); } else { displayresult(); } } } } } sub newgame { $datafile = "answers.dat"; open ANSWERDATA, $datafile; @answers = ; close (ANSWERDATA); srand(time ^ $$); $index1 = $#answers * rand; $theanswer = $answers[$index1]; chomp($theanswer); $themisses = "-"; $thehits = ""; for($loopindex = 0; $loopindex < length($theanswer); $loopindex++){ $thehits .= "-"; } displayresult(); } sub getguess { $theguess = "-"; if ($co->param('letters')){ $theguess = lc($co->param('letters')); } return $theguess; } sub displayresult { print $co->header, $co->start_html(-title=>'Perl Hangman', -bgcolor=>'#ffffff', -text=>'#000000'), # -link=>'#000000', -alink=>'#000000', -vlink=>'#000000'), $co->center( "", $co->h1('Perl Hangman'), $co->hr ); $len = length($themisses); if (-e "hang${len}.gif") { print $co->img({-src=>"hang${len}.gif", -align=>left, -vspace=>10, -hspace=>1}); } print $co->center( $co->h1($thehits), "", $co->h2("Misses (8 max): " . substr($themisses, 1)), $co->startform, $co->hidden(-name=>'newgame', -default=>"no", -override=>1), $co->hidden(-name=>'answer', -default=>"$theanswer", -override=>1), $co->hidden(-name=>'hits', -default=>"$thehits", -override=>1), $co->hidden(-name=>'misses', -default=>"$themisses", -override=>1), $co->br, "Guess a letter :", $co->br, ), "
", "A"; for ($loopindex = ord('B'); $loopindex <= ord('M'); $loopindex++) { $c = chr($loopindex); print "${c}"; } print $co->br; for ($loopindex = ord('N'); $loopindex <= ord('Z'); $loopindex++) { $c = chr($loopindex); print "${c}"; } print $co->br, $co->br, "Start new game :", $co->br, " Yes", " No", $co->br, $co->br, "          \n", "          \n", "   \n", $co->submit(-value=>'Submit'), "
", $co->endform, "
", $co->hr, "Program Source", $co->br, $co->end_html; } sub gethits { $temphits = $co->param('hits'); $thehits = ""; for($loopindex = 0; $loopindex < length($theanswer); $loopindex++){ $thechar = substr($temphits, $loopindex, 1); $theanswerchar = substr($theanswer, $loopindex, 1); if($theguess eq $theanswerchar){ $thechar = $theguess; } $thehits .= $thechar; } return $thehits; } sub getmisses { $themisses = $co->param('misses'); if(index($theanswer, $theguess) eq -1){ if(index($themisses, $theguess) eq -1){ $themisses .= $theguess; } } return $themisses; } sub youwin { print $co->header, $co->start_html(-title=>'Perl Hangman', -bgcolor=>'#ffffff', -text=>'#000000'), # -link=>'#000000', -alink=>'#000000', -vlink=>'#000000'), "
", "", $co->h1('Perl Hangman'), $co->hr, $co->br, "", ""; if (-e "hang10.gif") { print $co->img({-src=>"hang10.gif", -align=>left, -vspace=>10, -hspace=>1}); } print $co->h1("You got it : ", $theanswer), $co->h1("You win !"), $co->br, $co->br, $co->startform, $co->hidden(-name=>'newgame', -default=>"yes", -override=>1), $co->br, $co->br, $co->submit(-value=>'New Game'), $co->endform, "", "
", $co->br, $co->br, $co->hr, "Program Source", $co->br, $co->end_html; } sub youlose { print $co->header, $co->start_html(-title=>'Perl Hangman', -bgcolor=>'#ffffff', -text=>'#000000'), # -link=>'#000000', -alink=>'#000000', # -vlink=>'#000000'), "
", "", $co->h1('Perl Hangman'), $co->hr, $co->br, "", ""; if (-e "hang9.gif") { print $co->img({-src=>"hang9.gif", -align=>left, -vspace=>10, -hspace=>1}); } print $co->h1("The answer : ", $theanswer), $co->h1("Sorry, too many guesses taken !", $co->br, $co->br, "Better luck next time."), $co->br, $co->br, $co->startform, $co->hidden(-name=>'newgame', -default=>"yes", -override=>1), $co->br, $co->br, $co->submit(-value=>'New Game'), $co->br, $co->endform, "", "
", $co->hr, "Program Source", $co->br, $co->end_html; }