Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类之间的私有方法调用_Java_Methods - Fatal编程技术网

Java 类之间的私有方法调用

Java 类之间的私有方法调用,java,methods,Java,Methods,正在尝试获取连接到显示的区域。正如您所看到的,我已经尝试(我知道的很少)用nextArea方法替换这个方法。将在下面发布游戏世界类。请注意,我不希望有人为我做这项工作,只是给我指出正确的方向 /** * Auto Generated Java Class. */ import java.util.Scanner; public class WereWolfenstein2D { GameWorld gameOne = new GameWorld(true); pri

正在尝试获取连接到显示的区域。正如您所看到的,我已经尝试(我知道的很少)用nextArea方法替换这个方法。将在下面发布游戏世界类。请注意,我不希望有人为我做这项工作,只是给我指出正确的方向

 /**
  * Auto Generated Java Class.
  */
  import java.util.Scanner;

 public class WereWolfenstein2D {
  GameWorld gameOne = new GameWorld(true);
    private boolean tracing = false;
   Scanner sc = new Scanner(System.in);
   String input, answer, restart;
    int walk, current;
     char area;

      public void explain() {
      System.out.print("You are a hunter, defend the village and capture the                 Werewolf by shooting it three times. ");
   System.out.println("If you get bitten visit the village to be cured. " +
     "If you get bitten twice you'll turn." +     
     "If none of the town folk survive, you lose. " +
     "Now choose how what circumstances you're willing to work in.");

    }
        public void pickDifficulity(){
     {
  System.out.println("Easy, Normal or Hard?");
     input = sc.nextLine();
     input = input.toUpperCase();
     if (input.equals("EASY")){
      System.out.println(Difficulty.EASY);
      }
   else if (input.equals("NORMAL")) {
     System.out.println(Difficulty.NORMAL);
   }
   else if (input.equals("HARD")) {
     System.out.println(Difficulty.HARD);

   }
} 


  }
    public void preMove(){
          System.out.println("current stats");

       // no. of actions until dawn 
                 gameOne.checkForDawn();
// current population 
                 gameOne.getVillagePopulation();
// if they can hear village 
                 gameOne.isVillageNear();

// if not bitten: 
                 gameOne.getShotCount();
// current area no. 
                 gameOne.getCurrentArea();
// number of connecting areas 
             //   gameOne.nextArea(char direction);

               //gameOne.areasConnected(int s1, int s2);
// no of times werewolf shot

// no. of bullets left
                 gameOne.getShotCount();
// if the player can smell the wolf
                  gameOne.werewolfNear();
         }
         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.println("Pick a direction to walk in south(s), east(e), north(n), west(w)");
                            current = gameOne.getCurrentArea(); //current area they are in
                            char direction = sc.next().charAt(current); //direction they wish to travel

                           // char area = sc.next().charAt(current);  
                            char area1 = 's';
                            char  area2 = 'e';
                            char area3 = 'n';
                            char area4 = 'w';

                            System.out.println("the area to the south is: " + gameOne.nextArea(area1));                               
                            System.out.println("the area to the east is: " + gameOne.nextArea(area2));
                            System.out.println("the area to the north is: " + gameOne.nextArea(area3));
                            System.out.println("the area to the west is: " + gameOne.nextArea(area4));
                            System.out.println(current +"<< >>" + direction);
                            System.out.println("pick again");
                            int newcurrent = direction;
                            direction = sc.next().charAt(newcurrent);
                            System.out.println(newcurrent + "<< new >>" + direction);

                      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;
                             }
                          }




        public void play() {
        this.explain();
        this.pickDifficulity();
        this.preMove();
         this.pickMove();
      //  gameOne.reset();
          }

        public void setTracing(boolean onOff) {
        tracing = onOff;
      }

       public void trace(String message) {
       if (tracing) {
        System.out.println("WereWolfenstein2D: " + message);
          }
       }


         }
/**
*自动生成的Java类。
*/
导入java.util.Scanner;
狼人公开课{
GameWorld gameOne=新游戏世界(true);
私有布尔跟踪=false;
扫描仪sc=新的扫描仪(System.in);
字符串输入、应答、重启;
int步行,当前;
焦区;
公开无效解释(){
系统输出打印(“你是一名猎人,保卫村庄,并通过三次射击俘虏狼人。”);
println(“如果你被咬伤了,就去村里治疗。”+
“如果你被咬两次,你就会转身。”
“如果镇上没有人活下来,你就输了。”+
“现在选择你愿意在什么环境下工作。”);
}
公共财政困难(){
{
System.out.println(“简单、正常还是硬?”);
输入=sc.nextLine();
input=input.toUpperCase();
if(input.equals(“EASY”)){
System.out.println(难易);
}
else if(输入等于(“正常”)){
System.out.println(难度.正常);
}
else if(输入等于(“硬”)){
System.out.println(难度.硬);
}
} 
}
公开无效预先移动(){
System.out.println(“当前统计数据”);
//黎明前的行动次数
gameOne.checkForDawn();
//当前人口
gameOne.getVillagePopulation();
//如果他们能听到村庄的声音
gameOne.isVillageNear();
//如果没有被咬伤:
gameOne.getShotCount();
//当前区域编号。
gameOne.getCurrentArea();
//连接区域的数量
//gameOne.nextArea(字符方向);
//游戏一。连接的区域(int s1,int s2);
//狼人中枪的次数
//剩余子弹数
gameOne.getShotCount();
//如果玩家能闻到狼的味道
游戏一。狼人近距离();
}
公共移动{
系统输出打印(“步行拍摄、退出或重置?”);
答案=sc.nextLine();
//走
//如果区域未连接:警报
//如果进入村庄:提醒他们已经痊愈
//如果包含狼:改变他们被咬,如果已经被咬游戏结束
开关(应答){
案例“步行”:System.out.println(“选择一个方向在南(s)、东(e)、北(n)、西(w)”行走);
current=gameOne.getCurrentArea();//它们所在的当前区域
char direction=sc.next().charAt(当前);//他们希望移动的方向
//字符区域=sc.next().charAt(当前);
字符区域1='s';
字符区域2='e';
字符区域3='n';
字符区域4='w';
System.out.println(“南部区域为:“+gameOne.nextArea(区域1));
System.out.println(“东面的区域是:“+gameOne.nextArea(区域2));
System.out.println(“北面的区域是:“+gameOne.nextArea(区域3));
System.out.println(“西面区域为:“+gameOne.nextArea(区域4));
系统输出打印项次(当前+“>”+方向);
System.out.println(“再次拾取”);
int newcurrent=方向;
方向=sc.next().charAt(newcurrent);
System.out.println(newcurrent+“>”+方向);
打破
案例“射击”:
打破
案例“退出”:System.out.print(“您想重新开始吗?(是/否)”);
重新启动=sc.nextLine();
如果(重启.equals(“是”)){
gameOne.reset();
}
否则{
System.out.println(“谢谢玩”);
}
打破
案例“reset”://gameOne.reset();
这是困难;
打破
}
}
公共游戏{
这个。解释();
这是困难;
这个。preMove();
this.pickMove();
//gameOne.reset();
}
公共void setTracing(布尔onOff){
跟踪=开启关闭;
}
公共无效跟踪(字符串消息){
如果(跟踪){
System.out.println(“狼人:信息“);
}
}
}
游戏世界级>>> 公共类游戏世界{

// Final instance variables
public final int NIGHT_LENGTH = 3;      // three actions until dawn arrives (day is instantaneous)
private final int MAX_SHOTS_NEEDED = 3; // successful hits required to subdue werewolf

//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"

// Non-final instance variables
private int currentArea;    // current location of player
private int villagePos;     // area where the village is located
private int wolfPos;        // area where the werewolf can be found

private Difficulty level;   // difficulty level of the game
private int villagerCount;  // number of villagers remaining
private int stepsUntilDawn; // number of actions until night falls

private boolean isBitten;   // is the player currently bitten and in need of treatment? 
private int hitsRemaining;  // number of shots still needed to subdue the werewolf

private Random generator;   // to use for random placement in areas
private boolean tracing;    // switch for tracing messages


/**
 * Creates a game world for the WereWolfenstein 2D game.
 * @param traceOnOff whether or not tracing output should be shown
 */
public GameWorld(boolean traceOnOff) {
    trace("GameWorld() starts...");
    generator = new Random();
    generator.setSeed(101); //this default setting makes the game more predictable, for testing
    setTracing(traceOnOff); //this may replace random number generator        
    trace("...GameWorld() ends");
}


/**
 * Returns the number of the current area.
 * @return which area number player is within
 */
public int getCurrentArea() {
    trace("getCurrentArea() starts... ...and ends with value " + currentArea);
    return currentArea;
}


/**
 * Returns the number of shot attempts, formatted as "total hits/total required"
 * @return the fraction of shots that have hit the werewolf (out of the required number of hits)
 */
public String getShotCount() {
    String count; // formatted total

    trace("getShotCount() starts...");
    count = (MAX_SHOTS_NEEDED - hitsRemaining) + "/" + MAX_SHOTS_NEEDED;
    trace("getShotCount() ...ends with value " + count);
    return count;
}


/**
 * Returns the current number of villagers.
 * @return the villager count, >= 0
 */
public int getVillagePopulation() {
    return villagerCount;
}


/**
 * Returns the number of actions remaining until dawn arrives. 
 * @return actions remaining until dawn event
 */
public int getActionsUntilNight() {
    return stepsUntilDawn;
}


/**
 * Randomly determines a unique starting location (currentArea), village
 * position (villagePos) and werewolf position (wolfPos).
 * @param difficulty - the difficulty level of the game
 * @return the starting location (area)
 */
public int newGame(Difficulty difficulty) {
    trace("newGame() starts...");

    level = difficulty;

    //determine village location and initialise villagers and night length
    villagePos = generator.nextInt(numAreas);
    stepsUntilDawn = NIGHT_LENGTH;
    villagerCount = level.getVillagerCount();

    // determine player's position
    if (level.getPlayerStartsInVillage()) {
        //place player in village
        currentArea = villagePos;
    } else {
        //pick a random location for player away from the village
        do {
            currentArea = generator.nextInt(numAreas);
        } while (currentArea == villagePos);
    }
    trace("player starts at " + currentArea);

    // determine werewolf's position
    trace("calling resetTargetPosition()");
    resetWolfPosition();

    // define player's status
    trace("player is not bitten");
    isBitten = false;
    trace("werewolf is not hit");
    hitsRemaining = MAX_SHOTS_NEEDED;

    trace("...newGame() ends with value " + currentArea);
    return currentArea;
}


/** Randomly determines a unique location for werewolf (wolfPos). */
private void resetWolfPosition() {
    int pos;    // werewolf position

    trace("resetWolfPosition() starts...");

    pos = generator.nextInt(numAreas);
    while (pos == currentArea || pos == villagePos) {
        trace("clash detected");
        // avoid clash with current location
        pos = generator.nextInt(numAreas);
    }

    wolfPos = pos;
    trace("werewolf position is " + wolfPos);

    trace("...resetWolfPosition() ends");
}


/**
 * Returns the nearness of the werewolf.
 * @return Status of werewolf's location
 *    BITTEN: if player is currently bitten (and delirious)
 *    NEAR: if werewolf is in a connected area
 *    FAR: if werewolf is elsewhere
 */
public Result werewolfNear() {
    trace("werewolfNear() starts");
    if (isBitten) {
        trace("werewolfNear(): player is still delirious from a bite so cannot sense nearness of werewolf");
        return Result.BITTEN;
    }
    trace("werewolfNear() returning result of nearnessTo(" + wolfPos + ")");
    return nearnessTo(wolfPos);
}


/**
 * Returns true if the village is near the player (in an adjacent area),
 * false otherwise.
 * @return true if the player is near (but not in) the village, false otherwise.
 */
public boolean isVillageNear() {
    trace("villageNear() starts and returns result of nearnessTo(" + villagePos + ") == Result.NEAR");
    return nearnessTo(villagePos) == Result.NEAR;
}


/**
 * Returns the nearness of the player to the nominated area.
 * @param area  the area (werewolf or village) to assess
 * @return Nearness of player to nominated area
 *    NEAR: if player is adjacent to area
 *    FAR: if player is not adjacent to the area
 */
private Result nearnessTo(int area) {
    Result closeness;    // closeness of player to area

    trace("nearnessTo() starts...");
    if ((east[currentArea] == area) ||
        (west[currentArea] == area) ||
        (north[currentArea] == area) ||
        (south[currentArea] == area))
    {
        // player is close to area
        closeness = Result.NEAR;
        trace("area is close");
    } else {
        // player is not adjacent to area
        closeness = Result.FAR;
        trace("area is far");
    }

    trace("...nearnessTo() ends with value " + closeness);

    return closeness;
}


/**
 * Try to move the player to another area. If the move is not IMPOSSIBLE
 * then the number of actions remaining before dawn arrives is decremented.
 * @param into  the area to try to move into
 * @return Result of the movement attempt
 *    SUCCESS: move was successful (current position changed)
 *    VILLAGE: move was successful and player has arrived in the village (and is not longer bitten) 
 *    BITTEN: move was successful but player encountered the werewolf
 *    FAILURE: move was successful but already bitten player encountered the werewolf again
 *    IMPOSSIBLE: move was impossible (current position not changed)
 */
public Result tryWalk(int into) {
    Result result;    // outcome of walk attempt

    trace("tryWalk() starts...");

    if (areasConnected(currentArea, into)) {
        trace("move into area " + into );
        currentArea = into;
        if (currentArea != wolfPos) {
            // werewolf not found
            trace("werewolf not encountered");
            result = Result.SUCCESS;

            if (currentArea == villagePos) {
                isBitten = false;
                result = Result.VILLAGE;
            }
        } else {
            // werewolf encountered
            if (isBitten) {
                trace("werewolf encountered again");
                result = Result.FAILURE;
            } else {
                // not bitten
                trace("werewolf encountered");
                result = Result.BITTEN;
                isBitten = true;
                resetWolfPosition();                    
            }
        }
        stepsUntilDawn--; //one more action taken
    } else { // area not connected
        trace("move not possible");
        result = Result.IMPOSSIBLE;
    }

    trace("...tryWalk() ends with value " + result);

    return result;
}


/**
 * Try to shoot a silver bullet at the werewolf from the current area.
 * If the shot is not IMPOSSIBLE then the number of actions remaining
 * before dawn arrives is decremented.
 * @param into  the area to attempt to shoot into
 * @return status of attempt
 *    CAPTURED: werewolf has been subdued and captured
 *    SUCCESS: werewolf has been hit but is not yet captured
 *    VILLAGE: the shot went into the village and a villager has died
 *    FAILURE: werewolf not present
 *    IMPOSSIBLE: area not connected
 */
public Result shoot(int into) {
    Result result;    // outcome of shooting attempt

    trace("shoot() starts...");

    if (areasConnected(currentArea, into)) {
        // area connected
        trace("shoot into area " + into );
        if (into == villagePos) {
            result = Result.VILLAGE;
            villagerCount--;
            trace("shot into village");
        } else if (into != wolfPos) {
            // not at werewolf location (but at least didn't shoot into the village!)
            result = Result.FAILURE;
            trace("werewolf not present");
        } else {
            // at werewolf location
            hitsRemaining--;
            if (hitsRemaining == 0) {
                // last hit required to subdue the werewolf
                trace("werewolf subdued and captured");
                result = Result.CAPTURED;
            } else {
                // not the last shot
                result = Result.SUCCESS;
                if (level.getWolfMovesWhenShot()) {
                    resetWolfPosition();
                }
                trace("werewolf found but not yet captured");
            }
        }
        stepsUntilDawn--; //one more action taken
    } else {
        // not at valid location
        result = Result.IMPOSSIBLE;
        trace("area not present");
    }

    trace("...shoot() ends with value " + result);

    return result;
}


/**
 * Checks if there are no more actions left until dawn arrives. If dawn is
 * here then decrements the number of villagers, repositions the werewolf
 * and resets the number of actions until dawn arrives again. Returns true
 * if dawn occurred, false if it did not.
 * @return true if dawn just happened, false if has not yet arrived
 */
public boolean checkForDawn() {
    if (stepsUntilDawn == 0) {
        if (villagerCount > 0) { //dawn may arrive after shooting the last villager
            villagerCount--;
        }
        stepsUntilDawn = NIGHT_LENGTH;
        resetWolfPosition();
        return true;
    }
    return false;
}


/**
 * 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;
}


/** Resets all game values. */
public void reset() {
    trace("reset() starts...");

    // reset all game values
    trace("resetting all game values");
    newGame(level); //start a new game with the same difficulty

    trace("...reset() ends");
}


/**
 * Turn tracing messages on or off. If off then it is assumed that
 * debugging is not occurring and so a new (unseeded) random number
 * generator is created so the game is unpredictable.
 * @param shouldTrace  indicates if tracing messages should be displayed or not
 */
public void setTracing(boolean shouldTrace) {
    if (! shouldTrace) { // not tracing so get an unseeded RNG
        generator = new Random();
    }
    tracing = shouldTrace;
}

/**
 * Prints the given tracing message if tracing is enabled.
 * @param message  the message to be displayed
 */
public void trace(String message) {
    if (tracing) {
        System.out.println("GameWorld: " + message);
    }
}
//最终实例变量
public final int NIGHT_LENGTH=3;//三次行动直到黎明到来(白天是瞬间)
私人最终int MAX_SHOTS_NEEDED=3;//制服狼人需要成功命中
//这张地图是故意搞混的,尽管它实际上是一个规则的布局
private int[]east={1,2,0,4,5,3,7,8,6};//当前位置以东的区域(索引)
private int[]west={2,0,1,5,3,4,8,6,7};//当前位置以西的区域(索引)
私有int[]北={6,7,8,0,1,2,3,4,5};//当前位置以北的区域(索引)
私有int[]南={3,4,5,6,7,8,0,1,2};//当前位置以南的区域(索引)
private int numAreas=south.length;//世界中的区域数
//非最终实例变量
private int currentArea;//播放器的当前位置
private int villagePos;//村庄所在区域
private int wolfPos;//wer所在的区域