Created
November 22, 2015 03:20
-
-
Save MegaApuTurkUltra/5081a0c37ba31bc858e0 to your computer and use it in GitHub Desktop.
Naive Bruteforce Implementation for Mortal Coil
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
var username = ""; | |
var password = ""; | |
var DEBUG = false; | |
function request(x, y, path){ | |
var params = typeof x=="undefined" ? "": `&x=${x}&y=${y}&path=${path}`; | |
// yeah yeah, I don't care about my account :P | |
return $.get(`http://crossorigin.me/http://www.hacker.org/coil/index.php?name=${username}&password=${password}` + params); | |
} | |
function parse(res){ | |
var parts = $(res).find("param[name=FlashVars]").val() | |
.split("&"); | |
var x, y, boardStr; | |
for(var i = 0; i < parts.length; i++){ | |
var param = parts[i].split("="); | |
if(param[0] == "x") x = parseInt(param[1]); | |
if(param[0] == "y") y = parseInt(param[1]); | |
if(param[0] == "board") boardStr = param[1]; | |
} | |
var board = []; | |
for(var j = 0; j < y; j++){ | |
var arr = []; | |
for(var k = 0; k < x; k++){ | |
arr.push(boardStr[j*x + k]); | |
} | |
board.push(arr); | |
} | |
return board; | |
} | |
function get(board, x, y){ | |
return board[y][x]; | |
} | |
function log(){ | |
if(DEBUG){ | |
console.log.apply(console, arguments); | |
} | |
} | |
function set(board, x, y, c){ | |
board[y][x] = c; | |
} | |
function clone(board){ | |
return $.extend(true, [], board); | |
} | |
function validate(board){ | |
var boardStr = board.map(e=>e.join("")).join(""); | |
log("Board string", boardStr); | |
return boardStr.split(".").length == 1; | |
} | |
function replay(board, x, y, dirStr){ | |
log(board.map(e=>e.join('')).join('\n')); | |
for(dir of [...dirStr]){ | |
log(dir); | |
while(x >= 0 && x < board[0].length && y >= 0 && y < board.length && get(board, x, y) == "."){ | |
set(board, x, y, "C"); | |
x = x + (dir == "R") - (dir == "L"); | |
y = y + (dir == "D") - (dir == "U"); | |
log(x, y); | |
} | |
x = x - (dir == "R") + (dir == "L"); | |
y = y - (dir == "D") + (dir == "U"); | |
set(board, x, y, "."); | |
log(x, y); | |
log(board.map(e=>e.join('')).join('\n')); | |
} | |
} | |
function step(board, x, y, dir, dirs){ | |
log(`=======Level ${dirs.length}=======`, arguments); | |
dirs.push(dir); | |
board = clone(board); | |
var x1 = x + (dir == "R") - (dir == "L"); | |
var y1 = y + (dir == "D") - (dir == "U"); | |
log("New pos", x1, y1); | |
if(x1 < 0 || x1 >= board[0].length || y1 < 0 || y1 >= board.length || get(board, x1, y1) != "."){ | |
log("Invalid move, checking board"); | |
if(validate(board)){ | |
log("valid!"); | |
return dirs.slice(0, dirs.length - 1); | |
} else { | |
log("Not valid"); | |
dirs.pop(); | |
return false; | |
} | |
} | |
log("dir is", dir); | |
while(x1 >= 0 && x1 < board[0].length && y1 >= 0 && y1 < board.length && get(board, x1, y1) == "."){ | |
set(board, x1, y1, "C"); | |
x1 = x1 + (dir == "R") - (dir == "L"); | |
y1 = y1 + (dir == "D") - (dir == "U"); | |
log(x1, y1); | |
} | |
x1 = x1 - (dir == "R") + (dir == "L"); | |
y1 = y1 - (dir == "D") + (dir == "U"); | |
log("board is now", JSON.stringify(board)); | |
for(var dir1 of [..."LRDU"]){ | |
var res = step(board, x1, y1, dir1, dirs); | |
if(!res){ | |
} else { | |
return res; | |
} | |
} | |
dirs.pop(); | |
return false; | |
} | |
//replay(JSON.parse('[[".",".",".",".","."],[".","X",".",".","."],[".",".",".","X","."]]'), 2, 0, "LDRURUL"); | |
var lvlCount = 1; | |
function go(res){ | |
var board = parse(res); | |
var solution = {x:-1, y:-1, steps:[]}; | |
outer: | |
for(var x = 0; x < board[0].length; x++){ | |
for(var y = 0; y < board.length; y++){ | |
if(get(board, x, y) != ".") continue; | |
for(var dir1 of [..."LRDU"]){ | |
var board0 = clone(board); | |
set(board0, x, y, "C"); | |
var res = step(board0, x, y, dir1, []); | |
if(res !== false){ | |
solution.x = x; | |
solution.y = y; | |
solution.steps = res; | |
break outer; | |
} | |
} | |
} | |
} | |
log(solution); | |
request(solution.x, solution.y, solution.steps.join("")).then(function(res0){ | |
if(res0.contains("sucked")){ | |
console.warn("LEVEL FAILED!"); | |
} else { | |
console.info("Completed ", ++lvlCount, " levels"); | |
setTimeout(function(){ | |
go(res0); | |
}, 1); | |
} | |
}); | |
} | |
request().then(go); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment