推箱子Java Performmove错误

时间:2014-03-02 06:20:00

标签: java

这是java中的推箱子游戏。我尝试执行移动时遇到错误。任何人都可以指出我必须做出的改变才能使其发挥作用?

以下是我遇到的错误

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

at java.lang.String.charAt(Unknown Source)

在State.stateString(State.java:32)

at State。(State.java:15)

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

}

以下是boxpushing代码

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

1 个答案:

答案 0 :(得分:1)

提示:其中一个

  index++;

是多余的。

确定哪一个留作读者的练习。