-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
90 lines (71 loc) · 3.03 KB
/
main.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
VERSION = "0.2"
import argparse
from time import time
import distributors
import strategies
from game.constants import DEFAULT_SHIPS, BOARD_SIZE
from game.Game import Game
from game.Statistics import Statistics
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Python battleship simulator v%s" % VERSION)
parser.add_argument('count', metavar="N", type=int, nargs="?", default=1000,
help="count of games to simulate")
parser.add_argument('ships', metavar="S", type=int, nargs="*", default=DEFAULT_SHIPS,
help='list of ship lenghts')
parser.add_argument('--width', metavar="WIDTH", type=int, default=BOARD_SIZE,
help="board width")
parser.add_argument('--height', metavar="HEIGHT", type=int, default=BOARD_SIZE,
help="board height")
parser.add_argument('-d', '--distributor', choices=distributors.all, default="RandomDistributor",
help='Strategy to set ships on the board')
parser.add_argument('-s', '--strategy', choices=strategies.all, default="RandomStrategy",
help='Strategy to shoot ships')
parser.add_argument('--print-game', default=False, action='store_true',
help='Play one Game and print the board')
parser.add_argument('--animate-game', default=False, action='store_true',
help='Play one Game and animate it')
parser.add_argument('--steps', default=False, action='store_true',
help='Step through moves when animating game')
parser.add_argument('--plot-results', default=False, action='store_true',
help='Plot the results with numpy')
parser.add_argument('--no-multi-process', default=False, action='store_true',
help='Disable multiprocessing on all cores')
parser.add_argument('--version', action='version', version='%%(prog)s v%s' % VERSION)
args = parser.parse_args()
dist = __import__("distributors.%s" % args.distributor, fromlist=[args.distributor])
dist = getattr(dist, args.distributor)
strategy = __import__("strategies.%s" % args.strategy, fromlist=[args.strategy])
strategy = getattr(strategy, args.strategy)
g = Game(dist, strategy, args.width, args.height, args.ships)
if args.print_game:
g.prepare()
g.play()
print g
elif args.animate_game:
g.prepare()
try:
g.play(True, args.steps)
except KeyboardInterrupt:
pass
else:
s = Statistics(g)
t = time()
if not args.no_multi_process:
s.spawn(args.count)
else:
s.run(args.count)
print s
if args.plot_results:
try:
from numpy import zeros
import matplotlib.pyplot as plt
data = zeros(101)
for x in s.results: data[x[1]] += 1
plt.plot(data)
plt.show()
except:
print "couldn't plot. Modules numpy and matplotlib not found"
print
print "Testing took %.2f seconds" % (time() - t)