In this project, you will create an interactive command‐line game that allows the user to explore a 2D map, similar to Zork or Colossal Cave Adventure (some of the first interactive fiction video games). Most locations on the map are harmless and allow play to continue, but if the user navigates to a WIN/LOSE location, the game immediately ends. Although the map is laid out as a 2D grid, the user may not be able to navigate to all portions of the map.
The game's map will be read in from a file. Thus, your code will essentially be a game‐playing engine, where the game itself can easily be changed by modifying the text file map.

You will need to submit
• Java Project, including all source files
• Runnable jar
• Your own map.txt file, which should be unique

Requirements for reading the input file
• The first line of the file consists of the map's dimensions, separated by "x" with optional spaces on either side. Thus, "3 x 4", "3x 4", "3 x4" and "3x4" are all valid specifications of dimensions.
• Every other line in the file is either a comment or specifies a map location
‐ Commented lines begin with "#", and may optionally be preceded by any amount of white space (i.e., tab/space)
‐ A map location is a series of 4 tab‐separated fields: coordinates, location name, action (win, lose, continue), and location description. The fields will be separated by 1 or more tabs.
• Coordinates specify where the location is on the 2D map, which is stored in a 2D array. The coordinates are separated by a comma, which may optionally have spaces on either side.
• Game actions are not case sensitive (i.e., win, Win, WIN, wIN all indicate the game is won)
• A game map input file may contain errors. Other than not specifying the map dimensions as the first line, the remaining errors can be recovered from, and gameplay should proceed.
• All errors should cause an exception to be thrown with an informative error message that helps the user fix their mistake. There are at least 4 different types of errors in the input file, given these specifications.
• The only required map location is the default home location of 0,0. If the map file doesn't have a 0,0 coordinate (or if it contained an error), your GameMap code should print an exception (or print to System.err) and create a default home MapLocation.

Game Play Requirements
• During game play, the user can use the following commands: UP (1), DOWN (2), RIGHT (3),
LEFT (4), QUIT (5)
• If the player quits, the program should exit
• If the player wins or loses the game, they should have the option to play again or quit

Project Requirements
Your project should contain at least 3 java classes with at least the following fields/methods:
Game: manages game play
• private GameMap mapMgr
• private Scanner scan
• public Game(String filename): initializes mapMgr by calling the GameMap constructor
• public void printMap(): pretty-prints the map by using GameMap.toString()
• public void startGame(): handles gameplay logic
• private void resetGame(): resets the game to start from the beginning, including resetting to the home location
• private void quit(): prints a goodbye message & exits
• private void move(int dx, int dy): gets the new location if it exists, prints the location & takes the appropriate action
• main: should use "map.txt" as the map file if no command line argument given

GameMap: reads in map file & manages locations during game
• private MapLocation[][] map
• public GameMap(String filename): reads in file & loads into map
• public MapLocation getLocation(int x, int y): simply returns the map location at [x,y] or null otherwise
• public String toString(): returns a pretty string of the map, suitable for debugging purposes

MapLocation: encapsulates data for each map location in 2D map
• should have fields for name, description, & action
• each field should have a corresponding get & set method
• the constructor should have a parameter for each field

To test running your jar file, export a runnable jar from Eclipse & put the jar and your map.txt file in the same directory (such as the Desktop). Open a command prompt on your computer and navigate to that folder. Run "java ‐jar <your jar file name> map.txt". For example, if your jar file is named smithj2_project1.jar and your map file is named smithj2_map.txt, so I would run the command, "java ‐jar smithj2_project1.jar smithj2_map.txt".
* We will predominantly be determining your grade based on running your program from the command line, so be sure you test this!!!

• Suggested implementation steps: There are 2 ways to go about implementing the project after creating the MapLocation class: (1) hard‐code a 2D map of MapLocations, work on the Game class, and then focus on reading in & parsing the map file; OR (2) focus on reading in & parsing the map file & then working on the Game class. The last step in either case is to implement error handling & command‐line functionality.

Steps for working on the Game class first
1. Implement MapLocation
2. In the GameMap constructor, create a basic 2 x 2 map using statements like the following:
map = new MapLocation[2][2];
map[0][0] = new MapLocation(“Home”, “Safe”, “Continue”);
map[0][1] = new MapLocation(“MSU”, “Montclair State”, “Continue”);
map[1][0] = new MapLocation(“Diner”, “Red Hawk Cafe”, “Win”);
map[1][1] = new MapLocation(“Diner”, “Six Brothers”, “Lose”);
3. Implement GameMap’s toString & getLocation methods
4. Implement basic game logic in Game.startGame()
a. test starting up & exiting
b. implement moving (don't forget to handle invalid moves like left at location 0,0)
c. implement win/lose
5. Implement replay game logic: quit, and replay/quit if win/lose
6. Implement GameMap file parsing with NO error handling, & test using Game.printMap()
7. Implement GameMap error handling: try putting mistakes in your map file, and make sure game play can continue with the remaining map locations
8. Implement command‐line functionality in main
9. Export jar and test running on command‐line

Steps for working on the GamMap class first (parsing the file)
1. Implement MapLocation
2. Implement GameMap with NO error handling, and test using Game.printMap()
3. Implement basic game logic in Game.startGame()
a. test starting up & exiting
b. implement moving (don't forget to handle invalid moves like left at location 0,0)
c. implement win/lose
4. Implement replay game logic: quit, and replay/quit if win/lose
5. Implement GameMap error handling: try putting mistakes in your map file, and make sure game play can continue with the remaining map locations
6. Implement command‐line functionality in main
7. Export jar and test running on command‐line

• Suggested steps for parsing the map file:
// setup the scanner to read the map file
// get the first line & parse out the dimensions on either side of the x
// initialize the map field
// for each line in the map file
// if the line is not a comment
// split the line into it’s 4 fields, store in an array
// split the first element of the array into coordinates x & y, map[x][y] = new MapLocation(array[1], array[2], array[3]);
// ** Don’t forget to do error checking & handling at each step!

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

import java.util.Scanner;

* Game Class, controls all of the logic of the game
public class Game {

private GameMap mapMgr; //Holds the map data for the game
private Scanner scan;
private int xLoc, yLoc; //Current position in the game

public Game(String fileName)
mapMgr = new GameMap(fileName);
scan = new Scanner(;
xLoc = 0;
yLoc = 0;

public void printMap()

* Major logic of game. Lets players chose a direction and attempts to move them that way. Also lets users quit game
public void startGame()
int input;
System.out.println("Welcome to the game!");
System.out.println("1. Move up");
System.out.println("2. Move down");
System.out.println("3. Move right");
System.out.println("4. Move left");
System.out.println("5. Quit");
System.out.print("Choice: ");
input = scan.nextInt();

case 1:
move(xLoc, yLoc+1);
case 2:...

By purchasing this solution you'll be able to access the following files:

for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Upload a file
Continue without uploading

We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats