-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_handling.py
70 lines (52 loc) · 1.69 KB
/
file_handling.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
from tkinter import filedialog
import tkinter as tk
from settings import ROWS, COLS
from node import Node
def open_map():
start = None
end = None
# hidden Tkinter window
root = tk.Tk()
root.withdraw()
#get file name
fileName = filedialog.askopenfilename()
#open file
f = open(fileName)
#get data and convert it to matrix
map_data= []
map_data = [ line.split() for line in f]
#Make an N x N matrix with each index implemented as a Node
grid = []
# 0 -> 41
for i in range(ROWS):
grid.append([])
for j in range (COLS):
grid[i].append(Node(i,j))
if map_data[i][j] == '1':
grid[i][j].place_wall()
elif map_data[i][j] == '2':
grid[i][j].place_start()
start = grid[i][j]
elif map_data[i][j] == '3':
grid[i][j].place_end()
end = grid[i][j]
f.close()
return grid, start, end
def save_map(grid):
# hidden Tkinter window
root = tk.Tk()
root.withdraw()
#open save file dialog
f = filedialog.asksaveasfile(mode='w', defaultextension=".txt")
for i in range(ROWS):
for j in range (COLS):
if grid[i][j].is_wall():
f.write('1 ')
elif grid[i][j].is_start():
f.write('2 ')
elif grid[i][j].is_end():
f.write('3 ')
else:
f.write('0 ')
f.write('\n')
f.close()