Computer Science Canada

Hangman

Author:  Gackt [ Tue Mar 10, 2009 5:48 pm ]
Post subject:  Hangman

Hey, Im kind of stuck on my hangman game. Im looking for help on the basic coding. I created a .txt file to load all the words from. I just have no clue how to select a random word from it and guess each indivual letter. Could anyone possibly throw a basic coding line at me that I can work with?

Author:  metachief [ Tue Mar 10, 2009 6:23 pm ]
Post subject:  RE:Hangman

Code line: put "Hello!"
Work with it!

Author:  DemonWasp [ Tue Mar 10, 2009 6:40 pm ]
Post subject:  RE:Hangman

Do you know arrays yet?
Do you know file I/O yet (input specifically, though output is also useful)?
Do you know how to display stuff on the screen and ask for user input?

That's all you need.

Author:  Tony [ Tue Mar 10, 2009 6:40 pm ]
Post subject:  RE:Hangman

The simplest way is to read everything into an array, and pick a random element of that array.

Author:  Gackt [ Wed Mar 11, 2009 7:17 am ]
Post subject:  RE:Hangman

I don;'t have time to learn arrays.

Author:  hossack [ Wed Mar 11, 2009 7:51 am ]
Post subject:  Re: RE:Hangman

metachief @ Tue Mar 10, 2009 6:23 pm wrote:
Code line: put "Hello!"
Work with it!


Come on, post something that will help!

Author:  Tallguy [ Wed Mar 11, 2009 7:55 am ]
Post subject:  RE:Hangman

like tony said, the easiest way to do this is to have arrays, if the user picks a letter that is in the array, then have a counter or something, and store used letters in an array by themselves with another counter for numbert if guesses

this is just how i would do it . . .

Author:  DemonWasp [ Wed Mar 11, 2009 8:12 am ]
Post subject:  RE:Hangman

You do have time to learn arrays. They really aren't that hard, and they are absolutely necessary for anything more than a trivial program.

Author:  Gackt [ Wed Mar 11, 2009 8:55 am ]
Post subject:  RE:Hangman

If I use arrays then Im going to lose marks for not using the coding she taught us.



Edit: Okay, really all I need to know is how to randomize a string?

Author:  Tallguy [ Wed Mar 11, 2009 9:02 am ]
Post subject:  RE:Hangman

what is she teaching you?? arrays are basic programming

Author:  Gackt [ Wed Mar 11, 2009 9:11 am ]
Post subject:  Re: Hangman

Were doing crap like this. Im so lost.


Turing:

var stream : int
var word, search : string
var file : string


put "Enter the Name of a file, include the ending like .txt and crap."
get file

put "Enter a word to seach"
get search

open : stream, file, get
assert stream > 0



put "Words in file:"
put ""




loop
    exit when eof (stream)
    get : stream, word
    if index (word, search) > 0 then
        put word
        put "SHE'S THAR"
        delay (1000)
        exit
    else
        put "Its not thar"
        delay (1000)
        exit
    end if

end loop


close : stream







Thats to search for a word. Im using that to start off. And we have to do crap like

Turing:

    get : stream, skip
    exit when eof (stream)
    get : stream, word
    if length (word) > 2 and word (* -1 .. *) = "or" then
        word := word (1 .. * -2) + "our"


or what ever.

Mod Edit: Your forgot the add syntax tags for one block of code Smile

Author:  Gackt [ Wed Mar 11, 2009 4:05 pm ]
Post subject:  Re: Hangman

Actually, What I should start out with is just Guess a word and working from there. I'll post up my .Zip file.

What I mainly need help on is.. I can only guess the first word of my .txt file. Any other word that I have in there and try to guess, it says its not there.

Author:  Tony [ Wed Mar 11, 2009 5:05 pm ]
Post subject:  RE:Hangman

you should trace through the flow of your program. Read it out-loud -- it's doing things in order that I'm sure wasn't your intention.

Also, you might want to review what index actually does.

Author:  Gackt [ Thu Mar 12, 2009 7:17 am ]
Post subject:  RE:Hangman

Its just the coding that my teacher gave me. She we had to use index for like searching for words.

Author:  Tony [ Thu Mar 12, 2009 11:19 am ]
Post subject:  RE:Hangman

Well one of you is not understanding something. index is used to search inside a word, not for the whole word.

Author:  Gackt [ Thu Mar 12, 2009 12:13 pm ]
Post subject:  RE:Hangman

I really dont understand anything, I just do what she tells us.

Author:  crysis [ Thu Mar 12, 2009 5:19 pm ]
Post subject:  RE:Hangman

I started a hangman game Tuesday and I'm stuck where u are i don't understand how to get a random word out of a file but i understand how to replace a blank space with a letter.

Author:  Tony [ Thu Mar 12, 2009 5:48 pm ]
Post subject:  RE:Hangman

If you guys don't want to use arrays, then you can just read a random number of lines from a text file, and then use the last word read.

This might require having to read the file twice, to first figure out how long it is.

Author:  Gackt [ Thu Mar 12, 2009 7:54 pm ]
Post subject:  Re: Hangman

Right now, I just have an assigned word to guess. How can i get it so that when I guess it it checks to see if the letter I guess is correct and in the word and how wuold I get it to show up?




Turing:

View.Set ("nocursor")
var stream : int
var spaces : string := ""
var pos, offset : int
var key : string (1)
var key2 : string (1)
var guess : string
var file : string
var Intro : int := Pic.FileNew ("Intro.bmp")
var InstructionsPic : int := Pic.FileNew ("Instructions.bmp")
var word, newword : string := "nissan"

% THIS IS THE PROCEDURE FOR THE BACKGROUND AND GALLOWS
proc BackGround
    drawfillbox (0, 0, maxx, 30, green)             % Grass
    drawfillbox (0, 30, maxx, maxy, 54)             % Sky
    drawfillbox (20, 20, 150, 35, 114)              % Gallow Stand Thing
    drawfillbox (30, 20, 40, 220, 114)              % Gallow Pole
    drawfillbox (30, 220, 120, 230, 114)            % Gallow Over Head Thing
    drawfillbox (115, 220, 120, 180, 115)           % Rope

end BackGround



% THIS IS THE PROCEDURE FOR THE MAIN CODING OF THE GAME
proc MainGame

    BackGround

    for i : 1 .. length (word)
        spaces := spaces + "_ "
    end for

    locatexy (270, 380)
    colorback (54)
    color (white)
    put spaces

    colorback (54)
    color (white)
    locatexy (8, 400)
    View.Set ("cursor")
    put "guess a letter"
    get guess

    % If you choose a letter

    if index (word, guess) < 0 then
        pos := index (word, guess)
        spaces := spaces (1 .. pos * 2 - 2) + guess + spaces (pos * 2 .. *)

        loop
            offset := pos
            newword := word (offset + 1 .. *)
            exit when index (newword, guess) = 0
            pos := index (newword, guess) + offset
            spaces := spaces (1 .. pos * 2 - 2) + guess + spaces (pos * 2 .. *)
        end loop
    else
        put " letter not thar"
    end if

end MainGame



% THIS IS THE PROCEDURE FOR THE INSTRUCTIONS OF THE GAME
proc MENU
    Pic.Draw (Intro, 1, 0, picCopy)
end MENU


% THIS IS THE PROCEDURE FOR THE INSTRUCTIONS OF THE GAME
proc Instructions
    Pic.Draw (InstructionsPic, 1, 0, picCopy)
end Instructions


% THIS IS THE PROCEDURE FOR THE DRAWING OF THE BODY
proc HEAD
    drawoval (117, 170, 10, 10, white)
end HEAD

proc BODY
    drawline (117, 160, 117, 120, black)    % Torso
end BODY

proc LEFTARM
    drawline (117, 160, 112, 135, black)    % Left Arm
end LEFTARM

proc RIGHTARM
    drawline (117, 160, 122, 135, black)    % Right Arm
end RIGHTARM

proc LEFTLEG
    drawline (117, 120, 112, 100, black)    % Left Leg
end LEFTLEG

proc RIGHTLEG
    drawline (117, 120, 122, 100, black)    % Right Leg
end RIGHTLEG






% This is the entire coding for my MAIN MENU

Pic.Draw (Intro, 1, 0, picCopy)
getch (key)

if key = "A" or key = "a" then

    MainGame

elsif key = "B" or key = "b" then

    Instructions
    getch (key2)
    if key2 = "A" or key2 = "a" then

        MainGame

    elsif key2 = "B" or key2 = "b" then

    end if
elsif key = "C" or key = "c" then
end if


Author:  Tony [ Thu Mar 12, 2009 8:01 pm ]
Post subject:  Re: Hangman

Gackt @ Thu Mar 12, 2009 7:54 pm wrote:
How ... it checks to see if the letter I guess is correct and in the word

Tony @ Thu Mar 12, 2009 11:19 am wrote:
index is used to search inside a word

it will also tell you the (first!) position in the word, where it's located, so you know where to draw the letter. You'd have to think about what you can do to the word, so that index will skip over the first found location (for cases where a word has some letters more than once).

Author:  Gackt [ Mon Mar 23, 2009 9:16 am ]
Post subject:  RE:Hangman

Uhh.. what?

Author:  Gackt [ Mon Mar 23, 2009 4:48 pm ]
Post subject:  Re: Hangman

OKay, I've gotten farther.

Few things I would like to know, How come when I guess a letter, it says "You have already guessed that letter" when I havent. And how would I make it to that when the Word is fully guessed, it recongnizes (spelt wrong) that It is correct?

Author:  Gackt [ Wed Mar 25, 2009 9:54 am ]
Post subject:  Re: Hangman

Okay, wtf. Why is it trying Reading past the EOF. All the other files are in the Zip file above.


Turing:

View.Set ("nocursor")

randomize

var stream : int
var spaces : string := ""
var pos, offset : int
var key : string (1)
var guess : string
var file : int
var Intro : int := Pic.FileNew ("Intro.bmp")
var InstructionsPic : int := Pic.FileNew ("Instructions.bmp")
var word, newword : string := "nissan"
var Randy : int
var ex : int := 380
var ey : int := 60

% Font for the win!
var fonter : int
fonter := Font.New ("Comic Sans MS:18:bold")


% Counters
var WinnyCounty : int := 0
var count : int := 0
var BodyCount : int := 0



open : file, "HangManList.txt", get
assert file > 0


% THIS IS THE PROCEDURE FOR THE INSTRUCTIONS OF THE GAME
proc MENU
    Pic.Draw (Intro, 1, 0, picCopy)
end MENU


% THIS IS THE PROCEDURE FOR THE INSTRUCTIONS OF THE GAME
proc Instructions
    Pic.Draw (InstructionsPic, 1, 0, picCopy)
end Instructions

% THIS IS THE PROCEDURE FOR THE DRAWING OF THE BODY
proc HEAD
    drawoval (117, 170, 20, 20, black)
end HEAD

proc BODY
    drawline (117, 150, 117, 100, black)        % Torso
end BODY

proc LEFTARM
    drawline (117, 150, 102, 125, black)      % Left Arm
end LEFTARM

proc RIGHTARM
    drawline (117, 150, 132, 125, black)      % Right Arm
end RIGHTARM

proc LEFTLEG
    drawline (117, 100, 102, 60, black)    % Left Leg
end LEFTLEG

proc RIGHTLEG
    drawline (117, 100, 132, 60, black)       % Right Leg
end RIGHTLEG





% THIS IS THE PROCEDURE FOR THE BACKGROUND AND GALLOWS
proc BackGround
    drawfillbox (0, 0, maxx, 30, green)             % Grass
    drawfillbox (0, 30, maxx, maxy, 54)             % Sky
    drawfillbox (20, 20, 150, 35, 114)              % Gallow Stand Thing
    drawfillbox (30, 20, 40, 220, 114)              % Gallow Pole
    drawfillbox (30, 220, 120, 230, 114)            % Gallow Over Head Thing
    drawfillbox (115, 220, 120, 190, 115)           % Rope
end BackGround



% THIS IS THE PROCEDURE FOR THE MAIN CODING OF THE GAME

proc MainGame

    loop
       

        randint (Randy, 1, 30)


        for b : 1 .. Randy
            get : file, word
        end for

        put word
        for i : 1 .. length (word)
            spaces := spaces + "_ "
        end for


        BackGround

        locatexy (400, 70)
        colorback (54)
        put "Letters Guessed:"

        loop

            % This outputs all the spaces for the Word
            locatexy (270, 380)
            colorback (54)
            color (white)
            put spaces

            colorback (54)
            color (white)
            locatexy (8, 400)
            View.Set ("cursor")
            put "guess a letter"
            get guess




            % Guessed letters
            ex := ex + 10
            if ex > maxx - 20 then
                ey := 50
                ex := 380
            end if
            colorback (54)
            locatexy (ex, ey)
            put guess


            % If you choose a letter

                if index (word, guess) < 0 then
                    pos := index (word, guess)
                    spaces := spaces (1 .. pos - 1) + "_" + spaces (pos * 1 .. *)

                    exit
                else
                    locatexy (10, 300)
                    put "Guess Again"
                    exit
                end if

            end loop

            count := 0

            for a : 1 .. length (word)
                if word (a) = guess then
                    spaces := spaces (1 .. a * 2 - 2) + guess + spaces (a * 2 .. *)
                    count += 1
                    WinnyCounty := WinnyCounty + 1 % win thing
                    locatexy (270, 380)
                    put spaces
                end if
            end for
            if count = 0 then
                put "That Letter Is Not There"
                BodyCount := BodyCount + 1
                delay (2000)
                drawfillbox (0, 260, 200, 287, 54) % This Erases the Letter Not There Statement
            end if

            if BodyCount = 1 then
                HEAD
            elsif BodyCount = 2 then
                HEAD
                BODY
            elsif BodyCount = 3 then
                HEAD
                BODY
                LEFTARM
            elsif BodyCount = 4 then
                HEAD
                BODY
                LEFTARM
                RIGHTARM
            elsif BodyCount = 5 then
                HEAD
                BODY
                LEFTARM
                RIGHTARM
                LEFTLEG
            elsif BodyCount = 6 then
                HEAD
                BODY
                LEFTARM
                RIGHTARM
                LEFTLEG
                RIGHTLEG
            end if

            if WinnyCounty = length (word) then % This is if the program checks to see if the word is right
                Font.Draw ("You Win!!! No Dying For You TODAYYYYY!", 120, 230, fonter, brightred)
                delay (1000)
                cls
                put "Would you like to play again? Y/N"
                getch (key)
            end if
            if key = "y" or key = "Y" then

                spaces := ""
                WinnyCounty := 0
                count := 0
                BodyCount := 0

                locatexy (270, 380)
                colorback (54)
                color (white)
                put spaces

                MainGame

            elsif key = "n" or key = "n" then
                exit
            end if
            if BodyCount = 6 then
                locatexy (130, 230)
                put "You lost, The correct word was " ..
                put word
                locatexy (180, 210)
                put "Would you like to play again? Y/N"
                getch (key)
            end if
            if key = "y" or key = "Y" then

                spaces := ""
                WinnyCounty := 0
                count := 0
                BodyCount := 0

                locatexy (270, 380)
                colorback (54)
                color (white)
                put spaces

                MainGame

            elsif key = "n" or key = "n" then
                exit
            end if
        end loop
    end MainGame


    % This is the entire coding for my MAIN MENU

    Pic.Draw (Intro, 1, 0, picCopy)
    getch (key)

    if key = "A" or key = "a" then

        MainGame

    elsif key = "B" or key = "b" then

        Instructions
        getch (key)
        if key = "A" or key = "a" then

            MainGame

        elsif key = "B" or key = "b" then

        end if
    elsif key = "C" or key = "c" then
    end if


Author:  Gackt [ Thu Mar 26, 2009 1:02 pm ]
Post subject:  RE:Hangman

Thanks for the Help!


: