Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

java code problem level: introduction to java Write a constructor and a helper method for the class as shown below: Instance Variables final static char

java code problem

level: introduction to java

Write a constructor and a helper method for the class as shown below:

Instance Variables

final static char TRAIL_CHAR = '.'; final static char OBSTACLE_CHAR = 'X'; final static char SPACE_CHAR = ' '; final static char CURRENT_CHAR = 'O'; final static char GOAL_CHAR = '@'; final static char NEWLINE_CHAR = ' ';

char[][] board
int playerRow
int playerCol
int goalRow
int goalCol
boolean levelPassed 

Constructors

1.

Constructor I

public GameState(int height, int width, int playerRow, int playerCol, 
 int goalRow, int goalCol) 

This constructor initialize the board with the given parameters and fill it with SPACE_CHAR = and Initialize all other instance variables with the given parameters.

2.

public GameState(GameState other)

This is the copy constructor of GameState.

In this constructor, use GameState's detailed constructor to initialize all instance variables (not class variables) of this using the respective instance variables from other.

Methods

1.

public String toString()

Override the toString() method for GameState class. This method originates from the Object class in Java, and so every object created in Java inherently has a toString() method.

This method should return a String representation of the calling GameState object. You need to build the String that displays all the information of the GameState object, using the String or StringBuilder methods.

2.

void addRandomObstacles(int count)

Add count number of random obstacles (drawn on the board with OBSTACLE_CHAR) into this.board.

Things to consider:

  • If count is a larger number than there are empty spaces available or count is less than 0, return immediately. Do not add any obstacles.
  • The location of any obstacle should not overwrite the player's position, the goal position, or other obstacles.

You may want to use a Random object to generate random rows and columns for each obstacle.

3.

void rotateClockwise()

Rotate the board clockwise once.

Hints:

  • Take out a piece of paper and pencil and draw it out. Write down the indices and what the rotated indices should be. What is the relationship between these numbers?
  • The following diagram demonstrates rotating a particular 6x5 array. The program should be able to handle rotating 2D arrays of any size.

4.

void moveRight()

Move the Snake's current position (playerRow, playerCol) towards the right until it is stopped by an obstacle or edge. Leave a trail of dots (TRAIL_CHAR) for all positions on the board that were passed through before stopping.

If the player reaches the goal before reaching an obstacle or edge, set levelPassed to true and return.

Note: the player and the goal overlaps when the game ends, such that, if moveRight() results in passing the level, the following should all evaluate to true:

playerRow == goalRow
playerCol == goalCol
levelPassed == true

5.

public boolean equals(Object other)

Override the equals() method. In general, the equals() method compares two objects for equality and returns true if they are equal. This method originates from the Object class in Java, and so every object created in Java inherently has an equals() method.

By calling this method, it compare two GameState objects - that is, the calling object (i.e. this) and other. If all fields of the two GameState objects match, return true. Otherwise return false.

  • If the parameter is null, return false.
  • Since the parameter is an Object type, make sure you check whether it is actually a GameState type first. To do this type checking, you should use the instanceof operator. Return false for all edge cases.

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

Practical Issues In Database Management A Refernce For The Thinking Practitioner

Authors: Fabian Pascal

1st Edition

0201485559, 978-0201485554

More Books

Students also viewed these Databases questions

Question

What is the environment we are trying to create?

Answered: 1 week ago

Question

How can we visually describe our goals?

Answered: 1 week ago