2016-04-25 44 views
1

我一直在寻找一些使用Chrome魔方浏览器http://iamthecu.be/的人。基本的源代码在Rubik官方网站https://rubiks.com/chrome-cube-lab上找到。我想为这个平台编写一个自动求解器,但是我不确定如何获得我编写的代码来运行。使用Chrome魔方浏览器

这里是我在修改任何东西之前放置我的代码的文件。强调的要点是块注释和ERNO.Solver = function() {...部分:

/* 


    SOLVERS 

    Our Cube has its own animation loop conveniently called Cube.loop(). 
    If Cube.isSolving === true then within that loop Cube will call 
    window.solver.consider(cube). This means when you create your own 
    Solver instance you have to set window.solver equal to your instance. 

    Solver.consider() will do some very basic checking and if all's well 
    will pass the Cube instance to Solver.logic() which is the function that 
    you need to write yourself. 

    Your logic() should return false is the cube is solved or if something's 
    gone horribly wrong. This will set Cube.isSolving = false and stop the 
    solver from being called within the Cube's animation loop. 

    Your logic() should return true if an incremental improvement has been 
    made and the logic() should be run again in the next loop; For example, 
    run again after a twist queue completes. 

    -- 

    @author Mark Lundin - http://www.mark-lundin.com 
    @author Stewart Smith 


*/ 








ERNO.Solver = function(){ 


    // When you create your own Solver this is the only function you need to build yourself. 
    // Having said that, it will probably be the most intense function like ... ever! 
    // Check out my example in /scripts/solvers/stewart.js to see how you might go about it. 

    this.logic = function(cube){ return false };; 
} 




// This is the method called within Cube.loop() when Cube.isSolving === true. 
// It will call Solver.logic() which is the function you need to fill in. 

ERNO.Solver.prototype.consider = function(cube){ 


    // Was our solver passed a valid Cube? 
    // Kind of important, eh? 

    if(cube === undefined){ 

     console.warn('A cube [Cube] argument must be specified for Solver.consider().'); 
     return false; 
    } 
    else if(cube instanceof ERNO.Cube === false){ 

     console.warn('The cube argument provided is not a valid Cube.'); 
     return false; 
    } 


    // If we're solving we should really make certain we aren't shuffling! 
    // Otherwise our logic will never actually run. 
    // The hook for this is in Cube.loop() so look there to see what's up. 

    cube.isShuffling = false; 


    // If the cube is already solved then our job is done before it started. 
    // If not, we need to try solving it using our current solve method. 

    if(cube.isSolved()){ 

     ERNO.Solver.prototype.explain('I’ve found that the cube is already solved.'); 
     return false; 
    } 
    else return this.logic(cube); 
}; 




// We should always hit at what the Solver wants to do next 
// so we can hault auto-solving and give the user a chance to 
// figure out the next move for his/herself. 

ERNO.Solver.prototype.hint = function(text){ 

    console.log(

     '%c'+ text +'%c\n', 
     'background-color: #EEE; color: #333', '' 
    ); 
}; 


// If hinting is text displayed *before* a move is made 
// then explaining is text displayed *after* a move is made. 

ERNO.Solver.prototype.explain = function(text){ 

    console.log(

     'Solver says: %c '+ text +' %c\n', 
     'color: #080', '' 
    ); 
}; 

我修改了ERNO.Solver = function(){...看起来像下面的代码,如评论所说,并因为它是写在示例文件,stewart.js,和我放入一些测试代码来运行。

window.solver = new Solver(); 
solver.logic = function(cube){... 

但无济于事,当我运行的方法cube.solve(),或设置标志cube.isSolving == true,我的功能似乎并没有运行。如果我用stewart.js脚本代替我自己的脚本,同样的故事也是如此。它看起来像构造函数window.solver = new Solver(),因为Solver()未定义,因此失败,但我不确定它可以定义在哪里。它不是cubewindowERNO的成员,它们都是此环境中的所有对象。

我还没有真正能够找到一个好的资源,我应该如何设置它。

如果有人知道如何使用这个工具,或者有一个很好的资源,我可以从中学习,那会很棒。先谢谢你!

回答

1

我知道了,我放弃了一次后。原来我不明白ERNO课程是如何构建的,或者它如何适合大局。为了让内置函数调用Solver()函数,必须创建该对象的一个​​实例,并随后将其附加到window对象。在那之后,代码完全脱离盒子运行。

定义Solver()

ERNO.Solver = function(){... 

创建的Solver()一个实例,并且将其附接到window对象:

mySolver = new ERNO.Solver() 
window.solver = mySolver 

然后,通过调用cube.solve(),它设置cube.isSolvingtrue,所述cube.loop()函数将请致电window.solver(),它将运行编写在ERNO.Solver()函数中的代码d efinition!好极了!

+0

嗨,我想弄清楚迄今为止没有成功的同样的事情。据我所知,stweart.js上的solver.logic函数应该替代solvers.js上的ERNO.Solver函数。然后在主进程中创建一个新的求解器并使用window.solver = new Solver()将其附加到窗口对象; 。从那里可以调用cube.solve()方法,但它似乎并不奏效,我是否错过了一些东西? –

+0

对不起,双重评论,我应该提到求解器功能似乎运行,但我有以下错误:“solvers.js线90未捕获ReferenceError:UP未定义(...)” –

+0

@LorenzoPirondini你知道吗?如果没有更改为ERNO.UP –