We were assigned a game of hangman for our Perl class. In this Perl script, we must use an html page to get the user's name. I feel like I'm getting awfully close, but I can't quite seem to get the userName value to print after each time the user clicks a letter within the game. I would appreciate any help someone could provide!
HTML
<form action="hangman3.pl" method="post">
<h1>Welcome to Hangman!</h1>
<p>Please enter your name</p>
<input type="text" name="userName" />
<input type="submit" value="Submit" />
</form>
PERL
#!/usr/bin/perl
use CGI ':standard';
use Switch;
print header();
@unusedLetters = (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,"Q",R,S,T,U,V,W,X,Y,Z);
@usedLetters = ();
$letterChosen = param('letterChosen');
$numWrong = param('numWrong');
$wordNumber = param('wordNumber');
@usedLetters = split(' ',param('usedLetters'));
$userName = param('player');
if (!defined($letterChosen)) {
$word = getNewWord();
$numWrong = 0;
@unusedLetters = (A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,"Q",R,S,T,U,V,W,X,Y,Z);
@usedLetters = ();
}
else {
$word = getWord();
$numWrong ++;
push(@usedLetters, $letterChosen);
}
$numLetters = length($word);
print "The word is $word with $numLetters characters <br>";
print "The letter chosen is $letterChosen<br>";
foreach $letter (@usedLetters) {
print "The used letters so far are '$letter' <br>";
}
print "<hr>";
print "<h1><center><a href='player.html'>Enter your name</a></center></h1><br>";
print "<br>";
print "Player's name: $userName<br>";
print "<hr>";
startOverButton();
#lab 5 stuff
printImage();
printWord();
print "<br>";
printUnusedLetters();
exit(0);
#
#Subroutines
#
sub getNewWord {
#stub
#We know there are 5 words in the file
$wordNumber = int(rand(5));
return getWord();
}
sub getWord {
open (FILE,"wordfile.txt");
for ($i=0; $i<$wordNumber; $i++) {
$newWord = <FILE>;
}
chomp($newWord);
close FILE;
return $newWord;
}
sub printUnusedLetters {
print "<table align=center>";
print "<tr>";
print "<form method=get>";
foreach $letter (@unusedLetters) {
print "<td>";
#print
if ( !inUsedList($letter) ) {
print "<input type=submit name='letterChosen' value='$letter' />";
}
else {
print "";
}
print "</td>";
if ($letter eq 'M') {
print "</tr>";
print "<tr>";
}
}
print "<input type=hidden name=usedLetters value='@usedLetters' />";
print "<input type=hidden name=numWrong value='$numWrong' />";
print "<input type=hidden name=wordNumber value='$wordNumber' />";
print "<input type=hidden name=userName value='$userName' />";
print "</form>";
print "</tr>";
print "</table>";
}
sub startOverButton {
print "<form><input type=submit value='Start Over'></form>";
}
sub printWord {
print "<table align=center>";
print "<tr>";
for ($pos = 0; $pos < $numLetters; $pos ++) {
print "<td>";
if ($letter = inList($pos)) {
print " ", $letter, " ";
}
else {
print "____ ";
}
print "</td>";
}
print "</tr>";
print "</table>";
}
sub printImage {
switch ($numWrong) {
case 0 {
print "<img src='hang0.gif'>";
}
case 1 {
print "<img src='hang1.gif'>";
}
case 2 {
print "<img src='hang2.gif'>";
}
case 3 {
print "<img src='hang3.gif'>";
}
case 4 {
print "<img src='hang4.gif'>";
}
case 5 {
print "<img src='hang5.gif'>";
}
case 6 {
print "<img src='hang6.gif'>";
}
case 7 {
print "<img src='hang10.gif'>";
print "<br><br>You LOSE";
}
}
}
sub inList {
my ($pos) = @_; #$pos equals whatever was passed
@letters = split (//, $word);
$letter = $letters[$pos];
foreach $usedLetter (@usedLetters) {
if ($letter eq $usedLetter) {
return $letter;
}
}
return 0;
}
sub inUsedList {
my ($letter) = @_;
foreach $usedLetter (@usedLetters) {
if ($letter eq $usedLetter) {
return $letter;
}
}
return 0;
}