-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment_1_code_Calum_Lynch.py
172 lines (148 loc) · 4.44 KB
/
assignment_1_code_Calum_Lynch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# -*- coding: utf-8 -*-
"""
Created on Mon Oct 14 12:55:08 2019
@author: Calum Lynch
"""
import sys
import copy as c
import numpy as np
#Task 1
a,b = input("Please enter two CSVs for the start position: ").split(",")
a = int(a);b = int(b)
start = [a,b]
a,b = input("Please enter two CSVs for the final position: ").split(",")
a = int(a);b = int(b)
end = [a,b]
#Task 2
file_to_open = input("Input the map matrix file to open: ") #opening file
roads = open(file_to_open,"r")
print()
M = [] #setting up matrice
M = roads.readlines()
for x in range(len(M)): #cleaning up matrice
y = M[x].strip()
M[x] = y
for x in range(len(M)):
y = M[x].split()
M[x] = y
city_map = np.array(M, dtype = "int32") #switching to a np array and assigninng to var name city_map
#Task 3
def check_bounds(coord_pos):
"""Checks if a initial starting value is within bounds"""
if file_len(roads)-1 < coord_pos or 0 > coord_pos:
sys.exit("Error: Initial values outwith city_map!")
return
def file_len(d):
"""returns number of lines in the file"""
d.seek(0)
l = len(d.readlines())
return l
check_bounds(start[0]); check_bounds(start[1]); check_bounds(end[0]); check_bounds(end[1])
#Task 4
def is_valid(fpath, fcity_map, fc_pos, n_pos):
"""decides if next position is valid"""
if fcity_map[fc_pos[0]][fc_pos[1]] > fcity_map[n_pos[0]][n_pos[1]] and n_pos not in fpath:
return 1
return 0
#Task 5
def find_path():
"""calls sets up and calls functions to find path"""
global c_pos
global c_path
global path
global output
c_pos = c.copy(start)
c_path = []
path = []
output = None
procedure()
return
def procedure():
"""iterates the algorithem and runs checks"""
global c_pos
global path
global c_path
global output
path.append(c.copy(c_pos))
c_path.append(c.copy(c_pos))
if c_pos == end:
output = True
if output != None:
return
move()
procedure()
def move():
"""deciding which direction to move"""
global c_pos
if is_valid(path, city_map, c_pos, north(c_pos)) == 1:
c_pos = north(c_pos)
return
elif is_valid(path, city_map, c_pos, east(c_pos)) == 1:
c_pos = east(c_pos)
return
elif is_valid(path, city_map, c_pos, south(c_pos)) == 1:
c_pos = south(c_pos)
return
elif is_valid(path, city_map, c_pos, west(c_pos)) == 1:
c_pos = west(c_pos)
return
c_pos = backtrack()
return
"""These functions return the position moved by one space"""
def north(position):
fpos = c.copy(position)
fpos[0] = fpos[0]-1
fpos = perodic_boundary(fpos)
return fpos
def east(position):
fpos = c.copy(position)
fpos[1] = fpos[1]+1
fpos = perodic_boundary(fpos)
return fpos
def south(position):
fpos = c.copy(position)
fpos[0] = position[0]+1
fpos = perodic_boundary(fpos)
return fpos
def west(position):
fpos = c.copy(position)
fpos[1] = fpos[1]-1
fpos = perodic_boundary(fpos)
return fpos
def perodic_boundary(fx):
"""This function checks if the inputed coorinates has passed the edge of the map and need to loop"""
if fx[0] > file_len(roads) - 1:
fx[0] = fx[0] - file_len(roads)
if fx[1] > file_len(roads) - 1:
fx[1] = fx[1] - file_len(roads)
if fx[0] < 0:
fx[0] = fx[0] + file_len(roads)
if fx[1] < 0:
fx[1] = fx[1] + file_len(roads)
return fx
def backtrack():
"""backs up the current position and removes the move from the current path"""
global c_path
global output
c_path.pop()
if c_path == []:
output = False
return
pos = c_path[-1]
c_path.pop()
return pos
find_path()
#Task 6
if output == False:
print("No sutiable paths exist")
if output == True: #checks the output from task 5
print("Path found with {} steps.".format(len(c_path)))
#Task 7
for x1 in range(file_len(roads)): #define the number of rows going to be printed
print()
for y1 in range(file_len(roads)):#defines number of columns (same as rows as city_map is a square matrix)
if [x1,y1] in c_path:
print("{:^3}".format(c_path.index([x1,y1])+1), end="")
else:
print("__ ",end="")
roads.close()