Your task is to create a simple text-based map that allows the user to move thei
ID: 3844582 • Letter: Y
Question
Your task is to create a simple text-based map that allows the user to move their character, the
hero, around on the map.
Hero Class
•
A private String instance variable name
•
A private int instance variable row (indicates the hero's vertical position on the map)
•
A private int instance variable column (indicates the hero's
horizontal position on the map)
•
Constructor that takes in a name
•
Public getters for the name, row, and column
•
Public setters for the row and column
Map Class
•
A private Hero instance variable called hero for the hero on the map
•
A private char[][] instance variable called tiles that represents the tiles of the map
•
A private int instance variable called width for how many columns the map has
•
A private int instance variable called height for how many rows the map has
•
Constructor that takes in a hero, width, and height and also sets up the tiles
•
A public void method moveUp() that moves the hero up a tile
•
A public void method moveDown() that moves the hero down a tile
•
A public void method moveRight() that moves the hero right a tile
•
A public void method moveLeft() that moves the hero left a tile
•
Override the toString() method to return a text representation of the map, putting the first
letter of the hero's name in the position where the hero is currently located on the map
Game Class
•
Create a main() method that gets a name from the user and makes a Hero object and a Map
object
•
Repeatedly print the map and get a command from the user
Here's a sample run of the program:
Enter Name:
Jim
J . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
u - up
d - down
r - right
l - left
q - quit
Explanation / Answer
Please find the required program and output below: Please find the comments against each line for the description:
import java.util.Scanner;
class Game {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Enter name: ");
String name = scanner.next(); //read the input from user
Hero hero = new Hero(name); //create hero object using the name
Map map = new Map(hero, 10, 10); //create the map object
String c = "";
while(!c.equalsIgnoreCase("q")){ //repeatedly read user input until user enters q
System.out.println(map); //print map tiles
System.out.println("u - up"); //print the options
System.out.println("d - down");
System.out.println("r - right");
System.out.println("l - left");
System.out.println("q - quit");
c = scanner.next();
if(c.equalsIgnoreCase("u")) //call the move functions according to the user input
map.moveUp();
else if(c.equalsIgnoreCase("d"))
map.moveDown();
else if(c.equalsIgnoreCase("r"))
map.moveRight();
else if(c.equalsIgnoreCase("l"))
map.moveLeft();
}
}
}
class Hero {
String name;
int row;
int column;
public Hero(String name) {
this.name = name;
}
public String getName() {
return name;
}
public int getRow() {
return row;
}
public void setRow(int row) {
this.row = row;
}
public int getColumn() {
return column;
}
public void setColumn(int column) {
this.column = column;
}
}
class Map {
Hero hero;
char[][] tiles;
int width;
int height;
public Map(Hero hero, int width, int height) {
this.hero = hero;
this.width = width;
this.height = height;
this.tiles = new char[height][width];
for(int i=0; i<height; i++){ //initialize the tiles array
for(int j=0; j<width; j++){
if(i==0 && j==0)
tiles[i][j] = hero.getName().charAt(0); //for default hero pointer
else
tiles[i][j] = '.'; //for all other tile position
}
}
}
public void moveUp(){
if(hero.getRow() > 0){ //if not at top end
tiles[hero.getRow()][hero.getColumn()] = '.'; //reset old hero position to .
hero.setRow(hero.getRow()-1); //update the hero position to one up
tiles[hero.getRow()][hero.getColumn()] = hero.getName().charAt(0);
}
}
public void moveDown(){
if(hero.getRow() < height-1){
tiles[hero.getRow()][hero.getColumn()] = '.'; //reset old hero position to .
hero.setRow(hero.getRow()+1);//update the hero position to one down
tiles[hero.getRow()][hero.getColumn()] = hero.getName().charAt(0);
}
}
public void moveRight(){
if(hero.getColumn() < width-1){
tiles[hero.getRow()][hero.getColumn()] = '.'; //reset old hero position to .
hero.setColumn(hero.getColumn()+1);//update the hero position to one right
tiles[hero.getRow()][hero.getColumn()] = hero.getName().charAt(0);
}
}
public void moveLeft(){
if(hero.getColumn() > 0){
tiles[hero.getRow()][hero.getColumn()] = '.'; //reset old hero position to .
hero.setColumn(hero.getColumn()-1);//update the hero position to one left
tiles[hero.getRow()][hero.getColumn()] = hero.getName().charAt(0);
}
}
@Override
public String toString() {
String op = "";
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
op = op + tiles[i][j] + " ";
}
op = op + " ";
}
return op;
}
--------------------------------------------------------------------
OUTPUT:
Enter name:
Jim
J . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
u - up
d - down
r - right
l - left
q - quit
r
. J . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
u - up
d - down
r - right
l - left
q - quit
d
. . . . . . . . . .
. J . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
u - up
d - down
r - right
l - left
q - quit
d
. . . . . . . . . .
. . . . . . . . . .
. J . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
u - up
d - down
r - right
l - left
q - quit
d
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. J . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
u - up
d - down
r - right
l - left
q - quit
r
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . J . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
u - up
d - down
r - right
l - left
q - quit
l
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. J . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
u - up
d - down
r - right
l - left
q - quit
u
. . . . . . . . . .
. . . . . . . . . .
. J . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
u - up
d - down
r - right
l - left
q - quit
q
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.