Results 1 to 2 of 2
- 04-08-2012, 12:56 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
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
Java 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];
Java 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 }
Java Code:char move(){ char c = 0; try { c = (char)System.in.read(); //records a key for movement } catch (IOException e) { e.printStackTrace(); } return c; }
- 04-08-2012, 12:57 AM #2
Member
- Join Date
- Apr 2012
- Posts
- 2
- Rep Power
- 0
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
Similar Threads
-
Turn based game
By knightwriter in forum New To JavaReplies: 7Last Post: 12-14-2011, 07:02 PM -
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 05:49 AM -
creating a text based game
By Phobos0001 in forum New To JavaReplies: 1Last Post: 02-12-2008, 05:35 PM
Bookmarks