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

Username:   Password: 
 RegisterRegister   
 Hangman
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
HyperFlexed




PostPosted: Sun Nov 07, 2004 2:01 pm   Post subject: Hangman

Yay. I finally fixed it.

I am going to add a story line at the beginning, but at least the technical aspects of this game are done. No importing bs. Just a pure, unadultered, copy past and play game of hangman....

code:
% Hangman in Turing
% Coded by Johnny F

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

var input : string (1)                  % User's guess
var puzzle : string := ""               % Phrase to guess
var unString, unDisplay : string := ""  % Phrase with underlines, To display

var bodyCount : int := 0                % # of errors

var lettersUsed : string := ""          % contains all letters entered
var choiceDisplay : string := ""        % Centerable string or letters avail.

var playAgain : string (1)              % gets 'y' or 'n'

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Procedure centers text and displays it at current row as per Text.Whatrow
proc PutCenter (inputString : string)
    Text.Locate (Text.WhatRow, (maxcol div 2) - (length (inputString) div 2))
    put (inputString)
end PutCenter

% Displays a blinking message
proc BlinkMessage (row : int, blinkTimes : int, message : string, blinkColour : int)
    colour (blinkColour)
    for i : 1 .. blinkTimes * 2
        if i mod 2 = 1 then
            Text.Locate (row, 1)
            PutCenter (message)
        else
            Text.Locate (row, 1)
            PutCenter (repeat (" ", length (message)))
        end if
        View.Update
        delay (300)
    end for
    colour (2)
end BlinkMessage

% Draws a hangman
%   xLeftFoot - x position of left foot
%   yLeftFoot - y position of left foot
%   headRad - radius of head
%   legHeight - height of legs in pixels
%   torsoHeight - height of torso
%   footToTorso - distance of foot to torso (center) horizontally
%   armsOut - how far out the arms are horizontally (in pixels)
%   armsVertOffset - 0 will set arms straight, another number will allow offset
%   state - a value from 0-6. 0 = no hangman, 1 = head, 2 = torso, 3 = r arm, 4 = l arm, 5 = r leg, 6 = l leg
%   example call...  DrawHangman (100,120,10,50,45,15,50,-10,6,10)
proc DrawHangman (xLeftFoot : int, yLeftFoot : int, headRad : int, legHeight : int, torsoHeight : int, footToTorso : int, armsOut : int, armsVertOffset : int, state : int, colorOf : int)

    % Left Leg (6)
    if state > 5 then
        Draw.Line (xLeftFoot, yLeftFoot, xLeftFoot + footToTorso, yLeftFoot + legHeight, colorOf)
    end if

    % Right Leg (5)
    if state > 4 then
        Draw.Line (xLeftFoot + footToTorso * 2, yLeftFoot, xLeftFoot + footToTorso, yLeftFoot + legHeight, colorOf)
    end if

    % Left Arm (4)
    if state > 3 then
        Draw.Line (xLeftFoot + footToTorso, yLeftFoot + legHeight + (torsoHeight - (torsoHeight div 6)), (xLeftFoot + footToTorso) - armsOut, (yLeftFoot + legHeight + (torsoHeight - (torsoHeight
            div 6))) + armsVertOffset, colorOf)
    end if

    % Right Arm (3)
    if state > 2 then
        Draw.Line (xLeftFoot + footToTorso, yLeftFoot + legHeight + (torsoHeight - (torsoHeight div 6)), (xLeftFoot + footToTorso) + armsOut, (yLeftFoot + legHeight + (torsoHeight - (torsoHeight
            div 6))) + armsVertOffset, colorOf)
    end if

    % Torso (2)
    if state > 1 then
        Draw.Line (xLeftFoot + footToTorso, yLeftFoot + legHeight, xLeftFoot + footToTorso, (yLeftFoot + legHeight) + torsoHeight, colorOf)
    end if

    % Head (1)
    if state >= 1 then
        Draw.Oval (xLeftFoot + footToTorso, yLeftFoot + legHeight + torsoHeight + headRad, headRad, headRad, colorOf)
    end if

end DrawHangman

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Set colors and other graphics stuff
View.Set ("graphics:640;480")
View.Set ("title:HANGMAN")
RGB.SetColor (1, 0.11328125, 0.15234375, 0.1953125) % Dark blue
RGB.SetColor (2, 0.90234375, 0.99609375, 0.234375)  % Bright lime
colourback (1)
colour (2)
cls



% Welcome Screen
%include "welcomeScreen.t"

loop

    View.Set ("nooffscreenonly")
    cls

    lettersUsed := ""

    % Prompt user for phrase to be used FIXME - hangman not showing up!
    DrawHangman (335, 190, 15, 40, 50, 15, 10, -20, 6, 2)

    Text.Locate (2, 3)
    put "Please enter the phrase to be used:"
    Text.Locate (3, 3)
    View.Set ("cursor,echo")
    get puzzle : *

    % Convert puzzle into caps
    for i : 1 .. length (puzzle)
        if ord (puzzle (i)) > 96 and ord (puzzle (i)) < 123 then
            puzzle := puzzle + chr (ord (puzzle (i)) - 32)
        else
            puzzle := puzzle + puzzle (i)
        end if
    end for
    puzzle := puzzle ((length (puzzle) div 2) + 1 .. *)

    % Create underline string, store in unString
    unString := ""
    for i : 1 .. length (puzzle)
        if ord (puzzle (i)) > 64 and ord (puzzle (i)) < 91 then
            unString := unString + "_"
        else
            unString := unString + puzzle (i)
        end if
    end for

    View.Set ("offscreenonly")

    loop

        cls

        % set up unDisplay for centering
        unDisplay := ""
        for i : 1 .. length (unString)
            unDisplay := unDisplay + unString (i) + " "
        end for


        % Put underlines on third row
        Text.Locate (3, 1)
        PutCenter (unDisplay)

        % Draw gallows
        Draw.Box (View.maxx div 2 - ((View.maxx div 3) div 2), 130, View.maxx div 2 + ((View.maxx div 3) div 2), 134, 2)
        Draw.Box (View.maxx div 2 - ((View.maxx div 3) div 2) + 10, 134, View.maxx div 2 - ((View.maxx div 3) div 2) + 14, 364, 2)
        Draw.Box (View.maxx div 2 - ((View.maxx div 3) div 2) + 14, 360, View.maxx div 2 - ((View.maxx div 3) div 2) + 140, 364, 2)
        Draw.Line (View.maxx div 2 - ((View.maxx div 3) div 2) + 14, 324, View.maxx div 2 - ((View.maxx div 3) div 2) + 50, 360, 2)
        Draw.Line (View.maxx div 2 - ((View.maxx div 3) div 2) + 138, 360, View.maxx div 2 - ((View.maxx div 3) div 2) + 138, 310, 2)

        % Draw Hangman
        DrawHangman (335, 190, 15, 40, 50, 15, 10, -20, bodyCount, 2)

        View.Update

        exit when bodyCount = 6
        exit when unString = puzzle


        % set up choiceDisplay, Show choices for guess
        choiceDisplay := ""
        for i : ord ("A") .. ord ("Z")
            if index (lettersUsed, chr (i)) > 0 then
                choiceDisplay := choiceDisplay + " "
            else
                choiceDisplay := choiceDisplay + chr (i)
            end if
            choiceDisplay := choiceDisplay + " "
        end for

        locate (maxrow - 3, 1)
        PutCenter (choiceDisplay)

        loop

            % Prompt for input (guess)
            Text.Locate (5, 1)
            PutCenter ("Enter your guess.")
            View.Set ("nocursor,noecho")
            View.Update
            getch (input)

            exit when ord (input) > 64 and ord (input) < 91

            % Convert input to caps (if lower case)
            if ord (input) > 96 and ord (input) < 123 then
                input := chr (ord (input) - 32)

                % See if letter has been used
                if index (lettersUsed, input) > 0 then
                    BlinkMessage (5, 3, "You've already used that letter!", yellow)
                end if

                % If letter is not in lettersUsed, add it
                if index (lettersUsed, input) = 0 then
                    lettersUsed := lettersUsed + input
                    exit
                end if

            else
                BlinkMessage (5, 3, "Must be a letter!", yellow)
            end if

        end loop

        % If letter is there then go through and change unString
        if index (puzzle, input) > 0 then
            for i : 1 .. length (puzzle)
                if puzzle (i) = input then
                    unString := unString (1 .. i - 1) + input + unString (i + 1 .. *)
                end if
            end for
        elsif index (puzzle, input) = 0 then
            bodyCount += 1
        end if

        View.Update

    end loop



    % Victory Screen / Defeat Screen

    colour (brightmagenta)
    locate (5, 1)
    case bodyCount of
        label 0 :
            PutCenter ("Good job, he didn't even touch the noose.")
        label 1 :
            PutCenter ("Ok. is neck is a little sore.")
        label 2 :
            PutCenter ("A torso is pretty heavy eh?")
        label 3 :
            PutCenter ("That was close.")
        label 4 :
            PutCenter ("That was close")
        label 5 :
            PutCenter ("He's going to feel that in the morning!")
        label 6 :
            PutCenter ("Umm... He's not moving. I think you lose!")
    end case
    colour (2)

    locate (maxrow - 3, 1)
    PutCenter ("Play Again? (Y/N)")

    View.Update

    loop
        getch (playAgain)
        exit when (playAgain = "Y" or playAgain = "y") or (playAgain = "N" or playAgain = "n")
        BlinkMessage (maxrow - 2, 2, "Must be 'Y' or 'N'", yellow)
    end loop

    View.Update

    exit when playAgain = "n" or playAgain = "N"

end loop

cls
locate (4, 1)
PutCenter ("Thanks for playing!")
DrawHangman (335, 190, 15, 40, 50, 15, 10, -20, 6, 2)
View.Update
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sun Nov 07, 2004 3:39 pm   Post subject: (No subject)

Nice stuff, I enjoyed the comments you gave on each phase of his hanging. I haven't looked at your code. I'm wondering if you meant to leave the guy hanging while you picked another word. You don't want to let him down and hang him again for each new word?
I would also suggest losing the buttbar, using setscreen("nobuttonbar") and closing the window at the end of the program.
Well done, +BITS
HyperFlexed




PostPosted: Mon Nov 08, 2004 7:18 am   Post subject: (No subject)

yeah, I've fixed all that up and added a story Laughing

I'll post it up later today, but now I have to get to school to hand this in Laughing
HyperFlexed




PostPosted: Mon Nov 08, 2004 3:38 pm   Post subject: (No subject)

http://www.compsci.ca/v2/viewtopic.php?p=64163#64163
chunginator




PostPosted: Mon Nov 15, 2004 11:17 am   Post subject: (No subject)

what actually happens is that the variabels are just the same and u can just reset them. or u could do what cervantes said and close the window and create a new one. awesome job!! Claping Claping ClapingClaping
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: