Need help with my text based game
I've been creating a game in Java where you have to chase down an enemy character in a certain turn constraint. However, I have been having problems with the AI going out of the array limit for the map. I added a constraint that checks if it goes out of the map, but it hasn't solved it.
here are the variables defined at the top of the class
Code:
byte[][] map=new byte[10][10];
byte changex=0;
byte changey=0;
byte AIchangex=0;
byte AIchangey=0;
byte plrCoordinates[] = new byte[2];
byte AICoordinates[] = new byte[2];
here is the render code
Code:
void render(){
char a=i.move();
if (a=='w'){
if (plrCoordinates[0]==1){
render();
}else{
changey=-1;
}
}
if (a=='s'){
if (plrCoordinates[0]==8){
render();
}else{
changey=1;
}
}
if (a=='a'){
if (plrCoordinates[1]==1){
render();
}else{
changey=-1;
}
}
if (a=='d'){
if (plrCoordinates[1]==8){
render();
}else{
changey=1;
}
}
Random AIMove= new Random();
int x= AIMove.nextInt(3);
if (x==0){
if (AICoordinates[0]==8){
render();
}else{
AIchangex=-1;
}
}
if (x==1){
if (AICoordinates[0]==1){
render();
}else{
AIchangex=1;
}
}
if (x==2){
if (AICoordinates[1]==1){
render();
}else{
AIchangey=-1;
}
}
if (x==3){
if (AICoordinates[1]==8){
render();
}else{
AIchangey=1;
}
}
map[plrCoordinates[0]][plrCoordinates[1]]=0; //deletes the old image of the player+AI
map[AICoordinates[0]][AICoordinates[1]]=0;
plrCoordinates[0]=(byte) (plrCoordinates[0]+changex); //updates the location
plrCoordinates[1]=(byte) (plrCoordinates[1]+changey);
AICoordinates[0]=(byte) (AICoordinates[0]+AIchangey);
AICoordinates[1]=(byte) (AICoordinates[1]+AIchangey);
map[plrCoordinates[0]][plrCoordinates[1]]=2; //prints the new location
map[AICoordinates[0]][AICoordinates[1]]=3;
a=0; //resets the char to prevent infinite movement
}
here is the move thing from another class:
Code:
char move(){
char c = 0;
try {
c = (char)System.in.read(); //records a key for movement
} catch (IOException e) {
e.printStackTrace();
}
return c;
}
Re: Need help with my text based game
also here is the error i get when i try to move:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at pack.graphics.render(graphics.java:132)
at pack.main.run(main.java:11)
at pack.main.main(main.java:16)
line 132 is line 72 of the render code