Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |
 User Name: Password: Remember me

New Free Tools on Dev Shed!
We're Excited to announce that Dev Shed now has 70 free tools on the site. To learn more, click here!

 Dev Shed Forums Sponsor:
#1
December 2nd, 2012, 12:08 PM
 RoyalFlush
Registered User

Join Date: Dec 2012
Posts: 5
Time spent in forums: 2 h 45 m 12 sec
Reputation Power: 0
Psuedocode - Write a slot machine program using two functions

Write a slot machine program in psuedocode. The object of the game is to get 2 or 3 of the same numbers out of a total of 3 digits (each digit is in the range 1-9).

Requirements

· Welcome the user with an introduction and ask how much money they want to put in the slot machine.
· Ask the user how much of their money they want to bet
· Generate the three digits and show them to the user (hint: you will need to generate each digit separately.)
· Any 3 matching digits = Win ten 10 times your bet
· Any 2 matching digits = Win 2 times your bet
· Anything else = Lose your bet
· Tell the user how much they won or lost and how much money they have remaining
· Allow the user to play again, but only if they have money left
· When the user is out of money, thank them and end the game
· Make sure that you are validating what the user is doing. For example, don't let them make a bet higher than the amount they put in the machine.
· Use at least two FUNCTIONS to modularize the program (these functions must be written by you and they must pass and/or return arguments).

I think I might have the logic, which I've copied and pasted below, but I'm not sure how to modularize it. I'm lost regarding what the argument would be, the return, what to call, and what the local vs global would be. Sometimes I think I've got it, but then I do the ole' "ruffling of the paper" and starting from scratch. I think one function might be to validate if the balance is greater than the bet, with another function calculating the balance/profit/loss.

My logic

Code:
```declare int balance = 0
declare int bet = 0
declare int digit1 = 0
declare int digit2 = 0
declare int digit3 = 0
declare string play = ""

prompt "Welcome to Crazy Slots!"
prompt "How much money do you want to insert into the slot machine?"
input balance

DO
prompt "How much money do you want to bet?"
input bet
IF(bet > balance)THEN
output "You do not have a sufficient balance to bet that amount."
ENDIF
UNTIL(balance > bet)

DO
digit1=random(9)+1
output digit1
digit2=random(9)+1
output digit2
digit3=random(9)+1
output digit3
IF(digit1 == digit2)AND(digit1 == digit3)
bet = bet * 10
balance = balance + bet
output "You've won " + bet + " and have " + balance + " remaining."
ELSEIF(digit1 == digit2)AND(digit1 == digit3)OR(digit2 == digit3)THEN
bet = bet * 2
balance = balance + bet
output "You've won " + bet + " and have " + balance + " remaining."
ELSEIF(digit1 != digit2)AND(digit1 != digit3)AND(digit2 != digit3)THEN
bet = bet - bet
balance = balance - bet
output "You've lost " + bet + " and have " + balance + " remaining."
ENDIF
IF(balance > 0)THEN
prompt "Do you want to play again?(y/n)"
input play
ENDIF
UNTIL(play == "n")OR(balance <= 0)

IF(play == "n")THEN
"Thanks for playing!"
ELSEIF(balance <= 0)THEN
output "Your balance is null. Thank you for playing!"
ENDIF```

Thanks in advance for any help given.

#2
December 2nd, 2012, 01:13 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,261
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 13 m 24 sec
Reputation Power: 1827
Well I can see two easy functions

bet = getBet(balance)
balance = play(balance,bet)
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper

#3
December 2nd, 2012, 02:26 PM
 RoyalFlush
Registered User

Join Date: Dec 2012
Posts: 5
Time spent in forums: 2 h 45 m 12 sec
Reputation Power: 0
Per your reply, I gave it another try. As stated, I'm not too familiar on how to create the functions themselves. Did I get warmer? Colder?

Code:
```declare getBet(real bet1)
{
declare real bet = 0.0
declare real balance = 0.0

prompt "How much money do you want to bet?"
input bet
bet = getBet(balance1)

IF(bet1 > balance1)THEN
RETURN "You do not have a sufficient balance to bet that amount."
ENDIF
UNTIL(balance1 > bet1)

RETURN bet1
}

declare calcBalance(real balance1)

{
declare real balance = 0.0
declare real bet = 0.0

IF(digit1 == digit2)AND(digit1 == digit3)
bet = bet * 10
balance = balance + bet
output "You've won " + bet + " and have " + balance + " remaining."

ELSEIF(digit1 == digit2)AND(digit1 == digit3)OR(digit2 == digit3)THEN
bet = bet * 2
balance = balance + bet
output "You've won " + bet + " and have " + balance + " remaining."

ELSEIF(digit1 != digit2)AND(digit1 != digit3)AND(digit2 != digit3)THEN
bet = bet - bet
balance = balance - bet
output "You've lost " + bet + " and have " + balance + " remaining."
ENDIF
balance = play(balance, bet)

RETURN balance1
}

MAIN(

prompt "Welcome to Crazy Slots!"
prompt "How much money do you want to insert into the slot machine?"
input balance

DO
digit1=random(9)+1
output digit1

digit2=random(9)+1
output digit2

digit3=random(9)+1
output digit3

IF(balance > 0)THEN
prompt "Do you want to play again?(y/n)"
input play
ENDIF

UNTIL(play == "n")OR(balance <= 0)

IF(play == "n")THEN
"Thanks for playing!"
ELSEIF(balance <= 0)THEN
output "Your balance is null. Thank you for playing!"
ENDIF```

#4
December 2nd, 2012, 03:07 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,261
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 13 m 24 sec
Reputation Power: 1827
> declare getBet(real bet1)
What did I suggest as the interface?

This is a direct copy/paste of your post #1, wrapped in a function
Code:
```real getBet ( real balance )
real bet
DO
prompt "How much money do you want to bet?"
input bet
IF(bet > balance)THEN
output "You do not have a sufficient balance to bet that amount."
ENDIF
UNTIL(balance > bet)
return bet
end```

Now use the same idea for play.

#5
December 2nd, 2012, 03:27 PM
 RoyalFlush
Registered User

Join Date: Dec 2012
Posts: 5
Time spent in forums: 2 h 45 m 12 sec
Reputation Power: 0
Did I write play correctly in a function?

Code:
```string play(real balance, real bet)
{
IF(balance > 0)THEN
prompt "Do you want to play again? (y/n)"
input play
ENDIF
return play
}```

Quote:
 Originally Posted by salem > declare getBet(real bet1) What did I suggest as the interface? This is a direct copy/paste of your post #1, wrapped in a function Code: ```real getBet ( real balance ) real bet DO prompt "How much money do you want to bet?" input bet IF(bet > balance)THEN output "You do not have a sufficient balance to bet that amount." ENDIF UNTIL(balance > bet) return bet end``` Now use the same idea for play.

#6
December 2nd, 2012, 04:28 PM
 salem
Contributed User

Join Date: Jun 2005
Posts: 4,261
Time spent in forums: 2 Months 4 Weeks 1 Day 15 h 13 m 24 sec
Reputation Power: 1827
> Did I write play correctly in a function?
Think about it - what bit of your code takes the balance and a bet, which results in balance being modified.

How does your attempt even use bet ?

In short, no, you're still not getting it.

To show you any more would be just giving you your homework on a plate, so you're just going to have to try harder.

#7
December 2nd, 2012, 05:36 PM
 RoyalFlush
Registered User

Join Date: Dec 2012
Posts: 5
Time spent in forums: 2 h 45 m 12 sec
Reputation Power: 0
I thought about it, and I believe the following is the bit of code that takes the balance and a bet, resulting in the balance being modified.

Code:
``` string play(real balance, real bet)
{
real digit1 = 0.0
real digit2 = 0.0
real digit3 = 0.0

DO
digit1=random(9)+1
output digit1

digit2=random(9)+1
output digit2

digit3=random(9)+1
output digit3

IF(digit1 == digit2)AND(digit1 == digit3)
bet = bet * 10
balance = balance + bet
output "You've won " + bet + " and have " + balance + " remaining."

ELSEIF(digit1 == digit2)AND(digit1 == digit3)OR(digit2 == digit3)THEN
bet = bet * 2
balance = balance + bet
output "You've won " + bet + " and have " + balance + " remaining."

ELSEIF(digit1 != digit2)AND(digit1 != digit3)AND(digit2 != digit3)THEN
bet = bet - bet
balance = balance - bet
output "You've lost " + bet + " and have " + balance + " remaining."
ENDIF

IF(balance > 0)THEN
prompt "Do you want to play again?(y/n)"
input play
ENDIF
UNTIL(play == "n")OR(balance <= 0)
return play
}```

Quote:
 Originally Posted by salem > Did I write play correctly in a function? Think about it - what bit of your code takes the balance and a bet, which results in balance being modified. How does your attempt even use bet ? In short, no, you're still not getting it. To show you any more would be just giving you your homework on a plate, so you're just going to have to try harder.

#8
December 2nd, 2012, 06:13 PM
 RoyalFlush
Registered User

Join Date: Dec 2012
Posts: 5
Time spent in forums: 2 h 45 m 12 sec
Reputation Power: 0
Would the main be the following?

Code:
```MAIN(

prompt "Welcome to Crazy Slots!"
prompt "How much money do you want to insert into the slot machine?"
input balance
balance = play(balance,bet)

bet = getBet(balance)

IF(play == "n")THEN
"Thanks for playing!"
ELSEIF(balance <= 0)THEN
output "Your balance is null. Thank you for playing!"
ENDIF```

 Viewing: Dev Shed Forums > Other > Beginner Programming > Psuedocode - Write a slot machine program using two functions

## Developer Shed Advertisers and Affiliates

 Thread Tools Search this Thread Search this Thread: Advanced Search Display Modes Rate This Thread Linear Mode Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts vB code is On Smilies are On [IMG] code is On HTML code is Off
 View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox Forum Jump Please select one User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home -------------------- Programming Languages    PHP Development        PHP FAQs and Stickies    Perl Programming        Perl FAQs and Stickies    C Programming        C Programming FAQs and Stickies    Java Help        Java FAQs    Python Programming        Python Programming FAQs    Ruby Programming        Ruby Programming FAQs    Game Development        Game Development FAQs Programming Languages - More    ASP Programming        ASP Programming FAQs    .Net Development        .Net Development FAQs    Visual Basic Programming        Visual Basic Programming FAQs    Software Design        Software Design FAQs    ColdFusion Development        ColdFusion Development FAQs    Delphi Programming        Delphi Programming FAQs    Regex Programming        Regex Programming FAQs    XML Programming        XML Programming FAQs    Other Programming Languages        Other Programming Languages FAQs Web Design    HTML Programming        HTML Programming FAQs    JavaScript Development        JavaScript Development FAQs    CSS Help        CSS Help FAQs    Flash Help        Flash Help FAQs    Photoshop Help        Photoshop Help FAQs    Web Design Help        Web Design Help FAQs    Website Critiques        Website Critiques FAQs    Search Engine Optimization        Search Engine Optimization FAQs Mobile Programming    Mobile Programming        Mobile Programming FAQs    iPhone SDK Development        iPhone SDK Development FAQs    Android Development        Android Development FAQs    BlackBerry Development        BlackBerry Development FAQs Web Site Management    Business Help        Business Help FAQs    Development Software        Development Software FAQs    Scripts        Scripts FAQs Databases    Database Management        Database Management FAQs    DB2 Development        DB2 Development FAQs    MySQL Help        MySQL Help FAQs    PostgreSQL Help        PostgreSQL Help FAQs    Firebird SQL Development        Firebird SQL Development FAQs    MS SQL Development        MS SQL Development FAQs    Oracle Development        Oracle Development FAQs    LDAP Programming        LDAP Programming FAQs System Administration    Mail Server Help        Mail Server Help FAQs    Apache Development        Apache Development FAQs    Security and Cryptography        Security and Cryptography FAQs    Antivirus Protection        Antivirus Protection FAQs    DNS        DNS FAQs    IIS        IIS FAQs    Networking Help        Networking Help FAQs    FTP Help        FTP Help FAQs Operating Systems    BSD Help        BSD Help FAQs    Linux Help        Linux Help FAQs    UNIX Help        UNIX Help FAQs    Windows Help        Windows Help FAQs    Mac Help        Mac Help FAQs Web Hosting    Web Hosting        Web Hosting FAQs    Free Web Hosting        Free Web Hosting FAQs    Web Hosting Requests        Web Hosting Requests FAQs    Web Hosting Offers        Web Hosting Offers FAQs Computer Hardware    Computer Hardware    CPUs        CPUs FAQs    Cooling        Cooling FAQs    Embedded Programming        Embedded Programming FAQs    Motherboards        Motherboards FAQs    Multimedia Hardware        Multimedia Hardware FAQs Other    Dev Shed Lounge        Dev Shed Lounge FAQs    Development Articles        Development Articles FAQs    Beginner Programming        Beginner Programming FAQs    Hire A Programmer        Hire A Programmer FAQs    Project Help Wanted        Project Help Wanted FAQs Latest News Updated Hourly    Technology News    Business News    Science News Forum Information    Forum Rules/Guidelines        Forum Rules/Guidelines FAQs    Forum Announcements        Forum Announcements FAQs    Dev Shed Gaming Center        Go to the Dev Shed Battle Arena        Go to the Dev Shed Arcade Games        Go to the Legend of the Green Dragon    Suggestions & Feedback        Suggestions & Feedback FAQs

 Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |