gpt4 book ai didi

java - 推箱子 Java Performmove 错误

转载 作者:太空宇宙 更新时间:2023-11-04 07:00:54 28 4
gpt4 key购买 nike

这是 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com