Permalink
Browse files

Initial working version. Requires modern browsers.

  • Loading branch information...
1 parent 27e6d37 commit 7eef53706a879533a684857d5ad8cf81d35408ca @orderedlist committed Aug 11, 2011
View
@@ -0,0 +1 @@
+A JavaScript implementation of the Greed dice game.
View
@@ -0,0 +1,43 @@
+<!doctype html>
+<html>
+ <head>
+ <title>Kata</title>
+ <script src="javascripts/lib/jquery.js"></script>
+ <script src="javascripts/lib/underscore.js"></script>
+ <script src="javascripts/lib/ich.js"></script>
+ <script src="javascripts/lib/lucid.events.js"></script>
+ <script src="javascripts/models/die.model.js"></script>
+ <script src="javascripts/models/roll.model.js"></script>
+ <script src="javascripts/presenters/roll.presenter.js"></script>
+ <script src="javascripts/ui/roll.ui.js"></script>
+ <script src="javascripts/ui/app.js"></script>
+ <link rel="stylesheet" href="stylesheets/dice.css" />
+ </head>
+ <body>
+ <div id="dice">
+ <div id="d1" class="space" data-roll="1"><div class="die" data-number="1">1</div></div>
+ <div id="d2" class="space" data-roll="2"><div class="die" data-number="2">2</div></div>
+ <div id="d3" class="space" data-roll="3"><div class="die" data-number="3">3</div></div>
+ <div id="d4" class="space" data-roll="4"><div class="die" data-number="4">4</div></div>
+ <div id="d5" class="space" data-roll="5"><div class="die" data-number="5">5</div></div>
+ <div class="meta">Select how many dice to roll</div>
+ </div>
+
+ <div id="results">
+ <div class="empty">Your Rolls will Display Here</div>
+ </div>
+
+ <script type="text/html" id="roll_template">
+ <div class="roll">
+ {{#dice}}
+ <div class="space">
+ {{#die}}
+ <div class="die" data-number="{{number}}">{{number}}</div>
+ {{/die}}
+ </div>
+ {{/dice}}
+ <div class="meta"><strong>{{score}}</strong> points</div>
+ </div>
+ </script>
+ </body>
+</html>
Oops, something went wrong.
Oops, something went wrong.
@@ -0,0 +1,44 @@
+var Lucid;
+Lucid = Lucid || {};
+
+Lucid.Events = {
+ bind: function(event, callback) {
+ this._events_ = this._events_ || {}
+ this._events_[event] = this._events_[event] || [];
+ this._events_[event].push(callback);
+ return this;
+ },
+
+ trigger: function(name, data) {
+ this._events_ = this._events_ || {}
+ var events = this._events_[name] || [];
+
+ if(this.constructor._events_) {
+ events = events.concat(this.constructor._events_[name] || []);
+ }
+
+ for (var i = 0; i < events.length; i++) {
+ events[i].apply(this, data || []);
+ }
+
+ return this;
+ },
+
+ unbind: function(event, callback) {
+ this._events_ = this._events_ || {}
+
+ if (callback) {
+ var events = this._events_[event] || [];
+
+ for (var i = 0; i < events.length; i++) {
+ if (events[i] === callback) {
+ this._events_[event].splice(i, 1);
+ }
+ }
+ } else {
+ delete this._events_[event];
+ }
+
+ return this;
+ }
+};
Oops, something went wrong.
@@ -0,0 +1,9 @@
+var Die = function(num) {
+ this.roll(num);
+};
+
+_.extend(Die.prototype, {
+ roll: function(num) {
+ this.number = num || Math.ceil(Math.random() * 6);
+ }
+});
@@ -0,0 +1,56 @@
+var Roll = function(num, forced) {
+ this.dice = [];
+ this.init(num, forced || new Array(num));
+}
+
+_.extend(Roll, Lucid.Events);
+
+_.extend(Roll.prototype, Lucid.Events, {
+ score: 0,
+
+ init: function(num, forced) {
+ for (var i = 0; i < num; i++) {
+ this.dice.push(new Die(forced[i]));
+ }
+
+ this.process();
+ },
+
+ process: function() {
+ var numbers = _.pluck(this.dice, 'number'),
+ counts = _.groupBy(numbers, function(n) {return n}),
+ self = this;
+
+ for (num in counts) {
+ this.count(num, counts[num].length);
+ }
+
+ this.trigger('rolled');
+ },
+
+ count: function(number, qty) {
+ number = parseInt(number, 10);
+ while (qty > 0) {
+ console.log(qty, number);
+ if (qty > 2) {
+ switch(number) {
+ case 1:
+ this.score += 1000;
+ break;
+ default:
+ this.score += 100 * number;
+ }
+ qty -= 3;
+ } else {
+ switch(number) {
+ case 1:
+ this.score += 100;
+ break;
+ case 5:
+ this.score += 50;
+ }
+ qty--;
+ }
+ }
+ }
+});
@@ -0,0 +1,12 @@
+var RollPresenter = function(roll) {
+ var data = {
+ dice: [],
+ score: roll.score
+ };
+
+ for (var i = 0; i < 5; i++) {
+ data.dice.push({die: (roll.dice[i] || null)});
+ }
+
+ return data;
+}
View
@@ -0,0 +1,16 @@
+$(function() {
+ $('[data-roll]').mouseenter(function() {
+ $('[data-roll]').removeClass('active');
+ var q = parseInt($(this).attr('data-roll'), 10);
+ for (q; q > 0; q--) {
+ $('#d' + q).addClass('active');
+ }
+ }).click(function() {
+ var q = parseInt($(this).attr('data-roll'), 10);
+ new Roll(q);
+ });
+
+ $('#dice').mouseleave(function() {
+ $('[data-roll]').removeClass('active');
+ });
+});
@@ -0,0 +1,3 @@
+Roll.bind('rolled', function() {
+ $('#results').prepend(ich.roll_template(RollPresenter(this))).find('div.empty').remove();
+});
Oops, something went wrong.

0 comments on commit 7eef537

Please sign in to comment.