Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java Question

  • 06-01-2006 8:14pm
    #1
    Closed Accounts Posts: 2,653 ✭✭✭


    I'm currently in the planning process for a program I'm going to be working on and there's one thing that I'm a bit confused about, wondering if anyone could point me in the right direction.

    Game calculates stuff
    Game sends AI information
    AI returns decision
    Repeat

    What the game does isn't important, and if I was just hardcoding my AI I'd know exactly what I was doing, but here's the catch. I want it to support user-written AIs. But those AIs should never be hard coded into my program or effect anything.

    I want something like:
    User writes myAI.java (inheriting from masterAI that is hardcoded into my program perhaps?)
    User compiles myAI.class
    User runs program
    Game sends masterAI information
    User goes to menu in program, picks to load 'myAI.class'
    Game now sends myAI information.


    I figure it's possible, I'm just not very object orientated. is it possible to essentially swap in different classes (using inheritence/interfaces?) that have the same methods but do them in different ways, via a menu within the program?


Comments

  • Registered Users, Registered Users 2 Posts: 307 ✭✭Thordon


    Its definetely possible, have a look at RoboCode, its a java game where you write AI code for a tank, and then have your tank fight other peoples self coded tanks, there might some explanation of how it works listed on their site somewhere.

    http://robocode.sourceforge.net/

    You might have to use the reflection AI to dynamically cast a class to a certain subclass, Im not too sure how I would go about it myself.


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    You could make sure that the myAI class extends a predefined interface, and use a ClassLoader.


  • Closed Accounts Posts: 1,502 ✭✭✭MrPinK


    If you look for a tutorial on the Java Reflection API that'll give you a good start on handling classes dynamically, but that basics of it should be something like:
    - Load the user class
    - Check if it implements the required interface
    - Create an instance of the class
    - Return the instance to Game for it to use.


  • Closed Accounts Posts: 2,653 ✭✭✭steviec


    Ok I'll look into that thanks a lot :)


  • Registered Users, Registered Users 2 Posts: 1,865 ✭✭✭Syth


    The java reflection stuff is exactly what you want. I'm working on it right now. I need to be able to load and analyse classes that are specified on the command line.

    Look into java.lang.Class class and Class.forName(String className) method, it returns a java.lang.Class that represents that class. You can also do funky things with Method invocation, but that'd be a bit beyond your needs. You can invoke constructors for that class to get a new instance of it.


  • Advertisement
Advertisement