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;
1. 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.
2. 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.
In GameState.java class:
protected void loadFromFile (String filename) throws IOException / TODO void savFle0 try EY /I TODO: use OUTFILE_NAME as the filename and save ) catch (I0Exception e) e.printStackTrace) public class GameState ( // Used to populate char[][] board below and to display the // current state of play final static char TRAIL CHAR ; final static char OBSTACLE_CHAR - 'X final static char SPACE CHAR'; final static char CURRENT CHAR0 final static char GOAL CHAR final static char NEWLINE_CHAR - n // This represents a 2D map of the board char] board; // Location of the player int playerRow; int playerCol: // Location of the roal int goalRow; int goalCol; // true means the player completed this level boolean levelPassed public GameState (int height, int vidth, int playerRov, int playerCol, int goalRow, i int goalcol) this.levelPassedfalse this.playerRow - playerRov; this.playerCol -playerCol: this.goalRowgoalRow this.goalColgoalCol; this.board -nev char [height] [vidth]; for (int 1-0; 1
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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