Question: In this assignment we'll be using object oriented design to tackle the classic game format first introduced in the 1976 arcade game Blockade (Links to
In this assignment we'll be using object oriented design to tackle the classic game format first introduced in the 1976 arcade game Blockade (Links to an external site.), more commonly known today as "Snake". In a Snake game, the player controls a segmented "snake" that grows every time an object is eaten. This item appears in a random place on the screen after the previous item is eaten by the snake. Whenever the snake hits the side of the screen or itself, the game ends. There is no "winning" objective to the game, the goal is mostly to avoid the losing objective as long as possible and end the game with the longest possible snake. This game has been re-imagined many times over the years with new features, but the basic mechanics in general are almost always the same.
Your job is to build 3 classes to model the Snake game data, I need help writing the three classes item.java level.jave and snake.jave the other three classes are provided in the picture along with a unl diagram on how they interact 















the three given classes are as follows. direction.java
snakeapp.java
and gamestate.java as well as a driver file to run it.
//gamestate
package snake;
/**
* A simple type to represent the possible game states in Snake.
*
*
THIS ENUM DEFINITION IS PROVIDED AND SHOULD NOT BE EDITED.
*
* @author kreestman
*
*/
public enum GameState
{
/**
* Represents when the user has lost the game by allowing the Snake to hit the edge or hit itself
*/
LOST,
/**
* Represents when the game is still in progress.
*/
PLAYING
}
//direction
package snake;
/**
* A simple type to represent the directions the Snake can travel.
*
*
THIS ENUM DEFINITION IS PROVIDED AND SHOULD NOT BE EDITED.
*
* @author kreestman
*
*/
public enum Direction
{
/**
* Represents the snake moving upwards on the screen.
*/
UP,
/**
* Represents the snake moving downwards on the screen.
*/
DOWN,
/**
* Represents the snake moving right on the screen.
*/
RIGHT,
/**
* Represents the snake moving left on the screen.
*/
LEFT;
}
//snakeapp
package snake;
/**
* This class is a small stub class that is solely responsible
* for kicking off the Snake graphical user interface
* which is included through a pre-compiled library.
*
* @author kreestman
*
*/
public class SnakeApp
{
/**
* Initializes the graphical user interface for the game.
*
* @param args Command line arguments, which are not used.
*/
public static void main(String[] args)
{
SnakeGUI.main(args);
}
}
//snakedriver
package snake;
import java.util.Random;
public class SnakeDriver
{
public static void main(String[] args)
{
/* Some ideas for testing Item, uncomment after writing Item and modify to address problems you encounter */
/*
Item i1 = new Item();
Item i2 = new Item(10,30);
System.out.println("Item i1: " + i1.toString());
System.out.println("Item i2: " + i2.toString());
i1.setNewLocation(159, 371);
System.out.println("Item i1: " + i1.toString());
*/
/* Some ideas for testing Snake, uncomment after writing Snake and modify to address problems you encounter */
/*
Snake s = new Snake();
int[][] smap = s.getSnake();
System.out.println("Snake's head is at (" + s.getHeadX() + "," + s.getHeadY() + ")");
System.out.println("Snake's length is " + s.getLength());
System.out.println("Snake's direction is " + s.getDirection());
for (int i = 0; i
{
System.out.println("Snake at position " + i + " is (" + smap[i][0] + "," + smap[i][1] + ")");
}
s.changeDir(Direction.RIGHT);
System.out.println("Snake's direction is " + s.getDirection());
s.move();
for (int i = 0; i
{
System.out.println("Snake at position " + i + " is (" + smap[i][0] + "," + smap[i][1] + ")");
}
*/
/* Some ideas for testing Level, uncomment after writing Level and modify to address problems you encounter */
/*Level lev = new Level(50, 50, new Random(1));
lev.generateMap();
int[][] map = lev.getMap();
for(int row = 0; row
{
for(int col = 0; col
{
System.out.println("Level at position (" + col + "," + row + ") is " + map[row][col]);
}
}
int[][] smap = lev.getSnake().getSnake();
for (int i = 0; i
{
System.out.println("Snake at position " + i + " is (" + smap[i][0] + "," + smap[i][1] + ")");
}
*/
}
}
SnakeApp enumerations GameState enumeratione Direction Level Hem Snake Level.java Snake.java X GameState.java Item.java 1+ \/ COURSE: CSCI16201. 6 7 package snake; 8 9 public class Snake 10 { 11 12 } 13 Class Snake java.lang.Object o snake.Snake public class Snake extends java.lang. Object This class defines the Snake object for use in the Snake game. Author: kylereestman o Constructor Summary Constructors Constructor Description Snake() The default constructor for the Snake. Snake(int headxIn, int heady In) The specific constructor for the Snake. Method Summary All Methods Instance Methods Concrete Methods Modifier and Type Method Description void changeDir (Direction dirin) Changes the Direction of the Snake, except if the new Direction is directly opposite of the current one. Direction getDirection() The current Direction of the Snake. int getHeadx() Returns the current X value for the head of the Snake. int getHeady() Returns the current Y value for the head of the Snake. int getLength() The current length value of the Snake. int int int(0) boolean void void getHeady() getLength() getSnake hitself) increaseLength() move Returns the current Y value for the head of the Snake. The current length value of the Snake. Returns the current Snake 2d array. Determines if the head of the Snake has hit another part of the Snake. Increases the current length of the Snake by 1 and grows the snake. The move method should move the Snake forward one space in the current Direction. . Methods inherited from class java.lang.Object equals, getclass, hashCode, notify, notifyAli, toString, wait, wait, wait o Constructor Detail Snake public Snake) The default constructor for the Snake. It should do the same as the specific constructor except that it will use the value of 20 as the Default value for both the X and Y values for the head as the parameters. Snake public Snake(int headxIn, int headeIn) The specific constructor for the Snake. The snake 2d array should be initialized with a size of [5000][2]. The snake should have its head's X and Y values set. The snake should then form ou of the head to an initial length of 4. When forming out, this is the order that it should expand: 1) Above the head; if no space to continue doing so 2) Left of the head; if no space to continue doing so 3) Below the head We will not place the initial Snake too close to the top left corner so that cannot grow to its initial size. The initial direction of the snake should be DOWN. Parameters: headxIn -- the initial X value for the head of the Snake. heady In -- the initial Y value for the head of the Snake. o Method Detail get HeadX public int getHeadx() Returns the current X value for the head of the Snake. Returns: - The current X value for the head of the Snake. getHeady public int getHeady) Returns the current Y value for the head of the Snake. Returns: - The current Y value for the head of the Snake. I move public void move() The move method should move the Snake forward one space in the current Direction. HINT: Only the head will be in a "new" position, the rest will continue to follow the further up porti of the snake body. hitself public boolean hitself() Determines if the head of the Snake has hit another part of the Snake. Returns: true if the Snake hit itself changeDir public void changeDir(Direction dirin) Changes the Direction of the Snake, except if the new Direction is directly opposite of the current one. Parameters: dirIn -- the Direction that the Snake is trying to change to be facing getDirection public Direction getDirection() The current Direction of the Snake. Returns: - The current Direction of the Snake. getDirection public Direction getDirection() The current Direction of the Snake. Returns: - The current Direction of the Snake. getLength public int getLength() The current length value of the Snake. Returns: - The current length value of the Snake. getSnake public int[][] get Snake() Returns the current Snake 2d array. Returns: - The current Snake 2d array. increaseLength public void increaseLength() Increases the current length of the Snake by 1 and grows the snake. This means that it also sets the newly added part of the tail (n) to the value of the previous tail (n-1) values in the Snake. Package snake Class Level java.lang.Object snake.Level public class Level extends java.lang.Object This class defines the Level for use in the Snake game. It will control major portions of the game like updating the Snake and Item positions and determining when the game has been lost. Author: kylereestman Constructor Summary Constructors Constructor Description Level(int widthIn, int heightIn, java.util.Random randIn) Creates a default level with a given dimension. Method Summary All Methods Instance Methods Concrete Methods Modifier and Type Method Description void generateMap Generates the 2-dimensional map of the game area. GameState getGameState() Returns the current GameState. Item getItem() Returns the current Item. int[0 getMap Returns the current ad array map. Snake getSnake() Returns the current Snake. void updateOneStep() Updates a single step. Methods inherited from class java.lang.Object equals, getClass, hashCode, notify, notifyAli, toString, wait, wait, wait Constructor Detail Level public Level(int widthIn, int heightIn, java.util.Random randIn) Creates a default level with a given dimension. The map should use the given height and width values as its dimensions. The Snake and Item should be be the default version of themselves. The Random object passed in should be used in creation of new positions for the Item. The GameState should initially be PLAYING. Parameters: widthIn --The logical width of the new level. heightIn -- The logical height of the new level. randIn -- The Random object that will be used to generate new Item locations. Method Detail updateOne Step public void updateOneStep() Updates a single step. The algorithm you use should go as follows: 1) Move the Snake in its current direction 2) Determine if a losing condition has been met (the Snake hitting itself or a wall) 3) Determine if the Item has been grabbed by the Snake. If it has, generate the new position for the Item. Generate the X value for the new Item position, then the Y value for the new Item position. If the position generated is currently being occupied by the Snake, regenerate another position and check again. Do this until a valid position has been found. If the Item was grabbed, be sure to increase the length of the Snake as well. generate Map public void generateMap) Generates the 2-dimensional map of the game area. Please read the supplemental document on the differences between how the game map should look vs the Snake. The following pattern should be used for the map: If the space is open, place a o in that place in the map If the space is occupied by the Snake, place a 1 in that place in the map If the space is occupied by the Item, place a-1 in that place in the map getItem public Item getItem() Returns the current Item. Returns: - The current Item. getSnake public Snake getSnake() Returns the current Snake. Returns: - The current Snake. getMap public int getMap) Returns the current ad array map. Returns: - The current ad array map. getGameState public GameState getGameState() Returns the current GameState. Returns: - The current GameState. 1+ // COURSE: CSCI1620.. 6 7 package snake; 8 9 public class Item 10 { 11 12} 13 | Class Item java.lang.Object o snake.Item . public class Item extends java.lang.Object This class defines the Item object for use in the Snake game. Author: kylereestman Constructor Summary Constructors Constructor Description Item() The default constructor for an Item. Item(int itemXIn, int itemyIn) The specific constructor for an Item. o Method Summary All Methods Instance Methods Concrete Methods Modifier and Type Method Description int getX() Returns the current X value for the Item. int getY() The current Y value for the Item. void setNewlocation(int itemXIn, int itemyIn) Sets a new location for the item as long as both values are valid (>=0). java.lang. String toString() Returns a String representing the item. . Methods inherited from class java.lang.Object equals, getClass, hashcode, notify, notifyAll, wait, wait, wait o Constructor Detail Item public Item() The default constructor for an Item. It sets the default location of the Item at position 25,25. Item public Item(int itemXIn, int itemYIn) The specific constructor for an Item. It sets the location of the Item using the passed in parameters. Parameters: itemXIn -- the supposed starting X value for the Item. itemy In -- the supposed starting Y value for the Item. o Method Detail .getX public int getX() Returns the current X value for the Item. Returns: The current X value for the Item. getY public int getY() The current Y value for the Item. Returns: The current Y value for the Item. .getY public int getY() The current Y value for the Item. Returns: - The current Y value for the Item. setNewLocation public void setNewLocation(int itemXIn, int itemYIn) Sets a new location for the item as long as both values are valid (>=0). Parameters: itemXIn -- the supposed new X value for the Item. itemy In -- the supposed new Y value for the Item. .toString public java.lang.String toString() Returns a String representing the item. Overrides: toString in class java.lang.Object Returns: The String representation of the Item where X and Y are the item's X and Y values respectively e.g. The item is located at position (X, Y) * * Direction.java x Item.java Level.java Snake App.java 16 // COURSE: CSCI1620 11 12 package snake; 13 140 /** 15 * A simple type to represent the directions the Snake can travel. 16 17 *
THIS ENUM DEFINITION IS PROVIDED AND SHOULD NOT BE EDITED. 18 19 * Gauthor kreestman 20 21 */ 22 public enum Direction 23 { 240 /** 25 * Represents the snake moving upwards on the screen. 26 */ 27 28 29 /** 30 * Represents the snake moving downwards on the scree reen. 31 */ 32 DOWN, 33 340 /** 35 * Represents the snake moving right on the screen. 36 */ 37 38 39 40 * Represents the snake moving left on the screen. 41 */ 42 LEFT; 43 } UP RIGHT, M Direction.java Item.java Level.java Snake App.java X 16 // COURSE: CSCI1620 12 13 package snake; 14 150 /** 16 * This class is a small stub class that is solely responsible 17 * for kicking off the Snake graphical user interface 18 * which is included through a pre-compiled library. 19 20 * @author kreestman 21 22 27 23 public class SnakeApp 24 { 250 26 * Initializes the graphical user interface for the game. 28 * @param args Command line arguments, which are not used. */ 300 public static void main(String[] args) 31 { 32 SnakeGUI.main(args); } 34 } 29 SnakeApp enumerations GameState enumeratione Direction Level Hem Snake Level.java Snake.java X GameState.java Item.java 1+ \/ COURSE: CSCI16201. 6 7 package snake; 8 9 public class Snake 10 { 11 12 } 13 Class Snake java.lang.Object o snake.Snake public class Snake extends java.lang. Object This class defines the Snake object for use in the Snake game. Author: kylereestman o Constructor Summary Constructors Constructor Description Snake() The default constructor for the Snake. Snake(int headxIn, int heady In) The specific constructor for the Snake. Method Summary All Methods Instance Methods Concrete Methods Modifier and Type Method Description void changeDir (Direction dirin) Changes the Direction of the Snake, except if the new Direction is directly opposite of the current one. Direction getDirection() The current Direction of the Snake. int getHeadx() Returns the current X value for the head of the Snake. int getHeady() Returns the current Y value for the head of the Snake. int getLength() The current length value of the Snake. int int int(0) boolean void void getHeady() getLength() getSnake hitself) increaseLength() move Returns the current Y value for the head of the Snake. The current length value of the Snake. Returns the current Snake 2d array. Determines if the head of the Snake has hit another part of the Snake. Increases the current length of the Snake by 1 and grows the snake. The move method should move the Snake forward one space in the current Direction. . Methods inherited from class java.lang.Object equals, getclass, hashCode, notify, notifyAli, toString, wait, wait, wait o Constructor Detail Snake public Snake) The default constructor for the Snake. It should do the same as the specific constructor except that it will use the value of 20 as the Default value for both the X and Y values for the head as the parameters. Snake public Snake(int headxIn, int headeIn) The specific constructor for the Snake. The snake 2d array should be initialized with a size of [5000][2]. The snake should have its head's X and Y values set. The snake should then form ou of the head to an initial length of 4. When forming out, this is the order that it should expand: 1) Above the head; if no space to continue doing so 2) Left of the head; if no space to continue doing so 3) Below the head We will not place the initial Snake too close to the top left corner so that cannot grow to its initial size. The initial direction of the snake should be DOWN. Parameters: headxIn -- the initial X value for the head of the Snake. heady In -- the initial Y value for the head of the Snake. o Method Detail get HeadX public int getHeadx() Returns the current X value for the head of the Snake. Returns: - The current X value for the head of the Snake. getHeady public int getHeady) Returns the current Y value for the head of the Snake. Returns: - The current Y value for the head of the Snake. I move public void move() The move method should move the Snake forward one space in the current Direction. HINT: Only the head will be in a "new" position, the rest will continue to follow the further up porti of the snake body. hitself public boolean hitself() Determines if the head of the Snake has hit another part of the Snake. Returns: true if the Snake hit itself changeDir public void changeDir(Direction dirin) Changes the Direction of the Snake, except if the new Direction is directly opposite of the current one. Parameters: dirIn -- the Direction that the Snake is trying to change to be facing getDirection public Direction getDirection() The current Direction of the Snake. Returns: - The current Direction of the Snake. getDirection public Direction getDirection() The current Direction of the Snake. Returns: - The current Direction of the Snake. getLength public int getLength() The current length value of the Snake. Returns: - The current length value of the Snake. getSnake public int[][] get Snake() Returns the current Snake 2d array. Returns: - The current Snake 2d array. increaseLength public void increaseLength() Increases the current length of the Snake by 1 and grows the snake. This means that it also sets the newly added part of the tail (n) to the value of the previous tail (n-1) values in the Snake. Package snake Class Level java.lang.Object snake.Level public class Level extends java.lang.Object This class defines the Level for use in the Snake game. It will control major portions of the game like updating the Snake and Item positions and determining when the game has been lost. Author: kylereestman Constructor Summary Constructors Constructor Description Level(int widthIn, int heightIn, java.util.Random randIn) Creates a default level with a given dimension. Method Summary All Methods Instance Methods Concrete Methods Modifier and Type Method Description void generateMap Generates the 2-dimensional map of the game area. GameState getGameState() Returns the current GameState. Item getItem() Returns the current Item. int[0 getMap Returns the current ad array map. Snake getSnake() Returns the current Snake. void updateOneStep() Updates a single step. Methods inherited from class java.lang.Object equals, getClass, hashCode, notify, notifyAli, toString, wait, wait, wait Constructor Detail Level public Level(int widthIn, int heightIn, java.util.Random randIn) Creates a default level with a given dimension. The map should use the given height and width values as its dimensions. The Snake and Item should be be the default version of themselves. The Random object passed in should be used in creation of new positions for the Item. The GameState should initially be PLAYING. Parameters: widthIn --The logical width of the new level. heightIn -- The logical height of the new level. randIn -- The Random object that will be used to generate new Item locations. Method Detail updateOne Step public void updateOneStep() Updates a single step. The algorithm you use should go as follows: 1) Move the Snake in its current direction 2) Determine if a losing condition has been met (the Snake hitting itself or a wall) 3) Determine if the Item has been grabbed by the Snake. If it has, generate the new position for the Item. Generate the X value for the new Item position, then the Y value for the new Item position. If the position generated is currently being occupied by the Snake, regenerate another position and check again. Do this until a valid position has been found. If the Item was grabbed, be sure to increase the length of the Snake as well. generate Map public void generateMap) Generates the 2-dimensional map of the game area. Please read the supplemental document on the differences between how the game map should look vs the Snake. The following pattern should be used for the map: If the space is open, place a o in that place in the map If the space is occupied by the Snake, place a 1 in that place in the map If the space is occupied by the Item, place a-1 in that place in the map getItem public Item getItem() Returns the current Item. Returns: - The current Item. getSnake public Snake getSnake() Returns the current Snake. Returns: - The current Snake. getMap public int getMap) Returns the current ad array map. Returns: - The current ad array map. getGameState public GameState getGameState() Returns the current GameState. Returns: - The current GameState. 1+ // COURSE: CSCI1620.. 6 7 package snake; 8 9 public class Item 10 { 11 12} 13 | Class Item java.lang.Object o snake.Item . public class Item extends java.lang.Object This class defines the Item object for use in the Snake game. Author: kylereestman Constructor Summary Constructors Constructor Description Item() The default constructor for an Item. Item(int itemXIn, int itemyIn) The specific constructor for an Item. o Method Summary All Methods Instance Methods Concrete Methods Modifier and Type Method Description int getX() Returns the current X value for the Item. int getY() The current Y value for the Item. void setNewlocation(int itemXIn, int itemyIn) Sets a new location for the item as long as both values are valid (>=0). java.lang. String toString() Returns a String representing the item. . Methods inherited from class java.lang.Object equals, getClass, hashcode, notify, notifyAll, wait, wait, wait o Constructor Detail Item public Item() The default constructor for an Item. It sets the default location of the Item at position 25,25. Item public Item(int itemXIn, int itemYIn) The specific constructor for an Item. It sets the location of the Item using the passed in parameters. Parameters: itemXIn -- the supposed starting X value for the Item. itemy In -- the supposed starting Y value for the Item. o Method Detail .getX public int getX() Returns the current X value for the Item. Returns: The current X value for the Item. getY public int getY() The current Y value for the Item. Returns: The current Y value for the Item. .getY public int getY() The current Y value for the Item. Returns: - The current Y value for the Item. setNewLocation public void setNewLocation(int itemXIn, int itemYIn) Sets a new location for the item as long as both values are valid (>=0). Parameters: itemXIn -- the supposed new X value for the Item. itemy In -- the supposed new Y value for the Item. .toString public java.lang.String toString() Returns a String representing the item. Overrides: toString in class java.lang.Object Returns: The String representation of the Item where X and Y are the item's X and Y values respectively e.g. The item is located at position (X, Y) * * Direction.java x Item.java Level.java Snake App.java 16 // COURSE: CSCI1620 11 12 package snake; 13 140 /** 15 * A simple type to represent the directions the Snake can travel. 16 17 *
THIS ENUM DEFINITION IS PROVIDED AND SHOULD NOT BE EDITED. 18 19 * Gauthor kreestman 20 21 */ 22 public enum Direction 23 { 240 /** 25 * Represents the snake moving upwards on the screen. 26 */ 27 28 29 /** 30 * Represents the snake moving downwards on the scree reen. 31 */ 32 DOWN, 33 340 /** 35 * Represents the snake moving right on the screen. 36 */ 37 38 39 40 * Represents the snake moving left on the screen. 41 */ 42 LEFT; 43 } UP RIGHT, M Direction.java Item.java Level.java Snake App.java X 16 // COURSE: CSCI1620 12 13 package snake; 14 150 /** 16 * This class is a small stub class that is solely responsible 17 * for kicking off the Snake graphical user interface 18 * which is included through a pre-compiled library. 19 20 * @author kreestman 21 22 27 23 public class SnakeApp 24 { 250 26 * Initializes the graphical user interface for the game. 28 * @param args Command line arguments, which are not used. */ 300 public static void main(String[] args) 31 { 32 SnakeGUI.main(args); } 34 } 29
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
