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

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




PostPosted: Tue Nov 07, 2006 10:09 am   Post subject: Number Guessing

a game where you have to guess a number between 1 and 100. I think it needs to be improoved to what do ya think?


code:
/******************************/
/*        High-Low Game       */
/*  This program will ask the */
/*  user a to guess a random  */
/* number and will tell them  */
/* whether it is lower, higher*/
/*     or is the number       */
/******************************/


% The music that will play through the game
process music
    loop
        play ("4CDEFG5CDEFG6CCCDDDEEEFFFGGG2ACDE1CC2DEF1GGFDE8CCCDDDEEEFFF2ACED8GGGCCCDDDEEEFFF2ACED8GGGCCCDDDEEE")
    end loop
end music
fork music


% Variables
var num, ans : int % the random number and the users answer

var font1 : int
var any : string (1) % for the getch command
var name : string % the users name
var count : int := 0 % to tally the users guesses
var aver : real



% Title Screen
% The intro screen to show that the game started
drawfillbox (0, 0, 700, 400, 2)

font1 := Font.New ("lithograph:30:italic")
assert font1 > 0
Font.Draw ("Highs and Lows", 130, 192, font1, red)

for y : 90 .. 120
    drawarc (200, y, 120, 50, 0, 180, 7)
    drawarc (440, y, 120, 50, 180, 0, 7)
    delay (50)
end for

locate (23, 30)
colorback (2)
put "Press any key to continue"
locate (25, 43)
getch (any)
cls

% Instruction Screen
% Will inform the user of what to do and will get the users name
put "Lets start with your name. What is it?"
get name
put "Well ", name, " let me tell you the rules before we start."
getch (any)
put "This is a high low game, so you have to guess a random number between 1 and 100."
getch (any)
put "Please don't put any numbers over 100 or under 1."
delay (2500)
put "Ok, now that you have an idea of what to do, lets get started!"
put "Press Any Key"
getch (any)
cls


% High-Low Code
% will repeat if user wants to
loop
    % To tally the amount of times it was played
    var plays : real := 0
    plays := plays + 1
    % The random number that will be guessed
    randomize
    randint (num, 1, 100)

    put "Guess a number between 1 and 100"

    loop

        get ans

        % To see if the user gets the number right
        % This will repeat if the user gets it wrong, will exit when the user guesses it right
        if ans > 100
                then
            put "Please keep it below 100"
            count := count + 1
        elsif ans < 1
                then
            put "Please keep it above 1"
            count := count + 1
        elsif ans = num
                then
            put "You guessed right"
            count := count + 1
        elsif ans > num
                then
            put "Nope, your too high. Guess again"
            count := count + 1
        elsif ans < num
                then
            put "Nope, your too low. Guess again"
        end if

        exit when ans = num
    end loop
    delay (2000)
    cls

    % Info for the users to see their average and amount of tries
    aver := count / plays
    put "", name, " it took you ", count, " tries to guess the number."
    put "Your average amount of guesses is:", aver
    put "Would like to countinue?y/n"
    getch (any)
    cls

    % If the user chooses to quit, the game will end
    if any = "n"
            then
        cls
        put "Thanks for playing!"
        exit when any = "n"
    end if
end loop
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Nov 07, 2006 11:49 am   Post subject: (No subject)

Please use code tags.
bruized




PostPosted: Tue Nov 07, 2006 7:57 pm   Post subject: (No subject)

You should probably move the drawing of the arcs and text a little up because as soon as the text saying "press any key to continue" it slightly overlaps the arcs. Also you might want to use more contrasting colours because I have to strain to see the words. It just doesn't stand out as good as black and white Laughing
batman




PostPosted: Wed Nov 08, 2006 4:37 pm   Post subject: Code

Good job though if u want to make it perfect u would need to fix a bunch of stuff. Now i dont remember turing well so maybe someone who reads this afterwards can help me out. Now when u ask for a num between 1-100 ok, if i put a letter for example ur program should be able to handle that and say something like that was not a number try again or something. Little errors like that need to be fixed in order to make the code perfect. How u do that i dont remeber in turing. In java u would use a numberformatexception i believe. Very Happy
Mathwiz




PostPosted: Mon Nov 20, 2006 9:43 am   Post subject: that was okay

you should ask the user if they would like to play again before starting it all over again
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: