-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameJamDemo.py
executable file
·136 lines (100 loc) · 3.69 KB
/
gameJamDemo.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
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
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/python
import random
import copy
from runGame import Game
from output import *
from economy import Economy
from gamemap import GameMap
import os
from randomUnitEvolution import RandomUnitEvolution
import MapBuilder.unit_evolver
def updateArmyWithCurrentMap(fodderArmy, newMap):
for unit in fodderArmy:
unit.setMap(newMap)
return fodderArmy
def runGameMap(economy, map, army, output):
# Create an instance of our army, based on the original army, which we will use in a given game
fodderArmy = copy.deepcopy(army)
# Create a (changeable) map, based on the original map
changeableMap = copy.deepcopy(map)
updateArmyWithCurrentMap(fodderArmy, changeableMap);
# We create a new game, with the changeable map
g = Game(economy, changeableMap, fodderArmy, output, 0.0);
score = g.run()
output.saveToFile("GameTimeline.json")
return score
def doMapGenerationComparison():
# Init economy
economy = Economy(500);
# Init messaging
output = ConsoleOutput();
# Set colors
sAttackerColor = "white";
gameMap = GameMap(economy, 10, 10, 0.1, 0.1, 0.05, 0.05);
# Create an army
army = Game.selectArmy(economy, gameMap, "white", output)
# For this army, create 3 maps (1 easy, 1 average, 1 difficult)
for pParams in [(0.01, 0.15, 0.01, 0.01, "easy"), (0.05, 0.15, 0.11, 0.08, "medium"),
(0.03, 0.01, 0.12, 0.08, "difficult")]:
gameMap = GameMap(economy, 10, 10, pParams[0], pParams[1], pParams[2], pParams[3]);
# Run the easy 10 times
allScores = []
for iCnt in range(10):
score = runGameMap(economy, gameMap, army, output)
allScores += [score]
os.rename("GameTimeline.json", "GameTimeline%s.json"%(pParams[4]))
print str(pParams) + ":" + str(allScores)
# Record the avg performance and stdev
# Compare the two
# Show them
def doAgentTrainingComparison():
# Init economy
economy = Economy(500);
# Init messaging
output = ConsoleOutput();
# Set colors
sAttackerColor = "white";
# A medium map
gameMap = GameMap(economy, 10, 10, 0.05, 0.15, 0.11, 0.08);
# Create an army
# Select an army
army = Game.selectArmy(economy, gameMap, "white", output)
# Run game on map 10 times
allScores = []
for iCnt in range(10):
score = runGameMap(economy, gameMap, army, output)
allScores += [score]
# Record avg performance
os.rename("GameTimeline.json", "GameTimeline%s.json" % ("Untrained"))
print str("Scores for Untrained") + ":" + str(allScores)
# Evolve army RANDOM
myevol = RandomUnitEvolution()
myevol.evolveEachUnit(economy, gameMap, army)
# Reset scores
allScores = []
# Run game on map 10 times
# Record avg performance
for iCnt in range(10):
score = runGameMap(economy, gameMap, army, output)
allScores += [score]
os.rename("GameTimeline.json", "GameTimeline%s.json" % ("TrainedRandom"))
print str("Scores for TrainedRandom") + ":" + str(allScores)
# Reset scores
allScores = []
# Evolve army GENETIC
MapBuilder.unit_evolver.getArmy(army, gameMap, economy)
# Run game on map 10 times
# Record avg performance
for iCnt in range(10):
score = runGameMap(economy, gameMap, army, output)
allScores += [score]
# Record avg performance
os.rename("GameTimeline.json", "GameTimeline%s.json" % ("TrainedGenetic"))
print str("Scores for TrainedGenetic") + ":" + str(allScores)
def main():
### Demonstrate map generation
# doMapGenerationComparison()
### Demonstrate agent training
doAgentTrainingComparison()
if __name__ == '__main__':
main()