Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
COMPUTER SCIENCE 1027B – Foundations of Computer Science II Assignment 3
For this assignment you need to implement 5 Java classes: Position, DoubleNode, DoubleList, SnakeLinked, and BoardGameLinked. Your program must catch any exceptions that might be thrown. For each exception caught an appropriate message must be printed. The message must explain what caused the exception to be thrown.
Class Position
This is the same class as in Assignment 1.
Class DoubleNode
This class represents the nodes in a doubly linked list. The header of this class must be this:
public class DoubleNode<T>
This class will have the following instance variables:
private DoubleNode<T> next. A reference to the next node in the list.
private DoubleNode<T> prev. A reference to the previous node in the list.
private T data. The data stored in this node.
In this class you must implement the following methods:
public DoubleNode (). Creates an empty node, where all instance variables are null.
public DoubleNode (T newData). Creates a node storing the given data in which next and prev are null.
public DoubleNode<T> getNext (). Returns the value of next.
public T getData (). Returns the value of data.
public void set Next (DoubleNode<T> nextNode). Stores nextNode in next.
public void setPrev (DoubleNode<T> prevNode). Stores prevNode in prev.
public void setData (T newData). Stores newData in data.
Class DoubleList
This class represents a doubly linked list of nodes of the class DoubleNode. The header of this class must be this:
public class DoubleList<T>
This class will have the following instance variables:
private DoubleNode<T> head. This is a reference to the first node in the list.
private DoubleNode<T> rear. This is a reference to the last node in the list.
private int numDataItems. This is the number of nodes in the list. In this class you must implement the following methods:
public DoubleList(). This creates an empty list with zero nodes.
public void addData (int index, T newData) throws InvalidPositionException. This method must add a new node to the list storing newData. The node must be inserted in the index position of the list. Therefore, if index = 0 then the new node must be inserted at the beginning of the list. If index = 1, the new node is added after the first node in the list; if index = 2, then the new node is added after the second node of the list, and so on. If index = numDataItems, then the new node is added to the end of the list. If index < 0 or index > numDataItems an InvalidPositionException must be thrown.
public DoubleNode<T> getNode(int index) throws InvalidPositionException. Returns the node that is at the index position of the list. If index = 0, then the first node of the list must be returned; if index = 1 then the second node of the list is returned, and so on. If index = numDataItems – 1 then the last node of the list is returned. If index < 0 or index ≥ numDataItems then an InvalidPositionException must be thrown.
public void removeData(int index) throws InvalidPositionException. Removes the node that is at the index position of the list (see method getNode to see how indices are determined). If index < 0 or index ≥ numDataItems then an InvalidPositionException must be thrown.
public T getData(int index) throws InvalidPositionException. Returns the data stored in the node located at the index position of the list (see description of method getNode to see how indices are determined). If index < 0 or index ≥ numDataItems then an InvalidPositionException must be thrown.
public void setData(int index, T newData) throws InvalidPositionException. Store newData at the node in position index of the list (see description of method getNode to see how indices are determined). If index
< 0 or index ≥ numDataItems then an InvalidPositionException must be thrown.
Class BoardGameLinked
This class represents the board game where the snake moves around eating apples. This class will have the following private instance variables:
int boardLength: the number of columns of the grid on the game board.
int boardWidth: the number of rows of the grid.
SnakeLinked theSnake: an object of the class SnakeLinked representing the snake.
DoubleList<String>[] board: an array of doubly linked lists. The first entry of the array is a reference to a linked list representing all the tiles in the first row of the game board; the second entry of the array is a reference to a linked list representing all the tile in the second row of the game board, and so on.
Each node of the doubly linked list referenced by board[i] represents one tile of the corresponding row of the game board. Each node of the doubly linked list stores one of the following strings:
“empty”
board
Figure 2. Data structure to represent the game board.
In this class you need to implement the following public methods:
public BoardGame(String boardFile): this is the same method as in Assignment 1, except that now the board game is stored in an object of the class BoardGameLinked and the snake is stored in an object of class SnakeLinked.
public String getObject(int row, int col) throws InvalidPositionException: returns the string stored in the node with index = col of the doubly linked list referenced by board[row]. For example, for the board in Figure 2 invoking getObject(1,1) must return “apple” and getBoard(0,0) must return “rock”. An InvalidPositionException must be thrown if row or col are negative, if row ≥ boardWidth, or if col ≥ boardLength.
public void setObject(int row, int col, String newObject) throws InvalidPositionException: stores newObject in the node with index = col of the doubly linked list referenced by board[row]. An InvalidPositionException must be thrown if row or col are negative, if row ≥ boardWidth, or if col ≥ boardLength.
public SnakeLinked getSnakeLinked(): returns theSnake.
public void setSnakeLinked(SnakeLinked newSnake): stores the value of newSnake in instance variable
theSnake.
public int getLength(): returns boardLength.
public int getWidth(): returns boardWidth.
Class SnakeLinked
The class stores the information about the snake as it moves around the board. This class will have two private instance variables:
int snakeLength: this is the number of tiles of the game board occupied by the snake. For example, the snake shown in Figure 1 has a length of 4.
DoubleList<Position> snakeBody: the positions of the tiles of the game board occupied by the snake will be stored in this doubly linked list. The position of the tile with the head of the snake will be stored in the first node of the list (the node referenced by instance variable head of class DoubleList); the position of the tile where the tail of the snake is, will be stored in last node of the list (the node referenced by rear). For example, for the snake in Figure 1, snakeBody will contain the following list:
head rear
(1,4)
(2,4)
(2,4)
object of class Position
Figure 3. Doubly linked list for the snake in Figure 1.
In this class you need to implement the following public methods.
public SnakeLinked(int row, int col): this is the constructor for the class; the parameters are the coordinates of the head of the snake. Initially the snake has length 1, so in this method the value of the
instance variable snakeLength will be set to 1. Instance variable snakeBody is initialized so it contains a DoubleList of nodes containing Position objects. An object of class Position will be created storing the values of row and col and this Position object will then be stored in the first node of the list.
public int getLength(): returns the value of instance variable snakeLength.
public Position getPosition(int index): returns the Position object stored in the node of the doubly linked list with the given index. So, if index = 0 the method returns the Position object in the first node of the list; if index = 1, it returns the Position object in the second node of the list, and so on. This method must return null if index < 0 or index >= snakeLength.
public boolean snakePosition(Position pos): returns true if pos is in the doubly linked list of snakeBody, and it returns false otherwise.
public Position newHeadPosition(String direction): returns the new position that the head of the snake would occupy if the snake moved in the direction specified by the parameter; note that this method does not actually move the snake. The values that direction can take are “right”, “left”, “up” and “down”. If, for example, the head of the snake is at (2,3) and direction is “right” then the new position would be (2,4); if direction is “down” then the new position would be (3,3). If the head is at (0,0) and direction is “up” the new position would be (-1,0).
public void moveSnakeLinked(String direction): moves the snake in the specified direction; this means that the doubly linked list in snakeBody must be updated so it contains the positions of the grid tiles that the snake will occupy after it moves in the direction specified by the parameter. For example, for the snake in Figure 1 the doubly linked list in snakeBody is shown in Figure 3. If direction = “up” then the new doubly liked list in snakeBody must be this
head rear
(1,5)
(1,4)
(1,4)
object of class Position
Figure 4. List resulting after moving the snake in Figure 3 up.
public void shrink(): decreases the value of snakeLength by 1 and deletes the last node in the doubly linked list of snakeBody.
void grow(String direction): increases the length of the snake by 1 and moves the snake’s head in the direction specified. This method is very similar to method moveSnake, except that a new node will be added to the doubly linked list of snakeBody. For example, if the snake is as shown in Figure 1, and direction = “up” then the new doubly linked list in snakeBody would be this
head rear
(1,5)
(1,4)
(2,4)
(1,4)
(2,4)
Figure 4. List resulting after moving the snake in Figure 3 up.
In all above classes you can implement more private methods, if you want to, but you cannot implement more public methods. You can also add more private instance variables, but only if they are required. The use of unnecessary instance variables will be penalized.
Download from the course’s webpage the following java classes: PlayChasingGame.java, Chaser.java, MyFileReader.java, and InvalidPositionException and all the image files needed to display the game board. If you are running the program from the terminal place all the files in the same directory and then compile the program by running javac PlayChasingGame.java and then run it with java PlayChasingGame or with java PlayChasingGame board_file_name. If you run the program from Eclipse, read the instructions in the Assignment 1 and Assignment 2.
To start the game press any of the arrow keys. The arrow keys can then be used to change the direction in which the snake moves. Additionally, you can type the following keys:
f: increases the speed of the snake
s: reduces the speed of the snake
p: pauses the game
x: terminates the game
d: prints some debugging information that you might find useful when testing and debugging your program. You must first pause the game to print this information.
Since when running this program, there will be actually several programs running at the same time each controlling a different part of the game, occasionally you might see that some of the tiles of the game board are erased due to timing conflicts between the programs; despite this you should still be able to keep playing the game and the missing tiles will eventually be redrawn.
i = 1; // initialize the value of i to 1 i = i + 1; // increase the value of i
if (i == j) // compare i and j
for (int i = 0; i < TABLE_SIZE; ++i) table[i] = 0;
private int method() {
int position;
then you should not put the bracket in a separate line for another method:
private String anotherMethod()
{
return personName;