Python Slot Machine: Calculating Line Payouts - python

I'm a Python newbie attempting to create a slot machine simulator that mimics the payouts of the real machines. I'm running into an issue in calculating the line payouts, and I'm sure there's a smarter way of iterating through the lines and calculating them.
Defining some constants that I'll be using:
SymbolMap = ["Boats","Bonus","Buoys","Clams","Light Houses","Lobsters","Scatter","Seagulls","Starfish","Tuna","Wilds"]
#The ints in reels below are a simpler way to express the SymbolMap above
Reels = [[9,8,3,4,6,3,8,1,5,6,2,3,8,2,3,8,5,4,3,10,7,8,10,1,3,0,8,9,3,8,9,5,3,8,0,4,3,8,0,9,2,7,5,3,8,0,7],
[3,2,4,3,2,4,1,7,3,0,7,9,0,1,8,7,10,1,7,4,5,10,2,3,1,7,3,6,5,9,7,6,8,3,0,5,7,3,1,8,7,2,4,3,9,7,0],
[0,8,3,1,4,0,5,8,1,4,8,1,9,8,3,7,8,10,1,4,7,8,9,3,0,9,8,1,9,4,8,6,4,5,7,8,6,2,9,5,1,8,4,7,2,0,9],
[7,9,2,7,6,2,8,7,9,10,2,9,8,5,7,9,10,5,4,2,7,0,3,8,4,7,0,3,2,7,0,4,8,9,7,2,8,3,2,7,8,3,5,10,2,7,8],
[3,10,0,5,2,8,4,9,8,4,7,10,9,2,0,3,9,2,8,3,6,2,8,9,3,2,0,4,9,5,4,7,3,5,8,0,4,9,7,8,4,3,5,7,8,3,7]]
# Lines are the row to look for on each reel. i.e. Lines[0] is a straight line of the 2nd row.
# Lines[3] starts in top left corner of matrix, and forms inverted V shape.
Lines = [[1,1,1,1,1],
[0,0,0,0,0],
[2,2,2,2,2],
[0,1,2,1,0],
[2,1,0,1,2],
[2,2,1,0,0],
[0,0,1,2,2],
[1,2,1,0,1],
[1,0,1,2,1],
[2,1,1,1,0],
[0,1,1,1,2],
[1,2,2,1,0],
[1,0,0,1,2],
[1,1,2,1,0],
[1,1,0,1,2]]
#Payouts are how many credits won for symbols in a row. For example, Symbols[0] is Boats.
#2 boats is 0 credits, 3 boats is 25 credits, 4 boats is 100 credits, 5 boats is 500 credits.
#They must be continuous and from left to right. I.e. BOAT-BOAT-CLAM-BOAT-BOAT on a payline wins 0.
#Similarly, CLAM-CLAM-BOAT-BOAT-BOAT wins 0.
Payouts = [[0,25,100,500],
[0,0,0,0],
[0,25,100,500],
[0,5,30,200]]
#Initializing a 3X5 matrix to represent reels
SpinValues = [[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
#Initializing message
Message = ''
#Initializing TotalWin
TotalWin = 0
Spin logic, which properly generates a 3X5 matrix of random numbers. Is there a better way to handle the "if first 3 symbols match" portion, since I'll have to repeat again for 4 symbols and 5 symbols? Since each line is only entitled to one payout, I'll start with the 5 symbol payouts, then work down toward 2. I started with 3 because it's the most common and will be easiest to test. I'll also have to account for a wild equaling any symbol, which I haven't tried to tackle yet. Likewise, there is a scatter pay (meaning if you have X number of Scatter symbols anywhere in the matrix, you get a payout. That part will be easy). There is also a bonus game, which I'll be working on later:
def spin(linesPlayed, wager):
for i, object in enumerate(Reels):
length = len(Reels[i])
StopValue = random.randint(0,length-1)
SpinValues[1][i] = Reels[i][StopValue]
if StopValue == 0:
SpinValues[0][i] = Reels[i][-1]
else:
SpinValues[0][i] = Reels[i][StopValue - 1]
if StopValue == len(Reels[i])-1:
SpinValues[2][i] = Reels[i][0]
else:
SpinValues[2][i] = Reels[i][StopValue +1]
print(SpinValues[0])
print("\n")
print(SpinValues[1])
print("\n")
print(SpinValues[2])
for i in range(linesPlayed):
#if first 3 symbols match
if SpinValues[Lines[i][0]] == SpinValues[Lines[i][1]] == SpinValues[Lines[i][2]]:
PayTable(i,wager,3,SpinValues[Lines[i][0]])
#if first 4 symbols match
#if first 5 symbols match
#handle scatter pay
#wilds?
#handle bonus trigger
Handling wins:
def PayTable(i,wager,symbolCount,symbol):
LineWin = Payouts[symbol][symbolCount] * wager
TotalWin += Payouts[symbol][symbolCount] * wager
Message += "Line " + str(i) +" wins " + str(LineWin) + " credits with " + str(symbolCount) + " " + SymbolMap[symbol] + "!" + "\n"
I'm getting the error that both TotalWin and Message are undefined. I thought that I could defined them globally up top?

You need to use the global keyword in each function to access variables defined in a parent.
For example:
def PayTable(i,wager,symbolCount,symbol):
global TotalWin
global Message

Related

I used return, however the recursion does not end. help me please

I am doing a question that gives me a start coordinate, a end coordinate and the number of times of moving.Every time you can add 1 or minus 1 to x or y coordinate based on previous coordinate and the number of moving limit the time the coordinate can move. At last, I need to identify whether there is a possibility to get to the end coordinate
I decide to use recursion to solve this problem however, it does not end even if I wrote return inside a if else statement. Do you mind to take a look at it.
This is the code
# https://cemc.uwaterloo.ca/contests/computing/2017/stage%201/juniorEF.pdf
# input
start = input()
end = input()
count = int(input())
coo_end = end.split(' ')
x_end = coo_end[0]
y_end = coo_end[1]
end_set = {int(x_end), int(y_end)}
#processing
coo = start.split(' ')
x = int(coo[0])
y = int(coo[1])
change_x = x
change_y = y
sum = x + y+count
set1 = set()
tim = 0
timer = 0
ways = 4** (count-1)
def elit(x, y, tim,timer, ways = ways):
print(tim,timer)
tim = tim +1
co1 = (x, y+1)
co2 = (x+1, y)
co3 = (x, y-1)
co4 = (x-1, y)
if tim == count:
tim =0
set1.add(co1)
set1.add(co2)
set1.add(co3)
set1.add(co4)
print(timer)
timer = timer +1
if timer == ways:
print('hiii')
return co1, co2, co3, co4 #### this is the place there is a problem
elit(co1[0],co1[1],tim,timer)
elit(co2[0],co2[1],tim,timer)
elit(co3[0],co3[1],tim, timer)
elit(co4[0],co4[1],tim, timer)
#print(elit(change_x,change_y,tim)) - none why
elit(change_x,change_y,tim, timer)
#print(list1)
for a in set1:
if end_set != a:
answer = 'N'
continue
else:
answer = "Y"
break
print(answer)
In addition, if you have any suggestions about writing this question, do you mind to tell me since I am not sure I am using the best solution.
one of example is
Sample Input
3 4 (start value)
3 3 (end value)
3 (count)
Output for Sample Input
Y
Explanation
One possibility is to travel from (3, 4) to (4, 4) to (4, 3) to (3, 3).
the detailed question can be seen in this file https://cemc.uwaterloo.ca/contests/computing/2017/stage%201/juniorEF.pdf
It is question 3. Thank you
thank you guys
the function is returning properly however by the time you reach the recursive depth to return anything you have called so many instances of the function that it seems like its in an infinite loop
when you call elite the first time the function calls itself four more times, in the example you have given timer is only incremented every 3 cycles and the function only return once timer hits 16 thus the function will need to run 48 times before returning anything and each time the function will be called 4 more times, this exponential growth means for this example the function will be called 19807040628566084398385987584 times, which depending on your machine may well take until the heat death of the universe
i thought i should add that i think you have somewhat over complicated the question, on a grid to get from one point to another the only options are the minimum distance or that same minimum with a diversion that must always be a multiple of 2 in length, so if t the movement is at least the minimum distance or any multiple of 2 over the result should be 'Y', the minimum distance will just be the difference between the coordinates on each axis this can be found by add in the difference between the x and y coordinates
abs(int(start[0]) - int(end[0])) + abs(int(start[1]) -int(end[1]))
the whole function therefore can just be:
def elit():
start = input('start: ').split(' ')
end = input('end: ').split(' ')
count = int(input('count: '))
distance = abs(int(start[0]) - int(end[0])) + abs(int(start[1]) -int(end[1]))
if (count - distance) % 2 == 0:
print('Y')
else:
print('N')
input:
3 4
3 3
3
output:
Y
input:
10 4
10 2
5
output:
N

Paradox python algorithm

I am trying to solve a version of the birthday paradox question where I have a probability of 0.5 but I need to find the number of people n where at least 4 have their birthdays within a week of each other.
I have written code that is able to simulate where 2 people have their birthdays on the same day.
import numpy
import matplotlib.pylab as plt
no_of_simulations = 1000
milestone_probabilities = [50, 75, 90, 99]
milestone_current = 0
def birthday_paradox(no_of_people, simulations):
global milestone_probabilities, milestone_current
same_birthday_four_people = 0
#We assume that there are 365 days in all years.
for sim in range(simulations):
birthdays = numpy.random.choice(365, no_of_people, replace=True)
unique_birthdays = set(birthdays)
if len(unique_birthdays) < no_of_people:
same_birthday_four_people += 1
success_fraction = same_birthday_four_people/simulations
if milestone_current < len(milestone_probabilities) and success_fraction*100 > milestone_probabilities[milestone_current]:
print("P(Four people sharing birthday in a room with " + str(no_of_people) + " people) = " + str(success_fraction))
milestone_current += 1
return success_fraction
def main():
day = []
success = []
for i in range(1, 366): #Executing for all possible cases where can have unique birthdays, i.e. from 1 person to a maximum of 365 people in a room
day.append(i)
success.append(birthday_paradox(i, no_of_simulations))
plt.plot(day, success)
plt.show()
main()
I am looking to modify the code to look for sets of 4 instead of 2 and then calculate the difference between them to be less than equal to 7 in order to meet the question.
Am I going down the right path or should I approach the question differently?
The key part of your algorithm is in these lines:
unique_birthdays = set(birthdays)
if len(unique_birthdays) < no_of_people:
same_birthday_four_people += 1
Comparing the number of unique birthdays to the number of people did the work when you tested if two different people had the same birthday, but It wont do for your new test.
Define a new function that will receive the birthday array and return True or False after checking if indeed 4 different people had the a birthday in a range of 7 days:
def four_birthdays_same_week(birthdays):
# fill this function code
def birthday_paradox(no_of_people, simulations):
...
(this function can be defined outside the birthday_paradox function)
Then switch this code:
if len(unique_birthdays) < no_of_people:
same_birthday_four_people += 1
into:
if four_birthdays_same_week(birthdays):
same_birthday_four_people += 1
Regarding the algorithm for checking if there 4 different birthday on the same week: a basic idea would be to sort the array of birthdays, then for every group of 4 birthdays check if the day range between them is equal or lower to 7:
if it is, the function can immediately return True.
(I am sure this algorithm can be vastly improved.)
If after scanning the whole array we didn't return True, the function can return False.

Recursively find all possible paths with a set number of moves and varying options at each node

I have a 5 x 5 grid. Each grid is an object with an id, a value (initially empty), and a possible move set. The possible move set is that given any location on the grid it can move to any new position that is either -16,-21,-12,-3,3,12,21,16 of the current grid id but the position cannot be less than 1 or above 25 (since the grid is 25 positions) and cannot be already occupied.
The objective is to fill the whole grid with numbers 1-25 with the number being the number of the move that positioned there. But there isn't necessarily only one route per starting position so I would like to find all possible paths for a given starting point on the grid.
I have the grid creation working correctly, as well as selecting from the possible options to the next. My issue is concerning several aspects, the rollback to choose a different path if the chosen path was a dead-end or rolling back several levels of selection if there is repeated failure. Additionally finding all paths after one path has been found.
As seen above, for example, if I start at the center at 13 I can move to 2 then 17, etc. If I get stuck I would then move back to the previous point and try another option. I would try all possible options at that level but if it's a dead-end I would move back to another level. The idea is to find a path that uses ALL 25 cells
Given this below code. I can get to the 17th round out of 25 in the selection but I cannot seem to wrap my head around back tracking further to find a 1 complete path let alone multiple:
grid_size = 25
grid = {}
grid_list = []
#initialise grid details
pos_moves=[-16,-21,-12,-3,3,12,21,16 ]
### give all possible moves for this
x = 1
while x <= grid_size:
moves =[]
for i in pos_moves:
if 0 < (x + i) < 26:
moves.append(x + i)
#print(moves)
grid[x] ={ "id" : x, "moves" : moves}
grid_list.append({"id":x})
x += 1
print(grid)
visited = {}
next_location = int
round_level = 1
def choose_path(current_location):
global round_level
global next_location
options = grid[current_location]['moves']
print("################## Round " + str(round_level) + "##################\n" +
"current grid area: " + str(current_location) + "\n"
"current options: " + str(options) + "\n"
)
next_location = options.pop(0)
if next_location not in visited:
visited[next_location] = options
print("Remaining options: " + str(options) + " chosen option" + str(next_location))
print("visited: " + str(visited))
round_level += 1
return choose_path(next_location)
elif next_location in visited:
if len(options) == 0:
print("Visited: " + ",".join(str(key) for key in visited))
print("stopping here")
else:
next_location = options.pop(0)
return choose_path(next_location)
choose_path(13)

Simulating the transition of packages in a distribution line

I am new to python and am trying to run a simulation of a warehouse logistics. The problem is composed of four main agents:
a shed, trucks, motorcycles and a distribution line. The truck enters the shed in one side with a specified amount of boxes, it goes to center of the shed, stops and start unloading the boxes to the distribution line, the distribution line moves the boxes to the other side of the shed where motorcycles pickup one box each.
The objective is to vary the size of the shed and distribution line to find the shape that can deliver more boxes in fixed amount of time (or compute the time taken to distribute a fixed amount of boxes, as in my code for now)
The distribution line is a rectangle, a grid with variable amount of rows and columns, depending on the size of the shed, let's say each cell has 0,50m on each side.
In the code I simulated the truck passing through the shed, and the amount of trucks passing as iterations, the problems is:
how to simulate the boxes moving through the grid (distribution line) from one side to the other, maybe accumulating in the stock until a bike arrives, and have the motorcycles "grab" them and go out after the boxes arrive?
I tried to count the boxes with "+= 1" function but I don't know why it's not working (would not be very realistic as well)
This is the main code:
import time
from Vehicles import Truck, Motorbike
bike1 = Motorbike(10, 1)
truck1 = Truck(10, int(input("Enter how many loads the truck has: ")))
num_iterations = int(input("Enter number of iterations: "))
start = time.time()
shed_width = 4
shed_length = 12
truck_path = int(shed_length * truck1.truck_speed/2)
for n in range(num_iterations):
truck_middle = False
while truck_middle is not True:
for i in range(truck_path):
x = 100/truck_path
if i == truck_path/2:
truck_middle = True
else:
#the bar here is to just have some visual feedback while the code runs
print("\r[%-60s] %d%%" % ('=' * i, x * i), end='')
time.sleep(0.1)
print("\ntruck is in the middle")
truck_middle = True
# while truck_middle is True:
# box = 0
# if box < truck1.truck_load:
# box += 1
# else:
# truck_middle = False
print("This was iteration: " + str(n+1))
time.sleep(0.01)
end = time.time()
print("\nDone! \nThe simulation took " + str(end - start) + " seconds to complete!")
I also created a class in a file called "Vehicles" for the truck and the motorcycles, where I can define their speed and the load they can carry:
class Truck:
def __init__(self, truck_speed, truck_load):
self.truck_speed = truck_speed
self.truck_load = truck_load
class Motorbike:
def __init__(self, motorbike_speed, motorbike_load):
self.motorbike_speed = motorbike_speed
self.motorbike_load = motorbike_load
I am open to code suggestions, indications of libraries and other resources I can search and study, any help will be much appreciated! thanks!
box = 0
while truck_middle == True:
if box < truck1.truck_load:
box += 1
else:
truck_middle = False
In your way, box will always be 1 and truck_middle is always True, and it goes in a dead loop

Optimize search through array in python 2.7

I'm currently working on a piece of code for a school project, wherein we have to lead customers to an available register within a supermarket. Format is that we get first a string with two numbers "x y", where x is the amount of registers and y is the amount of people coming to/leaving a register. Next there is y lines of either "C X" or "L X" format, where "C X" means a customer arrives at register X, and "L X" would be a customer leaving register X. Now if a customer arrives at an empty register it should just print this register number, but if a customer arrives at an already taken register, it should print out the nearest free register, favoring lower numbers (say nr 4 is taken but a customer arrives and both 3 and 5 is free, it should print 3). I'm currently using an array wherein I'm keeping the amount of registers as 0's and then changing these to ones if the register is taken and if it is I then loop through +- 1, +- 2 and so on till I find a free register. However, for a full grade we need to be able to take inputs of up to a million M, and our code needs to execute within a minute and use up a max of 256 mb of memory. Current code is below, I keep hitting the time limit, probably because of the way I iterate over the array, any ideas on how I could optimize this?
N, M = map(int, raw_input().split())
entries = []
for x in range(0, M):
entries.append(raw_input())
registers = []
for x in range(0,M):
registers.append(0)
for line in entries:
if line[0] == "L":
registers[int(line[2])-1] = 0
elif registers[int(line[2])-1] == 1:
i = 0
n = int(line[2])-1
while True:
if registers[n-i] == 1:
pass
elif n-i >= 0:
registers[n-i] = 1
print n-i+1
break
if registers[n+i] == 1:
pass
else:
registers[n+i] = 1
print n+i+1
break
i += 1
else:
registers[int(line[2])-1] = 1
print int(line[2])

Categories