In this assignment we complete the implementation of our game. Your functionality should already be complete, other than issues surrounding the timer which we have not yet implemented.
In order to have your timer run constantly in the background, it must be run from another thread. To do this safely, we will make use of an AtomicInteger to keep track of how much time is left in the game. A Java Timer object will schedule a task to decrement the the left every second. Most of the code required to initialize the timer is provided below – you should add it somewhere in your project which makes sense. You may decide to change these items to static if it makes your code cleaner.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
private Timer timer; private AtomicInteger timeLeft; public void initTimer(int secs) { timeLeft = new AtomicInteger(secs); TimerTask task = new TimerTask() { @Override public void run() { int tl = timeLeft.decrementAndGet(); if (tl == 0) { // Add code to tell the user they lost and to exit the game. } } }; timer = new Timer(); timer.schedule(task, 0, 1000); } |
The timer should be initialized when play begins (after the XML data file is loaded). The project specification allows for time to be added to timeLeft when certain conditions occur. You should add methods for increasing the time by an amount, and perhaps getting the time left. Check the AtomicInteger API for how to get and set values. After every action the user takes, you should print the amount of time left.
After adding the timer your code should be functionally complete – remember to check that all conditions in the project description are met.
You will also complete the implementation of the singly linked list begun in class by implementing the following methods:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
// Inserts the specified element at the specified position // in this list. // Throw an IndexOutOfBoundsException if the index is out // of range (index < 0 || index > count) public void insert(T data, int index) // Returns true if this list contains the specified element. // More formally, returns true if and only if this list // contains at least one element e such that // (data==null ? e==null : data.equals(e)). public boolean exists(T data) // Removes the first occurrence of the specified element // from this list, if it is present. Use a similar check // as above to avoid null pointers. // Returns true if this list contained the specified element. public boolean remove(T data) // Returns the element at the specified position in this list. // Throw an IndexOutOfBoundsException if the index is out of range. public T get(int index) |
You may not use the Iterator in implementing these methods (that’s for external use only). Create a driver class to test your linked list thoroughly. Then, replace the array which contains the Characters in your Room with your linked list. Be careful to remove (or comment out) all code that is now irrelevant.
There is extra credit available in this assignment – 10% extra credit will be added for implementing the optional remove() method in the iterator, using appropriate techniques (e.g., don’t just call the linked list’s remove).