Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I have this code for hangman however I would like to modify it so that the words come from a text file

#! /usr/bin/perl ###hangman### use strict; my @words = ( "hi", "cat", "home", "country", "computer", "irc", "hat", "wow", "Berlin", "fly", "airplane", "cow", "maps", "make", "run", "sigh", "golf", "running", "hat", "television", "games", "hangman", "pearl", "cat", "dog", "snake", "black", "white", "cabin", "trees", "birds", "animals"); my @hangman = (">", "=", "(", ")", "-", "|", "-", "<"); my $hangman; my @word; my $word; my %found; my $input; #make the input variable global, so all functions can use i +t. &setup; while(1) { #start the game... $input = &getInput; &checkInputAgainstWord; &checkForWord; } exit; sub setup() { $hangman = 0; print "Welcome to hangman, the goal is to guess all the letters in +the word shown!\n"; print "If you guess a wrong letter, you'll see another body part of + this: \n @hangman. If you see all of the body, you lose.\n"; sleep(1); my $number = int( rand( scalar( @words ) ) ); if ($number >= scalar(@words)) { $number -= 1;} #just in case the r +and function is exactly the number of scalars in the words array, thi +s will subtract the number variable by 1 so that it doesn't get an in +dex out-of-bound error. $word = $words[$number]; @word = split //, $word; %found = (); foreach my $char (@word) { $found{$char} = 0; print "_"; } print "\n"; } sub getInput() { my $input; while (length($input) != 1) { $input = <STDIN>; chomp($input); if (length($input) > 1) { print "you entered more than one character.\n"; } } return $input; } sub checkInputAgainstWord() { my $true = 0; foreach my $char (@word) { if ($input =~ m/^$char$/) { $found{$char} = 1; $true = 1; } else { } } if ($true) { &showWord; } else { &showHangman; } } sub showWord() { #show the word with the guessed characters. foreach my $char (@word) { if ($found{$char} == 1) { print $char; } else { print "_"; } } print "\n"; } sub checkForWord() { my $true = 1; foreach my $char (@word) { if ($found{$char} == 1) { } else { $true = 0; } } if ($true==1) { &Won } } sub Won() { print "You got it! play again?y/n\n"; my $answer = <STDIN>; chomp($answer); if($answer =~ m/y/) { &setup; } else { exit; } } sub showHangman() { for (my $i = 0; $i < ($hangman+1); $i++) { print $hangman[$i]; } print "\n"; $hangman++; if ($hangman >= scalar(@hangman)) { &Lost; } } sub Lost() { print "\nYou lost! The word was $word. Play again? y/n\n"; my $input = <STDIN>; chomp($input); if ($input =~ m/y/) { &setup; } else { exit; } }

I was thinking something along the lines of...

open (FILE, '<', 'words.txt'); $count = 0; while ($line = <FILE>){ $Word = split ' ', $line; $listOfWords($count) = $Word; $count += 1;

But I run into errors like "Global symbol "@hangman" requires explicit package name... Any help is appreciated


In reply to Get hangman words from text file by jaffinito34

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (4)
    As of 2015-07-05 03:33 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (60 votes), past polls