Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Instance Variable Shape[][] grid; Note that this is the Shape class from JavaFX . This 2D array will have the same dimensions as game.currentState.board. It

Instance Variable

Shape[][] grid;

Note that this is the Shape class from JavaFX. This 2D array will have the same dimensions as game.currentState.board. It contains obstacle and trail marker shapes. Trail markers are made invisible by setting their fill color to Color.TRANSPARENT.

For example, to the right is a graphic representation of the contents of the grid array after the player has moved from the bottom left to the bottom right. The faded circles with dotted outlines represent invisible trail markers.

Hint: We recommend you allocate and initialize grid in resetGrid() .

Displaying the Grid

create the display of the board with the width and height of the current Streamline game's board. You will be filling the grid with Shapes to display the current state of the board. You will be using a RoundedSquare to represent the obstacle and goal, a Player object for player (i.e. a special RoundedSquare), and a Circle to represent the trail characters and empty spaces on the board (with a transparent Circle). To get the board's width and height, implement and use the following helper methods. You may find the game instance variable helpful for this. You will not need to do any conversion, as these return in units of squares not pixels.

public int getBoardWidth()
public int getBoardHeight()

Implement the following helper method to get the size of a single square on the board based on the width and height:

public double getSquareSize()

Assume we will stretch the board to completely fill the scene. You may find the width and height helper methods useful for this as well as the mainScene instance variable. Hint: The starter code includes helpful comments to guide your implementation.

public void resetGrid()

When we start the game, we want to make sure that the previous game settings are cleared and the current state is correctly loaded. This function will be useful when the player passes one level and a new game is loaded.

In this method:

  • Check the instance variables and determine which ones of them should be cleared
  • Reset corresponding variables based on the current board stored in Streamline object

image text in transcribedimage text in transcribed

In Streamline -

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

Example -

image text in transcribed

import javafx.scene.; import javafx.scene. shape.*; import javafx.stage.Stage; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javafx.animation.*; import javafx.animation.PathTransition.*; import javafx.application.Application; import javafx.event.EventHandler; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.paint.*; import javafx.util.Duration; public class Guistreamline extends Application static final double SCENE WIDTH 500; static final double SCENE-HEIGHT = 600; static final String TITLE "CSE 8b Streamline GUI"; static final String USAGE = "Usage: In" + "> java Guistreamline - to start a game with default" + - to start a game by reading g" + "> java Guistreamline to start a game by reading a"+ the specified directory and "+ " size 6 5 and random obstacles + " java GuiStreamline "ame state from the specified fileln"+ "1l game states from files inln" + "playing them in order n" static final Color TRAIL COLOR Color.PALEVIOLETRED static final Color GOAL_COLOR Color.MEDIUMAQUAMARINE; static final Color OBSTACLE-COLOR = Color. DIMGRAY; Trail radius will be set to this fraction of the size of a board square static final double TRAIL RADIUS FRACTION = 0.1 // Squares wilL be resized to this fraction of the size of a board square static final double SQUAREFRACTION # 0.8; - public class Streamline final static int DEFAULT HEIGHT6 final static int DEFAULT-WIDTH- 5; final static int DEFAULT PLAYERROW 5; final static int DEFAULT PLAYERCOL ; final static int DEFAULT-GOALROW- 0; final static int DEFAULT-GOALCOL 4; final static int DEFAULT-OBSTACLES 3; final static char SPACECHAR final static String SPACE STRING "" - final static String OUTFILE NAME saved streamline game GameState currentstate; List previousstates; public Streamline()f this.currentState new Gamestate(DEFAULT-HEGHT, DEFAULT-WIDTH, - DEFAULT PLAYERROW, DEFAULT PLAYERCOL DEFAULT GOALROW, DEFAULT GOALCOL); currentstate.addRandomObstacles (DEFAULT_OBSTACLES); this.previousStates new ArrayList); public Streamline (String filename) ( try loadFromFile(filename); ) catch (IOException e) f e.printstackTrace() protected void loadFromFile(String filename) throws IOException t Scanner scannernew Scanner(new File(filename)); int height scanner.nextInt); int width scanner.nextInt); int playerRold scanner.nextInt(); int playerco1 scanner.nextInt(); int goalRow Scanner.nextInt(): int goalcol # scanner.nextInt(); Gamestate gameLoad new GameState (height, width, playerRow, playerCol, goalRow, goalCol) string emptyString = scanner.nextLine(); while (row e) t for (int colone = 0; colone O; //Check if gameover if (goalRow player Row && goalcol -playerCol)f currentState.levelPassed - true; return; void recordAndMove (Direction direction) [ if (direction null)f return int current_size - previousStates.size); //checks is the list previousStates is empty and updates it if (previousStates.isEmpty)) GameState temp new GameState(currentState); previousStates.add(temp); currentState.move (direction); //checks if the currentstate is different from previousStates Last state else if (! (currentstate.equals(previousstates.get(current_size 1)))) GameState tempnew GameState (currentState); previousStates.add (temp) currentState.move (direction); //If the next move does not change the state, remove the Last state if (currentstate.equals(temp))f previousStates.remove (previousStates.size()-1); //if currentState similar to the Last state, then moves else currentState.move (direction); This mehtod undos the previous movement, and goes back to Last game state then updates the field to that previous movemnet. void undo) if (previousStates.isEmpty ()) ( return this.previousStates previousStates; this.currentState currentState; void play() while(currentstate.levelPassed !-true) System.out.println(currentstate.toString ()); System.out.print(">) Scanner theUser new Scanner (System.in); string theinput theUser.nextLine(); if ("w".equals (theInput)) t recordAndMove (Direction.UP); else if("a".equals (theInput)) f recordAndMove (Direction. LEFT); else if ("s".equals (theInput)) t recordAndMove (Direction. DOWN); else if ("d".equals (theInput)) t recordAndMove (Direction. RIGHT) else if ("u".equals (theInput)) undo (); else if ("o".equals (theInput)) t this.saveToFile() else if ("q".equals (theInput)) [ return; else System.out.println("Wrong Input, Try Again."); System.out.println(currentstate.tostring)); if (currentstate.levelPassed true) System.out.println("Level Passed!"); return void saveToFile) f try t PrintWriter saveFile new PrintWriter(OUTFILE NAME); String stringone currentstate.board.length SPACE_STRING + currentstate.board[e].length; saveFile.println(stringone); String stringTwo currentState.playerRow SPACE _STRING + currentstate.playerCol; saveFile.println(stringTwo); String stringThree currentState.goalRow SPACE_STRING currentstate.goalcol; saveFile.println (stringThree); for Loop the currentState's board and then print for (int i-e; icurrentState.board.length; H) { for (int j ; jcurrentState.board[e].1ength; j++) { if (currentState.board[i][j]SPACE CHAR){ saveFile.print (SPACE STRING); else ( saveFile.print (currentState.board[i][j]); saveFile.println); saveFile.close(): System.out.println("Saved current state to: saved streamline_game"); throw new IOException ): catch (IOException e) f e.printstackTrace (); import javafx.scene.; import javafx.scene. shape.*; import javafx.stage.Stage; import java.io.File; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javafx.animation.*; import javafx.animation.PathTransition.*; import javafx.application.Application; import javafx.event.EventHandler; import javafx.event.ActionEvent; import javafx.scene.Group; import javafx.scene.input.KeyCode; import javafx.scene.input.KeyEvent; import javafx.scene.paint.*; import javafx.util.Duration; public class Guistreamline extends Application static final double SCENE WIDTH 500; static final double SCENE-HEIGHT = 600; static final String TITLE "CSE 8b Streamline GUI"; static final String USAGE = "Usage: In" + "> java Guistreamline - to start a game with default" + - to start a game by reading g" + "> java Guistreamline to start a game by reading a"+ the specified directory and "+ " size 6 5 and random obstacles + " java GuiStreamline "ame state from the specified fileln"+ "1l game states from files inln" + "playing them in order n" static final Color TRAIL COLOR Color.PALEVIOLETRED static final Color GOAL_COLOR Color.MEDIUMAQUAMARINE; static final Color OBSTACLE-COLOR = Color. DIMGRAY; Trail radius will be set to this fraction of the size of a board square static final double TRAIL RADIUS FRACTION = 0.1 // Squares wilL be resized to this fraction of the size of a board square static final double SQUAREFRACTION # 0.8; - public class Streamline final static int DEFAULT HEIGHT6 final static int DEFAULT-WIDTH- 5; final static int DEFAULT PLAYERROW 5; final static int DEFAULT PLAYERCOL ; final static int DEFAULT-GOALROW- 0; final static int DEFAULT-GOALCOL 4; final static int DEFAULT-OBSTACLES 3; final static char SPACECHAR final static String SPACE STRING "" - final static String OUTFILE NAME saved streamline game GameState currentstate; List previousstates; public Streamline()f this.currentState new Gamestate(DEFAULT-HEGHT, DEFAULT-WIDTH, - DEFAULT PLAYERROW, DEFAULT PLAYERCOL DEFAULT GOALROW, DEFAULT GOALCOL); currentstate.addRandomObstacles (DEFAULT_OBSTACLES); this.previousStates new ArrayList); public Streamline (String filename) ( try loadFromFile(filename); ) catch (IOException e) f e.printstackTrace() protected void loadFromFile(String filename) throws IOException t Scanner scannernew Scanner(new File(filename)); int height scanner.nextInt); int width scanner.nextInt); int playerRold scanner.nextInt(); int playerco1 scanner.nextInt(); int goalRow Scanner.nextInt(): int goalcol # scanner.nextInt(); Gamestate gameLoad new GameState (height, width, playerRow, playerCol, goalRow, goalCol) string emptyString = scanner.nextLine(); while (row e) t for (int colone = 0; colone O; //Check if gameover if (goalRow player Row && goalcol -playerCol)f currentState.levelPassed - true; return; void recordAndMove (Direction direction) [ if (direction null)f return int current_size - previousStates.size); //checks is the list previousStates is empty and updates it if (previousStates.isEmpty)) GameState temp new GameState(currentState); previousStates.add(temp); currentState.move (direction); //checks if the currentstate is different from previousStates Last state else if (! (currentstate.equals(previousstates.get(current_size 1)))) GameState tempnew GameState (currentState); previousStates.add (temp) currentState.move (direction); //If the next move does not change the state, remove the Last state if (currentstate.equals(temp))f previousStates.remove (previousStates.size()-1); //if currentState similar to the Last state, then moves else currentState.move (direction); This mehtod undos the previous movement, and goes back to Last game state then updates the field to that previous movemnet. void undo) if (previousStates.isEmpty ()) ( return this.previousStates previousStates; this.currentState currentState; void play() while(currentstate.levelPassed !-true) System.out.println(currentstate.toString ()); System.out.print(">) Scanner theUser new Scanner (System.in); string theinput theUser.nextLine(); if ("w".equals (theInput)) t recordAndMove (Direction.UP); else if("a".equals (theInput)) f recordAndMove (Direction. LEFT); else if ("s".equals (theInput)) t recordAndMove (Direction. DOWN); else if ("d".equals (theInput)) t recordAndMove (Direction. RIGHT) else if ("u".equals (theInput)) undo (); else if ("o".equals (theInput)) t this.saveToFile() else if ("q".equals (theInput)) [ return; else System.out.println("Wrong Input, Try Again."); System.out.println(currentstate.tostring)); if (currentstate.levelPassed true) System.out.println("Level Passed!"); return void saveToFile) f try t PrintWriter saveFile new PrintWriter(OUTFILE NAME); String stringone currentstate.board.length SPACE_STRING + currentstate.board[e].length; saveFile.println(stringone); String stringTwo currentState.playerRow SPACE _STRING + currentstate.playerCol; saveFile.println(stringTwo); String stringThree currentState.goalRow SPACE_STRING currentstate.goalcol; saveFile.println (stringThree); for Loop the currentState's board and then print for (int i-e; icurrentState.board.length; H) { for (int j ; jcurrentState.board[e].1ength; j++) { if (currentState.board[i][j]SPACE CHAR){ saveFile.print (SPACE STRING); else ( saveFile.print (currentState.board[i][j]); saveFile.println); saveFile.close(): System.out.println("Saved current state to: saved streamline_game"); throw new IOException ): catch (IOException e) f e.printstackTrace ()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions

Question

1. Let a, b R, a Answered: 1 week ago

Answered: 1 week ago