CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Problem with loops

 



alfumao
Novice


Nov 3, 2010, 5:45 PM

Post #1 of 8 (1091 views)
Problem with loops Can't Post

I've tried to create a typical Hangman game script, but there is some issue with my loops I can't control. The program seems to perform all he operations I expected so far, but for each letter entered, it iterates each position of the word instead of searching for the right position and next for another input letter.
My question is not strictly about the game but about avoiding the loop to do those iterations (it must be an issue with the while loop I can't seem figure out).
Please somebody help me to solve this.

Here is my code:

#!/usr/bin/perl
use strict;
use warnings;

print "Enter a word to be guessed:\n";
chomp(my $word = <STDIN>);
my @word = split ('', $word);
my @hidden = ((0) x scalar (@word));

my @toy = ("","O","-","|","-","<");
my @palis =();
my $i = 0;
my $wrong = 0;
my $right = 0;

while (($wrong < 5)){ #full process loop
print"Enter a letter:\n";
chomp(my $guess = <STDIN>);
foreach $i (0..$#word){
if ($guess eq $word[$i]){ #Right guess loop
$right++;
$word[$i] = $guess;
$hidden[$i] = $guess;
&occult($i, $guess, @hidden);
print "Good Guess!\n";
print "@hidden\n";
next;
}
elsif ($guess ne $word[0..$#word]){ #Wrong guess loop
print "Wrong guess!\n";
$wrong++;
my $muneco = shift(@toy);
push (@palis,$muneco);
print"@palis\n";
next;
}
}
}

sub occult{ #hidden word printing subroutine
my($i,$guess, @hidden)=@_;
$hidden[$i] = $guess;
return @hidden;
}

I may be asking something very obvious, but I'm going crazy without finding an appropriate solution

Thanks in advance ;)


BillKSmith
Veteran

Nov 4, 2010, 7:11 AM

Post #2 of 8 (1076 views)
Re: [alfumao] Problem with loops [In reply to] Can't Post

Your most serious problem is that the 'wrong' processing is in the wrong place. It should not be inside the foreach block at all. If the foreach loop fails to find any matches, the guess is 'wrong'. There are other lesser errors. Fix the big one first. You may even fix some of the smaller ones in the process.



Why did you choose to use arrays of characters rather than strings? String handling is one of perl's greatest strengths.
Good Luck,
Bill


alfumao
Novice


Nov 4, 2010, 8:48 AM

Post #3 of 8 (1072 views)
Re: [BillKSmith] Problem with loops [In reply to] Can't Post

Dear Bill,

thanks for helping me on my first correction step. I'll keep trying to correct the other mistakes because it still doesn't work as it should.
About why using arrays instead of strings, it's because this exercise is supposed to be done using arrays, it's required by the book I'm reading in order to learn some PERL...I guess you refer to regular expressions, substitutions and so on (forgive me if I'm wrong, I've no previous programming experience and its a little bit hard for me to learn PERL from a scratch) but I haven't considered this issue from that point of view (I will if I'm ever able to make this script work properly).
Thank you again for your kind help and for giving me another point of view on this subject.


alfumao
Novice


Nov 6, 2010, 6:16 AM

Post #4 of 8 (1050 views)
Re: [alfumao] Problem with loops [In reply to] Can't Post

I've tried to fix the program, but still have a problem with the loop that prints the "hangman toy", because it keeps printing parts of the to while the letter entered is not the first letter of the word...I guess I still need more help...sorry for the inconvenience, but i didn't seem to find some of the mistakes BillKSmith warned me about.

Can somebody , please, Help Me?

Here's the new code:

#!/usr/bin/perl
use strict;
use warnings;

print "Enter secret word:\n";
chomp(my $word = <STDIN>);
my @word = split ('', $word);
my @hidden = ((0) x scalar (@word));

my @toy = ("","O","-","|","-","<");
my @palis =();
my $i = 0;
my $wrong = 0;
my $right = 0;

CORE:
while (($wrong < 6)){ #full process loop
print"Enter a letter:\n";
chomp(my $guess = <STDIN>);
foreach $i (0..$#word){
if ($guess eq $word[$i]){ #Right guess loop
$right++;
$word[$i] = $guess;
$hidden[$i] = $guess;
&occult($i, $guess, @hidden);
print "Good Guess!\n";
print "@hidden\n";
next;
}
}
if ($guess eq $word[$i]){ #Wrong guess loop
next CORE;
} else {
print "Wrong guess!\n";
$wrong++;
my $muneco = shift(@toy);
push (@palis,$muneco);
print"@palis\n";
next;
}
}
&guess_word;

sub occult{ #hidden word printing subroutine
my($i,$guess, @hidden)=@_;
$hidden[$i] = $guess;
return @hidden;
}

sub guess_word{
my $hidden = join ("", @hidden);
my $f_word = join ("", @word);
if ($f_word =~ /$hidden/){
print"YOU WIN\!\n";
}
else {
print "Try to guess the word:\n";
chomp(my $g_word = <STDIN>);
if ($g_word =~ /$word/){
print"YOU WIN\!\n";
}
}
}


(This post was edited by alfumao on Nov 6, 2010, 6:49 AM)


BillKSmith
Veteran

Nov 6, 2010, 8:04 AM

Post #5 of 8 (1042 views)
Re: [alfumao] Problem with loops [In reply to] Can't Post

You are making progress, but you did not finish what I told you before.


Quote
If the foreach loop fails to find any matches, the guess is 'wrong'.



Define a new variable $is_a_good_guess and set if to false before the foreach loop. Set it to true when you find a match. Test that variable in the statement marked "wrong guess loop".



I cannot tell what you expect the subroutine occult to do. It cannot do anything useful when it is called in a null context.
Good Luck,
Bill


alfumao
Novice


Nov 10, 2010, 7:19 AM

Post #6 of 8 (1012 views)
Re: [BillKSmith] Problem with loops [In reply to] Can't Post

Dear Bill,

I have modified the program, I guess that's what you told me to do ;)
Thanks for your help and patience.

#!/usr/bin/perl
use strict;
use warnings;

print "Enter your secret word:\n";
chomp(my $word = <STDIN>);
my @word = split ('', $word);
my @hidden = ((0) x scalar (@word));

my @toy = ("","O","-","|","-","<");
my @palis =();
my $i = 0;
my $wrong = 0;
my $right = 0;

CORE: #MAIN LOOP
while (($wrong < 6)){
print"Enter a letter:\n";
chomp(my $guess = <STDIN>);
my $right_guess = 0;
foreach $i (0..$#word){
if ($guess eq $word[$i]){ #Correct LOOP
$right++;
$right_guess = 1;
$word[$i] = $guess;
$hidden[$i] = $guess;
print "RIGHT!\n";
print "@hidden\n";
&compare;
next;
}
}
if ($right_guess){ #Wrong LOOP
$right_guess = 1;
next CORE;
}
else {
if (! $right_guess){
$wrong++;
print "WRONG guess!\n";
my $muneco = shift(@toy);
push (@palis,$muneco);
print"@palis\n";
next;
}
}
}
&guess_word;

sub compare{ #Final
my $hidden = join ("", @hidden);
my $e_word = join ("", @word);
if ($e_word =~ /$hidden/){
print"RIGHT\! -> You WIN\!\n!\n";
exit;
}
}
sub guess_word{ #Last chance
my $hidden = join ("", @hidden);
my $f_word = join ("", @word);
if ($f_word =~ /$hidden/){
print"You WIN\!\n";
return 1;
}else {
print "Guess the right word:\n";
chomp(my $g_word = <STDIN>);
if ($g_word =~ /$word/){
print"You WIN\!\n";
return 1;
} else {
print "HUNG\!";
exit;
}
}
}


BillKSmith
Veteran

Nov 10, 2010, 10:40 AM

Post #7 of 8 (1008 views)
Re: [alfumao] Problem with loops [In reply to] Can't Post

Delete the obsolete code.


Quote

Code
  

CORE: #MAIN LOOP
while ( ( $wrong < 6 ) ) {
print "Enter a letter:\n";
chomp( my $guess = <STDIN> );
my $right_guess = 0;
foreach $i ( 0 .. $#word ) {
if ( $guess eq $word[$i] ) { #Correct LOOP
$right++;
$right_guess = 1;
$word[$i] = $guess;
$hidden[$i] = $guess;
print "RIGHT!\n";
print "@hidden\n";
&compare;
next;
}
}
if ($right_guess) { #Wrong LOOP
$right_guess = 1;
next CORE;
}
else {
if ( !$right_guess ) {
$wrong++;
print "WRONG guess!\n";
my $muneco = shift(@toy);
push( @palis, $muneco );
print "@palis\n";
next;
}
}
}




This should fix your loops. Subroutine calls may be misplaced. I have not looked at the working of the subroutines.
Good Luck,
Bill


alfumao
Novice


Nov 10, 2010, 3:06 PM

Post #8 of 8 (998 views)
Re: [BillKSmith] Problem with loops [In reply to] Can't Post

Thank you for your extra help, it's better to get rid of as much extra code as possible (although it already worked fine)
Wink

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives