-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFile.py
76 lines (63 loc) · 1.48 KB
/
File.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
import os, sys
debug = False
logfile = True
stdout = True
# open log file
try:
log = open("gameResult.txt", 'w')
except PermissionError: "cannot write file"
class File:
def open_file(self):
# parse optional parameter
if len(sys.argv) >= 2:
if os.path.isfile(sys.argv[-1]):
filename = sys.argv[-1]
elif os.path.isfile("testCase.txt"):
filename = "testCase.txt"
else:
File.error("Usage " + sys.argv[0] + " [ply] [file]")
exit(1)
# default filename
elif os.path.isfile("testCase.txt"):
filename = "testCase.txt"
else:
self.error("unable to find 'testCase.txt'")
exit(1)
return filename
def test_file(board, game, player_x, player_y):
io = File()
filename = io.open_file()
# open file
try:
with open(filename, 'r', 1) as f:
num = 1
for line in f:
line = line.rstrip()
line = line.split(', ')
for i in range(len(line)):
game.parse_entry(line[i], game, board, player_x, player_y, num)
board.display()
num += 1
except ValueError: "cannot read file"
f.close()
def print(text):
if stdout:
print(text)
# write to file
if logfile:
log.write(text + '\n')
log.flush()
def prompt(text):
if stdout:
print("\033[1m" + "> " + text + "\033[0m")
# write to file
if logfile:
log.write("> " + text + '\n')
log.flush()
def debug(text):
if debug:
print("--> " + str(text))
def error(text):
print("\033[1;31m==> ERROR:\033[0m \033[1m" + text + "\033[0m")
def close():
log.close()