[JAVA] Please provide comments to my code import java.util.ArrayList; import java.util.HashMap; import java.util.Objects; public class Land { public HashMap map = new HashMap<>(); public ArrayList allGoblins = new ArrayList<>(); public ArrayList allTreasures = new ArrayList<>(); public Human player; public Land() { int i = 1; while (i <= 23) { if (i == 1 || i == 23) { String[] topAndBottomArray = {"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"}; this.map.put(i, topAndBottomArray); } else this.map.put(i, new String[] { "|", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "|" }); i++; } String[] temp = this.map.get(12); temp[15] = "H"; this.map.put(12, temp); } public ArrayList addGoblins(int totalNum) { ArrayList allGobs = new ArrayList<>(); for (int i = 0; i < totalNum; i++) { int upAndDown = (int) Math.floor(Math.random() * 21) + 2; int sideToSide = (int) Math.floor(Math.random() * 30) + 1; String[] temp = this.map.get(upAndDown); if (!Objects.equals(temp[sideToSide], "*")) { i--; } else { temp[sideToSide] = "G"; this.map.put(upAndDown, temp); allGobs.add(new int[] { upAndDown, sideToSide }); } } return allGobs; } public ArrayList addTreasure(int totalNum) { ArrayList allTreasure = new ArrayList<>(); for (int i = 0; i < totalNum; i++) { int upAndDown = (int) Math.floor(Math.random() * 21) + 2; int sideToSide = (int) Math.floor(Math.random() * 30) + 1; String[] temp = this.map.get(upAndDown); if (!Objects.equals(temp[sideToSide], "*")) { i--; } else { temp[sideToSide] = "T"; this.map.put(upAndDown, temp); allTreasure.add(new int[] { upAndDown, sideToSide }); } } return allTreasure; } public boolean moveEntityIcon(int[] currentPos, int[] newPos, Object entity) throws Exception { String[] currentLine = this.map.get(currentPos[0]); String[] newLine = this.map.get(newPos[0]); String type; if (entity.getClass() == Human.class) type = "H"; else if (entity.getClass() == Goblin.class) type ="G"; else throw new Exception("Treasure Chests can not be moved!"); if (!currentLine[currentPos[1]].equals(type)) { throw new Exception("The Entity does not exist at the cords provided thus can not be moved!"); } if (!newLine[newPos[1]].equals("*")) return false; if (newPos[0] == 1 || newPos[0] == 23) return false; if (newPos[1] == 0 || newPos[1] == 31) return false; currentLine[currentPos[1]] = "*"; this.map.replace(currentPos[0], currentLine); currentLine = this.map.get(newPos[0]); currentLine[newPos[1]] = type; this.map.replace(newPos[0], currentLine); return true; } public HashMap checkSurroundings(int[] currentPos) { HashMap entitiesNearby = new HashMap<>(); ArrayList directions = new ArrayList<>(); directions.add(new int[]{currentPos[0] - 1, currentPos[1]}); directions.add(new int[]{currentPos[0] + 1, currentPos[1]}); directions.add(new int[]{currentPos[0], currentPos[1] - 1}); directions.add(new int[]{currentPos[0], currentPos[1] + 1}); String[] direct = new String[] {"N", "S", "W", "E"}; int i = 0; for (int[] direction : directions) { if (this.map.get(direction[0])[direction[1]].equals("G")) { for (Goblin gob : this.allGoblins) { if (gob.cords[0] == direction[0] && gob.cords[1] == direction[1]) { entitiesNearby.put(direct[i], gob); } } } else if (this.map.get(direction[0])[direction[1]].equals("T")) { for (Treasure treasure : this.allTreasures) { if (treasure.cords[0] == direction[0] && treasure.cords[1] == direction[1]) { entitiesNearby.put(direct[i], treasure); } } } else if (this.map.get(direction[0])[direction[1]].equals("H")) { entitiesNearby.put(direct[i], player); } i++; } return entitiesNearby; } public void removeEntity(Goblin gob) { String[] newArray = this.map.get(gob.cords[0]); newArray[gob.cords[1]] = "*"; this.map.put(gob.cords[0], newArray); int i = 0; for (Goblin goblin : allGoblins) { if (goblin.cords[0] == gob.cords[0] && goblin.cords[1] == gob.cords[1]) { allGoblins.remove(i); break; } i++; } } public void removeEntity(Treasure chest) { String[] newArray = this.map.get(chest.cords[0]); newArray[chest.cords[1]] = "*"; this.map.put(chest.cords[0], newArray); int i = 0; for (Treasure loot : allTreasures) { if (loot.cords[0] == chest.cords[0] && loot.cords[1] == chest.cords[1]) { allTreasures.remove(i); break; } i++; } } @Override public String toString() { StringBuilder stringToPrint = new StringBuilder(); for (int i = 1; i <= 23; i++) { StringBuilder tempLine = new StringBuilder(); for (int j = 0; j < this.map.get(i).length; j++) { tempLine.append(this.map.get(i)[j]).append(" "); } stringToPrint.append(tempLine).append("\n"); } return stringToPrint.toString(); } }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

[JAVA] Please provide comments to my code

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Objects;


public class Land {
public HashMap<Integer, String[]> map = new HashMap<>();
public ArrayList<Goblin> allGoblins = new ArrayList<>();
public ArrayList<Treasure> allTreasures = new ArrayList<>();
public Human player;

public Land() {
int i = 1;
while (i <= 23) {
if (i == 1 || i == 23) {
String[] topAndBottomArray = {"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-",
"-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-", "-"};
this.map.put(i, topAndBottomArray);
} else
this.map.put(i, new String[] { "|", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*",
"*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "*", "|" });
i++;
}
String[] temp = this.map.get(12);
temp[15] = "H";
this.map.put(12, temp);
}

public ArrayList<int[]> addGoblins(int totalNum) {
ArrayList<int[]> allGobs = new ArrayList<>();
for (int i = 0; i < totalNum; i++) {
int upAndDown = (int) Math.floor(Math.random() * 21) + 2;
int sideToSide = (int) Math.floor(Math.random() * 30) + 1;
String[] temp = this.map.get(upAndDown);
if (!Objects.equals(temp[sideToSide], "*")) {
i--;
} else {
temp[sideToSide] = "G";
this.map.put(upAndDown, temp);
allGobs.add(new int[] { upAndDown, sideToSide });
}
}
return allGobs;
}

public ArrayList<int[]> addTreasure(int totalNum) {
ArrayList<int[]> allTreasure = new ArrayList<>();
for (int i = 0; i < totalNum; i++) {
int upAndDown = (int) Math.floor(Math.random() * 21) + 2;
int sideToSide = (int) Math.floor(Math.random() * 30) + 1;
String[] temp = this.map.get(upAndDown);
if (!Objects.equals(temp[sideToSide], "*")) {
i--;
} else {
temp[sideToSide] = "T";
this.map.put(upAndDown, temp);
allTreasure.add(new int[] { upAndDown, sideToSide });
}
}
return allTreasure;
}

public boolean moveEntityIcon(int[] currentPos, int[] newPos, Object entity) throws Exception {
String[] currentLine = this.map.get(currentPos[0]);
String[] newLine = this.map.get(newPos[0]);
String type;
if (entity.getClass() == Human.class) type = "H";
else if (entity.getClass() == Goblin.class) type ="G";
else throw new Exception("Treasure Chests can not be moved!");
if (!currentLine[currentPos[1]].equals(type)) {
throw new Exception("The Entity does not exist at the cords provided thus can not be moved!");
}
if (!newLine[newPos[1]].equals("*")) return false;
if (newPos[0] == 1 || newPos[0] == 23) return false;
if (newPos[1] == 0 || newPos[1] == 31) return false;
currentLine[currentPos[1]] = "*";
this.map.replace(currentPos[0], currentLine);
currentLine = this.map.get(newPos[0]);
currentLine[newPos[1]] = type;
this.map.replace(newPos[0], currentLine);
return true;
}

public HashMap<String, Object> checkSurroundings(int[] currentPos) {
HashMap<String, Object> entitiesNearby = new HashMap<>();
ArrayList<int[]> directions = new ArrayList<>();
directions.add(new int[]{currentPos[0] - 1, currentPos[1]});
directions.add(new int[]{currentPos[0] + 1, currentPos[1]});
directions.add(new int[]{currentPos[0], currentPos[1] - 1});
directions.add(new int[]{currentPos[0], currentPos[1] + 1});
String[] direct = new String[] {"N", "S", "W", "E"};
int i = 0;
for (int[] direction : directions) {
if (this.map.get(direction[0])[direction[1]].equals("G")) {
for (Goblin gob : this.allGoblins) {
if (gob.cords[0] == direction[0] && gob.cords[1] == direction[1]) {
entitiesNearby.put(direct[i], gob);
}
}
} else if (this.map.get(direction[0])[direction[1]].equals("T")) {
for (Treasure treasure : this.allTreasures) {
if (treasure.cords[0] == direction[0] && treasure.cords[1] == direction[1]) {
entitiesNearby.put(direct[i], treasure);
}
}
} else if (this.map.get(direction[0])[direction[1]].equals("H")) {
entitiesNearby.put(direct[i], player);
}
i++;
}
return entitiesNearby;
}

public void removeEntity(Goblin gob) {
String[] newArray = this.map.get(gob.cords[0]);
newArray[gob.cords[1]] = "*";
this.map.put(gob.cords[0], newArray);
int i = 0;
for (Goblin goblin : allGoblins) {
if (goblin.cords[0] == gob.cords[0] && goblin.cords[1] == gob.cords[1]) {
allGoblins.remove(i);
break;
}
i++;
}
}

public void removeEntity(Treasure chest) {
String[] newArray = this.map.get(chest.cords[0]);
newArray[chest.cords[1]] = "*";
this.map.put(chest.cords[0], newArray);
int i = 0;
for (Treasure loot : allTreasures) {
if (loot.cords[0] == chest.cords[0] && loot.cords[1] == chest.cords[1]) {
allTreasures.remove(i);
break;
}
i++;
}
}

@Override
public String toString() {
StringBuilder stringToPrint = new StringBuilder();
for (int i = 1; i <= 23; i++) {
StringBuilder tempLine = new StringBuilder();
for (int j = 0; j < this.map.get(i).length; j++) {
tempLine.append(this.map.get(i)[j]).append(" ");
}
stringToPrint.append(tempLine).append("\n");
}
return stringToPrint.toString();
}
}

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY