Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #68 from bilts/performance
Browse files Browse the repository at this point in the history
Turn slow mode into fast mode
  • Loading branch information
Rob Speer committed Jan 20, 2012
2 parents e6f3456 + b51bf28 commit a99d12a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 56 deletions.
8 changes: 5 additions & 3 deletions basicAI.coffee
Expand Up @@ -82,13 +82,15 @@ class BasicAI
choiceSet = {}
for choice in choices
choiceSet[choice] = choice

nullable = null in choices

# Get the priority list.
priority = priorityfunc.bind(this)(state, my)
priority = priorityfunc.call(this, state, my)
# Now look up all the preferences in that list. The moment we encounter
# a valid choice, we can return it.
for preference in priority
if preference is null and null in choices
if preference is null and nullable
return null
if choiceSet[preference]?
return choiceSet[preference]
Expand All @@ -103,7 +105,7 @@ class BasicAI
if (choice is null) or (choice is no)
value = 0
else
value = valuefunc.bind(this)(state, choice, my)
value = valuefunc.call(this, state, choice, my)
if value > bestValue
bestValue = value
bestChoice = choice
Expand Down
27 changes: 0 additions & 27 deletions playWeb.coffee
Expand Up @@ -24,10 +24,6 @@ makeStrategy = (changes) ->
ai[key] = value
ai

# Setting `fast` to true will takesome shortcuts to play the game
# really quickly. These include
# producing no output, and not returning control to the browser between
# game steps.
playGame = (strategies, options, ret) ->
ais = (makeStrategy(item) for item in strategies)

Expand All @@ -38,30 +34,7 @@ playGame = (strategies, options, ret) ->

state = new State().setUpWithOptions(ais, options)
ret ?= options.log
if options.fast
options.log = () ->
playFast(state, options, ret)
else
window.setZeroTimeout -> playStep(state, options, ret)

playStep = (state, options, ret) ->
if state.gameIsOver()
ret(state)
else
try
state.doPlay()
if state.phase == 'buy' and (not state.extraturn) and options.grapher?
options.grapher.recordMoney(state.current.ai.name, state.current.turnsTaken, state.current.coins)
if state.phase == 'cleanup' and (not state.extraturn) and options.grapher?
options.grapher.recordVP(state.current.ai.name, state.current.turnsTaken, state.current.getVP(state))
window.setZeroTimeout -> playStep(state, options, ret)
catch err
errorHandler = options.errorHandler ? (alert ? console.log)
errorHandler(err.message)
window.donePlaying()
throw err

playFast = (state, options, ret) ->
until state.gameIsOver()
try
state.doPlay()
Expand Down
16 changes: 15 additions & 1 deletion web/multiLog.coffee
Expand Up @@ -21,6 +21,12 @@ class MultiLog
getCurrent: ->
@pages[@currentPage-1]

getCurrentPage: ->
@currentPage

getLastPage: ->
@pages.length

updateOutput: ->
if @pages[@currentPage-1]?
@outputElt.html(@pages[@currentPage-1])
Expand All @@ -39,10 +45,16 @@ class MultiLog
"next page"

updatePaginator: ->
# Avoid updating the DOM if the new pages are the same as the old
pages = @pagesShown()
oldPages = @pagesRendered
if oldPages? and oldPages.length == pages.length and pages[0] == oldPages[0]
return if @currentPage == @renderedPage

prev = "<li class='#{this.prevButtonClass()}'><a href='#'>&larr; Previous</a></li>"
next = "<li class='#{this.nextButtonClass()}'><a href='#'>Next &rarr;</a></li>"
items = [prev]
for pageNum in this.pagesShown()
for pageNum in pages
if pageNum == @currentPage
item = "<li class='active page'><a href='#'>#{pageNum}</a></li>"
else
Expand All @@ -51,6 +63,8 @@ class MultiLog
items.push(next)
@paginatorElt.html('<ul>' + items.join('') + '</ul>')
this.updateEvents()
@pagesRendered = pages
@renderedPage = @currentPage

updateEvents: ->
$('.page').click (event) =>
Expand Down
46 changes: 21 additions & 25 deletions web/play.html
Expand Up @@ -91,13 +91,6 @@ <h2>Player 2</h2>
<span>Include Colony and Platinum</span>
</label>
</li>
<li>
<label>
<input type="checkbox" name="optionsCheckboxes" value="fast"
id="fast" />
<span>Play fast (without much output)</span>
</label>
</li>
</ul>
<a id="playButton" href="#" class="btn success">
Start playing
Expand Down Expand Up @@ -203,22 +196,27 @@ <h2>Game logs</h2>
singleGameResult = (state) =>
result = state.getWinners().toString() + " wins!"
multiLog.addLineToEnd(result)
multiLog.updateOutput()
# Avoid refreshing the output if it's not visible
multiLog.updateOutput() if multiLog.getCurrentPage() == multiLog.getLastPage()
tracker.recordGame(state)
errorHandler = (error) => alert(error.message)
startTime = null
begunPlaying = ->
window.playing = true
$('#playButton').removeClass('success').addClass('danger')
$('#playButton').text('Stop playing')
startTime = new Date()

donePlaying = ->
donePlaying = (count) ->
window.playing = false
grapher.updateGraphs()
tracker.updateScoresOnPage()
$('#playButton').removeClass('danger').addClass('success')
$('#playButton').text('Start playing')
console?.log?("Played #{count} games in #{(new Date() - startTime) / 1000.0}s");
window.donePlaying = donePlaying

playOneGame = ->
Expand All @@ -231,7 +229,6 @@ <h2>Game logs</h2>
log: log
colonies: $('#colonies').is(':checked')
randomizeOrder: $('#randomize').is(':checked')
fast: $('#fast').is(':checked')
tracker: tracker
grapher: grapher
}
Expand All @@ -241,6 +238,7 @@ <h2>Game logs</h2>


playUntil = (condition, count) ->
startTime = new Date();
scripts = [leftEd.getValue(), rightEd.getValue()]
compiled = compileStrategies(scripts, errorHandler)
return if not compiled
Expand All @@ -249,32 +247,30 @@ <h2>Game logs</h2>
log: log
colonies: $('#colonies').is(':checked')
randomizeOrder: $('#randomize').is(':checked')
fast: $('#fast').is(':checked')
tracker: tracker
grapher: grapher
}
playCount = 0
begunPlaying()
timeControlStarted = new Date()
playLoop = (result) ->
singleGameResult(result)
if condition() or (playCount >= count) or (not window.playing)
donePlaying()
donePlaying(playCount)
else
if (not options.fast) then multiLog.addPageQuietly('')
nextIteration = ->
playCount++
playGame(compiled, options, playLoop)
if (not options.fast) and (playCount % 10 == 0)
grapher.updateGraphs()
if (not options.fast) or (playCount % 20 == 0)
tracker.updateScoresOnPage()
if (not options.fast) or (playCount % 10) == 0
window.setZeroTimeout(nextIteration)
multiLog.addPageQuietly('')
msWithControl = new Date() - timeControlStarted
playCount++
if msWithControl > 250
tracker.updateScoresOnPage()
grapher.updateGraphs()
window.setZeroTimeout ->
timeControlStarted = new Date()
playGame(compiled, options, playLoop)
else
# no time to stop and give control to the Web browser!
nextIteration()
playGame(compiled, options, playLoop)

if (not options.fast) then multiLog.addPageQuietly('')
multiLog.addPageQuietly('')
playGame(compiled, options, playLoop)

$('#playButton').click (event) ->
Expand Down

0 comments on commit a99d12a

Please sign in to comment.