Simulating Slot Machine Python

SlotMachine
import random
print(''Welcome to the Slot Machine Simulator
You'll start with $50. You'll be asked if you want to play.
Answer with yes/no. you can also use y/n
No case sensitivity in your answer.
For example you can answer with YEs, yEs, Y, nO, N.
To win you must get one of the following combinations:
BARtBARtBARttpayst$250
BELLtBELLtBELL/BARtpayst$20
PLUMtPLUMtPLUM/BARtpayst$14
ORANGEtORANGEtORANGE/BARtpayst$10
CHERRYtCHERRYtCHERRYttpayst$7
CHERRYtCHERRYt -ttpayst$5
CHERRYt -t -ttpayst$2
'')
#Constants:
INIT_STAKE = 50
ITEMS = ['CHERRY', 'LEMON', 'ORANGE', 'PLUM', 'BELL', 'BAR']
firstWheel = None
secondWheel = None
thirdWheel = None
stake = INIT_STAKE
def play():
global stake, firstWheel, secondWheel, thirdWheel
playQuestion = askPlayer()
while(stake != 0 and playQuestion True):
firstWheel = spinWheel()
secondWheel = spinWheel()
thirdWheel = spinWheel()
printScore()
playQuestion = askPlayer()
def askPlayer():
''
Asks the player if he wants to play again.
expecting from the user to answer with yes, y, no or n
No case sensitivity in the answer. yes, YeS, y, y, nO . . . all works
''
global stake
while(True):
answer = input('You have $' + str(stake) + '. Would you like to play? ')
answer = answer.lower()
if(answer 'yes' or answer 'y'):
return True
elif(answer 'no' or answer 'n'):
print('You ended the game with $' + str(stake) + ' in your hand.')
return False
else:
print('wrong input!')
def spinWheel():
''
returns a random item from the wheel
''
randomNumber = random.randint(0, 5)
return ITEMS[randomNumber]
def printScore():
''
prints the current score
''
global stake, firstWheel, secondWheel, thirdWheel
if((firstWheel 'CHERRY') and (secondWheel != 'CHERRY')):
win = 2
elif((firstWheel 'CHERRY') and (secondWheel 'CHERRY') and (thirdWheel != 'CHERRY')):
win = 5
elif((firstWheel 'CHERRY') and (secondWheel 'CHERRY') and (thirdWheel 'CHERRY')):
win = 7
elif((firstWheel 'ORANGE') and (secondWheel 'ORANGE') and ((thirdWheel 'ORANGE') or (thirdWheel 'BAR'))):
win = 10
elif((firstWheel 'PLUM') and (secondWheel 'PLUM') and ((thirdWheel 'PLUM') or (thirdWheel 'BAR'))):
win = 14
elif((firstWheel 'BELL') and (secondWheel 'BELL') and ((thirdWheel 'BELL') or (thirdWheel 'BAR'))):
win = 20
elif((firstWheel 'BAR') and (secondWheel 'BAR') and (thirdWheel 'BAR')):
win = 250
else:
win = -1
stake += win
if(win > 0):
print(firstWheel + 't' + secondWheel + 't' + thirdWheel + ' -- You win $' + str(win))
else:
print(firstWheel + 't' + secondWheel + 't' + thirdWheel + ' -- You lose')
play()

commented Dec 14, 2015

JavaScript implementation of an 'advanced slot machine', a penny arcade casino game. Uses HTML, JavaScript, CSS, and Images. Javascript css html js simulation gambling slot casino slot-machine playout las-vegas spielhalle geldspiel sonderspiele geldspielautomat penny-arcade special-games einarmiger-bandit ausspielung risiko.

Simulating Slot Machine Python Programming

Instead of;
if(answer 'yes' or answer 'y'):

Do;
if answer.lower() in ['yes',y']

commented Jun 2, 2017

I run it on python 2 ,it's need to modify the 43 line (input -> raw_input)

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

CS 115 Program 2 Slot Machine Fall 2020

Due Dates:
Design and Test Cases: Friday, October 30, midnight
Source Phase I: Friday November 6, midnight
Graphics Source Phase II: Friday November 13, midnight

The graphics description is now posted for Phase II. 10/25/2020

Assignment total points = Design (20 points) + Answers for test cases (10 points) + Implementation Phase I (90 points) + Phase II (50 points) = 170 points

Simulating Slot Machine Python Compiler

Submit all program materials (.py files) with the Canvas link.

A reminder that you are allowed ONE classmate as apartner. Make sure that there are NOT more people involved from the class. You ARE allowedand encouraged to work with any TA or Dr. Keen.If you have a partner, make sure you reference them and they reference you or there will be a penalty.

The educational goals of this program are to use the concepts of

  • design with pseudocode
  • if, while loop and function control structures
  • random numbers
  • accumulators
  • self-written functions and how to document them
  • string comparison
  • input validation
  • testing with valid and invalid inputs
  • documentation
  • use Boolean operators
  • use the graphics library (graphics version)

A short reading about how to document a function

Your program will simulate a slot machine, a 'one-armed bandit'. You willlet the player enter their wager, use pseudorandom numbersto 'spin the wheels' on the machine,and calculate the player's winnings.

The rules of the game:
The slot machine has 3 wheels with symbols of acherry, apple, banana, orange, melon, lemon, grape, a bell and a JACKPOT.There are 9 symbols on each wheel.A player wagers an amount of money (no more than they have!) (an integer).When the player 'pulls the handle', the wheels spin around,and then each one stops at one of the symbols.If some of the wheels come up with matching symbols, theplayer wins various amounts of money.

How the player wins:

  • If three JACKPOTS come up, the player wins 20 times the amountthey bet.
  • If three bells come up, they win 10 times the bet.
  • If any other symbol comes up three times, they win 3 times the bet.
  • If any symbol comes up just two times, they win twice the amountthey bet.
  • Otherwise they lose the amount they bet.

The game is ended in two ways: when the player says they don'twant to play any more, or when they lose all their money.The program should report the amount of money that the playerleaves with or that they are bankrupt.

Here is an example of the interaction between the user andyour program on the screen. These do not show any graphics, just text.

Another Example Run (Note the input validations):

Another Example Run (user loses it all):

There are two Phases to this program. The first phase usesjust text graphics as shown above.The second phase uses the graphics library.The graphics version is due a week after the text version.If you have thelogic worked out in the text version, the graphics version is MUCH easier.

Here is where a description of the graphics part is

Test cases and design do not need to consider the graphics part.

(20 points) Test Cases:

Complete this test plan for the program. These answers should be put in the file with the design at the bottom of the file and turned in at thefirst deadline.

We have to consider tests at different levels of detail.

  • Think about inputs from the user (lowest level detail).
  • Think about how to test the mechanisms of the game, the winning or losing.
  • Think about unit (function) testing (read the reading about functions).

Slot Machine Games

These need different tables to organize. The results of tests will not be just outputs (numbers) but actions (“player loses”). Since the program uses random numbers, some of the actions are not directly controllable by the user. You can only control the program by inputs.
User enters invalid bet (low), pot is $50-5 'too small', user is prompted to enter a valid input, another input accepted
User enters invalid bet (high), pot is $5075 'too much', user is prompted to enter a valid input, another input accepted
User makes another spin
User does not want to play again____A.__(the complete row)_
User gives invalid input____B.__(the complete row)_
User leaves the game with total winnings of first spin
User ends game after 2 spinsy, n ___C.____
Game ends because of bankruptcy bet equal to pot, loses all___D.____
User only wants to play one spinn when asked 'Play again?'user told how much they have, leaves the program
User wants to play two spins (Pot bigger than 55) 25, y, 30, n when asked 'Play again?' ___E.___ NOTE: same as C

No need for testing getaspin().

output prompt 'John do you want to play again?', returns True
normal, input Y 'John','Y' output prompt 'John do you want to play again?', returns True
normal, input n'Mary', 'n' ___F.____
normal, input N'Mary', 'N' ___F.____
invalid, then valid input'Sam', inputs t, pie, same, y ___G.___
bet too high 200, 300 outputs prompt, gives error message 'too much', asks for another bet
bet too low 200, -1 ___H.___
outputs 'three of a kind', returns 500
Triple bell'bell','bell','bell', 50 ___I.__
Two of a kind 'apple','apple','cherry' ___J.___
Losing spin 'apple','bell','cherry' ___K.___

(20 points) Design

Write the steps in pseudocode (NOT Python!) in comments in a Python file.Save this Python file as 'design2.py'.Submit this .py file through Canvas as usual.

The whole program has a prolog, at the top as usual, which discuss the whole PROGRAM(and the main function), just as we have always written.The 3 P's you write for the whole program count as being for the main function.

Each function definition, not just the main, must have a prolog.You write the three P's at the top of each function.The purpose of the FUNCTION, not the whole program,
the preconditions of the FUNCTION, which are the parameters (if any)and the input from the user (if any)
the postconditions of the FUNCTION, which are the outputs of the functionor actions or any return value.

As usual, give the types of these variables and their meaning. Don't say'two integer parameters', say 'two integers parameters, first one is height, second one iswidth', for example.

After the 3 P's of each function, you comment all the control structures you will use in the function to achieve the function's purpose.They are mostly very short functions, very few control structures.

(80 points) Implementation Phase I

Slot

There are some specifications that your program needs to meet.

  • Your code must be documented with the design. You must use meaningful variable names.
  • Your design comments must appear between the lines of code.
  • You must have a main function.
  • The output prompts and labels of your program MUST match theones given in the example runs.
  • Any library imports must be OUTSIDE the main function.
  • ALL variables must be defined and used INSIDE a function definition;global variables are not allowed!
  • Unnecessary typecasts should not be used.
  • Please use seed of 0 for starting your random numbers.
  • Make sure you eliminate any syntax and semantics errors. Use the test cases!
  • All guarantees of structured programming must be obeyed; that means NO breaks, continues or passes! Major penalties!
  • Each function has one return statement.
  • NOTE: you are not allowed to use recursion. This is any function calling itself. People do it becausethey do not know how to use a while loop. Your program has a while loop in the main function. There are more while loops in the other functions.

Your program must have and use the following functions:
PLEASE use the names given for the functions!

  • validate_yorn: has one parameter, a string which is the name of the player.The function asks the user (by name) if they want to play again. The function validatesthe user's input. They must enter either 'y', 'Y', 'n', or 'N' to be valid.All other inputs are rejected and more input is requested.The function returns True if the user entered y or Y, False if n or N.
  • validate_bet: receives a parameter, an integer which represents the pot.The function asks the user what they want to bet based on the size of the pot, and does input validation on that input.Numbers between 1 and the size of the pot, inclusive, are valid. Any other numbers arenot accepted. Numbers are either too high or too low, an appropriate error message is outputand more input is requested.The function returns the validated integer that is the bet.
  • getaspin: has no parameters.Returns a random string choice from the list ['cherry', 'apple', 'banana', 'orange', 'melon', 'lemon', 'grape', 'bell', 'JACKPOT']. Each choice is equally likely.
  • calculate_win: has 4 parameters, first 3 are strings which are the spin results, the fourth is the amount of the bet, an integer.The function uses the parameters to determine how much the user won (or lost). The rules described at the top of the assignment are used to determine whether the user winsor loses and how much they win or lose.No loops are needed, just logic. If the user lost the bet, the negative of the bet is returned.The amount won or lost is returned as an integer.