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

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




PostPosted: Wed May 31, 2006 7:58 am   Post subject: lottery time!

heres a fairly simple program used that allows the user to play the lottery, your bet is how much money *up to 100$* that your are willing to bet, the number your pick is randomly generated, if you pick the correct number *from 1-10* then you win the amount of money you bet *5, comments are appreciated

code:
var bet : int
var guess : int
var randnum : int
var current_win : int
var counter : int
var total_win : int
var status : string
var answer : string
var counters : int

counter := 0
counters := 0

randint (randnum, 1, 10)
total_win := 0
current_win := 0
loop

    put "please enter your bet"
    get bet
    put "now please enter your guess"
    get guess

    if guess = randnum
            then
        current_win := bet * 5
        status := "Won"
        counter := counter + 1
        total_win := total_win + current_win
        put "you have won this is your summary"
        delay (1000)
        cls
        put "********Summary********"
        put "Winnings: " : 2, current_win
        put "Your Bet: " : 2, bet
        put "status " : 2, status
        put "  "
        put "Total Bets: " : 2, counter
        put "Total Loses: " : 2, counters
        put "Total Winnings: " : 2, total_win
    else
        status := "Lost"
        current_win := current_win - bet
        counters := counters + 1
        total_win := total_win + current_win
        put "You have lost this is your summary"
        delay (1000)
        cls
        put "********Summary********"
        put "Winnings: " : 2, current_win
        put "Your Bet: " : 2, bet
        put "Status:   " : 2, status
        put "  "
        put "Total Bets: " : 2, counter
        put "Total Loses: " : 2, counters
        put "Total Winnings: " : 2, current_win
    end if
    delay (1000)
    put "would you like to play again?"
    get answer
    if answer = "no"
            then
        exit
    end if
end loop
cls
put "thank you for playing!"
Sponsor
Sponsor
Sponsor
sponsor
TheOneTrueGod




PostPosted: Wed May 31, 2006 1:23 pm   Post subject: (No subject)

Not too bad, but there is plenty of room for improvement. My first big concern is code optimization.

Look at the two if statements. Some major redundant code there. Why not just put the "output" part of it after the two statements?

Second, some problems with the whole guessing thing. You only randomize the number once, so the "random" number is going to be the same every time you play the game... Also, you don't tell the person that their number has to be between 1 and 10, and you don't regulate it between those values either.

You should probably keep track of the user's current amount of money, because as anyone who has gambled knows, you can't just reach down your pants and get more money... You have to have that money to begin with.

Also, you didn't account for answers like "n" or "No" or "nO" or something like "I would rather play polo". Also, I can crash it with ease by entering a string when it asks for my bet or my guess. When making a program based around input you need to validate that input. For doing things like this, make sure you have a loop around your checking area, and use the commands intstrok and intstr.

Good luck Very Happy
upthescale




PostPosted: Wed May 31, 2006 2:04 pm   Post subject: (No subject)

neat, but shudn't it be in sourcecode?
Jenkinz




PostPosted: Wed May 31, 2006 4:18 pm   Post subject: (No subject)

upthescale wrote:
neat, but shudn't it be in sourcecode?


sorryt worng section am farily new to the forums and turing
ohgeez_




PostPosted: Wed May 31, 2006 9:31 pm   Post subject: (No subject)

neat program.

now as theonetruegod said, ur code can be shortened by displaying ur summary after the if statement, ur random number needs to be different time everytime.

other things u do to shorten code.

- u can initialize ur variables with values by
code:
var counter : int := 0

which i think makes it easier to read since ur not going to change it back to zero later on in your program.

- u can also group ur variables together with a comma instead of all those lines
code:
var bet, guess, randnum : int


- same goes for the initialization
code:
var counter, counters : int := 0


otherwise. pretty good program =D
good luck
[Gandalf]




PostPosted: Wed May 31, 2006 10:21 pm   Post subject: (No subject)

Note: There is a difference between optimization and making your code shorter. Just because one program has less lines than another does not neccessarily mean that it runs faster and more efficiently than longer code.
Jenkinz




PostPosted: Fri Jun 02, 2006 8:40 am   Post subject: (No subject)

yea i just learend to do it that way so i stuck with it
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  [ 7 Posts ]
Jump to:   


Style:  
Search: