14 */ |
14 */ |
15 public class Mine |
15 public class Mine |
16 { |
16 { |
17 private Map<Character, Cell> cellPrototypes = new LinkedHashMap<Character, Cell>(); |
17 private Map<Character, Cell> cellPrototypes = new LinkedHashMap<Character, Cell>(); |
18 |
18 |
19 private Cell cells[][]; |
19 private Cell cells[][][]; |
|
20 private int index = 0; |
20 |
21 |
21 public Mine(int n, int m, String content) |
22 public Mine(int n, int m, String content) |
22 { |
23 { |
23 initCellPrototypes(); |
24 initCellPrototypes(); |
24 |
25 |
25 cells = new Cell[n][m]; |
26 cells = new Cell[2][n][m]; |
26 |
27 |
27 int index = 0; |
28 int index = 0; |
28 |
29 |
29 for(int i=0; i<n; ++i) |
30 for(int i=0; i<n; ++i) |
30 for(int j=0; j<m; ++j) |
31 for(int j=0; j<m; ++j) |
31 { |
32 { |
32 cells[i][j] = new Cell( cellPrototypes.get(content.charAt(index++)) ); |
33 cells[index][i][j] = cellPrototypes.get(content.charAt(index++)); |
33 } |
34 } |
|
35 } |
|
36 |
|
37 private Cell[][] firstBuffer() |
|
38 { |
|
39 return cells[index]; |
|
40 } |
|
41 |
|
42 private Cell[][] secondBuffer() |
|
43 { |
|
44 return cells[index==0 ? 1 : 0]; |
|
45 } |
|
46 |
|
47 private void swapBuffers() |
|
48 { |
|
49 index = index==0 ? 1 : 0; |
34 } |
50 } |
35 |
51 |
36 private void initCellPrototypes() |
52 private void initCellPrototypes() |
37 { |
53 { |
38 cellPrototypes.put('*', new Cell(CellTypes.ROCK, '*')); |
54 for(Cell c : Cell.values()) |
39 cellPrototypes.put('\\', new Cell(CellTypes.LAMBDA, '\\')); |
55 { |
40 cellPrototypes.put('#', new Cell(CellTypes.WALL, '#')); |
56 cellPrototypes.put(c.getASCII(), c); |
41 cellPrototypes.put('R', new Cell(CellTypes.ROBOT, 'R')); |
57 } |
42 cellPrototypes.put('L', new Cell(CellTypes.CLOSED_LAMBDA_LIFT, 'L')); |
|
43 cellPrototypes.put('O', new Cell(CellTypes.OPEN_LAMBDA_LIFT, 'O')); |
|
44 cellPrototypes.put(' ', new Cell(CellTypes.SPACE, ' ')); |
|
45 cellPrototypes.put('.', new Cell(CellTypes.EARTH, '.')); |
|
46 } |
58 } |
47 |
59 |
48 void print() |
60 public void print() |
49 { |
61 { |
50 for(int i=0; i<cells.length; ++i) |
62 for(int i=0; i<cells.length; ++i) |
51 { |
63 { |
52 for(int j=0; j<cells[i].length; ++j) |
64 for(int j=0; j<cells[i].length; ++j) |
53 { |
65 { |
54 System.out.print(cells[i][j].getASCII()); |
66 System.out.print(firstBuffer()[i][j]); |
55 } |
67 } |
56 System.out.println(); |
68 System.out.println(); |
57 } |
69 } |
58 } |
70 } |
|
71 |
|
72 public void updateAfterMove() |
|
73 { |
|
74 for(int i=0; i<cells.length; ++i) |
|
75 for(int j=0; j<cells[i].length; ++j) |
|
76 { |
|
77 secondBuffer()[i][j] = firstBuffer()[i][j]; |
|
78 } |
|
79 |
|
80 for(int i=1; i<cells.length-1; ++i) |
|
81 for(int j=cells[i].length-1; j>1; --j) |
|
82 { |
|
83 switch(firstBuffer()[i][j]) |
|
84 { |
|
85 case ROCK: |
|
86 { |
|
87 if(firstBuffer()[i][j-1] == Cell.EMPTY) |
|
88 { |
|
89 secondBuffer()[i][j] = Cell.EMPTY; |
|
90 } |
|
91 } |
|
92 break; |
|
93 } |
|
94 } |
|
95 } |
|
96 |
59 } |
97 } |