Java help

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Java help

Postby bobthebob1234 » Tue Nov 02, 2010 9:01 pm

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
User avatar
bobthebob1234
LXF regular
 
Posts: 1370
Joined: Thu Jan 03, 2008 9:38 pm
Location: A hole in a field

Postby ethernet » Wed Nov 03, 2010 9:02 am

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! :)
ethernet
LXF regular
 
Posts: 170
Joined: Sat Oct 07, 2006 8:00 pm
Location: Ireland

Postby AndyBaxman » Wed Nov 03, 2010 12:40 pm

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"
User avatar
AndyBaxman
LXF regular
 
Posts: 523
Joined: Tue Oct 04, 2005 8:47 am

Postby bobthebob1234 » Wed Nov 03, 2010 5:33 pm

Thanks for the replys.

checkFreeSpace() does access instance variables in maze

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
User avatar
bobthebob1234
LXF regular
 
Posts: 1370
Joined: Thu Jan 03, 2008 9:38 pm
Location: A hole in a field

Postby AndyBaxman » Thu Nov 04, 2010 10:16 am

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: Select all
public void startGame(){
   m_gameMaze = new Maze();
   m_gamePlayer = new player(m_gameMaze);
   m_gameMaze.initialise(); // or whatever....
}
Bomb #20: "Let there be light"
User avatar
AndyBaxman
LXF regular
 
Posts: 523
Joined: Tue Oct 04, 2005 8:47 am

Postby bobthebob1234 » Thu Nov 04, 2010 1:33 pm

This is some of what I have so far:
RunGame.java - This is run by the user hence the main method
Code: Select all
public class RunGame {
   public static void main(String[] args) {
      maze myMaze = new maze();
   }
}


maze.java
Code: Select all
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: Select all
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
User avatar
bobthebob1234
LXF regular
 
Posts: 1370
Joined: Thu Jan 03, 2008 9:38 pm
Location: A hole in a field


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests