Zookal
Zookal

We’d love to hear any feedback or comment from you!

© 2011-2021 Zookal Pty Ltd

View question and answer

From our collection of questions and answers
Engineering · Computer Science
Question details

package Game;

import java.util.Random;

public class Apple {

private int x; // The x coordinate of the Apple

private int y; // The y coordinate of the Apple

private int effect; // The effect of the Apple (grow (when > 0) or shrink (when -1))

// DO NOT EDIT BELOW

public int getX() {

return x;

}

public int getY() {

return y;

}

public int getEffect() {

return effect;

}

// DO NOT EDIT ABOVE

/**

*

* setEffect (2 marks)

*

* This method should set the instance variable effect.

*

* The effect should be set randomly.

*

* The effect should be set to 1, 50% of the time.

*

* The effect should be set to 2, 25% of the time.

*

* The effect should be set to -1, 25% of the time.

*

*/

public void setEffect() {

//To Be Completed

}

/**

* Apple(int,int) (3 marks)

*

@param w - the width of the screen

@param h - the height of the screen

*

* This constructor should select a random position for the Apple on the screen.

* It should also set the effect variable.

*

* The x and y instance variables must be set to points within the borders of the game.

*

* Imagine the game screen as a grid of 20x20 rectangles, the x and y variables should

* correspond to the center of those rectangles.

*

* For example if the screen is 800x600, some possible values of x and y are:

*

* (20,20) (20,40) (80,100) (560,700) (400,300) (780,580)

*

*/

public Apple(int w, int h) {

// To be completed

}

/**

* DIFFICULT QUESTION (leave to the end)

*

* Apple(int,int,Snake) (4 marks)

*

* You will be awarded another 2 marks if you implement this constructor correctly within Game.java.

*

@param w - the width of the screen

@param h - the height of the screen

@param s - a Snake object

*

* This constructor should select a random position on the screen for the Apple.

*

* The Apple MUST NOT overlap the snake.

*

* Hint: answer getFreePositions in Snake.java first.

*

*/

public Apple(int w, int h, Snake s) {

// To be completed

}

// DO NOT EDIT BELOW

public boolean eat(Snake s) {

return s.eat(this);

}

}

package Game;

import java.util.ArrayList;

public class Game {

private ArrayList<Apple> apples;

private ArrayList<Score> scores;

private int score;

private Snake s;

private int page;

// DO NOT EDIT BELOW

public Snake getSnake() {

return s;

}

public ArrayList<Apple> getApples() {

return apples;

}

public ArrayList<Score> getScores() {

return scores;

}

public int getCurrScore() {

return score;

}

public int getPage() {

return page;

}

public void setPage(int page) {

this.page = page;

}

// DO NOT EDIT ABOVE

/**

* Game(int,int) (2 marks)

*

@param width - the width of the screen

@param height - the height of the screen

*

* This constructor should call the startGame method

* and store a new ArrayList of Score objects into the

* instance variable scores.

*

*/

public Game(int width, int height) {

// To be completed

startGame(width, height);

}

/**

* startGame(int,int) (3 marks)

*

@param width - the width of the screen

@param height - the height of the screen

*

* This method will run whenever a new game starts.

*

* It should:

* Create a new Snake object and store it in the instance variable s.

* Create a new ArrayList of Apple objects and store it in the instance variable apples.

* Add an Apple to the ArrayList apples.

* Set the instance variable score to 0.

* Set the page to 0.

*

* Hint: your answer should only be 5 lines long.

*/

public void startGame(int width, int height) {

// To be completed

}

/**

* step(int,int) (10 marks)

*

@param width - the width of the screen

@param height - the height of the screen

*

* This method will be executed every frame.

*

* The method should:

*

* Move the snake

* Call the gameOver method if the snake hits itself or the walls

* Check if the Snake has eaten any Apples

* If the Snake ate an Apple then:

* The score should increase by 1

* The Apple should be reset

* Add a Apple object to apples if the score is a multiple of 5

*

* Hint: complete move, hitSelf, hitWalls and eat in Snake.java first.

*

*/

public void step(int w, int h) {

// To be completed

}

/**

* sortScores(boolean) (5 marks)

*

@param asc - whether to sort in ascending or descending order.

*

* This method should sort all the Score objects in scores in either

* ascending or descending order.

*

* When the parameter variable asc is true, it should sort in ascending order.

*

* When the parameter variable asc is false, it should sort in descending order.

*

*/

public void sortScores(boolean asc) {

// To be completed

}

// DO NOT EDIT BELOW

public void gameOver() {

page = 2; // Move to Game Over screen

s.allRed(s.getHead()); // Make the snake red

}

public void addScore(String name) {

scores.add(new Score(name, score)); // Add the new score to scores

sortScores(false); // Sort the scores in descending order

}

}

package Game;

public class Snake {

private Body head; // the head of the Snake

// The horizontal direction of the Snake

private int dirX; // 1 (right), -1 (left) or 0

// The vertical direction of the Snake

private int dirY; // 1 (down), -1 (up) or 0

// DO NOT EDIT BELOW

public Body getHead() {

return head;

}

public void setDir(int dirX,int dirY) {

this.dirX=dirX;

this.dirY=dirY;

}

// DO NOT EDIT ABOVE

/**

* getSize (2 marks)

*

@return number of Body objects within the Snake

*

*/

public int getSize() {

// To be completed

return -1;

}

/**

* getSnakeBody (4 marks)

*

* This method is used to draw the Snake within Client.java.

*

@return an array which contains all the snake's Body objects.

*

*/

public Body[] getSnakeBody(){

// To be completed

return null;

}

/**

* Snake(int,int) (4 marks)

*

@param w - the width of the screen

@param h - the height of the screen

*

* This constructor must set the head of the Snake to be

* a new Body object positioned in the center of the screen.

*

* Three more Body Objects must be added below the head of the Snake

*

* The snake's direction must also be set so that it moves upwards. (Look at setDir above)

*

* (This is so that when the Game starts the Snake consists of 4 parts in total)

*

* Hint: your solution should be around 6 lines long including a loop.

*

*/

public Snake(int w, int h) {

// To be completed

}

/**

* move (10 marks)

*

* This method must move the snake.

*

* The head of the snake must move 20 pixels according to dirX and dirY.

*

* The Body object after head must take the old co-ordinates of the head.

*

* This process must then be repeated.

*

* That is, the co-ordinates of the next Body object must be set to the old

* co-ordinates of the previous Body object.

*

* Hint: you only need 1 loop here.

*/

public void move() {

// To be completed

}

/**

* grow(int) (10 marks)

*

@param size - the number of Body objects to add to the snake.

*

* This method should add Body objects to the end of the snake.

*

* The co-ordinates of the new Body object should match the co-ordinates

* of the last Body object in the snake.

*

*/

public void grow(int size) {

// To be completed

}

/**

* shrink (2 marks)

*

* This method should reduce the length of the snake by 1 Body object.

*

* The length of the snake should only be reduced if the size of the snake

* is greater than 3.

*

*/

public void shrink() {

// To be completed

}

/**

* hitSelf (4 marks)

*

@return true if the snake's head hits any other part of its body.

* false otherwise.

*/

public boolean hitSelf() {

// To be completed

return false;

}

/**

* hitWalls (2 marks)

*

@param w - the width of the screen

@param h - the height of the screen

*

@return true if the snake's head hits or passes the edges of the screen.

* false otherwise.

*

*/

public boolean hitWalls(int w, int h) {

// To be completed

return false;

}

/**

* eat(Apple) (4 marks)

*

@param a - the Apple to check collision with

*

* This method should check to see whether the head of the snake collides

* with the parameter Apple.

*

* If there is a collision then either the shrink or grow methods must be called

* depending on the effect of the Apple.

*

@return true if the head of the snake collides with the Apple.

* false otherwise.

*

*/

public boolean eat(Apple a) {

// To be completed

return false;

}

/**

* allRed(Body) (5 marks)

*

* YOUR SOLUTION TO THIS METHOD MUST BE RECURSIVE

*

@param current - A body Object which represents the current location within the Snake

*

* This method should set every Body object from current onwards to red.

*

* Hint: read Body.java and look at how allRed is used in Game.java (gameOver).

*

*/

public void allRed(Body current) {

// To be completed

}

/**

* DIFFICULT QUESTION (leave to the end)

*

* getFreePositions(int,int) (10 marks)

*

@param w - the width of the screen

@param h - the height of the screen

*

@return a two dimensional array which contains the co-ordinates in which the

* Snake is NOT located.

*

* The first dimension must correspond to an individual set of co-ordinates.

*

* The second dimension must correspond to the x and y co-ordinates.

*

* Example:

*

* int[][] coords = getFreePositions(w,h);

*

* coords[1][0] must correspond to the x co-ordinate of the first point

* coords[1][1] must correspond to the y co-ordinate of the first point

*

* coords[3][1] must correspond to the y co-ordinate of the third point

*

*/

public int[][] getFreePositions(int w, int h) {

// To be completed

return null;

}

}

Answer
Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.

Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.Find step-by-step answers from expert tutors to questions asked by students like you. Start 14-day free trial.