我想根据玩家输入的步数返回每个相邻的方向。输入不能是字符,它们必须是整数(不能只说去南方,它必须是地图上的步骤,我将放在底部)。使用nextArea方法感觉超级迷失。 请不要为我写答案只是想要一些指导。
这些是来自GameWorld类的方法
/**
* Returns true if areas s1 and s2 are connected, false otherwise.
* Also returns false if either area is an invalid area identifier.
* @param s1 the first area
* @param s2 the second area
* @return true if areas are connected, false otherwise
*/
private boolean areasConnected(int s1, int s2) {
if (Math.min(s1, s2) >= 0 && Math.max(s1, s2) < numAreas) { //valid areas...
//...but are they connected?
return east[s1] == s2 || north[s1] == s2 || west[s1] == s2 || south[s1] == s2;
}
//Either s1 or s2 is not a valid area identifier
return false;
}
/**
* Determine ID number of an adjacent area given its direction from the
* current area.
* @param direction the direction to look (n for north, e for east, s for south, w for west)
* @return number of the area in that direction
* @throws IllegalArgumentException if direction is null
*/
public int nextArea(char direction) {
int nextIs; // area number of area in indicated direction
//Valid values
final char N = 'n', E = 'e', S = 's', W = 'w';
trace("nextArea() starts...");
// examine adjacent areas
switch (direction) {
case N: trace("determining number of area to the north");
nextIs = north[currentArea];
break;
case E: trace("determining number of area to the east");
nextIs = east[currentArea];
break;
case S: trace("determining number of area to the south");
nextIs = south[currentArea];
break;
case W: trace("determining number of area to the west");
nextIs = west[currentArea];
break;
default: throw new IllegalArgumentException("Direction must be one of " + N + ", " + E + ", " + S + " or " + W);
}
trace("...nextArea() ends with value for '" + direction + "' of " + nextIs);
return nextIs;
}
我目前的尝试在WereWolfenstein2D类中看起来像这样
public void walk(){
current = gameOne.getCurrentArea();
walk = sc.nextInt();
// pick a direction to walk into
// from that space show connected areas
}
public void pickMove(){
System.out.print("walk shoot, quit or reset?");
answer = sc.nextLine();
//walk
//if area not connected: alert
//if into village: alert that they are healed
//if containing wolf: altert they are bitten, if already bitten game ends
switch(answer) {
case "walk":System.out.print("from " + current + " a direction to walk into south(3), east(1), west(2), north(6),");
this.walk();
gameOne.tryWalk(walk);
gameOne.getCurrentArea();
//what I want>>>get current areas to s, e, w and n
gameOne.nextArea();
break;
case "shoot":
break;
case "quit": System.out.print("would you like to start again? (yes/no)");
restart = sc.nextLine();
if (restart.equals("yes")) {
gameOne.reset();
}
else {
System.out.println("Thanks for playing");
}
break;
case "reset": //gameOne.reset();
this.pickDifficulity();
break;
}
}
这是来自GameWorld类的方法,它有地图
//This map is _deliberately_ confusing, although it actually a regular layout
private int[] east = {1,2,0,4,5,3,7,8,6}; // areas to east of current location (index)
private int[] west = {2,0,1,5,3,4,8,6,7}; // areas to west of current location (index)
private int[] north = {6,7,8,0,1,2,3,4,5}; // areas to north of current location (index)
private int[] south = {3,4,5,6,7,8,0,1,2}; // areas to south of current location (index)
private int numAreas = south.length; // number of areas in the "world"
答案 0 :(得分:0)
如果我正确地解释了您的问题,那么您就会问如何将索引列表(代表位置)更改为必须采取的步骤才能达到这些职位。
你已经要求提示而不是代码所以我会给出一些一般性的指示,如果你想要更多细节,你可以告诉我。
如果您将路线转换为enum
,我认为您会发现您的解决方案更具可读性和清晰度。这样,与任何方向相关联的所有代码都在枚举内,您可以避免在其他地方使用switch
语句。
例如:
public enum Direction {
N(north), S(south), E(east), W(west);
private final int[] locations;
Direction(int[] locations) {
this.locations = locations;
}
public int next(int current) {
return locations[current];
}
}
您可以使用Direction.valueOf(dir.toUpper())
从名称获取路线,然后使用dir.next(current)
获取方向的下一个位置。
您的评论指出您需要朝相反方向前进:采取立场并返回通往该位置的位置。这需要迭代:您需要在数组中搜索该位置。同样,这可能是枚举中的一种方法。这是一个例子:
public int previous(int current) {
for (int i = 0; i < locations.length; i++) {
if (locations[i] == current)
return i;
}
return -1;
}
那么从北到3的位置是N.previous(3)
。
为了获得所有可能的先前位置,我建议将地图从一个位置返回到一个方向(解释为从该位置的那个位置开始到达当前位置)。
Map<Integer,Direction> allPrevious(int location) {
Map<Integer,Direction> map = new HashMap<>();
for (Direction dir: Direction.values()) {
int prev = dir.previous(location);
if (prev >= 0)
map.put(prev, dir);
}
}
获得该地图后,您可以轻松获取所有先前的位置(map.keySet()
)以及指定位置(map.get(location)
)的跟踪方向。