Eydeet
0
Ich hab mich heute mal daran gemacht, ein Programm für das Spiel "Mortal Coil" [1] zu schreiben. Es funktioniert so weit, ist allerdings ab etwa Level 40 EXTREM langsam.
Ich habe bis jetzt nur einen ganz simplen rekursiven Ansatz implementiert, also keine Optimierungen drin, da ich keine Idee habe, wie diese zu implementieren wären.
Nach ein wenig nachdenken bin ich auf folgende Möglichkeiten gekommen:
- Sackgassen vermeiden, abbruch wenn nicht vermeidbar
- Abbruch beim erkennen von unerreichbaren Bereichen
Wie das nun implementiert werden könnte, weiß ich allerdings nicht.
Vielleicht gibt es hier ja jemanden, der mir da einen Ansatz nennen kann.
Oder - was mir gerade einfällt - was wäre mit einer Hackerboard.de-Lösung?
Hier ist mein Programm bis jetzt:
[1] http://hacker.org/coil/index.php
Ich habe bis jetzt nur einen ganz simplen rekursiven Ansatz implementiert, also keine Optimierungen drin, da ich keine Idee habe, wie diese zu implementieren wären.
Nach ein wenig nachdenken bin ich auf folgende Möglichkeiten gekommen:
- Sackgassen vermeiden, abbruch wenn nicht vermeidbar
- Abbruch beim erkennen von unerreichbaren Bereichen
Wie das nun implementiert werden könnte, weiß ich allerdings nicht.
Oder - was mir gerade einfällt - was wäre mit einer Hackerboard.de-Lösung?
Hier ist mein Programm bis jetzt:
Code:
import java.util.BitSet;
import java.io.*;
import java.net.URL;
public class MortalCoil {
private static int[][] offset = {{0, -1}, {1, 0}, {0, 1}, {-1, 0}};
private static char[] dirInf = {'U', 'R', 'D', 'L' };
private String boardString; // the plain board string as recieved from hacker.org
private BitSet board; // the board (0=free, 1=filled)
private String path;
private int width, height;
private boolean solutionFound;
private String user, pw;
private int solvX=-1, solvY=-1;
private int unfilled;
public static void main(String[] args) {
if (args.length != 2) {
System.err.println("usage: MortailCoil <user> <pw>");
return;
}
try {
MortalCoil mc = new MortalCoil(args[0], args[1]);
mc.start();
} catch (Exception e) {
System.out.println(e.toString());
}
}
public MortalCoil(String user, String pw) throws Exception {
this.user = user;
this.pw = pw;
this.board = new BitSet(width * height);
this.solutionFound = false;
getPage(false);
}
/**
* Manages the class' actions
*/
public void start() throws Exception {
resetBoard();
this.solutionFound = false;
printBoard();
// start solve method for each free field
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
if (validField(board, x, y)) {
System.out.println("x="+x+" y="+y);
solve(board, x, y, "", 1);
if (this.solutionFound) {
solvX = x;
solvY = y;
break;
}
}
}
if (this.solutionFound) break;
}
System.out.println("http://hacker.org/coil/index.php?user="+user+"&password="+pw+"&x=" + solvX + "&y=" + solvY + "&path=" + path);
getPage(true);
start();
}
private void getPage(boolean sendResult) throws Exception {
String url = "http://www.hacker.org/coil/index.php?name=" + user + "&password=" + pw;
if (sendResult) {
url += "&x="+solvX+"&y="+solvY+"&path="+path;
}
URL u = new URL(url);
InputStream is = u.openStream();
input2level(is);
}
/**
* Parses the HTML page hacker.org/coil/index.php
*/
private void input2level(InputStream is) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = in.readLine()) != null) {
if (!line.startsWith(" FlashVars")) continue;
line = line.split("\"")[1]; // get just the value
String[] ss = line.split("(=|&)"); // split up the name value pairs
width = Integer.parseInt(ss[1]);
height = Integer.parseInt(ss[3]);
boardString = ss[5];
break;
}
in.close();
}
/**
* Solves the board
*/
private void solve(BitSet board, int x, int y, String path, int filled) {
for (int dir = 0; dir < 4; ++dir) {
if (!validField(board, x+offset[dir][0], y+offset[dir][1])) continue;
BitSet newBoard = new BitSet(width * height);
newBoard.or(board);
String newPath = new String(path + dirInf[dir]);
int walked = walkBoard(newBoard, x, y, dir);
if ((filled+walked) == unfilled) { // ready
this.path = newPath;
this.board = newBoard;
this.solutionFound = true;
return;
}
// start new from here
solve(newBoard, x+offset[dir][0] * walked, y+offset[dir][1] * walked, newPath, filled+walked);
if (this.solutionFound) return;
}
}
/**
* Walks the board till it hits something
* @return the fields walked
*/
private int walkBoard(BitSet board, int x, int y, int dir) {
int filled = -1;
while (true) { //move till we hit something
setFilled(board, x, y);
++filled;
x += offset[dir][0];
y += offset[dir][1];
if (!validField(board, x, y)) {
return filled;
}
}
}
private boolean getField(BitSet board, int x, int y) {
return board.get(x + y*width);
}
private void setFilled(BitSet board, int x, int y) {
board.set(x + y*width);
}
/**
* Tests if the field is valid
*/
private boolean validField(BitSet board, int x, int y) {
if (x < 0 || y < 0 || x >= width || y >= height) return false;
return (!getField(board, x, y));
}
/**
* Copies the boardString into the board array
*/
private void resetBoard() {
board.clear();
unfilled = width * height;
for (int i = 0; i < boardString.length(); ++i) {
if (boardString.charAt(i) == 'X') {
board.set(i);
--unfilled;
}
}
}
/**
* Prints the board to stdout
*/
public void printBoard() {
for (int h = 0; h < height; ++h) {
for (int w = 0; w < width; ++w) {
System.out.print(getField(board, w, h) ? "X " : "? ");
}
System.out.println();
}
}
}