Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Can Experts help improve my Hangman Code?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
shooter35




PostPosted: Fri May 01, 2009 1:18 pm   Post subject: Can Experts help improve my Hangman Code?

please help me improve till its a fun and hard game to play with.

Turing:
% Modification done to Tony Zhang's program
%By En Luo & Wei Luo
% declaration of varibles
setscreen ("graphics:1000;650")
var x, y, nx, button, letter : int := 0
var colo : int := 9
var lettersI : array 1 .. 10 of string := init ("A", "B", "C", "D", "E", "F", "G", "H", "I", "J")
var lettersII : array 1 .. 10 of string := init ("K", "L", "M", "N", "O", "P", "Q", "R", "S", "T")
var lettersIII : array 1 .. 6 of string := init ("U", "V", "W", "X", "Y", "Z")
var words : array 1 .. 15 of string := init ("biotic", "combustion", "bedrock", "grassland", "exothermic", "glucose", "ozone", "microclimate", "precipitation", "ecosystem", "nitrogen", "tangent",
    "vector", "weather",
    "alkaline")
var random, strike, right : int := 0
var word, choice, let : string := ""

colourback (64)
cls

% Winning stage
process victory
    choice := "n"
    for w : 1 .. 5
        Draw.Text ("Good Job, You Won Dude!", maxx div 2 - 100, maxy div 2, Font.New ("serif:24:bold"), Rand.Int (1, 100))
        delay (100)
        exit when choice not= "n"
    end for
end victory
% Losing stage
process failure
    choice := "n"
    for l : 1 .. 5
        Draw.Text ("You Lost Man!", maxx div 2 - 50, maxy div 2 - 50, Font.New ("serif:24:bold"), Rand.Int (155, 200))
        Draw.Text ("The word was:", maxx div 2 - 150, maxy div 2, Font.New ("serif:24:bold"), Rand.Int (155, 200))
        Draw.Text (words (random), maxx div 2 + 60, maxy div 2, Font.New ("serif:24:bold"), Rand.Int (155, 200))
        delay (100)
        exit when choice not= "n"
    end for
end failure

loop
    %Title
    Draw.Text ("Welcome to En & Wei's Hangman Puzzle Game: Science Terms", maxx div 2 - 300, maxy - 20, Font.New ("serif:20:bold"), 70)

    %Question
    locate (3, 1)
    put "Please choose letters you think will appear in the word"

    %Hang
    drawfillbox (740, 170, 760, 410, 212)     %post
    drawfillbox (760, 390, 860, 410, 212)
    drawfillbox (700, 160, 900, 180, 115)     %ground
    drawfillbox (830, 390, 840, 358, 114)     %wire
    drawarc (835, 345, 13, 13, 0, 360, 214)

    %Random Words and Dashes
    randint (random, 1, 15)
    var dash : array 1 .. length (words (random)) of string
    for a : 1 .. length (words (random))
        dash (a) := "_"
    end for

    %Keyboard
    for a : 1 .. 10
        %First Row
        drawbox (90 + nx, 250, 140 + nx, 300, black)
        drawfillbox (95 + nx, 255, 135 + nx, 295, grey)
        Draw.Text (lettersI (a), 105 + nx, 260, Font.New ("serif:22:bold"), 56)

        %Second Row
        drawbox (90 + nx, 200, 140 + nx, 250, black)
        drawfillbox (95 + nx, 205, 135 + nx, 245, grey)
        Draw.Text (lettersII (a), 105 + nx, 210, Font.New ("serif:22:bold"), 56)

        %Third Row
        if nx >= 100 and nx <= 350 then
            letter += 1
            drawbox (90 + nx, 150, 140 + nx, 200, black)
            drawfillbox (95 + nx, 155, 135 + nx, 195, grey)
            Draw.Text (lettersIII (letter), 105 + nx, 160, Font.New ("serif:22:bold"), 56)
        end if
        nx += 50
    end for


    %Output
    loop
        %Draw Dash
        locate (8, 15)
        for a : 1 .. length (words (random))
            put dash (a) : 3, " " ..
        end for

        % Alphabets
        loop
            if Mouse.ButtonMoved ("down") then
                var buttonnumber, buttonupdown : int
                Mouse.ButtonWait ("down", x, y, buttonnumber, buttonupdown)
                if x >= 95 and x <= 135 and y >= 255 and y <= 295 then
                    let := "a"
                elsif x >= 145 and x <= 185 and y >= 255 and y <= 295 then
                    let := "b"
                elsif x >= 195 and x <= 235 and y >= 255 and y <= 295 then
                    let := "c"
                elsif x >= 245 and x <= 285 and y >= 255 and y <= 295 then
                    let := "d"
                elsif x >= 295 and x <= 335 and y >= 255 and y <= 295 then
                    let := "e"
                elsif x >= 345 and x <= 385 and y >= 255 and y <= 295 then
                    let := "f"
                elsif x >= 395 and x <= 435 and y >= 255 and y <= 295 then
                    let := "g"
                elsif x >= 445 and x <= 485 and y >= 255 and y <= 295 then
                    let := "h"
                elsif x >= 495 and x <= 535 and y >= 255 and y <= 295 then
                    let := "i"
                elsif x >= 545 and x <= 585 and y >= 255 and y <= 295 then
                    let := "j"
                elsif x >= 95 and x <= 135 and y >= 205 and y <= 245 then
                    let := "k"
                elsif x >= 145 and x <= 185 and y >= 205 and y <= 245 then
                    let := "l"
                elsif x >= 195 and x <= 235 and y >= 205 and y <= 245 then
                    let := "m"
                elsif x >= 245 and x <= 285 and y >= 205 and y <= 245 then
                    let := "n"
                elsif x >= 295 and x <= 335 and y >= 205 and y <= 245 then
                    let := "o"
                elsif x >= 345 and x <= 385 and y >= 205 and y <= 245 then
                    let := "p"
                elsif x >= 395 and x <= 435 and y >= 205 and y <= 245 then
                    let := "q"
                elsif x >= 445 and x <= 485 and y >= 205 and y <= 245 then
                    let := "r"
                elsif x >= 495 and x <= 535 and y >= 205 and y <= 245 then
                    let := "s"
                elsif x >= 545 and x <= 585 and y >= 205 and y <= 245 then
                    let := "t"
                elsif x >= 195 and x <= 235 and y >= 155 and y <= 195 then
                    let := "u"
                elsif x >= 245 and x <= 285 and y >= 155 and y <= 195 then
                    let := "v"
                elsif x >= 295 and x <= 335 and y >= 155 and y <= 195 then
                    let := "w"
                elsif x >= 345 and x <= 385 and y >= 155 and y <= 195 then
                    let := "x"
                elsif x >= 395 and x <= 435 and y >= 155 and y <= 195 then
                    let := "y"
                elsif x >= 445 and x <= 485 and y >= 155 and y <= 195 then
                    let := "z"
                end if
                exit when ord (let) > 95
            end if
        end loop

        %Wrong Letter
        if index (words (random), let) = 0 then
            strike += 1
        end if

        %Hangman face
        if strike = 1 then
            drawfilloval (835, 355, 15, 15, brown)    % head
        elsif strike = 2 then
            drawfilloval (828, 357, 3, 3, blue)     % left eye
        elsif strike = 3 then
            drawfilloval (842, 357, 3, 3, blue)     %right eye
        elsif strike = 4 then
            drawarc (835, 345, 5, 5, 20, 160, red)     %mouth

            %Hangman body
        elsif strike = 5 then
            drawline (835, 340, 835, 280, purple)   % chest
        elsif strike = 6 then
            drawline (835, 310, 865, 340, brown)     %right arm
        elsif strike = 7 then
            drawline (835, 310, 805, 340, brown)     %left arm
        elsif strike = 8 then
            drawline (835, 280, 865, 250, black)     %right leg
        elsif strike = 9 then
            drawline (835, 280, 805, 250, black)     %left leg
        end if

        %Right letter
        for a : 1 .. length (words (random))
            if let = words (random) (a) then
                dash (a) := let
            end if
        end for

        locate (8, 15)
        for a : 1 .. length (words (random))
            put dash (a) : 3, " " ..
        end for

        for a : 1 .. length (words (random))
            right := right + index (dash (a), "_")
        end for

        %Exits
        exit when right = 0
        right := 0
        exit when strike = 9
    end loop

    %Play again
    delay (1000)
    cls
    Draw.Text ("Would you like to play again?", maxx div 2 - 130, maxy - 100, Font.New ("serif:16:bold"), 56)
    drawbox (maxx div 2 - 100, maxy - 250, maxx div 2, maxy - 200, black)
    drawfillbox (maxx div 2 - 95, maxy - 245, maxx div 2 - 5, maxy - 205, grey)
    Draw.Text ("Yes", maxx div 2 - 75, maxy - 235, Font.New ("serif:24:bold"), black)
    drawbox (maxx div 2 + 100, maxy - 250, maxx div 2, maxy - 200, black)
    drawfillbox (maxx div 2 + 95, maxy - 245, maxx div 2 + 5, maxy - 205, grey)
    Draw.Text ("No", maxx div 2 + 30, maxy - 235, Font.New ("serif:24:bold"), black)

    %Choice
    if strike = 9 then
        fork failure
        loop
            if Mouse.ButtonMoved ("down") then
                var buttonnumber, buttonupdown : int
                Mouse.ButtonWait ("down", x, y, buttonnumber, buttonupdown)
                if x >= maxx div 2 - 95 and x <= maxx div 2 - 5 and y >= maxy - 245 and y <= maxy - 205 then
                    choice := "y"
                elsif x >= maxx div 2 + 95 and x <= maxx div 2 + 5 and y >= maxy - 245 and y <= maxy - 205 then
                    choice := "n"
                end if
                exit
            end if
        end loop
    elsif word = words (random) or right = 0 then
        fork victory
        loop
            if Mouse.ButtonMoved ("down") then
                var buttonnumber, buttonupdown : int
                Mouse.ButtonWait ("down", x, y, buttonnumber, buttonupdown)
                if x >= maxx div 2 - 95 and x <= maxx div 2 - 5 and y >= maxy - 235 and y <= maxy - 205 then
                    choice := "y"
                elsif x >= maxx div 2 + 95 and x <= maxx div 2 + 5 and y >= maxy - 235 and y <= maxy - 205 then
                    choice := "n"
                end if
                exit
            end if
        end loop
    end if

    nx := 0
    letter := 0
    strike := 0
    exit when choice = "n"
    cls
end loop


Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="turing"]Code Here[/syntax]
Sponsor
Sponsor
Sponsor
sponsor
ecookman




PostPosted: Fri May 01, 2009 3:11 pm   Post subject: RE:Can Experts help improve my Hangman Code?

well...where are you looking for the program to be improved...
i like having people keep proects as they would want them and guiding them through the process of doing that (so do most people here)
Dusk Eagle




PostPosted: Fri May 01, 2009 5:02 pm   Post subject: Re: Can Experts help improve my Hangman Code?

I just have one question: Is this code for school?

Either way, one suggestion I have it to avoid the use of processes. Instead, use procedures and functions, and pass values into them using parameters, rather than using global variables (explained at url above). This may not make your game more fun, but it will make it's coding better, which can only help you in the long run.

Finally, do you see anything repetitive about this code?
Turing:

                if x >= 95 and x <= 135 and y >= 255 and y <= 295 then
                    let := "a"
                elsif x >= 145 and x <= 185 and y >= 255 and y <= 295 then
                    let := "b"
                elsif x >= 195 and x <= 235 and y >= 255 and y <= 295 then
                    let := "c"
                elsif x >= 245 and x <= 285 and y >= 255 and y <= 295 then
                    let := "d"
                elsif x >= 295 and x <= 335 and y >= 255 and y <= 295 then
                    let := "e"
                elsif x >= 345 and x <= 385 and y >= 255 and y <= 295 then
                    let := "f"
                elsif x >= 395 and x <= 435 and y >= 255 and y <= 295 then
                    let := "g"
                elsif x >= 445 and x <= 485 and y >= 255 and y <= 295 then
                    let := "h"
                elsif x >= 495 and x <= 535 and y >= 255 and y <= 295 then
                    let := "i"
                elsif x >= 545 and x <= 585 and y >= 255 and y <= 295 then
                    let := "j"


Here's a much better way of doing this. I'm giving you the first section of the code this time so that you learn to avoid ultra-repetitive lines in programming and instead look for better algorithms. Think how much effort this could have saved you:
Turing:

for i : 0..9
    %insert the y statements yourself
    if x >= i*50+95 and x <= i*50+95+40
        put chr(i+ord("a"))
    end if
end for
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: