Last active
October 24, 2016 18:48
-
-
Save protolambda/04d6b29e870b2b87e3b6f9ffba3fb535 to your computer and use it in GitHub Desktop.
2x2x2 Rubik's Cube bitboard generator, for edges. By @protolambda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Quick but powerful Rubik's cube bitboard generator. For 2x2x2 cubes, could be extended for 3x3x3. | |
* Made by @protolambda | |
* | |
* The bitboards are used for rotating the edges of a face clockwise or counter-clockwise. | |
* | |
*/ | |
void main() { | |
List<Face> fa = []; | |
for(int i = 0; i < 3; i++){ | |
for(int j = 0; j < 2; j++){ | |
fa.add(new Face(i, j)); | |
} | |
} | |
fa.forEach((Face f){ | |
fa.where((a)=>a.dim != f.dim).forEach((a){ | |
f.edges.add(new Edge(f, a)); | |
}); | |
}); | |
print("edges done"); | |
fa.forEach((Face a){ | |
fa.forEach((Face b){ | |
Edge x = a.edges.firstWhere((e)=>e.edging == b, orElse: ()=> null); | |
Edge y = b.edges.firstWhere((e)=>e.edging == a, orElse: ()=> null); | |
if(x == null || y == null) return; | |
x.mirror = y; | |
y.mirror = x; | |
}); | |
}); | |
print("mirrors done"); | |
Face white = fa.first; | |
Face blue = white.edges.first.edging; | |
Face green = white.edges.firstWhere((e)=> e.edging != blue && !e.edging.edges.any((a)=>a == blue)).edging; | |
Face red = white.edges.firstWhere((e)=> e.edging != blue && e.edging != green).edging; | |
Face orange = white.edges.firstWhere((e)=> e.edging != blue && e.edging != green && e.edging != red).edging; | |
Face yellow = orange.edges.firstWhere((e)=> e.edging != white && e.edging != green && e.edging != blue).edging; | |
white.name = "white"; | |
blue.name = "blue"; | |
green.name = "green"; | |
red.name = "red"; | |
orange.name = "orange"; | |
yellow.name = "yellow"; | |
print("$white, $blue, $green, $red, $orange, $yellow"); | |
//top | |
white.getEdge(blue).clockwise = white.getEdge(red); | |
white.getEdge(red).clockwise = white.getEdge(green); | |
white.getEdge(green).clockwise = white.getEdge(orange); | |
white.getEdge(orange).clockwise = white.getEdge(blue); | |
print("top done"); | |
//sides top | |
white.edges.forEach((e){ | |
Face top = e.edging; | |
Face cw = e.clockwise.edging, ccw = e.counterClockwise.edging; | |
top.getEdge(cw).clockwise = e.mirror; | |
top.getEdge(ccw).counterClockwise = e.mirror; | |
}); | |
print("sides top done"); | |
//bottom | |
yellow.getEdge(blue).clockwise = yellow.getEdge(orange); | |
yellow.getEdge(orange).clockwise = yellow.getEdge(green); | |
yellow.getEdge(green).clockwise = yellow.getEdge(red); | |
yellow.getEdge(red).clockwise = yellow.getEdge(blue); | |
print("bottom done"); | |
//sides bottom | |
yellow.edges.forEach((e){ | |
Face top = e.edging; | |
Face cw = e.clockwise.edging, ccw = e.counterClockwise.edging; | |
top.getEdge(cw).clockwise = e.mirror; | |
top.getEdge(ccw).counterClockwise = e.mirror; | |
}); | |
print("sides bottom done"); | |
//connect sides with bottom | |
fa.forEach((f)=>f.edges.forEach((e){ | |
if(e != null && e.counterClockwise != null && e.clockwise != null && e.clockwise.clockwise == null){ | |
e.clockwise.clockwise = f.edges.firstWhere((a) => a != e && a != e.clockwise && a != e.counterClockwise); | |
e.clockwise.clockwise.clockwise = e.counterClockwise; | |
} | |
})); | |
print("connect done"); | |
fa.forEach((f){ | |
Edge a = f.edges.first, b = f.edges.first.clockwise, c = f.edges.first.clockwise.clockwise, d = f.edges.first.counterClockwise; | |
//sort edges | |
f.edges = [a, b, c, d]; | |
}); | |
print("clockwise sort done"); | |
fa = [white, blue, red, green, orange, yellow]; | |
print("face sort done"); | |
print("outputting bitboards...\n\n"); | |
fa.forEach((Face face){ | |
Edge e; | |
//this, clockwise, counterclockwise, clockwise board-index, counterclockwise board-index | |
int w, cw, ccw, cw_i, ccw_i; | |
String line = "\tedge${face.name.substring(0, 1).toUpperCase()+face.name.substring(1)}:\t.long "; | |
List<String> entries = []; | |
for(Face f in fa){ | |
e = face.edges.firstWhere((e)=>e.edging == f, orElse: ()=> null); | |
if(e == null){ | |
entries.add("0x00000000"); | |
continue; | |
} | |
w = e.mirror.onto.edges.indexOf(e.mirror); | |
cw = e.clockwise.mirror.onto.edges.indexOf(e.clockwise.mirror); | |
ccw = e.counterClockwise.mirror.onto.edges.indexOf(e.counterClockwise.mirror); | |
int | |
cw_i = fa.indexOf(e.clockwise.edging), | |
ccw_i = fa.indexOf(e.counterClockwise.edging), | |
aW = 1 << w, | |
bW = 1 << ((w + 1) % 4), | |
aCW = 1 << cw, | |
bCW = 1 << ((cw + 1) % 4), | |
aCCW = 1 << ccw, | |
bCCW = 1 << ((ccw + 1) % 4); | |
entries.add("0x"+hex(cw_i)+hex(ccw_i)+hex(aW)+hex(bW)+hex(aCW)+hex(bCW)+hex(aCCW)+hex(bCCW)); | |
} | |
print(line + entries.join(", ")); | |
}); | |
} | |
String hex(int i) => i.toRadixString(16); | |
class Face { | |
final int dim, side; | |
int get face => dim * 3 + side; | |
List<Edge> edges = []; | |
Face(this.dim, this.side); | |
String name; | |
Edge getEdge(Face a){ | |
return edges.firstWhere((e)=>e.edging == a); | |
} | |
} | |
class Edge { | |
final Face onto, edging; | |
Edge mirror, _clockwise, _counterClockwise; | |
Edge get clockwise => _clockwise; | |
Edge get counterClockwise => _counterClockwise; | |
set clockwise(Edge e){ | |
_clockwise = e; | |
e._counterClockwise = this; | |
} | |
set counterClockwise(Edge e){ | |
_counterClockwise = e; | |
e._clockwise = this; | |
} | |
Edge(this.onto, this.edging); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment