Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Java help

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
bobthebob1234
LXF regular


Joined: Thu Jan 03, 2008 9:38 pm
Posts: 1368
Location: A hole in a field

PostPosted: Tue Nov 02, 2010 9:01 pm    Post subject: Java help Reply with quote

I'm not to sure what to google so I have come here first.

Here is the problem:
I have two classes, player and maze. The player object is created in the maze. In maze there is a method called checkFreeSpace and in player there is a method called move. I need the move method to use the checkFreeSpace method. Is this possible?

There isn't any inheritance or anything so I don't think parent or super would work?

Thanks
_________________
For certain you have to be lost to find the places that can't be found. Elseways, everyone would know where it was
Back to top
View user's profile Send private message
ethernet
LXF regular


Joined: Sat Oct 07, 2006 9:00 pm
Posts: 170
Location: Ireland

PostPosted: Wed Nov 03, 2010 9:02 am    Post subject: Reply with quote

Does checkFreeSpace() need to access instance variables in maze?

If you want move to be able to call checkFreeSpace(), is this method public? Depending on what checkFreeSpace() is actually doing, you could make it a static method. So you could then invoke in in player as player.checkFreeSpace() instead of invoking through an instance of player.

Feel free to post the code! Smile
Back to top
View user's profile Send private message
AndyBaxman
LXF regular


Joined: Tue Oct 04, 2005 9:47 am
Posts: 523

PostPosted: Wed Nov 03, 2010 12:40 pm    Post subject: Reply with quote

I assume you mean that the Player Class is created by an instance of the Maze Class.

If this is the case then the simplest solution would be to create a constructor in Player that takes an instance of Maze as its parameter, then store this in a Class instance variable.

Then all you would need to do in the move method would be to call m_myMaze.checkFreeSpace();

If there is only ever going to be one maze, then you could look at using a Singleton pattern. Then you could just:

Maze.getInstance().checkFreeSpace();

With regard to inheritance, there, indeed, shouldn't be any as Player isn't a kind of Maze, or vice-versa.
_________________
Bomb #20: "Let there be light"
Back to top
View user's profile Send private message
bobthebob1234
LXF regular


Joined: Thu Jan 03, 2008 9:38 pm
Posts: 1368
Location: A hole in a field

PostPosted: Wed Nov 03, 2010 5:33 pm    Post subject: Reply with quote

Thanks for the replys.

checkFreeSpace() does access instance variables in maze

Quote:

I assume you mean that the Player Class is created by an instance of the Maze Class.

I think so (I'm new to java and OO as you may have guessed)
In the maze constructor a new player is created. So when I create a maze, a player is also created at the same time.

I will have a play with the constructor parameter, thats quite a simple idea! Thanks

I'll also have a look at singleton patterns.

Thanks to both of you.
_________________
For certain you have to be lost to find the places that can't be found. Elseways, everyone would know where it was
Back to top
View user's profile Send private message
AndyBaxman
LXF regular


Joined: Tue Oct 04, 2005 9:47 am
Posts: 523

PostPosted: Thu Nov 04, 2010 10:16 am    Post subject: Reply with quote

From what you say it seems like you are creating a game with a Maze and a single player.

Possibly the best idea would be to instantiate the Player outside of the maze. While the Player uses the Maze it isn't a part of the Maze, so in strict OO methodology it shouldn't be created by the Maze.

Possibly create a Game Class and then:
Code:

public void startGame(){
   m_gameMaze = new Maze();
   m_gamePlayer = new player(m_gameMaze);
   m_gameMaze.initialise(); // or whatever....
}

_________________
Bomb #20: "Let there be light"
Back to top
View user's profile Send private message
bobthebob1234
LXF regular


Joined: Thu Jan 03, 2008 9:38 pm
Posts: 1368
Location: A hole in a field

PostPosted: Thu Nov 04, 2010 1:33 pm    Post subject: Reply with quote

This is some of what I have so far:
RunGame.java - This is run by the user hence the main method
Code:

public class RunGame {
   public static void main(String[] args) {
      maze myMaze = new maze();
   }
}


maze.java
Code:

public class maze {
   private String[][] mazeArray = new String[25][25];
   Player myPlayer = new Player(this);

   /**
    * Constructor for the maze. Put all +, etc into a 2d array
    */
   public maze(){
      //Some code to fill the mazeArray
      //Make a player
      int[] playerPos = this.giveSmartChord();
      myPlayer.SetPosition(playerPos[0],playerPos[1]);
      this.playGame();
   }

   public void drawMaze() {
      System.out.println("+-----------------------------------------------+");
      System.out.println("|              My Amazing Maze game             |");
      System.out.println("+-----------------------------------------------+");
      //Some more stuff
      System.out.println("+-----------------------------------------------+");
      System.out.println("| Level : "+level+" | Coins : "+coins+" | Watch out for the H!  |");
      System.out.println("+-----------------------------------------------+");
   }
   public void waitForInput() {
      System.out.println("Waiting for input...");
      char input = Keyboard.readChar();
      //System.out.println("You typed" + input);
      myPlayer.takeInput(input);
      
   }
   
   public void playGame() {
      this.drawMaze();
      this.waitForInput();
      this.playGame();
   }
}


Player.java
Code:

public class Player {
   protected int x,y;
   protected maze mazeName;

   public Player(maze mmaze) {
      mazeName = mmaze;
   }
   public void takeInput(char input) {
      //OOOH Look some pretty input
      int newChordX = x;
      int newChordY = y;
      switch(input){
         case 'g':
            //Move left, decrease x by 2
            newChordX = (x-2);
         break;
         case 'h':
            //More right
            newChordX = (x+2);
         break;
         case 'j':
            //Move up
            newChordY = (y-2);
         break;
         case 'k':
            //Move down
            newChordY = (y+2);
         break;
         default:
            System.out.println("I'm sorry, I don't know what to do with that");
      }
   }
}

_________________
For certain you have to be lost to find the places that can't be found. Elseways, everyone would know where it was
Back to top
View user's profile Send private message
nicephotog



Joined: Fri Nov 21, 2008 6:32 am
Posts: 14
Location: Australia

PostPosted: Wed Dec 08, 2010 2:28 am    Post subject: Reply with quote

It is much better managed this way

Using a "nested subclass" so the subclass can call any of the methods of the super class(does not need to notate super in the code unless preventing "this" class overridden methods of supers methods - anti ambiguity) and has access to all methods and variables of the superclass it is nested in.

In your way where both classes code is separate but in the same package level(folder)...
To access a class methods and variables from the class it was instantiated(started) in, requires the instantiated sub class using java.lang.reflect and java.lang.Class classForName and finding the classloader and thread. ...its excessively difficult for security restriction of access or process.

Code:
public class Maze{



// ---nested--- sub class Player can be created in maze
// as many times as you want/require (on a list or Array)
public class Player{



}//end Player


 
   public static void main(String[] args) {
   new Maze();
   }
}
Back to top
View user's profile Send private message Send e-mail
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast