Computer Science Canada

my primitive hangman game, with 5 words :)

Author:  Paul [ Thu Jan 08, 2004 10:44 pm ]
Post subject:  my primitive hangman game, with 5 words :)

welcome to any suggestions, especially if you know how to add more words easily.
code:

%hangman by paul bian
%creates procedures for drawing hangman
%head

procedure draw1
    drawoval (500, 250, 20, 20, black)
end draw1
%body
procedure draw2
    drawline (500, 230, 500, 170, black)
end draw2
%arms
procedure draw3
    drawline (500, 200, 475, 170, black)
end draw3

procedure draw4
    drawline (500, 200, 525, 170, black)
end draw4
%legs
procedure draw5
    drawline (500, 170, 475, 130, black)
end draw5

procedure draw6
    drawline (500, 170, 525, 130, black)
end draw6
%noose
procedure draw7
    drawline (485, 230, 475, 290, black)
    drawline (500, 290, 400, 290, black)
    drawline (400, 290, 400, 100, black)
    drawline (375, 100, 450, 100, black)
    locate (10, maxcol - 18)
    put "X"
    locate (10, maxcol - 16)
    put "X"
    drawoval (500, 230, 15, 5, black)
end draw7

%end drawing stickman

var d,f : int := 1
var word : array 1 .. 5 of string := init ("dog", "cat", "mouse", "tiger", "human") %some words to guess from
var a, counter, ind, counter1, counter3, add : int := 0
var letter : string
var font := Font.New ("serif:24")
var t: string(1)
randint (a, 1, 5) %chooses a random word
%inputs same number of lines as the number of letters
for b : 1 .. length (word (a))
    Draw.Text (" _ ", maxx div 2 + add - 13, maxy div 2, font, red)
    add := add + 20
end for
loop
    locate (1, 1)
    put "Please guess a letter: " ..
    get letter
    ind := index (word (a), letter) %checks if letter is in the word
    if ind not= 0 then%if letter is found, then put the letter above the corresponding space
        Draw.Text (letter, maxx div 2 + 20 * ind - 24, maxy div 2, font, red)
        counter := counter + 1
    end if
    %calls procedures to draw stickman, if letter not found
    if ind = 0 and counter3 = 0 then
        draw1
        counter3 := counter3 + 1
    elsif ind = 0 and counter3 = 1 then
        draw2
        counter3 := counter3 + 1
    elsif ind = 0 and counter3 = 2 then
        draw3
        counter3 := counter3 + 1
    elsif ind = 0 and counter3 = 3 then
        draw4
        counter3 := counter3 + 1
    elsif ind = 0 and counter3 = 4 then
        draw5
        counter3 := counter3 + 1

    elsif ind = 0 and counter3 = 5 then
        draw6
        counter3 := counter3 + 1
    end if
   
    exit when counter3 = 6 or counter = length (word (a))

end loop
delay (1000)
cls
font := Font.New ("serif:26")
if counter3 = 6 then
loop %if you dont guess right in time, you loose
randint (a, 32, 150)
    Draw.Text ("Sorry, You got HANGED, please try again!", 0, maxy - 50, font, a)
    draw1
    draw2
    draw3
    draw4
    draw5
    draw6
    draw7
    delay (100)
 
    end loop
   end if


if counter = length (word (a)) then

randint (f, 1, maxy)
randint (d, 1, maxx)
loop% if you do guess right, you win
randint (a, 32, 150)
randint (f, 1, maxy)
randint (d, 1, maxx)
    Draw.Text ("YOU WIN!!!", 0, maxy - 50, font, a)
    drawfillstar (0+d, 0+f, 25+d, 25+f, a)
    delay (100)

end loop
end if   


Author:  Dan [ Thu Jan 08, 2004 11:11 pm ]
Post subject: 

not bad. you may whont to store the words in a file and then have them read in to the progame, that way you could probly put in more words easer.

and that endless loop at the end is a bit anyoing.

but overall not bad so have some bits++;

Author:  Thuged_Out_G [ Sat Jan 10, 2004 12:44 pm ]
Post subject: 

reading from file(incase you dont know):

var words:array 1..number of words in the file
var stream:int
open:stream,"filename.txt",get
assert stream > 0
var line:string

for i:1..number of words in the file
exit when eof(stream) %this isnt necessary, just makes it easier
get:stream,skip
get:stream,line
words(i):=line
end for

that will read thw file word by word and store it into an array.

Author:  Paul [ Tue Jan 13, 2004 7:07 pm ]
Post subject:  improved hangman

I've improved it, now its got 10 words and you can guess the whole word.
code:

%hangman
%creates procedures for drawing hangman
%head


var font := Font.New ("serif:24")

Draw.Text ("Welcome to Paul's Hangman Game!", 180, 10, font, blue)
proc draw1
    drawoval (560, 250, 20, 20, black)
end draw1
%body
proc draw2
    drawline (560, 230, 560, 170, black)
end draw2
%arms
proc draw3
    drawline (560, 200, 540, 170, black)
end draw3

proc draw4
    drawline (560, 200, 585, 170, black)
end draw4
%legs
proc draw5
    drawline (560, 170, 530, 130, black)
end draw5

proc draw6
    drawline (560, 170, 585, 130, black)
end draw6
%noose
proc draw7
drawline (550, 255, 555, 245, black)
    drawline (555, 255, 550, 245, black)
end draw7
proc draw8
    drawline (565, 255, 570, 245, black)
    drawline (570, 255, 565, 245, black)
end draw8
proc draw9
    drawarc (563, 230, 7, 7, 20, 160, black)
end draw9

%end drawing stickman

var d, f : int := 1
var word : array 1 .. 10 of string := init ("dog", "cat", "mouse", "tiger", "human",
    "box", "fox", "map", "rat", "car") %some words to guess from
var a, counter, ind, counter1, counter3, add : int := 0
var letter, guess, guessed, wor : string := ""
var font2 := Font.New ("serif:12")
var z := 15
randint (a, 1, 10) %chooses a random word
%inputs same number of lines as the number of letters

loop
    add := 0
    for b : 1 .. length (word (a))
        Draw.Text (" _ ", maxx div 2 + add - 13, maxy div 2, font, red)
        add := add + 20
    end for
    wor := word (a)
    drawline (545, 230, 535, 290, black)
    drawline (560, 290, 460, 290, black)
    drawline (460, 290, 460, 100, black)
    drawline (435, 100, 510, 100, black)
    drawoval (560, 230, 15, 5, black)

    loop

        locate (1, 1)
        put "Please guess a letter: " ..

        get letter
Draw.Text ("Already guessed, please enter another letter.", 0, 250, font2, white)
        ind := index (word (a), letter) %checks if letter is in the word
        if ind not= 0 and index (guessed, letter) = 0

                then %if letter is found, then put the letter above the corresponding space

            Draw.Text (letter, maxx div 2 + 20 * ind - 24, maxy div 2, font, red)
            counter := counter + 1
            locate (5, 1)
            put "Would you like to guess the word?(Y/N)?"
            get guess
            if guess = "Y" or guess = "y"
                    then
                locate (7, 1)
                put "Guess: " ..
                get guess
                if guess = word (a) then
                    counter := length (word (a))
                    locate (5, 1)
                    put "                             "
                    locate (7, 1)
                    put "                             "
                else
                    cls
                    counter3 := 6
                    cls
                end if
            end if
            exit

        end if
        if guessed not= "" then
            if index (guessed, letter) not= 0 then
                locate (maxrow - 10, 1)
                Draw.Text ("Already guessed, please enter another letter.", 0, 250, font2, red)

            end if
        end if
        exit
    end loop
    guessed := guessed + letter
    locate (maxrow - 5, 10)
    if index (guessed, letter) not= 0 then
        Draw.Text ("Guessed Letters: ", 17, 85, font2, black)
        Draw.Text (letter, z, 65, font2, black)

        z := z + 20
    end if
    %calls procedures to draw stickman, if letter not found
    if ind = 0 and counter3 = 0 then
        draw1
        counter3 := counter3 + 1
    elsif ind = 0 and counter3 = 1 then
        draw2
        counter3 := counter3 + 1
    elsif ind = 0 and counter3 = 2 then
        draw3
        counter3 := counter3 + 1
    elsif ind = 0 and counter3 = 3 then
        draw4
        counter3 := counter3 + 1
    elsif ind = 0 and counter3 = 4 then
        draw5
        counter3 := counter3 + 1

    elsif ind = 0 and counter3 = 5 then
        draw6
        counter3 := counter3 + 1

    elsif ind = 0 and counter3 = 6 then
        draw7
        counter3 := counter3 + 1
    elsif ind = 0 and counter3 = 7 then
        draw8
        counter3 := counter3 + 1
    elsif ind = 0 and counter3 = 8 then
        draw9
        counter3 := counter3 + 1
    end if

    exit when counter3 = 9 or counter = length (word (a))

end loop
delay (1000)

font := Font.New ("serif:26")
if counter3 = 9 then
    loop %if you dont guess right in time, you loose
        cls
        randint (a, 32, 150)
        Draw.Text ("THE WORD IS: ", 0, maxy - 50, font, a)
        Draw.Text (wor, 230, maxy - 50, font, a)
        Draw.Text ("Sorry, You got HANGED, please try again!", 0, maxy - 80, font, a)
        drawline (545, 230, 535, 290, black)
        drawline (560, 290, 460, 290, black)
        drawline (460, 290, 460, 100, black)
        drawline (435, 100, 510, 100, black)
        drawoval (560, 230, 15, 5, black)
        draw1
        draw2
        draw3
        draw4
        draw5
        draw6
        draw7
        draw8
        draw9
        delay (100)

    end loop
end if


if counter = length (word (a)) then
    cls
    randint (f, 1, maxy)
    randint (d, 1, maxx)
    loop % if you do guess right, you win
        randint (a, 32, 150)
        randint (f, 1, maxy)
        randint (d, 1, maxx)
        Draw.Text ("YOU WIN!!!", 0, maxy - 200, font, a)
        Draw.Text ("THE WORD IS: ", 0, maxy - 50, font, a)
        Draw.Text (wor, 230, maxy - 50, font, a)
        drawfillstar (0 + d, 0 + f, 25 + d, 25 + f, a)
        delay (100)

    end loop
end if



: