/**
  * 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();
 }
  /**
   * 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();
    }
  }
 /** 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();
 }
Пример #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];
 }
Пример #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);
  }
Пример #6
0
  public void processInput(String input) {
    String[] tokens = input.split(" ");
    String word = tokens[0];
    if ("NEWGAME".compareToIgnoreCase(word) == 0) {
      String myName = tokens[1];
      String oppName = tokens[2];
      int stackSize = Integer.parseInt(tokens[3]);
      int bb = Integer.parseInt(tokens[4]);
      numHands = Integer.parseInt(tokens[5]);

      maj = new Historian(myName, oppName, stackSize, bb);
      // newGame();
    } else if ("KEYVALUE".compareToIgnoreCase(word) == 0) {
      if (tokens.length < 2) return;
      String[] smallTokens = tokens[1].split(":");
      if (smallTokens.length < 2) return;
      maj.notifyValue(smallTokens[0], smallTokens[1], tokens[2]); // the key value pair
    } else if ("NEWHAND".compareToIgnoreCase(word) == 0) {
      int handNum = Integer.parseInt(tokens[1]);

      if (handNum == numHands / 4) cons = true;
      if (handNum == numHands * 2 / 4) {
        System.out.println("Reg earnings: " + regEarnings + " consEarnings: " + consEarnings);
        if (consEarnings > regEarnings) cons = true;
        else cons = false;
      }

      System.out.println("Using conservative brain? " + cons);

      Card[] hand = new Card[3];
      hand[0] = CardUtils.getCardByString(tokens[3]);
      hand[1] = CardUtils.getCardByString(tokens[4]);
      hand[2] = CardUtils.getCardByString(tokens[5]);

      double timebank = Double.parseDouble(tokens[8]);

      Random rand = new Random();

      boolean callRaise = false, checkRaise = false;

      if (rand.nextDouble() < 0.5) {
        callRaise = true;
      }
      if (rand.nextDouble() < 0.5) {
        checkRaise = true;
      }

      if (cons) brain = new ConservativeBrain(maj, hand, timebank, callRaise, checkRaise);
      else brain = new Brain(maj, hand, timebank, callRaise, checkRaise);

      brain.handId = Integer.parseInt(tokens[1]);
      brain.button = Boolean.parseBoolean(tokens[2]);

      brain.board = new Card[5];

      brain.myBank = Integer.parseInt(tokens[6]);
      brain.oppBank = Integer.parseInt(tokens[7]);

      // newHand();
    } else if ("GETACTION".compareToIgnoreCase(word) == 0) {
      brain.potSize = Integer.parseInt(tokens[1]);

      brain.numBoardCards = Integer.parseInt(tokens[2]);
      int i = 3;
      for (; i < brain.numBoardCards + 3; i++)
        brain.board[i - 3] = CardUtils.getCardByString(tokens[i]);

      brain.numLastActions = Integer.parseInt(tokens[i]);
      brain.lastActions = new PerformedAction[brain.numLastActions];
      int j = i + 1;
      for (; j < brain.numLastActions + i + 1; j++) {
        brain.lastActions[j - i - 1] = ActionUtils.getPerformedActionByString(tokens[j]);
      }

      brain.numLegalActions = Integer.parseInt(tokens[j]);
      brain.legalActions = new LegalAction[brain.numLegalActions];

      int k = j + 1;
      for (; k < brain.numLegalActions + j + 1; k++)
        brain.legalActions[k - j - 1] = ActionUtils.getLegalActionByString(tokens[k]);

      brain.timebank = Double.parseDouble(tokens[k]);

      // String res = ActionUtils.performedActionToString((PerformedAction)brain.act());
      // System.out.println(res);
      outStream.println(ActionUtils.performedActionToString((PerformedAction) brain.act()));
    } else if ("HANDOVER".compareToIgnoreCase(word) == 0) {
      brain.myBank = Integer.parseInt(tokens[1]);
      brain.oppBank = Integer.parseInt(tokens[2]);

      if (cons) {
        consEarnings = brain.myBank - regEarnings;
      } else {
        regEarnings = brain.myBank;
      }

      brain.numBoardCards = Integer.parseInt(tokens[3]);
      int i = 4;
      for (; i < brain.numBoardCards + 4; i++)
        brain.board[i - 4] = CardUtils.getCardByString(tokens[i]);

      brain.numLastActions = Integer.parseInt(tokens[i]);
      brain.lastActions = new PerformedAction[brain.numLastActions];
      int j = i + 1;
      for (; j < brain.numLastActions + i + 1; j++) {
        brain.lastActions[j - i - 1] = ActionUtils.getPerformedActionByString(tokens[j]);
      }

      maj.update(brain.lastActions);
      maj.numHandsPlayed++;

      System.out.println("\nPFR: " + maj.getPFR());
      System.out.println("SDW: " + maj.getSDWRate() + "\n");
    } else if ("REQUESTKEYVALUES".compareToIgnoreCase(word) == 0) {
      // At the end, engine will allow bot to send key/value pairs to store.
      // FINISH indicates no more to store.
      outStream.println("DELETE " + maj.oppName);
      outStream.println("PUT " + maj.oppName + ":PFR " + maj.getValueToSave("PFR"));
      outStream.println("PUT " + maj.oppName + ":SDW " + maj.getValueToSave("SDW"));
      outStream.println("FINISH");
    }
  }