Ik wil dus met behulp van msn.pm en Perl een MSN botje maken, waarmee je hangman kan speluh, maar dat lukt niet echt, kan iemand mij vertellen wat ik fout doe?
Hangman.pl
Hij geeft dan onderstaande foutmelding
Hangman.pl
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
| sub hangman {
$victim = shift;
$msg = shift;
my $dict='c:\words.txt'; #change this later??
my $guesses=6;
my $word;
my $flags;
my $count;
my @alpha;
my $g;
my $i;
open(WORDS,"<$dict") || die "Cannot read from $dict: $!\n";
srand; # seed the random generator
rand($.) < 1 && ($word = $_) while <WORDS>;
close(WORDS);
chomp($word);
$word =~ tr/[A-Z]/[a-z]/;
$flags = $word;
$count = 0;
while($count<$guesses) {
$reply = "\nBad guesses: $count (upto $guesses allowed)\n";
$$self->sendmsg($victim, "$reply");
$reply = "";
for($i=0;$i<length($word);$i++){
if(substr($flags,$i,1) ne '1'){$reply .= '_'}
else{
$reply = substr($word,$i,1);
}
}
$$self->sendmsg("$reply");
$reply = "\nYour guesses:\n\n ";
foreach (@alpha) {
$reply .= $_;
}
$$self->sendmsg($victim, "$reply");
while(join('',@alpha)=~/$g/) {
$$self->sendmsg($victim, "Already guessed\n");
$$self->sendmsg($victim, "\nNext letter: ");
chomp($_ = $msg);
tr/[A-Z]/[a-z]/;
$g=$_;
}
push(@alpha,$g);
if($flags=~/$g/) {
$flags=~s/$g/1/g;
}
else {
$count++;
}
if($flags!~/[a-z]/) {
$$self->sendmsg($victim, "\nYou got it!");
last;
}
$reply = "The word was `$word'\n";
$$self->sendmsg($victim, "$reply");
}
return $reply;
}
1; |
Hij geeft dan onderstaande foutmelding
code:
1
2
3
4
| print() on closed filehandle MSN::LOG at C:/Perl/lib/MSN.pm line 77, <GEN3> line 5. Can't call method "sendmsg" on an undefined value at commands/hangman.pl line 28 . |