Results 1 to 2 of 2
  1. #1
    mathias is offline Member
    Join Date
    Jul 2007
    Rep Power

    Default Help with class loading in java

    Hi, My friend and I are trying to make this text-based tactics game with java.

    We are trying to make an user interface, via console, as interactive as possible. So, we need quiet of commands. How would you manage all these commands? I was thinking about shoving all the classes into Object[] and calling them as users need it, but we hit the wall when I figured out that it is not possible to instance them all automatically.

    What is a good way to manage insane amount of class files and calling them as they fit? Or, should i just paste the whole program of 20000 if/else statements?


  2. #2
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Rep Power


    OK If I understand correctly, in your game if I typed "kill" you want it to match up with a class that handles the "kill" command and then runs some method on that class to perform the operation right?

    This is how I would do it. Create a file of commands and the class name ex.

    I would make all command classes implement an interface or inherit from a common base class. For my example I'll call it the Command interface.

    Then in the startup read the file and load the commands into a Map<String, Command>. You can do this by reading each line and splitting it on the '='. The left side is the key in the map and the right you use to instantiate the class like so:
    Java Code:
    Map<String, Command> commandMap = new HashMap<String, Command>();
    Class commandClass = this.getClass().getClassLoader().loadClass(rightSideClassName);
    Command com = (Command)commandClass.newInstance();
    commandMap.put(commandKey, com);
    Then when they type in a command just look it up in the map, pull out the Command class and call the method.

Similar Threads

  1. Loading Images - Imp
    By Thulasiraman in forum Advanced Java
    Replies: 0
    Last Post: 01-28-2008, 09:33 AM
  2. Loading An Image Help Please!
    By shaungoater in forum Java 2D
    Replies: 2
    Last Post: 01-09-2008, 08:14 AM
  3. Java Applet loading bar
    By genette in forum Java Applets
    Replies: 2
    Last Post: 12-27-2007, 03:39 AM
  4. Help Loading Up Pictures
    By marco in forum Java Applets
    Replies: 3
    Last Post: 08-14-2007, 10:32 PM
  5. How can I set a time out on the loading process
    By paul in forum Advanced Java
    Replies: 1
    Last Post: 08-07-2007, 04:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts