Jump to content

Object accessor help

- - - - -

  • Please log in to reply
11 replies to this topic

#1
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Hi all,

Having a bit of a problem with a part of my project for Uni, and am hoping someone can shed some light on the issue! I'm quite new to java and heavily OO concepts, so it might well be something simple, but after searching and searching, I can't find the reason for it.

Basically, I'm writing a maze game. My Maze is built upon a data map held in a 2D array. This "map" is a sequence of 2 letter strings, representing a different object in the maze (wall, player, coin etc...)

My problem comes though, when I create a class for my player, I want to be able to access this array, so that I can get locations of stuff from it and modify it's contents. When I try and access this array from my new class (which extends the class the object is created in) it says that every element within the array contains "null", therefore when searching my array, it doesn't come up with a lot! :P

I'm hoping that made sense. I'm really looking for some pointers on how to access objects like this from other, inheriting, classes - because I think this must be the problem. I've looked at Accessors and Mutators, but I'm not sure this is the problem in this case - even just calling methods to return the values of the array does the same thing.

Thanks in advance,
so1i
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs

#2
Vladimir

Vladimir

    Learning Programmer

  • Members
  • PipPipPip
  • 79 posts
You can make that array static and it will be shared between all instances. But better solution is to redesign you class inheritance, because I guess you misuse inheritance in this case. Anyway I can't advice anything without looking at the code.

#3
isuru

isuru

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 233 posts
I don't know whether this is cheating. But there is a Maze game developed by other developer. And there is a UML documentation available too. You can get ideas from that and develop your own game.
Java Graphical User Interface Game. Example of java swing programming.
Lost!

#4
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Thanks for replies guys, appreciate it. I understand it is hard without the code - here is an example of my problem.


//class 1

public class Class1 {

System.out.println(Class3.searchArray());

}


//class 2

public class Class2 extends Class1 {

String[][] strArray = new String[][];

}


//class 3

public class Class3 extends Class2 {

public int searchArray {

for(int y=0; y<25; y++){

for(int x=0; x<25; x++){

if (strArray[x][y]=="this") {

return x;

}

}

}

}

}

Obviously in the above code, there is something actually in the array, that's just an example of the bits that matter (I think). But happens is that instead of my array being full of the strings, it stays the same size, but when I print it it contains "null" in every element.

Making the array static, is that "bad practice"? - Even though it might work in this case, not really sure if it would cost me marks or not.

Thanks for the help so far!
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs

#5
nicckk

nicckk

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 629 posts
I'm not sure if this is causing the problem, but in Class3 your method searchArray's return type is void, therefore you should not have the return statement.

#6
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts

nicckk said:

I'm not sure if this is causing the problem, but in Class3 your method searchArray's return type is void, therefore you should not have the return statement.

Sorry man, typo. Was just trying to put together an example so as to get to the heart of the problem!
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs

#7
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Creating an array of type String doesn't actually fill it with Strings. As you've noticed it will all contain null. If you expect it to contain empty String you need to fill the whole array with empty Strings yourself.
Making the maze static would be bad practice.. it would mean that when you run your game 2 times the 2 will always have the same maze, if the player takes a coin in game 1 it will also be taken in game 2.

More important, i have to agree with what Vladimir said

Quote

you misuse inheritance in this case
Because you do :) Correct cases would be
if you have a Car class with subclasses like Toyota, Honda, Volvo, ... That makes sence because toyota, honda, volvo actually are cars.
If you have a Person class with subclasses like Teacher, Student, Mechanic,... Makes sence again because a teacher, student, mechanic ARE persons.

You can't let your Player class extend Maze because that would not make any sence.
To solve your problem the Player class should have a reference to Maze, given to it in the constructor

public class Player{

  private Maze maze;


  public Player(Maze maze){

    this.maze = maze

  }


  public void doSomething(){

    String[][] iNeedTheMaze = maze.getMaze();

  }

}


public class Maze{

  String[][] mazeArray;


  public Maze(){

    mazeArray = new String[25][25];

    for(int i=0; i<25; i++){

      for(int j=0; j<25; j++){

        mazeArray[i][j] = "";

      }

    }


  }

}


//somewhere else

public static void main(String[] args){

  Maze maze = new Maze();

  Player player = new Player(maze);

}


Now you could argue if it actually makes sence that the Player class has access / knows the whole maze and is able to change the maze from within the player class. That doesn't fully make sence(again).
If you'd also like to solve this, then the player no longer has a maze object, but another class has. Every time the player moves, that move is send to that other class which has access to the maze and the player, and this other class will do whatever is needed for the move.

Edited by wim DC, 29 November 2010 - 10:40 AM.


#8
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Ok, thanks for the reply. I think I get what you're saying.

At the moment I have 3 classes, Game, Maze and Player, which inherit like my example. But actually, none of them should inherit at the moment, so to get the array I need in my Player class, I need to create a Maze object (maybe within another class), and make a method within Maze to get the current array and contents - then use that method in Player (or other class) to store it into a new variable within that class?

Then I guess after I've changed it a bit, I send it back via the same way, but in reverse?
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs

#9
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Is this with a GUI, or text-based?

I think if I would make it i would have
public class Game{

  private Maze maze;

  private Player player;


  public Game(){

    maze = new Maze();

    player = new Player();

  }


  public void doMove(x, y){

    if(!player.isNeighbor(x,y) ){ return; }   //check if x,y is left, right, up or down of the player's current position. If it's no illegal move

    if(!maze.isPossible(x,y) ) { return; }    //check if position x,y in the grid isn't a wall or something, or isn't outOfBounds

    

    player.move(x,y);                         //basicly a setter of x and y together.

    player.addScore(maze.getContent(x,y);     //if there is a coin or something on x,y, add it to the player's score :P


    if( maze.isExit(x,y) ){

      OMGWIN();

    }

       

  }

}

And I only interact with the Game class. The Game class controls the Maze and Player class.

#10
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
It is text based! Just getting us to work with arrays, and accept user input, then work on some path-finding etc algorithms for some later levels where we must make our "hunters" chase the player via the shortest route.

Thanks for you're replies, oxana, it's been a big help. I have done it similar to how you stated above, making my user interact with only the Game class, and having the maze sent to the Player class as a parameter in it's constructor.

Cheers for the help! ...now just to get it finished and written up!
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
I hope you may use Lists for the path-finding stuff then :D... and otherwise i would suggest writing your own "list" class (not so hard) so you can dynamicly add and remove items from an array and if it goes out of bounds it automatically increases in size.

#12
so1i

so1i

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 312 posts
Cheers mate, will look them up! Thanks for the assistance!
My Company - My Homepage - My Twitter - My Google+ - My LinkedIn

"Things don’t have to change the world to be important.” - Steve Jobs




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users