-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathsolvers.js
More file actions
executable file
·115 lines (64 loc) · 2.79 KB
/
solvers.js
File metadata and controls
executable file
·115 lines (64 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
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.
*/
function Solver(){
// 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.
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 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() ){
if( erno.verbosity >= 0.5 ) 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.
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.
Solver.prototype.explain = function( text ){
console.log(
'Solver says: %c '+ text +' %c\n',
'color: #080', ''
)
}