Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | daniel-mar | 1 | package gdi1sokoban.game; |
2 | |||
3 | import java.util.LinkedList; |
||
4 | import java.util.List; |
||
5 | |||
6 | /** |
||
7 | * GameLevelHistory class |
||
8 | * |
||
9 | * manages processed game steps |
||
10 | */ |
||
11 | class GameLevelHistory { |
||
12 | private LinkedList<Character> moveHistory = new LinkedList<Character>(); // stores all moves made in a level |
||
13 | private int steps = 0; // completed valid steps by the player |
||
14 | private char[][] historyBegin; // the level when no moves are made |
||
15 | /** |
||
16 | * Adds a move with the given direction to the History |
||
17 | * and updates the step-counter |
||
18 | * @param direction |
||
19 | */ |
||
20 | void addMove(final char direction){ |
||
21 | steps++; // increment step counter |
||
22 | moveHistory.add(direction); // Add direction char to history |
||
23 | } |
||
24 | /** |
||
25 | * deletes all steps made after the current one |
||
26 | */ |
||
27 | void deleteAfterCurrent(){ |
||
28 | if(moveHistory.size() >= steps){ |
||
29 | final List<Character> currHistory = moveHistory.subList(0, steps); // List to store all moves untill current one |
||
30 | moveHistory = new LinkedList<Character>(); // reset history |
||
31 | moveHistory.addAll(0, currHistory); // insert moves into History |
||
32 | } |
||
33 | } |
||
34 | /** |
||
35 | * resets the counter |
||
36 | */ |
||
37 | void reset(){ |
||
38 | steps = 0; |
||
39 | moveHistory.clear(); |
||
40 | } |
||
41 | |||
42 | /** |
||
43 | * sets the steps counter |
||
44 | * @param steps value of the step counter |
||
45 | */ |
||
46 | public void setSteps(final int steps) { |
||
47 | this.steps = steps; |
||
48 | } |
||
49 | /** |
||
50 | * sets the level Map with that the history begins |
||
51 | * @param gameMap map of this level |
||
52 | */ |
||
53 | void setStart(final char[][] startMap){ |
||
54 | // copy given array into historyBegin array |
||
55 | historyBegin = new char[startMap.length][startMap[0].length]; |
||
56 | for(int i = 0; i<startMap.length; i++) |
||
57 | for(int j=0; j<startMap[0].length; j++) |
||
58 | historyBegin[i][j] = startMap[i][j]; |
||
59 | |||
60 | } |
||
61 | /** |
||
62 | * returns the map where the history started logging |
||
63 | * @return game Map where everything is placed as it was when the History logger was started |
||
64 | */ |
||
65 | char[][] getStart(){ |
||
66 | // always return copy of array because otherwise java returns a reference |
||
67 | final char[][] historyBeginCopy = new char[historyBegin.length][historyBegin[0].length]; |
||
68 | for(int i = 0; i<historyBegin.length; i++) |
||
69 | for(int j=0; j<historyBegin[0].length; j++) |
||
70 | historyBeginCopy[i][j] = historyBegin[i][j]; |
||
71 | return historyBeginCopy; |
||
72 | } |
||
73 | /** |
||
74 | * get number of steps made so far |
||
75 | * @return the number of steps made in this level ( = the position is history) |
||
76 | */ |
||
77 | int getSteps() { |
||
78 | return steps; |
||
79 | } |
||
80 | /** |
||
81 | * sets the history-entries to the given value |
||
82 | * @param listOfDirections LinkedList of directions |
||
83 | */ |
||
84 | void setHistory(final LinkedList<Character> listOfDirections){ |
||
85 | moveHistory = listOfDirections; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * get the move History as LinkedList |
||
90 | * @return moveHistory |
||
91 | */ |
||
92 | LinkedList<Character> getMoveHistory(){ |
||
93 | return moveHistory; |
||
94 | } |
||
95 | } |