## General programming

Moderators: None (Apply to moderate this forum)
Number of posts: 2784

This Forum Only

Psuedocode - Write a slot machine program using at least two functions Posted by RoyalFlush on 2 Dec 2012 at 10:47 AM
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 or how to create functions. 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

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.

## Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic