Skip to content

Commit

Permalink
wiring up stub html
Browse files Browse the repository at this point in the history
  • Loading branch information
mattsly committed Apr 30, 2012
1 parent 737a868 commit 2bfe7ef
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 25 deletions.
23 changes: 22 additions & 1 deletion Halfcourt.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,33 @@
<meta charset="utf-8">
<title>Hoops!</title>


<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="HalfcourtMinimax.js"></script>

<script type="text/javascript">
$(document).ready(function(){
$("#js-decide").click(function(){
$("#js-result").text(decide(
{ kGoodGuy : parseInt($("#js-good-guy-score").val()) ,
kBadGuy : parseInt($("#js-bad-guy-score").val()) } ))
})
})

</script>

</head>
<body>

<h1>Hi</h1>
<label>Your Score</label><input id="js-good-guy-score" type="text" value="0"></input>
<label>Their Score</label><input id="js-bad-guy-score" type="text" value="0"></input>


<button id="js-decide">What should I do?</button>

<div id="js-result">

</div>

</body>
</html>
65 changes: 41 additions & 24 deletions HalfcourtMinimax.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@

var gMaxDepth = 10
var gPlayingUpTo = 6
var gWin = gPlayingUpTo * 10
var gMaxDepth = 15
var gPlayingUpTo = 21

var gWin = 100
var gLose = -1 * gWin
var gGoodGuyThreePct = (1/3)
var gGoodGuyTwoPct = (1/2)
Expand All @@ -11,40 +12,56 @@ var gBadGuyTwoPct = (1/2)
var gGoodGuy = "GoodGuy"
var gBadGuy = "BadGuy"

/*
var kGoodGuy = "a"
var kBadGuy = "b"
var kPossession = "c"
var kEventType = "d"
*/

//the node types - either it's a decision, or it's a random event
var gDecision = "Decision"
var gThreePointShot = "ThreePointShot"
var gTwoPointShot = "TwoPointShot"

function decide (options) {

var root = { kGoodGuy : 0 , kBadGuy : 0 , kPossession: gGoodGuy , kEventType: gDecision}

//override default root w/ any options
for ( var key in options )
root[ key ] = options[ key ]

var shootTheThree = clone(root)
shootTheThree.kEventType = gThreePointShot
var shootTheThreeScore = expectimax(shootTheThree, 0)

var shootTheTwo = clone(root)
shootTheTwo.kEventType = gTwoPointShot
var shootTheTwoScore = expectimax(shootTheTwo, 0)

console.log("Three: " + shootTheThreeScore + " Two: " + shootTheTwoScore)
if (shootTheThreeScore > shootTheTwoScore)
return "Shoot the Three!"
else if (shootTheThreeScore < shootTheTwoScore)
return "Shoot the Two!"
else
return "Total Wash - Do Whatever"
end

}


function expectimax (node, depth) {

if (node == null) {
//initialize
var root = { kGoodGuy : 0 , kBadGuy : 0 , kPossession: gGoodGuy , kEventType: gDecision}
return expectimax(root, 0)
}
else if (node.kGoodGuy >= gPlayingUpTo) {
if (node.kGoodGuy >= gPlayingUpTo) {
return gWin
}
else if (node.kBadGuy >= gPlayingUpTo) {
return gLose
}
else if (depth >= gMaxDepth) {
return (node.kGoodGuy - node.kBadGuy) //simple point difference heuristic
return (gWin * (node.kGoodGuy/gPlayingUpTo) + gLose * (node.kBadGuy/gPlayingUpTo))
//(node.kGoodGuy - node.kBadGuy) //simple point difference heuristic
}
else if ( node.kEventType == gDecision ) {
var choices = [gTwoPointShot, gThreePointShot]
var best = node.kPossession == gGoodGuy ? gLose : gWin
/* best outcome depends on who has the ball
if we have it, best is anything better than a loss
if we have it, best is anything better than us losing
if they have it, best is anything lower than them winning
*/
for (var i = 0; i < choices.length; i++) {
Expand All @@ -67,10 +84,10 @@ function expectimax (node, depth) {

var miss = clone(node)
miss.kEventType = gDecision
miss.kPossession = node.kPossession == gGoodGuy ? gBadGuy : gGoodGuy //swap possession on a miss
miss.kPossession = node.kPossession == gGoodGuy ? gBadGuy : gGoodGuy //swap possession on a miss (no offensive rebounds!)

//TODO - vary percentage by player
return ( gGoodGuyThreePct * expectimax(make, depth + 1) + (1 - gGoodGuyThreePct) * expectimax(miss, depth + 1) )
var shootingPct = node.kPossession == gGoodGuy ? gGoodGuyThreePct : gBadGuyThreePct
return ( shootingPct * expectimax(make, depth + 1) + (1 - shootingPct) * expectimax(miss, depth + 1) )
}
else if (node.kEventType == gTwoPointShot) {
var make = clone(node)
Expand All @@ -79,10 +96,10 @@ function expectimax (node, depth) {

var miss = clone(node)
miss.kEventType = gDecision
miss.kPossession = node.kPossession == gGoodGuy ? gBadGuy : gGoodGuy //swap possession on a miss
miss.kPossession = node.kPossession == gGoodGuy ? gBadGuy : gGoodGuy //swap possession on a miss (no offensive rebounds!)

//TODO - vary percentage by player
return ( gGoodGuyTwoPct * expectimax(make, depth + 1) + (1 - gGoodGuyTwoPct) * expectimax(miss, depth + 1) )
var shootingPct = node.kPossession == gGoodGuy ? gGoodGuyTwoPct : gBadGuyTwoPct
return ( shootingPct * expectimax(make, depth + 1) + (1 - shootingPct) * expectimax(miss, depth + 1) )
}
else
alert("shouldn't be here")
Expand Down

0 comments on commit 2bfe7ef

Please sign in to comment.