Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package m;
- import robocode.*;
- import java.util.LinkedList;
- import java.util.Iterator;
- import java.util.Stack;
- public class m extends Robot {
- static boolean enemyPresent;
- static LinkedList<Rule> list;
- static Stack<Integer> stack;
- public void run() {
- System.out.println("I've reached run!"); // even this line doesn't print to the robot's console
- stack = new Stack<Integer>();
- while (true) {
- enemyPresent = false;
- System.out.println("Calling rules method");
- rules();
- }
- }
- public void rules() {
- noEnemySpotted noEnemySpottedObject = null;
- enemySpotted enemySpottedObject = null;
- list = new LinkedList<Rule>();
- list.add(noEnemySpottedObject);
- list.add(enemySpottedObject);
- Iterator<Rule> iterator = list.iterator();
- while(iterator.hasNext()) {
- Object element = iterator.next();
- if(element instanceof enemySpotted) {
- enemySpottedObject = new enemySpotted(enemyPresent);
- if(enemySpottedObject.condition()) {
- stack.push(enemySpottedObject.ruleID);
- }
- }
- if(element instanceof noEnemySpotted) {
- noEnemySpottedObject = new noEnemySpotted(enemyPresent);
- if(noEnemySpottedObject.condition()) {
- stack.push(noEnemySpottedObject.ruleID);
- }
- }
- }
- while(stack.empty() == false) {
- int action = stack.pop();
- System.out.println("Popping " + action);
- switch(action) {
- case 0: explore();
- break;
- case 1: shoot();
- break;
- }
- }
- }
- public void onScannedRobot(ScannedRobotEvent e) {
- enemyPresent = true;
- rules();
- }
- public void explore() {
- ahead(100);
- turnGunRight(360);
- back(100);
- turnGunRight(360);
- }
- public void shoot() {
- fire(1);
- }
- }
- abstract class Rule {
- int ruleID;
- boolean enemyPresent;
- public abstract boolean condition();
- }
- class noEnemySpotted extends Rule {
- noEnemySpotted(boolean enemyPresent) {
- this.enemyPresent = enemyPresent;
- ruleID = 0;
- }
- public boolean condition() {
- return (enemyPresent == false); // condition satisfied when there is no enemy detected
- }
- }
- class enemySpotted extends Rule {
- enemySpotted(boolean enemyPresent) {
- this.enemyPresent = enemyPresent;
- ruleID = 1;
- }
- public boolean condition() {
- return (enemyPresent == true); // condition satisfied when an enemy is detected
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement