Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA programming PART - III: Caterpillar Game (30 points) In PART-III, you will write a small caterpillar game. The figure below shows a 2D map

JAVA programming

image text in transcribedimage text in transcribedimage text in transcribed

PART - III: Caterpillar Game (30 points) In PART-III, you will write a small caterpillar game. The figure below shows a 2D map with a caterpillar (blue) and food (red). The caterpillar can move around in this 2D map. Whenever the caterpillar's head reaches the food, it can "eat" the food and grow one cell bigger. You will write a small program that moves the caterpillar, and the final goal is to eat everything on the map. Figure 2: The caterpillar (blue) and the food (red) in a 2D map. The xy-coordinate of the top-left corner is (0,0), and the xy-coordinate of the bottom-right corner is (15,15). [ 3 points] Write a Region class that represents a 2D world. The Region class has the following private fields: - A int indicating the minimum x-position - A int indicating the minimum y-position - A int indicating the maximum x-position - A int indicating the maximum y-position The Region class has the following public methods: - A constructors with 4 inputs as int which indicates minimum x position, minimum y position, maximum x position, and maximum y position (in exactly this order). - A contains method that takes an input as Position. The method returns true if the position is within the range of the Region and returns false otherwise. For example, if the range is x[0,5],y[3,6], then the minimum x and y positions are 0 and 3 , and the maximum x and y positions are 5 and 6 (inclusive). The method contains should return true if the input is (0,6) and return false if the input is (10,11). [10 points] Write a Caterpillar class that extends MyDoublyLinkedList . A Caterpillar is represented as a MyDoublyLinkedList that completely determines the positions of the body of a caterpillar within a 2D map. The Caterpillar class must have the following public methods: - A constructor that takes no input and sets the initial state of the caterpillar at the beginning of a game. When the game starts, the initial size of the caterpillar should be 1 and its initial position is at (7,7). - A getHead method that returns a Position object representing the head position of the caterpillar. - An eat method that takes as input a Position and adds the input to the front of the list, making this be the new position for the head of the caterpillar. If the input position is not orthogonally adjacent to the current head position, throw an IllegalArgumentException. - A move method that takes as input a Position, adds the input to the front of the list, and removes one element from the back of the list. If the input position is not adjacent to the current head position, throw an IllegalArgumentException. - A selfCollision method that takes as input a Position and returns true if the input overlaps with one of the body parts, and returns false otherwise. [2 points] Write a GameState enumeration. The GameState enumeration contains the following labels: WALL_COLLISION, SELF_COLLISION, NO_MORE_ACTION, EAT, MOVE, DONE. [15 points] Write a World class. The World class should have the following private fields (You can add more private fields whenever fits): - A Caterpillar that represents the caterpillar. - A Position that indicates the current food position. - A Region that represents the 2D map where the caterpillar can move. - A Actionqueue that stores a list of actions to take for the caterpillar. - A TargetQueue that stores a list of food positions that the caterpillar should travel to. - A GameState which indicates the current state of the game. The World class must also have the following public methods: - A constructor that takes two input arguments; the first argument is a TargetQueue and the second argument an ActionQueue. The constructor uses the inputs to initialize the appropriate fields. The Region of the game is set as x[0,15],y[0,15], and the caterpillar is initialized with its initial state (its size is 1 , and it is positioned at (7,7) ). Before the game starts, dequene the food position from the TargetQueue, and set the GameState to MDVE. You can assume that the game will start with at least one food to be eaten by the caterpillar. A step method that makes a step in the game. Here are the procedures: - First, take the next heading direction from the ActionQueue. If the Actionqueue is empty, set the GameState to NO_MORE_ACTION. - Second, if the GameState is not MOVE or EAT, return. - Then, get the current head position of the caterpillar and calculate the next head position assuming it is moving according to the heading direction. For example, if the head position of the caterpillar is (5,7) and the direction is EAST, the next position will be (6,7). Depending on the next position calculated above, You will need to handle the following scenarios: * If moving to the next position will result in the caterpillar moving out of the map, set the GameState to WALL_COLLISION. * If moving to the next position will result in a self-collision of the caterpillar, set the GameState to SELF_COLLISION. * If the food is located at the next position, the caterpillar can "eat" it. If there is not more food on the TargetQueue, the caterpillar must have eaten everything, and we can set the GameState to DONE. Otherwise, dequeue the next food position from the TargetQueue and set the GameState to EAT. * Otherwise, move the caterpillar to the next position and set the GameState to MOVE. - A getState method that returns the current state of the game as a GameState. - A getCaterpillar method that returns the caterpillar as a Caterpillar object. - A getFood method that returns the food position as a Position object. - An isRunning method that returns true if the game is still running (MOVE or EAT) or false if the game is over

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases And Information Systems 22nd European Conference Adbis 2018 Budapest Hungary September 2 5 2018 Proceedings Lncs 11019

Authors: Andras Benczur ,Bernhard Thalheim ,Tomas Horvath

1st Edition

3319983970, 978-3319983974

More Books

Students also viewed these Databases questions