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 previousStates;
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.
3. protected void loadFromFile(String filename) throws IOException
This helper method takes in the parameter filename. Read the files content, and initialize the appropriate instance variables. After loading everything from the file, check whether the game is already over. You can assume that the file you read in should always be correct.
The file to read in has the following format:
1. The first line is the board's height, a space, and then the board's width.
2. The second line is the player's position height, a space, and then the player's position width.
3. The third line is the goal's position height, a space, and then the goal's position width.
4. Finally, there is the printed the board. There should be a space for every empty part of the board and a capital X for every "block". The board here doesn't contain the goal and player positions nor the boundaries. In the example above, there are trailing spaces. Each line representing a row of the board has exactly the same number characters.
Hint:
- Give me some space: Since the space character is part of the grid, we need to read the spaces, instead of ignoring them. Using next() from the Scanner class might not be a good idea since it skips all the spaces. Look at the documentation for Scanner to see which method(s) might be good for this purpose.
4. void saveToFile()
This method writes the Streamline game to a file in the exact format mentioned above (See the diagram under loadFromFile() for the format and explanation). Use OUTFILE_NAME as the filename which you will save to (See the provided constant at the top of Streamline.java). Make sure that your formatting matches the formatting specified above exactly.
Once finished writing to the file, close it and print a message in the following format informing the user that the file was successfully saved:
Saved current state to: saved_streamline_game
Hints:
- To write to a file, you can use PrintWriter. Read the documentation to understand how it works and check out the methods you can use.
- Don't forget to close the stream.
For methods for recordAndMove(Direction direction):
In GameState.java 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
void move(Direction direction) {
for(int i=0;i
rotateClockwise();
moveRight();
for(int i=0;i
rotateClockwise();
}
public String toString() { StringBuilder str = new StringBuilder(); str.append("------- ");
/*deep copy the board*/ char[][] temp_Board = new char[this.board.length][this.board[0].length]; for (int row = 0; row
/*print the temp board*/ for (int row = 0; row
str.append("------- "); return str.toString(); }
}
In Direction.java class:
public enum Direction RIGHT (0), UP (1) LEFT (2), DOVN (3) private int rotationCount; *Constructor @par rotationcount see getRotationCount() Direction (int rotationCount) this. rotationcount rotationcount; *A convenient nethod that returns rotationCount, the nube of clockvise rotation needed to rotate the gane state before moving right. *return clockvise rotation count public int getRotationCount) i return this.rotationCount; public enum Direction RIGHT (0), UP (1) LEFT (2), DOVN (3) private int rotationCount; *Constructor @par rotationcount see getRotationCount() Direction (int rotationCount) this. rotationcount rotationcount; *A convenient nethod that returns rotationCount, the nube of clockvise rotation needed to rotate the gane state before moving right. *return clockvise rotation count public int getRotationCount) i return this.rotationCountStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started