Skip to content

Instantly share code, notes, and snippets.

@tachiba
Created May 11, 2011 02:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tachiba/965782 to your computer and use it in GitHub Desktop.
Save tachiba/965782 to your computer and use it in GitHub Desktop.
hogehoge
import java.io.*;
import java.util.*;
public class Soko{
char[][] map;
char[][] mapSource;
int N, M, a, b, task, cost, mapIndex;
boolean isClear = false;
int[][] move = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
List<Integer> log;
public static void main(String... arg){
try {
new Soko().run(new Scanner(System.in));
} catch (FileNotFoundException e){
sysout("実行に必要なファイルが見つかりません");
} catch (IOException e) {
sysout(e.toString());
}
}
void run(Scanner sc) throws FileNotFoundException, IOException {
log = new LinkedList<Integer>();
BufferedReader br = new BufferedReader(new FileReader("splush.txt"));
String line;
while ((line = br.readLine()) != null) {
sysout(line);
}
line = null;
br = null;
sysout("Please Enter Key... Game Start");
// TODO choosing map
mapIndex = 1;
build();
while (true) {
/* parse input */
String in = sc.nextLine();
if (in.length() > 0) switch (in.charAt(0)) {
case 's': foward(0); break;
case 'd': foward(1); break;
case 'w': foward(2); break;
case 'a': foward(3); break;
case 'u': back(); break;
case '@': reset(); break;
}
/* show map */
for (char[] c : map) {
sysout(new String(c));
}
System.out.printf("移動回数: %02d回\n", cost++);
sysout("移動: (上->w, 左->a, 下->s, 右->d) + Enter");
sysout("戻す->u, リセット->@, 入力キャンセル->!を含める");
}
}
void foward(int ci) {
log.add(ci);
/* move to */
int at = a + move[ci][0];
int bt = b + move[ci][1];
if (map[at][bt] == 'o' || map[at][bt] == 'O') {
int at2 = at + move[ci][0];
int bt2 = bt + move[ci][1];
if (map[at2][bt2] == ' ') {
map[at2][bt2] = 'o';
map[at][bt] = 'p';
map[a][b] = ' ';
a = at;
b = bt;
} else if (map[at2][bt2] == '.') {
map[at2][bt2] = 'O';
map[at][bt] = 'p';
map[a][b] = ' ';
a = at;
b = bt;
task--;
}
} else if (map[at][bt] == ' ') {
map[a][b] = (map[a][b] == 'p') ? ' ' : '.';
map[at][bt] = 'p';
a = at;
b = bt;
} else if (map[at][bt] == '.') {
map[a][b] = ' ';
map[at][bt] = 'P';
a = at;
b = bt;
}
}
void back(){}
void reset() throws FileNotFoundException, IOException {
cost = 0;
build();
}
void build() throws FileNotFoundException, IOException {
BufferedReader br = new BufferedReader(new FileReader("map/gamemap_" + mapIndex + ".txt"));
String line;
LinkedList<String> data = new LinkedList<String>();
while ((line = br.readLine()) != null) {
if (line.length() == 0 || line.charAt(0) == ';') continue;
data.add(line);
}
line = null;
N = data.size();
M = data.getFirst().length();
map = new char[N][M];
for (int i = 0; i < N; i++) {
map[i] = data.get(i).toCharArray();
for (int j = 0; j < M; j++) {
if (map[i][j] == 'p') {
a = i;
b = j;
} else if (map[i][j] == '.') {
task++;
}
}
}
// sysout(Arrays.toString(map[0]));
mapSource = new char[N][M];
deepcopy(map, mapSource);
}
void deepcopy(char[][] source, char[][] to){
for (int i = 0; i < source.length; i++) {
to[i] = source[i].clone();
}
}
public static void sysout(String s){
System.out.println(s);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment