Created
March 5, 2014 15:36
-
-
Save YellowAfterlife/9369618 to your computer and use it in GitHub Desktop.
Solves puzzle from Prequel Adventure comic (Mar 04, 2014 http://www.prequeladventure.com/2014/03/aggy-extrapolate/ ). Sample output: https://gist.github.com/YellowAfterlife/9369636
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
import flash.display.Bitmap; | |
import flash.display.BitmapData; | |
import flash.display.Graphics; | |
import flash.display.Sprite; | |
import flash.events.KeyboardEvent; | |
import flash.events.TimerEvent; | |
import flash.geom.Rectangle; | |
import flash.Lib; | |
import flash.text.TextField; | |
import flash.utils.Timer; | |
import openfl.Assets; | |
// | |
class Board { | |
public var pegs:Array<Array<Bool>>; | |
public var x1:Int = -1; | |
public var y1:Int = -1; | |
public var x2:Int = -1; | |
public var y2:Int = -1; | |
public function new() { | |
var o = true, _ = false; | |
pegs = [ | |
[o], | |
[o,o], | |
[o,_,o], | |
[o,o,o,o], | |
[o,o,o,o,o] | |
]; | |
} | |
public function clone(y1:Int, x1:Int, y2:Int, x2:Int):Board { | |
var r = new Board(); | |
for (i in 0 ... pegs.length) { | |
var m = pegs[i], q = r.pegs[i]; | |
for (j in 0 ... m.length) q[j] = m[j]; | |
} | |
r.x1 = x1; r.y1 = y1; | |
r.x2 = x2; r.y2 = y2; | |
return r; | |
} | |
public function solve(m:Array<Board>):Bool { | |
var n:Int = pegs.length, l:Int, r:Board, c:Int = count(); | |
m.push(this); | |
if (c == 1) return true; | |
for (i in 0 ... n) { | |
l = pegs[i].length; | |
for (j in 0 ... l) if (pegs[i][j]) { | |
// up-left: | |
if (i >= 2 && j >= 2 && pegs[i - 1][j - 1] && !pegs[i - 2][j - 2]) { | |
r = clone(i, j, i - 2, j - 2); | |
r.pegs[i][j] = false; | |
r.pegs[i - 1][j - 1] = false; | |
r.pegs[i - 2][j - 2] = true; | |
if (r.solve(m)) return true; | |
} | |
// up-right: | |
if (i >= 2 && j < i - 1 && pegs[i - 1][j] && !pegs[i - 2][j]) { | |
r = clone(i, j, i - 2, j); | |
r.pegs[i][j] = false; | |
r.pegs[i - 1][j] = false; | |
r.pegs[i - 2][j] = true; | |
if (r.solve(m)) return true; | |
} | |
// down-left: | |
if (i + 2 < n && pegs[i + 1][j] && !pegs[i + 2][j]) { | |
r = clone(i, j, i + 2, j); | |
r.pegs[i][j] = false; | |
r.pegs[i + 1][j] = false; | |
r.pegs[i + 2][j] = true; | |
if (r.solve(m)) return true; | |
} | |
// down-right: | |
if (i + 2 < n && pegs[i + 1][j + 1] && !pegs[i + 2][j + 2]) { | |
r = clone(i, j, i + 2, j + 2); | |
r.pegs[i][j] = false; | |
r.pegs[i + 1][j + 1] = false; | |
r.pegs[i + 2][j + 2] = true; | |
if (r.solve(m)) return true; | |
} | |
// left: | |
if (j >= 2 && pegs[i][j - 1] && !pegs[i][j - 2]) { | |
r = clone(i, j, i, j - 2); | |
r.pegs[i][j] = false; | |
r.pegs[i][j - 1] = false; | |
r.pegs[i][j - 2] = true; | |
if (r.solve(m)) return true; | |
} | |
// left: | |
if (j + 2 < l && pegs[i][j + 1] && !pegs[i][j + 2]) { | |
r = clone(i, j, i, j + 2); | |
r.pegs[i][j] = false; | |
r.pegs[i][j + 1] = false; | |
r.pegs[i][j + 2] = true; | |
if (r.solve(m)) return true; | |
} | |
} | |
} | |
m.pop(); | |
return false; | |
} | |
public function toString():String { | |
var r = "", n:Int = pegs.length, m:Array<Bool>; | |
for (i in 0 ... n) { | |
r += StringTools.rpad("\n", " ", n - i); | |
m = pegs[i]; | |
for (j in 0 ... m.length) r += m[j] | |
? ((j == x2 && i == y2) ? "x " : "o ") | |
: ((j == x1 && i == y1) ? "* " : ". "); | |
} | |
return r; | |
} | |
public function count():Int { | |
var r:Int = 0; | |
for (m in pegs) for (v in m) if (v) r++; | |
return r; | |
} | |
} | |
class Main { | |
public function new() { | |
var board = new Board(); | |
var moves:Array<Board> = []; | |
board.solve(moves); | |
for (i in 0 ... moves.length) { | |
Lib.trace("Turn " + (i + 1) + ": " + moves[i]); | |
} | |
} | |
@:keep static function main() new Main(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cripes man, really, really cool, but how come you came up with that in just half an hour... frack I really need to work on my skills! hahaha