forked from rspeer/dominiate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
playWeb.coffee
54 lines (46 loc) · 1.35 KB
/
playWeb.coffee
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
# This is the main entry point for playing strategies against each
# other on the Web.
#
# Needs more documentation.
compileStrategies = (scripts, errorCallbacks) ->
strategies = []
usedNames = []
for i in [0...scripts.length]
try
strategy = CoffeeScript.eval(scripts[i], {bare: yes})
while strategy.name in usedNames
strategy.name += "Clone"
usedNames.push(strategy.name)
strategies.push(strategy)
catch e
errorCallbacks[i](e)
return null
return strategies
makeStrategy = (changes) ->
ai = new BasicAI()
for key, value of changes
ai[key] = value
ai
playGame = (strategies, options, ret) ->
ais = (makeStrategy(item) for item in strategies)
# Take note of the player names, in order, while they're
# still in this order.
window.tracker.setPlayers(ai.name for ai in ais)
# Handle options from the checkboxes on the page.
if options.colonies
tableau = tableaux.all
else
tableau = tableaux.noColony
if options.randomizeOrder
shuffle(ais)
state = new State().initialize(ais, tableau, options.log)
ret ?= options.log
window.setZeroTimeout -> playStep(state, ret)
playStep = (state, ret) ->
if state.gameIsOver()
ret(state)
else
state.doPlay()
window.setZeroTimeout -> playStep(state, ret)
this.compileStrategies = compileStrategies
this.playGame = playGame