java - 推箱子 Java Performmove 错误

标签 java

这是 Java 中的推箱子游戏。当我尝试执行移动时出现错误。任何人都可以指出我必须做出哪些更改才能使其正常工作?

这是我遇到的错误

java.lang.StringIndexOutOfBoundsException:字符串索引超出范围:49

位于 java.lang.String.charAt(来源未知)

在 State.stateString(State.java:32)

在状态。(State.java:15)

在 boxpushing.perform(boxpushing.java:14)

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;

public class State{

  private int posX; 
  private int posY;
  private ArrayList<Integer> goalX;
  private ArrayList<Integer> goalY;

  private char[][] state;

  public State(String s){
    state=stateString(s);
    goalX=new ArrayList<Integer>();
    goalY=new ArrayList<Integer>();
    locatePositionPlayerAndTargets();
  }

  public char [][] stateString(String s){

    //need to put the state string into respective array position
    int width = s.indexOf("\n"); //to find how many number of rows below.

    int height = s.length() / width;

    char newState[][] = new char[height][width];
    int index = 0;
    for (int i = 0; i <height; i++){
      for(int j = 0; j< width; j++){
        newState[i][j]=s.charAt(index);
        index++;
      }
      index++;
    }
    return newState;
  }

  public char[][] getState(){
    return state;
  }

  private void locatePositionPlayerAndTargets(){
    //for player on target, target and crate on target
    for (int i=0; i< state.length; i++){
      for(int j=0; j<state[0].length; j++){

        if(state[i][j] == 'P'){
          posX=j;
          posY=i;
        } else if (state[i][j] == '+' || state[i][j] == 'o' || state[i][j] == '@' ){
          goalX.add(j); //save the goal/targets values (X and Y position)
          goalY.add(i);
        }
      }
    }
  }

  public ArrayList<Integer> getGoalY(){
    return goalY; //return the goal/target Y's position
  }

  public ArrayList<Integer> getGoalX(){
    return goalX; //return the goal/target X's position
  }

  public int getPosX(){
    return posX;
  }


  public int getPosY(){
    return posY;
  }

  public void setChar(int y, int x, char c){
    //to save the current element on that particular coordinates/position
    state[y][x] = c;
  }

  public char getChar(int y, int x){
    //will return what is on that particular coordinates/position
    return state[y][x];
  }



  //To replace the symbol/character after player move is initiated
  public void replacePlayerOnMove(int dy, int dx){
    // to get the char value on the current position 
    char charOnPos = state[posY][posX]; 

    switch(charOnPos){

      case '+':
        //if current position has 'playerOnTarget' replace with target
        setChar(posY,posX, 'o');
        break;
      case '@':
        //if current position has 'crateontarget' replace with playeronTarget
        setChar(posY,posX, '+');
        break;
      case 'P':
        //if current position has 'player' replace with background
        setChar(posY,posX, '.');
        break;    
    }
    posX += dx;
    posY += dy;
  }


  //To replace the symbol/character player is pushing the crate 
  public void replacePlayerOnPush(int dy, int dx){

    char charOnPos = state[posY][posX];

    switch(charOnPos){

      case '@':
        //if current position has 'crateOnTarget' replace with 'playerontarget'
        setChar(posY,posX, '+');
        break;
      case '*':
        //if current position has 'crate' replace with player
        setChar(posY,posX, 'P');
        break;
    }
  }

   //To replace the symbol/character when crate is moved 
  public void replaceCrateOnMove(int dy, int dx, char c){

    switch(c){

      case 'o':

        setChar(posY,posX, '@');
        break;
      case '.':

        setChar(posY,posX, '*');
        break;
    }
  }


  public boolean checkValidMove(int dx, int dy){
    try{ // need to use try and catch as this might not work / error might occur furthe debugging needed

      int tmpX = getPosX()+dx;
      int tmpY = getPosY()+dy;
      // this is for index out of bound array checks
      // then also check for crate / wall for pushing iniatiation
      char newPosition = getChar(tmpY, tmpX);
      if (newPosition == '*' || newPosition == '@'){
        return checkValidPush(dx,dy);
      } else if (newPosition == '#'){
        return false;
      } else {
        return true;
      }
    } catch (ArrayIndexOutOfBoundsException e){
      //if outofbound occurs just return false (possible end of boundary/map)
      return false;
    } catch (Exception e){
      System.out.println("Error Occur tadaaaa..Yeehaa =( ");
      return false;
    }
  }

  public boolean checkValidPush(int dx, int dy){
    int tmpPushX = getPosX() + dx*2; //new position of crate (x coordinates)
    int tmpPushY = getPosY() + dy*2; //new position of crate (x coordinates)

    char newCratePosition = getChar(tmpPushY,tmpPushX);

    if (newCratePosition != '#' && newCratePosition != '*' && newCratePosition != '@'){
      return true;
    } else {
      return false; //crate new position is invalid (possible collision might occur)
    }
  }

  public void cratePush(int my, int mx){
    //new position of crate after moved
    int newCratePosX = posX+mx*2;
    int newCratePosY = posY+my*2;

    //get current symbol / item on the new position above
    char c = getChar(newCratePosY, newCratePosX);
    //replace box with the symbol / item above.
    replaceCrateOnMove(newCratePosY, newCratePosX, c);
  }

  public void playerMove(int mx, int my){
    if (checkValidMove(mx, my)){

      int tmpNewX = posX+mx;
      int tmpNewY = posY+my;

      char newPosition = getChar(tmpNewY, tmpNewX);

      switch(newPosition){

        case '@':
          cratePush(my,mx);
          replacePlayerOnMove(my,mx);
          replacePlayerOnPush(my,mx);
          return;

        case '*':
          cratePush(my,mx);
          replacePlayerOnMove(my,mx);
          replacePlayerOnPush(my,mx);
          return;

        case '.':
          setChar(tmpNewY,tmpNewX,'P');
          replacePlayerOnMove(my,mx);
          return;

        case 'o':
          setChar(tmpNewY,tmpNewX,'+');
          replacePlayerOnMove(my,mx);
          return;

        default:
          System.out.println("The move is not valid");
          return;

      }
    }
  }

  public String toString(){
    String printString = "";
    for (int i=0; i<state.length; i++){
      for (int j=0; j<state[0].length; j++){
        printString += state[i][j];
      }

      printString += "\n";
    }
    return printString;
  }

}

这是推箱代码

public class boxpushing {

private final static char player = 'P';
private final static char playerOnTarget = '+';
private final static char wall = '#';
private final static char crate = '*';
private final static char crateOnTarget= '@';
private final static char target = 'o';
private final static char background = '.';

private State currentState;

public String perform(String state, char move){
currentState = new State(state);
performMove(move);

System.out.println("Before");
System.out.println(state);

System.out.println("");

return currentState.toString();
}



public void performMove(char moveInput){
switch (moveInput){
  case 'u':
    currentState.playerMove(0, 1);
    break;
  case 'd':
    currentState.playerMove(0, -1);        
    break;
  case 'l':
    currentState.playerMove(-1, 0);
    break;
  case 'r':
    currentState.playerMove(1, 0);
    break;
  }
 }
}

最佳答案

提示:其中之一

  index++;

是多余的。

找出剩下哪一个作为读者的练习。

关于java - 推箱子 Java Performmove 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22124557/

相关文章:

java - JAX-WS 连接不会关闭,并在 x 条消息后停止消息传递

java - 如何修复此错误 : incompatible types: possible lossy conversion from int to byte

java - Json 对象项目顺序

java - Android:有什么方法可以精确区分Phone-Movement和-Rotation吗?

java - 进行持续交付时,自动项目版本的 Maven 方式是什么?

java - 将 jar 部署到 unix 服务器

java - 调用 JNI 函数创建对象

java - 在 android volley、php、xampp mysql 中将响应转换为 JSON ARRAY 或 OBJECT

java - JUnit 测试用例中的 Thread.sleep

java - 从集合中查找 2d 中点之间的最短路线