Results 1 to 12 of 12
  1. #1
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Talking why dosent the 2d array change?

    in the Cplayer class i set it in the constructure to change the map but it dosent happen! the @ never shows up!

    Java Code:
    public class Cmap {
    	private int col,row;	
    	char[][] room= new char[5][5];
    
    	
    	public Cmap(){
    		for(row=0;row<5;row++){
    			for(col=0;col<5;col++){
    				room[row][col]='#';
    		}
    	}
    }
    
    public void Msqre(){
    	room[1][1]=' ';
    	room[1][2]=' ';
    	room[1][3]=' ';
    	room[2][1]=' ';
    	room[2][2]=' ';
    	room[2][3]=' ';
    	room[3][1]=' ';
    	room[3][2]=' ';
    	room[3][3]=' ';
    }
    
    
    public void prntRm(){
    	for(row=0;row<5;row++){
    		if(row>0){
    			System.out.printf("\n");
    		}
    		for(col=0;col<5;col++){
    			System.out.printf("%c",room[row][col]);
    			}
    		}
    	}
    }
    Java Code:
    import java.util.Scanner;
    
    public class Cplayer{
    public Cplayer(){
    	mpo.room[pPos[0]][pPos[1]]='@';
    	}
    
    public int pPos[]={3,3};
    Cmap mpo = new Cmap();
    Scanner DirInp = new Scanner(System.in);
    
    
    public void Pmve(){
    	System.out.printf("%d,%d\n", pPos[0],pPos[1]);
    	}
    }
    Java Code:
    public class TheBrain {	
    	public static void main(String[] args) {
    		Cmap cmo = new Cmap();
    		Cplayer cpo = new Cplayer();
    		cmo.prntRm();
    		cmo.Msqre();
    		cpo.Pmve();
    		cmo.prntRm();
    		
    	}
    }

  2. #2
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    oh come`on if you dont answer i cant continue with my game plz.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by TheBreadCat View Post
    oh come`on if you dont answer i cant continue with my game plz.
    Why are you bumping a thread less than 1 hour after it was posted? The usual appropriate time period to bump is 1 day. And patience is a virtue that is appreciated here. Best of luck.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Your problem is that your Cplayer changes the 2 day array held by a Cmap, but it's not the same Cmap object that is in your main method. If you want player to change this object, you have to pass a reference of this object into player, perhaps by giving Cplayer's constructor a Cmap parameter and passing the Cmap variable's reference into this constructor.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Do some debugging by adding some println statements to see where and when execution goes.

    Also some comments in the code about where the problem is would help.

  6. #6
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    soory about that Fubarable. any way im not 100% sure what you mean could you make a little simple examble?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    What Fubarable is saying is that you have 2 different CMap objects. One is created in the main() method: Cmap cmo = new Cmap();
    and the other in the CPlayer class: Cmap mpo = new Cmap();

    The CPlayer constructor changes its local version, not the one created in the main() method.

  8. #8
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    so how woud that seems like in code?

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,618
    Rep Power
    25

    Default

    Remove one of the definitions so there is only ONE instance of a Cmap object.
    Then pass that to any method that needs to see it or update it.

    Reread Fubarable's comment.

  10. #10
    TheBreadCat is offline Member
    Join Date
    May 2010
    Posts
    17
    Rep Power
    0

    Default

    YAY it workd what i did:
    made the cmo static and put it in the Cplayer class !

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    That is not the correct way to fix this as it will cause problems later. The correct way is to (again) give the Cplayer class a constructor that takes a Cmap object and then to store this object in a field of Cplayer and use it in Cplayer.

    Again, I strongly advise you not to do what you have suggested above.

  12. #12
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    In a general setting, I'd agree with Fubarable, but in the case of a game map, I acctually think the static aproach is the right one. If your think about it, all of the game objects inhabit the same map, the same instance of the object holding the map information. Of course, passing the map to the Player object is still the right way to do it, not simulating a global variable. So I'd do this:
    Java Code:
    public class GameMap {
    //bla bla bla
    }
    public class GameChar {
      private static GameMap map;
      //methods, constructors...
      public static setMap(GameMap m) {
        map = m;
      }
    }
    public class Game {
      public static void main(String[] args) {
        GameChar.setMap(new GameMap());
        GameChar c = new GameChar();
        //etc etc
      }
    }
    Last edited by m00nchile; 05-15-2010 at 02:25 PM.
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Need to change an ArrayList to an Array
    By trojansc82 in forum New To Java
    Replies: 9
    Last Post: 07-25-2010, 09:26 PM
  2. How to change JDK?
    By mew in forum Eclipse
    Replies: 5
    Last Post: 05-21-2010, 06:21 AM
  3. change string to array of integer
    By prof.deedee in forum New To Java
    Replies: 4
    Last Post: 11-09-2009, 03:47 AM
  4. Replies: 1
    Last Post: 03-31-2009, 07:40 AM
  5. the code dosent work
    By pcman in forum Java 2D
    Replies: 1
    Last Post: 03-20-2008, 09:20 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
  •