Question: Need help in creating a save and load file method, understanding try and catch usage, and creating directional movement capabilities for the game. Please comment

Need help in creating a save and load file method, understanding try and catch usage, and creating directional movement capabilities for the game. Please comment out the logic for these parts.

3.) GamePacman.java (Need Help Implementing)

* Need Help Here*

Here is an example of what your program should do/output when loading a game:

Game Manager ( Need help)

import java.util.*; import java.io.*; public class GameManager { // Instance variables private Board board; // The actual Pacman board private String outputFileName; // File to save the board to when exiting /* * GameManager Constructor * * Purpose: Constructs a GameManager to Generate new game * Parameter: int boardSize - Integer player want to set as board size. * String outputBoard - the filename the Board will be saved as. * Random random - the Random generator player wanna use. * */  GameManager(int boardSize, String outputBoard) throws Exception { // TODO } /* * GameManager Constructor * * Purpose: Constructs a GameManager to Load a saved game. * Parameter: String inputBoard - the filename the Board will be inputted. * String outputBoard - the filename the Board will be saved as. * Random random - the Random generator player wanna use. * */  GameManager(String inputBoard, String outputBoard) throws Exception { // TODO } // Main play loop // Takes in input from the user to specify moves to execute // valid moves are: // w - Move up // s - Move Down // a - Move Left // d - Move Right // q - Quit and Save Board // // If an invalid command is received then print the controls // to remind the user of the valid moves. // // Once the player decides to quit or the game is over, // save the game board to a file based on the outputFileName // string that was set in the constructor and then return // // If the game is over print "Game Over!" to the terminal /* * Name: play * Purpose: Takes in input from the user to specify moves to execute. * Parameter: Null. * Return: void. * */  public void play() throws Exception { // TODO } /* * Name: printControls() * Purpose: Print the Controls for the Game. * Parameter: Null. * Return: void. */ private void printControls() { System.out.println(" Controls:"); System.out.println(" w - Move Up"); System.out.println(" s - Move Down"); System.out.println(" a - Move Left"); System.out.println(" d - Move Right"); System.out.println(" q - Quit and Save Board"); System.out.println(); } }

Game Pacman

The only change to this file is the addition of the new command-line arguments. Namely, processArgs should process the command-line arguments to determine the size of the board (-s), the input file from which to load game progress (-i), and the output file to which game progress is saved (-o). None of these arguments are required. If no size argument is provided, or the provided value of size is less than 3, initialize a board of default size 10. If no output file is provided, save game progress to a default file named Pac-Man.board.

Handling Exceptions

The way we report and handle these errors is by throwing and catching exceptions. In our case, the only possibility for an exception is an IOException, which is thrown when there is a fatal error originating from the process of getting input or providing output. For this assignment, wherever an IOException is possible, catch the IOException and return a new Exception initialized with the String at the top of Board.java (IO_EXCEPTION).

*Anything bolded below is to be created, normal font is given*

Given Code:

1.) Board.java ( Used to generate board and object logic for ghosts and pacman reside here, for space sake assume pac man and ghost AI are working.)

import java.lang.StringBuilder; import java.util.*; import java.io.*;

public class Board {

// FIELD public final int GRID_SIZE;

// Exception string that is thrown whenever an I/O exception occurs public static final String IO_EXCEPTION = "I/O Exception!"

private char[][] grid; // String Representation of Pac-man Game Board private boolean[][] visited; // Record of where Pac-man has visited private PacCharacter pacman; // Pac-man that user controls private PacCharacter[] ghosts; // 4 Ghosts that controlled by the program private int score; // Score Recorded for the gamer

public Board(int size) {

// Initialize instance variables GRID_SIZE = size; grid = new char[GRID_SIZE][GRID_SIZE]; visited = new boolean[GRID_SIZE][GRID_SIZE]; score = 0;

pacman = new PacCharacter(GRID_SIZE/2, GRID_SIZE/2, 'P'); ghosts = new PacCharacter[4]; ghosts[0] = new PacCharacter( 0, 0, 'G'); ghosts[1] = new PacCharacter( 0, GRID_SIZE-1, 'G'); ghosts[2] = new PacCharacter(GRID_SIZE-1, 0, 'G'); ghosts[3] = new PacCharacter(GRID_SIZE-1, GRID_SIZE-1, 'G');

setVisited(GRID_SIZE/2, GRID_SIZE/2);

refreshGrid(); }

public Board(String inputBoard) throws Exception { // TODO add your code here

}

public void saveBoard(String outputBoard) throws Exception { // TODO }

public String toString(){

StringBuilder outputString = new StringBuilder(); outputString.append(String.format("Score: %d ", this.score));

for (int row = 0; row

outputString.append(" "); } return outputString.toString();

}

}

2.) PacCharacter.java (Constructor class)

** Completed **

3.) GamePacman.java ( main method here, has logic to take command line number for board size)

public class GamePacman {

private static final int DEFAULT_SIZE = 10;

private static String inputBoard; private static String outputBoard; private static int boardSize;

public static void main(String[] args) throws Exception {

processArgs(args);

System.out.println("Welcome to Pac-Man!");

GameManager game;

if (inputBoard == null) { System.out.println("Generating a New Board"); game = new GameManager(boardSize, outputBoard); } else { System.out.println("Loading Board from " + inputBoard); game = new GameManager(inputBoard, outputBoard); }

game.play();

}

// Needs to incorporate new command line arguments -o(output file for save progress file), -s(determine board size), and -i(load game progress)

private static void processArgs(String[] args) { // Arguments must come in pairs if((args.length % 2) != 0) { printUsage(); System.exit(-1); } // TODO } private static void printUsage() 
Welcome to Pac-Man Loading Board from Pac-Man.board Controls: w - Move Up S -Move Down a -Move Left d -Move Right q-Quit and Save Board Score: 70 * k * k GP *

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!