Question: Java code problem level: introduction to java Write a constructor and a helper method for the Streamline.java class as shown below: Instance Variables final static
Java code problem
level: introduction to java
Write a constructor and a helper method for the Streamline.java class as shown below:
Instance Variables
final static int DEFAULT_HEIGHT = 6; final static int DEFAULT_WIDTH = 5;
final static String OUTFILE_NAME = "saved_streamline_game";
GameState currentState;
List
Constructor
public Streamline()
This is the no-argument constructor of Streamline
- Initialize currentState with a default height of 6, a default width of 5, a current player position at the lower left corner of the board, and a goal position of the top right corner of the board.
- Add 3 random obstacles to the current state.
- Initialize previousStates to an empty ArrayList (there have been no previous states yet).
Methods
1. void recordAndMove(Direction direction)
This method first updates previousStates by saving a copy of the current state into the list and then makes a move in the given direction on currentState (by calling move()).
If direction is null, do nothing.
Hint:
- Because all the hard work was already done by methods in the GameState class, this should be a very short method with just a few lines.
- If your board state does not change from the previous board, then you should not add it to the list of previousStates. Remember that you have coded a method to check equality.
- Think about what could go wrong if you append currentState (instead of a copy) to previousStates.
2. void undo()
The purpose of this method is to allow the player to undo their last step. This method should update previousStates and currentState appropriately to undo the most recent move made by the player.
If previousStates is empty, do nothing.
Hint:
- Look at the documentation for previousStates. It is a List object.
For helper methods for recordAndMove(Direction direction):
In GameState class:
public class GameState {
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;
public GameState(int height, int width, int playerRow, int playerCol, int goalRow, int goalCol) { this.levelPassed = false; this.playerRow = playerRow; this.playerCol = playerCol; this.goalRow = goalRow; this.goalCol = goalCol; this.board = new char[height][width]; for(int i = 0; i < height; i++) { for(int j = 0; j < width; j++) { this.board[i][j] = SPACE_CHAR; } } }
void move(Direction direction) {
for(int i=0;i rotateClockwise(); moveRight(); for(int i=0;i<4-direction.getRotationCount();i++) rotateClockwise(); } In Direction.java class: public enum Direction { RIGHT(0), UP(1), LEFT(2), DOWN(3); private int rotationCount; Direction(int rotationCount) { this.rotationCount = rotationCount; } public int getRotationCount() { return this.rotationCount; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
