JavaCode(review)
Map Interface - Collection - Adventure Game Challenge.
◀ ▷ ▶ ♤ ♠ ♡ ♥ ♧ ♣ ⊙e
2020. 7. 6. 06:22
반응형
동굴탐험. locaton, direction
Location Class ... Map<String, Interger> exits = new HashMap<>(); |
Main Class Map<Interger, Location> location= new HashMap<>(); |
그림과 같은 게임 프로그램 만들어보자.
> Main을 기본으로 위치설정(위치당 값설당-Map)
// Location Class의 add exits method(Map exits)로 방향 설정.
1. 값 설정 코딩 짜보자.(Location class / Main class)
Location Class
package com.timbuchalka;
import java.util.HashMap;
import java.util.Map;
public class Location {
private final int locationNum;
private final String description;
private final Map<String, Integer> exits; //Map define V
//Constructor
public Location(int locationNum, String description) {
this.locationNum = locationNum;
this.description = description;
this.exits = new HashMap<String, Integer>(); // V
}
//add method
public void addExit(String direction, int location) {
exits.put(direction, location); //Map.put(); 추가 v
}
//getter
public int getLocationNum() {
return locationNum;
}
//getter
public String getDescription() {
return description;
}
//getter
public Map<String, Integer> getExits() {
return new HashMap<String, Integer>(exits);
} // It's own object for secruity reasons.
}
Main Class
package com.timbuchalka;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
private static Map<Integer, Location> locations = new HashMap<>(); // V
//Location Class
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//값추가
//map.put(scanner Number 입력값, Location값 설정-call constructor(locationNum,description));
locations.put(0, new Location(0, "You are sitting in front of a computer learning Java"));
locations.put(1, new Location(1, "You are standing at the end of a road before a small brick building"));
locations.put(2, new Location(2, "You are at the top of a hill"));
locations.put(3, new Location(3, "You are inside a building, a well house for a small spring"));
locations.put(4, new Location(4, "You are in a valley beside a stream"));
locations.put(5, new Location(5, "You are in the forest"));
//출력
int loc = 1;
while(true) {
System.out.println(locations.get(loc).getDescription());
if(loc == 0) {
break;
}
loc = scanner.nextInt();
if(!locations.containsKey(loc)) {
System.out.println("You cannot go in that direction");
}
}
}
}
//Output
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=49801:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\Java-Collections-Map-Continued-and-Adventure-Game-Source-code\out\production\Adventure com.timbuchalka.Main
You are standing at the end of a road before a small brick building
1
You are standing at the end of a road before a small brick building
3
You are inside a building, a well house for a small spring
5
You are in the forest
0
You are sitting in front of a computer learning Java
Process finished with exit code 0
2. exits 를 이용하여 방향설정까지 해보자. (W,E,N,S)
(3. 문장west,east,north,south으로 입력해도 W,E,N,S 으로 프로그램이 인식하도록)
Location Class - 바뀌는거 없음. constructor 에 한줄 추가
package com.timbuchalka;
import java.util.HashMap;
import java.util.Map;
public class Location {
private final int locationNum;
private final String description;
private final Map<String, Integer> exits;
public Location(int locationNum, String description) {
this.locationNum = locationNum;
this.description = description;
this.exits = new HashMap<String, Integer>();
this.exits.put("Q", 0); // V
//어차피 방향설정시(Main) 모든 방향설정에 해당되어 걍 constructor에 넣어버림.
}
public void addExit(String direction, int location) { //main서 방향설정시 이용예정. V
exits.put(direction, location);
}
public int getLocationNum() {
return locationNum;
}
public String getDescription() {
return description;
}
public Map<String, Integer> getExits() {
return new HashMap<String, Integer>(exits);
}
}
Main
package com.timbuchalka;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class Main {
private static Map<Integer, Location> locations = new HashMap<Integer, Location>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 값설정(key, value)
locations.put(0, new Location(0, "You are sitting in front of a computer learning Java"));
locations.put(1, new Location(1, "You are standing at the end of a road before a small brick building"));
locations.put(2, new Location(2, "You are at the top of a hill"));
locations.put(3, new Location(3, "You are inside a building, a well house for a small spring"));
locations.put(4, new Location(4, "You are in a valley beside a stream"));
locations.put(5, new Location(5, "You are in the forest"));
// 2. 방향설정 Location Class의 add method 이용
locations.get(1).addExit("W", 2); //key 1의 방향들 V
locations.get(1).addExit("E", 3);
locations.get(1).addExit("S", 4);
locations.get(1).addExit("N", 5);
//locations.get(0).addExit("Q",0);// 처음으로 -> 모든 방향설정에 공통이니 constructor에 설정하면 편함.
locations.get(2).addExit("N", 5); //key 2방향들
//locations.get(0).addExit("Q",0);// 처음으로
locations.get(3).addExit("W", 1); //key 3방향들
//locations.get(0).addExit("Q",0);// 처음으로
locations.get(4).addExit("N", 1); //key 4방향들
locations.get(4).addExit("W", 2);
//locations.get(0).addExit("Q",0);// 처음으로
locations.get(5).addExit("S", 1); //key 5방향들
locations.get(5).addExit("W", 2);
//locations.get(0).addExit("Q",0);// 처음으로
//3. The player should be able to type commands such as "Go West", "run South", or just "East"
// 입력을 문장으로 했을때, 방향값 정의> map.put(key, value);
Map<String, String> vocabulary = new HashMap<String, String>(); // V
vocabulary.put("QUIT", "Q");
vocabulary.put("NORTH", "N");
vocabulary.put("SOUTH", "S");
vocabulary.put("WEST", "W");
vocabulary.put("EAST", "E");
int loc = 1;
while(true) {
System.out.println(locations.get(loc).getDescription());
if(loc == 0) {
break;
}
//방향 입력 //Location Class의 getExists = new HashMap<String, Integer>(exits)
Map<String, Integer> exits = locations.get(loc).getExits(); // V
System.out.print("Available exits are ");
for(String exit: exits.keySet()) {
System.out.print(exit + ", ");
}
System.out.println();
String direction = scanner.nextLine().toUpperCase(); //모든 입력값 대문자로 인식.
//3.
if(direction.length() > 1) { // 입력을 문장으로 했을때,한글자로 output
String[] words = direction.split(" ");
for(String input : words) {
if(vocabulary.containsKey(input)) {
direction = vocabulary.get(input);
break;
}
}
}
// 검색 - 방향 출력
if(exits.containsKey(direction)) {
loc = exits.get(direction);
} else {
System.out.println("You cannot go in that direction");
}
} //while 끝
}
}
Output
D:\IT\JDK\jdk11.0.6_10\bin\java.exe "-javaagent:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=51225:D:\IT\IDEA\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath D:\IT\Java-Collections-Adventure-Game-challenge-Source-code\out\production\Adventure com.timbuchalka.Main
You are standing at the end of a road before a small brick building
Available exits are Q, S, E, N, W,
go west
You are at the top of a hill
Available exits are Q, N,
w
You cannot go in that direction
You are at the top of a hill
Available exits are Q, N,
e
You cannot go in that direction
You are at the top of a hill
Available exits are Q, N,
n
You are in the forest
Available exits are Q, S, W,
s
You are standing at the end of a road before a small brick building
Available exits are Q, S, E, N, W,
join north
You are in the forest
Available exits are Q, S, W,
참고) Spring split method