Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: adding to a multidimensional array.

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default adding to a multidimensional array.

    Hiya,

    I am creating a game. I have two player classes, a gameState class and a gamePlay class (and a few other classes).

    -computerPlayer extends Player
    -keyboardPlayer extends Player
    - GameState
    -playGame

    In playGame:
    Player red = new keyboardPlayer();
    Player yellow = new computerPlayer();

    I have a method move(int mov) which adds to a multidimensional array board[i][j] = turn(); in gameState. Both player classes use it, the only difference is keyboard has a int keyboard input and computer is a random int. When I used the move method, however (which also prints the current board (as 2D array in the terminal)) something odd happened. Each time the board printed itself only the moves from that player displayed (ie all reds moves and its past moves), instead of red and yellow on one board. There is only one array and i am not aware of copying it. So I have a red board - with all reds previous moves - and the new one, or on yellows turn, a yellow board with all yellow previous moves - and the new one. Why is this? All that is in each player class is in input type (keyboard or random) and
    double number = (Math.random()*20);
    int mov = (int) number;
     gameState.move(mov).

    move would work fine if I didnt have to have two player classes. The only problem is they dont print on the same board/Not in the same array.
    Last edited by Scotty; March 26th, 2011 at 03:27 PM. Reason: capital


  2. #2
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: adding to a multidimensional array.

    It sets up the board by adding the first elements to the array, but then, because move(mov), is called from different classes:.

    red -- > adds R to the board. yellow --> adds R to a NEW board?!. red --> add Y to first board.yellow --> add Y to the new board. instead of add R,Y,R,Y to the same board I get from only using using red or yellow player class.

    If I just use red (two keyboard players) or yellow (two random) it works.

    Help!
    Last edited by Scotty; March 26th, 2011 at 03:09 PM.

  3. #3
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: adding to a multidimensional array.

    I assume to do with the fact I have GameState gameState = new GameState(); in both player classes, but I get a null pointer exception at move(mov) in both if I take out the new.

  4. #4
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: adding to a multidimensional array.

    I am assuming this makes no sense to anyone. Thanks anyway.

Similar Threads

  1. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 2
    Last Post: September 15th, 2011, 08:11 AM
  2. multidimensional array 'advancing'?
    By kitube in forum Java Theory & Questions
    Replies: 5
    Last Post: January 25th, 2011, 02:23 PM
  3. Need help in multidimensional array
    By Stefan_Lam in forum Algorithms & Recursion
    Replies: 3
    Last Post: January 14th, 2010, 08:52 PM
  4. Adding to Array from JavaSpace problem
    By rtumatt in forum Collections and Generics
    Replies: 3
    Last Post: January 5th, 2010, 07:19 PM
  5. Looping through a multidimensional array
    By MysticDeath in forum Loops & Control Statements
    Replies: 2
    Last Post: October 11th, 2009, 06:41 PM