-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·125 lines (94 loc) · 3.16 KB
/
main.py
File metadata and controls
executable file
·125 lines (94 loc) · 3.16 KB
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
#!/usr/bin/env python
import time
try:
import conf
except ImportError:
import utilities
utilities.makeconf()
import stats
stats.dbcheck()
raise SystemExit
import selectvisual
if __name__ == '__main__':
import sys
import os
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-T", "--testmode", dest="testmode",
action="store_true", default=False,
help="run in test mode")
parser.add_option("-t", "--tournament", dest="tournament",
action="store_true", default=False,
help="run a tournament")
parser.add_option("-n", "--battles", dest="nbattles",
action="store", type='int', default=5,
help="number of battles in tournament")
parser.add_option("-g", "--no-graphics", dest="nographics",
action="store_true", default=False,
help="non graphics mode")
parser.add_option("-D", "--upgrade-db", dest="upgrade_db",
action="store_true", default=False,
help="upgrade database (WARNING! Deletes database!)")
(options, args) = parser.parse_args()
testmode = options.testmode
tournament = options.tournament
nbattles = options.nbattles
nographics = options.nographics
upgrade_db = options.upgrade_db
if nographics:
selectvisual.select_view_module('none')
else:
selectvisual.select_view_module('pygame')
view = selectvisual.get_view_module()
from game import Game
import world
from world import box2d
import stats
def dbcheck():
if not stats.dbcheck():
print 'Run TankAI with -D switch.'
print 'WARNING: This will delete your current database!'
import sys
sys.exit(0)
def runmain():
if upgrade_db:
stats.dbremove()
stats.initialize()
dbcheck()
global testmode
if testmode:
if not os.path.exists(conf.logdir):
print 'Log directory does not exist:', conf.logdir
print 'test mode disabled'
testmode = False
stats.dbopen()
if tournament:
import datetime
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print 'Beginning tournament with %s battles.' % nbattles
for battle in range(nbattles):
print 'Battle', battle+1
game = Game(testmode, dt)
game.run()
world.Tank.ntanks = 0
view.Tank.ntanks = 0
results = stats.tournament_results(dt)
print;print;print;
print 'Tournament Results'
print nbattles, 'battles between', len(results), 'tanks'
print
for line in results:
print line[1], ':', line[4], 'wins', ':', line[6], 'defeated', ':', line[7], 'dmg caused'
else:
game = Game(testmode)
game.run()
stats.dbclose()
if not testmode and os.path.exists(conf.logdir):
for f in os.listdir(conf.logdir):
fpath = os.path.join(conf.logdir, f)
os.remove(fpath)
if __name__ == '__main__':
try:
runmain()
except KeyboardInterrupt:
pass