Commits

Anonymous committed db1f345

added first command in the controller + debug in the last command

Comments (0)

Files changed (4)

coffee/app.coffee

         @model.combination(mov)
         @renderColors()
 
+    initState: () =>
+        @model.init()
+        @renderColors()
+
     turn: (mov, callback) ->
         @movcallback = callback
         @ismoving = true
             @stop()
         @view.$container.querySelector('.last').addEventListener 'click', =>
             @last()
+        @view.$container.querySelector('.first').addEventListener 'click', =>
+            @first()
 
     end_callback: =>
         if @mode is 'play'
         window.setTimeout =>
             if @mode is 'stop'
                 @current = 0 if @current is -1
-                remaining_turnlist = @turnlist[@current..@turnlist.length-1]
+                remaining_turnlist = @turnlist[@current+1..@turnlist.length-1]
                 @view.straightturn(remaining_turnlist.join(''))
                 @current = @turnlist.length
+    first : =>
+        window.setTimeout =>
+            if @mode is 'stop'
+                @current = -1
+                @view.initState()
 
 document.addEventListener 'DOMContentLoaded', ->
 

coffee/cube.coffee

 window.Cube = class Cube
+    init: =>
+        @state = (face[0..] for face in @initstate) # deep copy
     constructor: ->
-        @state = [
+        @initstate = [
             ['1', '1', '1', '1', '1', '1', '1', '1', '1']
             ['2', '2', '2', '2', '2', '2', '2', '2', '2']
             ['3', '3', '3', '3', '3', '3', '3', '3', '3']
             ['5', '5', '5', '5', '5', '5', '5', '5', '5']
             ['6', '6', '6', '6', '6', '6', '6', '6', '6']
         ]
+        @init()
         @movs =
             'R' : [
                 [[0, 2], [3, 6], [5, 2], [1, 2]]
         @combination(scramble.split(' ').join(''))
         return scramble
 
-    toString : ->
+    toString : (state) ->
+        thestate = state
+        if thestate is undefined
+            thestate = @state
         "\n
     #{@state[0][0..2].join('')}\n
     #{@state[0][3..5].join('')}\n
     function CubeView($container) {
       this.transitionend = __bind(this.transitionend, this);
 
+      this.initState = __bind(this.initState, this);
+
       this.straightturn = __bind(this.straightturn, this);
 
       this.renderColors = __bind(this.renderColors, this);
       return this.renderColors();
     };
 
+    CubeView.prototype.initState = function() {
+      this.model.init();
+      return this.renderColors();
+    };
+
     CubeView.prototype.turn = function(mov, callback) {
       var $cube, movclass, _i, _len, _ref, _results;
       this.movcallback = callback;
     CubeController.prototype.turnlist = ["R2", "U", "R", "U", "R'", "U'", "R'", "U'", "R'", "U", "R'"];
 
     function CubeController(cubeview) {
+      this.first = __bind(this.first, this);
+
       this.last = __bind(this.last, this);
 
       this.stop = __bind(this.stop, this);
       this.view.$container.querySelector('.last').addEventListener('click', function() {
         return _this.last();
       });
+      this.view.$container.querySelector('.first').addEventListener('click', function() {
+        return _this.first();
+      });
     }
 
     CubeController.prototype.end_callback = function() {
           if (_this.current === -1) {
             _this.current = 0;
           }
-          remaining_turnlist = _this.turnlist.slice(_this.current, (_this.turnlist.length - 1) + 1 || 9e9);
+          remaining_turnlist = _this.turnlist.slice(_this.current + 1, (_this.turnlist.length - 1) + 1 || 9e9);
           _this.view.straightturn(remaining_turnlist.join(''));
           return _this.current = _this.turnlist.length;
         }
       });
     };
 
+    CubeController.prototype.first = function() {
+      var _this = this;
+      return window.setTimeout(function() {
+        if (_this.mode === 'stop') {
+          _this.current = -1;
+          return _this.view.initState();
+        }
+      });
+    };
+
     return CubeController;
 
   })();
 // Generated by CoffeeScript 1.3.3
 (function() {
-  var Cube;
+  var Cube,
+    __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
 
   window.Cube = Cube = (function() {
 
+    Cube.prototype.init = function() {
+      var face;
+      return this.state = (function() {
+        var _i, _len, _ref, _results;
+        _ref = this.initstate;
+        _results = [];
+        for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+          face = _ref[_i];
+          _results.push(face.slice(0));
+        }
+        return _results;
+      }).call(this);
+    };
+
     function Cube() {
-      this.state = [['1', '1', '1', '1', '1', '1', '1', '1', '1'], ['2', '2', '2', '2', '2', '2', '2', '2', '2'], ['3', '3', '3', '3', '3', '3', '3', '3', '3'], ['4', '4', '4', '4', '4', '4', '4', '4', '4'], ['5', '5', '5', '5', '5', '5', '5', '5', '5'], ['6', '6', '6', '6', '6', '6', '6', '6', '6']];
+      this.init = __bind(this.init, this);
+      this.initstate = [['1', '1', '1', '1', '1', '1', '1', '1', '1'], ['2', '2', '2', '2', '2', '2', '2', '2', '2'], ['3', '3', '3', '3', '3', '3', '3', '3', '3'], ['4', '4', '4', '4', '4', '4', '4', '4', '4'], ['5', '5', '5', '5', '5', '5', '5', '5', '5'], ['6', '6', '6', '6', '6', '6', '6', '6', '6']];
+      this.init();
       this.movs = {
         'R': [[[0, 2], [3, 6], [5, 2], [1, 2]], [[0, 5], [3, 3], [5, 5], [1, 5]], [[0, 8], [3, 0], [5, 8], [1, 8]], [[2, 0], [2, 2], [2, 8], [2, 6]], [[2, 1], [2, 5], [2, 7], [2, 3]]],
         'U': [[[0, 0], [0, 2], [0, 8], [0, 6]], [[0, 1], [0, 5], [0, 7], [0, 3]], [[1, 0], [4, 0], [3, 0], [2, 0]], [[1, 1], [4, 1], [3, 1], [2, 1]], [[1, 2], [4, 2], [3, 2], [2, 2]]],
       return scramble;
     };
 
-    Cube.prototype.toString = function() {
+    Cube.prototype.toString = function(state) {
+      var thestate;
+      thestate = state;
+      if (thestate === void 0) {
+        thestate = this.state;
+      }
       return "\n    " + (this.state[0].slice(0, 3).join('')) + "\n    " + (this.state[0].slice(3, 6).join('')) + "\n    " + (this.state[0].slice(6, 9).join('')) + "\n\n" + (this.state[4].slice(0, 3).join('')) + " " + (this.state[1].slice(0, 3).join('')) + " " + (this.state[2].slice(0, 3).join('')) + " " + (this.state[3].slice(0, 3).join('')) + "\n" + (this.state[4].slice(3, 6).join('')) + " " + (this.state[1].slice(3, 6).join('')) + " " + (this.state[2].slice(3, 6).join('')) + " " + (this.state[3].slice(3, 6).join('')) + "\n" + (this.state[4].slice(6, 9).join('')) + " " + (this.state[1].slice(6, 9).join('')) + " " + (this.state[2].slice(6, 9).join('')) + " " + (this.state[3].slice(6, 9).join('')) + "\n\n    " + (this.state[5].slice(0, 3).join('')) + "\n    " + (this.state[5].slice(3, 6).join('')) + "\n    " + (this.state[5].slice(6, 9).join('')) + "\n";
     };