Guest User

Untitled

a guest
Dec 10th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Soko{
  5. char[][] map;
  6. char[][] mapSource;
  7. int N, M, a, b, task, cost, mapIndex;
  8. boolean isClear = false;
  9. int[][] move = new int[][]{{1,0},{0,1},{-1,0},{0,-1}};
  10. List<Integer> log;
  11.  
  12. public static void main(String... arg){
  13. try {
  14. new Soko().run(new Scanner(System.in));
  15. } catch (FileNotFoundException e){
  16. sysout("実行に必要なファイルが見つかりません");
  17. } catch (IOException e) {
  18. sysout(e.toString());
  19. }
  20. }
  21.  
  22. void run(Scanner sc) throws FileNotFoundException, IOException {
  23. log = new LinkedList<Integer>();
  24.  
  25. BufferedReader br = new BufferedReader(new FileReader("splush.txt"));
  26. String line;
  27. while ((line = br.readLine()) != null) {
  28. sysout(line);
  29. }
  30. line = null;
  31. br = null;
  32.  
  33. sysout("Please Enter Key... Game Start");
  34. // TODO choosing map
  35.  
  36. mapIndex = 1;
  37. build();
  38.  
  39. while (true) {
  40. /* parse input */
  41. String in = sc.nextLine();
  42. if (in.length() > 0) switch (in.charAt(0)) {
  43. case 's': foward(0); break;
  44. case 'd': foward(1); break;
  45. case 'w': foward(2); break;
  46. case 'a': foward(3); break;
  47. case 'u': back(); break;
  48. case '@': reset(); break;
  49. }
  50.  
  51. /* show map */
  52. for (char[] c : map) {
  53. sysout(new String(c));
  54. }
  55. System.out.printf("移動回数: %02d回\n", cost++);
  56. sysout("移動: (上->w, 左->a, 下->s, 右->d) + Enter");
  57. sysout("戻す->u, リセット->@, 入力キャンセル->!を含める");
  58. }
  59. }
  60.  
  61. void foward(int ci) {
  62. log.add(ci);
  63.  
  64. /* move to */
  65. int at = a + move[ci][0];
  66. int bt = b + move[ci][1];
  67.  
  68. if (map[at][bt] == 'o' || map[at][bt] == 'O') {
  69. int at2 = at + move[ci][0];
  70. int bt2 = bt + move[ci][1];
  71.  
  72. if (map[at2][bt2] == ' ') {
  73. map[at2][bt2] = 'o';
  74. map[at][bt] = 'p';
  75. map[a][b] = ' ';
  76. a = at;
  77. b = bt;
  78.  
  79. } else if (map[at2][bt2] == '.') {
  80. map[at2][bt2] = 'O';
  81. map[at][bt] = 'p';
  82. map[a][b] = ' ';
  83. a = at;
  84. b = bt;
  85.  
  86. task--;
  87. }
  88.  
  89. } else if (map[at][bt] == ' ') {
  90. map[a][b] = (map[a][b] == 'p') ? ' ' : '.';
  91. map[at][bt] = 'p';
  92. a = at;
  93. b = bt;
  94.  
  95. } else if (map[at][bt] == '.') {
  96. map[a][b] = ' ';
  97. map[at][bt] = 'P';
  98. a = at;
  99. b = bt;
  100.  
  101. }
  102. }
  103.  
  104. void back(){}
  105.  
  106. void reset() throws FileNotFoundException, IOException {
  107. cost = 0;
  108. build();
  109. }
  110.  
  111. void build() throws FileNotFoundException, IOException {
  112. BufferedReader br = new BufferedReader(new FileReader("map/gamemap_" + mapIndex + ".txt"));
  113. String line;
  114. LinkedList<String> data = new LinkedList<String>();
  115.  
  116. while ((line = br.readLine()) != null) {
  117. if (line.length() == 0 || line.charAt(0) == ';') continue;
  118. data.add(line);
  119. }
  120. line = null;
  121.  
  122. N = data.size();
  123. M = data.getFirst().length();
  124.  
  125. map = new char[N][M];
  126. for (int i = 0; i < N; i++) {
  127. map[i] = data.get(i).toCharArray();
  128.  
  129. for (int j = 0; j < M; j++) {
  130. if (map[i][j] == 'p') {
  131. a = i;
  132. b = j;
  133. } else if (map[i][j] == '.') {
  134. task++;
  135. }
  136. }
  137. }
  138. // sysout(Arrays.toString(map[0]));
  139.  
  140. mapSource = new char[N][M];
  141. deepcopy(map, mapSource);
  142. }
  143.  
  144. void deepcopy(char[][] source, char[][] to){
  145. for (int i = 0; i < source.length; i++) {
  146. to[i] = source[i].clone();
  147. }
  148. }
  149.  
  150. public static void sysout(String s){
  151. System.out.println(s);
  152. }
  153. }
Add Comment
Please, Sign In to add comment