Question
Create a new class, SnakeGame, which will be used to calculate someones final score. You will be given the game board and the position of
Create a new class, SnakeGame, which will be used to calculate someones final score. You will be given the game board and the position of the snakes head, the goal will be to find the position of the snakes tail, and the final length. You will do this by doing both an exhaustive search and recursive search as described below. You will need to also create unit tests using JUnit that will test the two methods correctness and the running time (at least 5 for each method independently, or 10 that test both methods at once). Each test should have a comment describing what is being tested. Note the access labeling each element and method below.
Elements
- private bool[][] game -- Stores the final game state, cells that are true contain a part of the snake, and false are the background. Note that in this version of the game the snake will always have a barrier of one cell (i.e. every neighborhood of 9 cells around a portion of the snake will only ever have at most 3 true cells).
- private int[] headPosition -- Stores the location of the snake's head.
- private static int exhaustiveChecks -- counts the number of positions checked when performing the tail search using exhaustive enumeration, across all instances of the SnakeGame.
- private static int recursiveChecks -- counts the number of positions checked when performing the tail search using recursive search, across all instances of the SnakeGame.
Constructors
- the default constructor, which initializes an empty 1 x 1 gameboard, and
- a constructor that takes a 2-dimensional boolean array, and the x and y position of the snakes "head".
Methods
- public int[] findTailExhaustive() -- will find the tail of the snake by searching across the whole grid to find the grid position where a true element is surrounded by only one other true cell (see figure below), but is not the head, and return 3 items: the x and y position of the tail in the grid, and the length of the snake on the board. Incremenets the exhaustiveChecks counter with each (x',y') cell that is examined.
- public int[] findTailRecursive() -- will find the tail of the snake by conducting a search starting at the head location and recursively following the snake's body, and return 3 items: the x and y position of the tail in the grid, and the length of the snake on the board. Increments the recursiveChecks counter with each (x',y') cell that is examined.
- private int[] findTailRecursive(int[] currentPosition, int[] previousPosition) -- overloads the previous method, and is similar in definition, but starts at a position other than the head position (used for the recursive calls), also takes in the position of the previous body position (to exclude it from deciding the next position). Increments the recursiveChecks counter with each (x',y') cell that is examined. Hint: the call for starting from the head position made from the public method should be findTailRecursive(headPosition, headPosition).
- private void resetCounters() -- resets both the exhaustiveChecks and recursiveChecks counters to 0.
- private static int getRecursiveChecks() -- gets the current state of the recursiveChecks counter.
- private static int getExhaustiveChecks() -- gets the current state of the exhaustiveChecks counter.
I don't need similar implements.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started