Results 1 to 2 of 2
  1. #1
    czarcalvinsk is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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];
    here is the render code
    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
    	}
    here is the move thing from another class:
    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;
    	}

  2. #2
    czarcalvinsk is offline Member
    Join Date
    Apr 2012
    Posts
    2
    Rep Power
    0

    Default 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

  1. Turn based game
    By knightwriter in forum New To Java
    Replies: 7
    Last Post: 12-14-2011, 07:02 PM
  2. Replies: 2
    Last Post: 08-05-2010, 05:49 AM
  3. creating a text based game
    By Phobos0001 in forum New To Java
    Replies: 1
    Last Post: 02-12-2008, 05:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •