Instructions
Objective
Write a program to implement maze in python.
Requirements and Specifications
Source Code
import random
# define your Maze class here
class Maze:
def __init__(self, N=4, p=0.3, maze_style='basic'):
# Ensure that N is in [4,8]
if N < 4:
N = 4
elif N > 8:
N =8
self.N = N
# Ensure that P is in [0, 1)
if p < 0:
p = 0
elif p >= 1:
p = 0.5
if maze_style != 'basic' and maze_style != 'xspace':
maze_style = 'basic'
self.p = p
self.maze_style = maze_style
def gen_maze(self):
'''
Randomly generate an embedded list with N sublists, each of which has N integer elements with values either 0 or 1. For example, if N = 4, the generated list named maze could be
[[0, 0, 1, 0],
[1 0, 0, 1],
[0, 1, 0, 0],
[1 0, 0, 0]].
The starting point is at maze[0][0], and the end point is at maze[N-1][N-1].
'''
self.maze = [] # initialization the embedded list
# implement the gen function here (2 marks)
for i in range(self.N):
# Create a sublist of N elements with random 0 and 1
sublist = []
for j in range(self.N):
# Generate random number
x = random.random()
if x <= self.p:
x = 1
else:
x = 0
sublist.append(x)
# If the current index is zero, then set the elment (0,0) as 0
if i == 0:
sublist[0] = 0
elif i == self.N-1: # if the current index is N-1 then set the element (N,N) to 0
sublist[-1] = 0
self.maze.append(sublist)
def print_maze(self):
"""
Print the maze on the screen (standard output).
"""
print('maze style is: ' + self.maze_style)
# write your code below (2 marks)
if self.maze_style == 'basic':
print(self.maze)
elif self.maze_style == 'xspace':
for i in range(self.N):
row = self.maze[i]
for j in range(self.N):
if (i == 0 and j == 0) or (i == self.N-1 and j == self.N-1):
print('*', end = "")
else:
if self.maze[i][j] == 0:
print('O', end='')
else:
print('X', end='')
print()
print('Done!')
cout << "Enter number of hours: ";
cin >> hours;
if(hours > 0) {
break;
}
else {
cout << "Please enter a positive number of hours." << endl;
}
}
// Now, calculate
double monthly_cost;
double hour_cost;
double total_cost;
int max_hours;
if(package_name.compare("A") == 0 || package_name.compare("a") == 0) {
monthly_cost = 9.95;
hour_cost = 2.00;
max_hours = 10;
total_cost = monthly_cost;
// Calculate
if(hours > max_hours) {
total_cost = monthly_cost + (hours - max_hours)*hour_cost;
}
}
else if(package_name.compare("B") == 0 || package_name.compare("b") == 0) {
monthly_cost = 13.95;
hour_cost = 1.00;
max_hours = 20;
total_cost = monthly_cost;
// Calculate
if(hours > 120) {
total_cost = monthly_cost + (hours - max_hours)*hour_cost;
}
}
else // it is package C
{
monthly_cost = 19.95;
hour_cost = 0.00;
total_cost = monthly_cost;
}
// Finally, display
cout << "Your total bill for this month is: $" << total_cost << endl;
}