Skip to content

Commit

Permalink
First commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan committed May 18, 2011
0 parents commit 6ff37e5
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tmp
10 changes: 10 additions & 0 deletions Buildfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ==========================================================================
# Project: TiBattles
# Copyright: @2011 My Company, Inc.
# ==========================================================================

# Add initial buildfile information here
config :all,
:required => ["sproutcore/core_foundation", "sproutcore/datastore"],
:theme => "sproutcore/empty_theme"

8 changes: 8 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
==========================================================================
Project: TiBattles
Copyright: @2011 Shawn Garbett, Jonathan Hicks
==========================================================================

This is a web app for determining the best probability for the game
Twilight Imperium.

10 changes: 10 additions & 0 deletions apps/ti_battles/resources/stylesheets/ti_battles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/* ==========================================================================
* Project: TiBattles
* Copyright: @2011 My Company, Inc.
* ==========================================================================
*/

.sc-view {
position: relative;
overflow: visible;
}
17 changes: 17 additions & 0 deletions apps/ti_battles/resources/templates/ti_battles.handlebars
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<h1>Welcome to Twilight Imperium Stats!</h1>
{{#view TiBattles.Die1View}}
<label>Probability Die #1
<input type="range" min="0.0" max="1.0" step="0.1" {{bindAttr value="probability"}}>
</label>
<label>Number of Rolls #1
<input type="range" min="0" max="10" step="1" {{bindAttr value="numRoll"}}>
</label>
{{/view}}
{{#view TiBattles.Die2View}}
<label>Probability Die #2
<input type="range" min="0.0" max="1.0" step="0.1" {{bindAttr value="probability"}}>
</label>
<label>Number of Rolls #2
<input type="range" min="0" max="10" step="1" {{bindAttr value="numRoll"}}>
</label>
{{/view}}
90 changes: 90 additions & 0 deletions apps/ti_battles/ti_battles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// ==========================================================================
// Project: TiBattles
// Copyright: @2011 My Company, Inc.
// ==========================================================================
/*globals TiBattles */

TiBattles = SC.Application.create();


TiBattles.Die = SC.Record.extend({
probability: SC.Record.attr(Number),
rollNum: SC.Record.attr(Number),

distribution: function (n, p) {
var n = this.get('rollNum'),
result = [];
for (var k = 0; k <= n; k++) {
result[k] = binomial(n, k);
}
return result;
}.property("probability", "rollNum"),

binomial: function (n, k) {
var p = this.get('probability');
return Math.pow(p, k) * Math.pow(1-p, n-k) * this._choose(n, k);
},

_choose: function (n, k) {
if (k == 0) { return 1; }
if (n == 0) { return 0; }

return this._choose(n-1, k-1) * n / k;
}

});

TiBattles.die1 = new TiBattles.Die({
probability: 0.8,
rollNum: 3
});

TiBattles.die2 = new TiBattles.Die({
probability: 0.2,
rollNum: 6
});

TiBattles.dieStats = SC.ArrayController.create({
convolute: function () {
var f = TiBattles.die1.distribution,
g = TiBattles.die2.distribution,
result = [],
s = f.length + g.length - 1,
i;
for (var n = 0; n < s; n++) {
result[n] = 0.0;
for (var m = 0; m <= n && m < f.length; m++) {
i = n - m;
if (i >= 0 && i < g.length) {
result[n] += f[m] * g[i];
}
}
}
return result;
},

content: function() {
var convolute = this.convolute(), d = [];
for (var i = 0; i < convolute.length; i++) { d.push([i, convolute[i]]); }
console.log(d);
return d;
}.property("die1", "die2")

});

TiBattles.Die1View = SC.TemplateView.extend({
probabilityBinding: 'TiBattles.die1.probability',
numRollBinding: 'TiBattles.die1.numRoll'
});

TiBattles.Die2View = SC.TemplateView.extend({
probabilityBinding: 'TiBattles.die2.probability',
numRollBinding: 'TiBattles.die2.numRoll'
});

SC.ready(function() {
TiBattles.mainPane = SC.TemplatePane.append({
layerId: 'ti_battles',
templateName: 'ti_battles'
});
});

0 comments on commit 6ff37e5

Please sign in to comment.