Advertisement
Guest User

Untitled

a guest
Mar 28th, 2014
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.48 KB | None | 0 0
  1. package m;
  2.  
  3. import robocode.*;
  4. import java.util.LinkedList;
  5. import java.util.Iterator;
  6. import java.util.Stack;
  7.  
  8. public class m extends Robot {
  9.     static boolean enemyPresent;
  10.     static LinkedList<Rule> list;
  11.    
  12.     static Stack<Integer> stack;
  13.    
  14.     public void run() {
  15.         System.out.println("I've reached run!"); // even this line doesn't print to the robot's console
  16.         stack = new Stack<Integer>();
  17.         while (true) {
  18.                 enemyPresent = false;
  19.                 System.out.println("Calling rules method");
  20.                 rules();
  21.             }
  22.         }
  23.    
  24.     public void rules() {
  25.        
  26.         noEnemySpotted noEnemySpottedObject = null;
  27.         enemySpotted enemySpottedObject = null;
  28.        
  29.        
  30.         list = new LinkedList<Rule>();
  31.         list.add(noEnemySpottedObject);
  32.         list.add(enemySpottedObject);
  33.        
  34.         Iterator<Rule> iterator = list.iterator();
  35.         while(iterator.hasNext()) {
  36.             Object element = iterator.next();
  37.            
  38.             if(element instanceof enemySpotted) {
  39.                 enemySpottedObject = new enemySpotted(enemyPresent);
  40.                 if(enemySpottedObject.condition()) {
  41.                     stack.push(enemySpottedObject.ruleID);
  42.                 }
  43.             }
  44.            
  45.             if(element instanceof noEnemySpotted) {
  46.                 noEnemySpottedObject = new noEnemySpotted(enemyPresent);
  47.                 if(noEnemySpottedObject.condition()) {
  48.                     stack.push(noEnemySpottedObject.ruleID);
  49.                 }
  50.             }
  51.         }
  52.        
  53.         while(stack.empty() == false) {
  54.             int action = stack.pop();
  55.             System.out.println("Popping " + action);
  56.             switch(action) {
  57.                 case 0: explore();
  58.                 break;
  59.                 case 1: shoot();
  60.                 break;
  61.             }
  62.         }
  63.     }
  64.    
  65.     public void onScannedRobot(ScannedRobotEvent e) {
  66.          enemyPresent = true;
  67.          rules();
  68.      }
  69.      
  70.      public void explore() {
  71.          ahead(100);
  72.          turnGunRight(360);
  73.          back(100);
  74.          turnGunRight(360);
  75.      }
  76.      
  77.      public void shoot() {
  78.          fire(1);
  79.      }
  80.    
  81.    
  82. }
  83.  
  84. abstract class Rule {
  85.    
  86.     int ruleID;
  87.     boolean enemyPresent;
  88.    
  89.     public abstract boolean condition();
  90. }
  91.  
  92. class noEnemySpotted extends Rule {
  93.    
  94.     noEnemySpotted(boolean enemyPresent) {
  95.         this.enemyPresent = enemyPresent;
  96.         ruleID = 0;
  97.     }
  98.    
  99.     public boolean condition() {
  100.         return (enemyPresent == false); // condition satisfied when there is no enemy detected
  101.     }
  102. }
  103.  
  104. class enemySpotted extends Rule {
  105.    
  106.     enemySpotted(boolean enemyPresent) {
  107.         this.enemyPresent = enemyPresent;
  108.         ruleID = 1;
  109.     }
  110.    
  111.     public boolean condition() {
  112.         return (enemyPresent == true); // condition satisfied when an enemy is detected
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement