The code:
# N S E W U D
room0 = [0,0,0,0,0,0,0]
room1 = [0,0,0,0,0,0,0] #Pad the first place so that rooms may
room2 = [0,0,0,0,0,0,0] #be referenced naturally
room3 = [0,0,0,0,0,0,0]
room4 = [0,0,0,0,0,0,0] #First places will have special purposes
room5 = [0,0,0,0,0,0,0]
room6 = [0,0,0,0,0,0,0] #initialize the array with zeros
room7 = [0,0,0,0,0,0,0]
room = [room0,room1,room2,room3,room4,room5,room6,room7]
def clearscreen(numlines=100):
"""Clear the console.
numlines is an optional argument used only as a fall-back.
"""
import os
if os.name == "posix":
# Unix/Linux/MacOS/BSD/etc
os.system('clear')
elif os.name in ("nt", "dos", "ce"):
# DOS/Windows
os.system('CLS')
else:
# Fallback for other operating systems.
print('\n' * numlines)
print(os.name)
def print_map():
print("+-------------+")
print('|','MAP: ','N','S','E','W','U','D','|', sep="", end="")
for x in range(1,8):
print()
print("|","room",x,"> ", sep="", end="")
for y in range(1,7):
print(room[x][y], end="")
print("|", end="")
print()
print("+-------------+", end="")
def read_maze_data_file():
roomx = [0,0,0,0,0,0,0]
n, m = 0, 0
try:
filename = 'mazegame.dat'
textf = open(filename, 'r')
except IOError:
print ('Cannot open file %s for reading' % filename)
import sys
sys.exit(0)
# reads one line at a time
for line in textf:
print("raw line in file: ",line, end="")
tempwords = line.split(None)
print ("line as read from file: ",tempwords)
for n in range(0, len(room)-1):
roomx[n] = tempwords[n]
#print(roomx[n])
print("roomx",roomx)
room[m] = roomx
print("room ",m,room[m])
print("current state of room array")
print("room 0",room[0])
print("room 1",room[1])
print("room 2",room[2])
print("room 3",room[3])
print("room 4",room[4])
print("room 5",room[5])
print("room 6",room[6])
print("room 7",room[7])
m += 1
textf.close()
return(room)
#----END read_maze_data_file()
#---------MAIN SECTION---------
clearscreen()
print("LOAD AN ARRARY FROM A FILE")
print(" by Bill Allen")
print()
print("initial state of room array")
print(room)
print()
print("data from file")
room_final = read_maze_data_file()
print("from MAIN")
print(room_final)
print()
print("a PRINT_MAP call from MAIN")
print_map()
print()
print()
=======================
partial sample output showing the current incorrect results:
a PRINT_MAP call from MAIN
+-------------+
|MAP: NSEWUD|
|room1> 000050|
|room2> 000050|
|room3> 000050|
|room4> 000050|
|room5> 000050|
|room6> 000050|
|room7> 000050|
+-------------+
=========================
the contents of the data file, mazegame.dat
0 0 0 0 0 0 0
0 0 4 2 0 0 0
0 0 0 1 3 6 0
0 0 5 0 2 0 0
0 1 0 5 0 0 0
0 3 0 0 4 0 7
0 0 0 0 0 0 2
0 0 0 0 0 5 0
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor