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 Streamline.java class as shown below: Instance Variables GameState currentState

Java code problem

level: introduction to java

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

Instance Variables

GameState currentState
List previousStates

Constructor

public Streamline()

This is the no-argument constructor of Streamline

  1. 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.
  2. Add 3 random obstacles to the current state.
  3. Initialize previousStates to an empty ArrayList (there have been no previous states yet).

Methods

image text in transcribedimage text in transcribedimage text in transcribed

For recordAndMove(Direction direction)

In GameState.java

image text in transcribed

In Direction.java

image text in transcribed

recordAndMove0 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. Think about what could go wrong if you append currentstate (instead of a copy) to previousstates 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 undo0 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. loadFromFile(0 protected void loadFromFile(String filename) throws IOException This helper method takes in the parameter filename. Read the file's 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 saveToFile) format Explanation print rows and columns of the game state> 0 To summarize what is in the file you are reading: 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. Hints: Always a good file: We will always use an existing and well-formatted file when testing your program. You do not need to consider any edge cases in this method. Loadable: You can now also run the game by using java Streamline (e.g. java Streamline game.txt) to load a game from the file - 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 saveToFile0 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_NAHE 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. //ove tovards any direction given // accomplish this by rotating, move right, rotating back void ove (Direction direction) ( for (int 1-0; print rows and columns of the game state> 0 To summarize what is in the file you are reading: 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. Hints: Always a good file: We will always use an existing and well-formatted file when testing your program. You do not need to consider any edge cases in this method. Loadable: You can now also run the game by using java Streamline (e.g. java Streamline game.txt) to load a game from the file - 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 saveToFile0 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_NAHE 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. //ove tovards any direction given // accomplish this by rotating, move right, rotating back void ove (Direction direction) ( for (int 1-0;

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

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions

Question

=+ (b) If F is continuous, then E[F(X)) =;.

Answered: 1 week ago