Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello I need help with this assignment, the instructions are bellow and I will also leave the Snake code bellow, thank you in advance. 1

Hello I need help with this assignment, the instructions are bellow and I will also leave the Snake code bellow, thank you in advance.
1.Implement an immutable 2D Point class. In the constructor, you should be initializing xcoord and ycoord properties and provide getters but no setters for these two properties.
2.In the Snake class, set its initial location to a new Point with x and y coordinates of 0.
3.In the Snake class, introduce turnLeft and turnRight methods. These methods will now need else if statements to appropriately adjust the direction from the four possibilities (left, right, up, and down). Leave your turn method in the code but mark it as deprecated in TypeDoc meaning it should no longer be used.
4.Modify the move method to also include if/else if/else if/else statements for each of the four directions. Because points are immutable (no setters), the move method will need to construct new points from the existing ones each time and need to adjust the x or y coordinates by positive or negative 1, depending on the Snake's direction. To do this, you would use this.position = new Point(_________,__________) filling in the blanks with this.position.x and this.position.y (including a +/-1 for the coordinate that's changing). Note: The Snake class is accessing the Point class's getters x and y; it should not access its private data variables xcoord and ycoord directly.
5.Next, implement a WorldModel class which will for now contain one Snake. The class should be an aggregation of a Snake, not a composition so you should create the Snake outside of the class and pass it to the constructor.
6.Add a method update(steps) to the WorldModel class, which will call its one Snake's move method, passing an argument of steps and a getter to retrieve the Snake.
7.Update the tests in Snake.test.js to use your new methods correctly. Also, create a new file and add tests in WorldModel.test.ts in which you call the WorldModel's update method. Tell the Snake to turn. Call the update method again. See if the Snake's positions are what you expect them to be. Check the tests terminal to make sure all the tests pass.
8.After creating a new Snake, create a new WorldModel, passing this Snake to the constructor.
9.DOCUMENT ALL METHODS/CLASSES and run the command to generate the documentation.
import display from "./display";
// import display from "./display";
// place your code on line 5 above the export statement below
class Snake {
private currentPosition: number;
private currentDirection: number;
constructor(){
this.currentPosition =0;
this.currentDirection =1;
}
move(distance: number){
if (this.currentPosition ===1){
this.currentPosition += distance;
} else {
this.currentPosition -= distance;
}
}
turn(){
this.currentDirection *=-1;
}
get position(): number {
return this.currentPosition;
}
}
export default Snake;

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

Database Design Application Development And Administration

Authors: Michael V. Mannino

4th Edition

0615231047, 978-0615231044

More Books

Students also viewed these Databases questions

Question

Organizing Your Speech Points

Answered: 1 week ago