Ejemplo n.º 1
0
 /**
  * The generic activators of entities. If there is brain, this brain is activated via the doIt
  * method.
  */
 public void doIt() {
   beforeDoIt();
   if (myBrain != null) {
     myBrain.doIt();
   } else bodyDoIt();
   afterDoIt();
 }
Ejemplo n.º 2
0
  /**
   * Creates the brain and launches if it is an agent. The brain class is given as a String. The
   * name argument is used to instantiate the name of the corresponding agent. If the gui flag is
   * true, a bean is created and associated to this agent.
   */
  public void makeBrain(String className, String name, boolean gui, String behaviorFileName) {
    try {
      Class c;
      // c = Class.forName(className);
      c = madkit.kernel.Utils.loadClass(className);
      myBrain = (Brain) c.newInstance();
      myBrain.setBody(this);
      if (myBrain instanceof AbstractAgent) {
        String n = name;
        if (n == null) {
          n = getLabel();
        }
        if (n == null) {
          n = getID();
        }
        if (behaviorFileName != null) setBehaviorFileName(behaviorFileName);
        getStructure().getAgent().doLaunchAgent((AbstractAgent) myBrain, n, gui);
      }

    } catch (ClassNotFoundException ev) {
      System.err.println("Class not found :" + className + " " + ev);
      ev.printStackTrace();
    } catch (InstantiationException e) {
      System.err.println("Can't instanciate ! " + className + " " + e);
      e.printStackTrace();
    } catch (IllegalAccessException e) {
      System.err.println("illegal access! " + className + " " + e);
      e.printStackTrace();
    }
  }
Ejemplo n.º 3
0
 /** Delete an entity, and its brain if there is one. Do not ask directly this method. */
 public void delete() {
   // System.out.println("Actually deleting " + this);
   if (deleted) return;
   deleted = true;
   if (myBrain != null) {
     if (myBrain instanceof AbstractAgent)
       getStructure().getAgent().doKillAgent((AbstractAgent) myBrain);
     else myBrain.delete();
   }
   super.delete();
 }
Ejemplo n.º 4
0
 private Piece getWorstPiece(boolean hurt_player) {
   Brain.Move wMove = null;
   Brain.Move tMove;
   int index = 0;
   for (int i = 0; i < pieces.length; i++) {
     tMove = mBrain.bestMove(board, pieces[i], board.getHeight() - TOP_SPACE, null);
     if (i == 0) wMove = tMove;
     if (tMove == null) { // this piece loses the game now
       return pieces[i];
     }
     if ((hurt_player && tMove.score >= wMove.score)
         || (!hurt_player && tMove.score <= wMove.score)) {
       wMove = tMove;
       index = i;
     }
   }
   return pieces[index];
 }
Ejemplo n.º 5
0
  public void tick(int verb) {
    if (brainPlay.isSelected()) {
      board.undo();
      if (verb == DOWN) {
        if (cur_count != super.count) {
          mMove = mBrain.bestMove(board, currentPiece, board.getHeight() - TOP_SPACE, mMove);
          cur_count = super.count;
        }
        if (mMove == null || mMove.piece == null || currentPiece == null) {
          stopGame();
          return; // game over
        }
        if (!currentPiece.equals(mMove.piece)) super.tick(ROTATE);

        if (currentX < mMove.x) super.tick(RIGHT);
        if (currentX > mMove.x) super.tick(LEFT);
      }
    }
    super.tick(verb);
  }