#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,406
    Rep Power
    1871
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,406
    Rep Power
    1871
    > 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.
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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
    }

    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.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,406
    Rep Power
    1871
    > 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.
    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
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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
    }
    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.
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    5
    Rep 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

IMN logo majestic logo threadwatch logo seochat tools logo