Java: Making a simple tiled map, based on a coordinate plane. • Limitations: ? 2
ID: 3700953 • Letter: J
Question
Java: Making a simple tiled map, based on a coordinate plane.
• Limitations:
? 2 classes
? A third class to test them
? A main method
• Assume the X and Y axis will have 16 points from each direction of the 0,0 middle coordinate (including negative points)
• In the classes:
? Create the map as a whole
? Use one class to create the map, which will also define how large the map is
? Use the other class to be each "cell" or coordinate of the map
? Specify where the current position is
? Change the current position
? Set an initial starting position (also provide a default starting position if the target position is out of bounds)
? Determine which direction needs to be travelled to get from A to B
? Keep notes about each cell in the map and to retrace a path travelled.
Example Output:
Welcome! What coordinates are you at now?
Enter the X coordinate: 2
Enter the Y coordinate: -2
Okay, you are at position (2,-2), what would you like to do now?
1. Move to a new position.
2. Calculate the direction of a new position.
3. Make a note about the current position.
4. Retrace your steps.
5. Exit.
2
Enter the x coordinate of the position you’d like to find the direction to: 0
Enter the y coordinate of the position you’d like to find the direction to: 0
To travel from (2,-2) to (0,0) you need to move: SW
Okay, you are at position (2,-2), what would you like to do now?
1. Move to a new position.
2. Calculate the direction of a new position.
3. Make a note about the current position.
4. Retrace your steps.
5. Exit.
1
Enter the x coordinate you’d like to move to: 0
Enter the y coordinate you’d like to move to: 0
Okay, you are at position (0,0), what would you like to do now?
1. Move to a new position.
2. Calculate the direction of a new position.
3. Make a note about the current position.
4. Retrace your steps.
5. Exit.
5
Farewell!
Explanation / Answer
//------- Application.java
import java.util.Scanner;
public class Application {
private static int X = 0;
private static int Y = 0;
private static int DEFAULT_X = 0;
private static int DEFAULT_Y = 0;
private static int NORTH_SIZE = 16;
private static int SOUTH_SIZE = 16;
private static int EAST_SIZE = 16;
private static int WEST_SIZE = 16;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int opt = 2;
int newX = 0;
int newY = 0;
Map map = new Map(NORTH_SIZE, SOUTH_SIZE, EAST_SIZE, WEST_SIZE);
System.out.println("Welcome! What coordinates are you at now?");
System.out.println("Enter the X coordinate:");
X = scanner.nextInt();
System.out.println("Enter the Y coordinate:");
Y = scanner.nextInt();
do {
System.out.println("Okay, you are at position (" + X + "," + Y + "), what would you like to do now?");
System.out.println("1. Move to a new position.");
System.out.println("2. Calculate the direction of a new position.");
System.out.println("3. Make a note about the current position.");
System.out.println("4. Retrace your steps.");
System.out.println("5. Exit.");
opt = scanner.nextInt();
switch (opt) {
case 1:
System.out.println("Enter the x coordinate you’d like to move to: ");
newX = scanner.nextInt();
System.out.println("Enter the y coordinate you’d like to move to: ");
newY = scanner.nextInt();
if(map.isValidPoint(newX, newY)) {
map.moveToNewPosition(newX,newY);
X = newX;
Y = newY;
} else {
System.out.println("You are moving out of map. Choose another coordinates.");
moveToDefault();
}
break;
case 2:
System.out.println("Enter the x coordinate of the position you’d like to find the direction to: ");
newX = scanner.nextInt();
System.out.println("Enter the y coordinate of the position you’d like to find the direction to: ");
newY = scanner.nextInt();
if(map.isValidPoint(newX, newY)) {
String direction = map.calculateDirection(X,Y,newX,newY);
System.out.println("To travel from (" + X + "," + Y + ") to (" + newX + "," + newY + ") you need to move:"+direction);
} else {
System.out.println("You are moving out of map. Choose another coordinates.");
moveToDefault();
}
break;
case 3:
System.out.println("Your current location is noted.");
break;
case 4:
map.tracePath();
break;
case 5:
System.out.println("Farewell !!!");
scanner.close();
System.exit(1);
break;
default:
System.out.println("Choose appropriate option.");
}
//TODO : Read opt
opt=1;
} while (opt != 5);
scanner.close();
}
public static void moveToDefault() {
X = DEFAULT_X;
Y = DEFAULT_Y;
}
}
//------- Map.java
import java.util.LinkedList;
import java.util.List;
public class Map {
private List<Cell> points;
private int northSize;
private int southSize;
private int eastSize;
private int westSize;
public Map(int northSize, int southSize, int eastSize, int westSize) {
//Linked List to maintain order.
points = new LinkedList<>();
System.out.println("Initialising map...");
System.out.println("Directions are as show in below graph.");
System.out.println();
System.out.println(" North ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println("West----------|----------East");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" | ");
System.out.println(" South ");
System.out.println();
this.northSize = northSize;
this.southSize = southSize;
this.eastSize = eastSize;
this.westSize = westSize;
}
public List<Cell> getPoints() {
return points;
}
public void setPoints(List<Cell> points) {
this.points = points;
}
public int getNorthSize() {
return northSize;
}
public void setNorthSize(int northSize) {
this.northSize = northSize;
}
public int getSouthSize() {
return southSize;
}
public void setSouthSize(int southSize) {
this.southSize = southSize;
}
public int getEastSize() {
return eastSize;
}
public void setEastSize(int eastSize) {
this.eastSize = eastSize;
}
public int getWestSize() {
return westSize;
}
public void setWestSize(int westSize) {
this.westSize = westSize;
}
public void addPoint(Cell cell) {
points.add(cell);
}
public String calculateDirection(int x,int y,int newX,int newY) {
String direction = "Unknown";
if(newX == x && newY > y) {
direction = "North";
} else if (newX == x && newY < y) {
direction = "South";
} else if (newX > x && newY == y) {
direction = "East";
} else if(newX < x && newY == y) {
direction = "West";
} else if(newX > x && newY > y) {
direction = "North East";
} else if(newX < x && newY > y) {
direction = "North West";
} else if(newX > x && newY < y) {
direction = "South East";
} else if(newX < x && newY < y) {
direction = "South West";
}
return direction;
}
public boolean isValidPoint(int x, int y) {
if(x < getEastSize() && x > -(getWestSize()) && y < getNorthSize() && y > -(getSouthSize())) {
return true;
}
return false;
}
public void moveToNewPosition(int x, int y) {
points.add(new Cell(x,y));
}
public void tracePath() {
if(points != null && points.size() > 0) {
System.out.println("You started moving from: (" + points.get(0).getX() + "," + points.get(0).getY() + ")");
for(int i=1; i < points.size(); i++) {
System.out.println("then moved to : (" + points.get(i).getX() + "," + points.get(i).getY() + ")");
}
System.out.println("Now at : (" + points.get(points.size()-1).getX() + "," + points.get(points.size()-1).getY() + ")");
} else {
System.out.println("You have not moved yet");
}
}
public void resetMap() {
points = new LinkedList<>();
}
}
//------- Cell.java
public class Cell {
private int x;
private int y;
public Cell(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
return "Cell [x=" + x + ", y=" + y + "]";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.