1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*******************************************************************************
* Copyright (c) 2001, 2008 Mathew A. Nelson and Robocode contributors
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://robocode.sourceforge.net/license/cpl-v10.html
*
* Contributors:
* Pavel Savara
* - Initial implementation
*******************************************************************************/
package sampleex;
import robocode.AdvancedRobot;
import robocode.HitByBulletEvent;
import robocode.ScannedRobotEvent;
import robocode.robotinterfaces.IAdvancedEvents;
import robocode.robotinterfaces.IAdvancedRobot;
import robocode.robotinterfaces.IBasicEvents;
import robocode.robotinterfaces.IInteractiveEvents;
import robocode.robotinterfaces.peer.IBasicRobotPeer;
import java.io.PrintStream;
/**
* @author Pavel Savara (original)
*/
public class MasterAndSlave extends MasterBase implements IAdvancedRobot {
/**
* This is not showing any aditional qualities over normal MyFirst robot.
* But it could, because architecture is no more tied by inheritance from Robot base class.
*/
public void run() {
while (true) {
ahead(100); // Move ahead 100
turnGunRight(360); // Spin gun around
back(100); // Move back 100
turnGunRight(360); // Spin gun around
}
}
public void onScannedRobot(ScannedRobotEvent e) {
fire(1);
}
public void onHitByBullet(HitByBulletEvent e) {
turnLeft(90 - e.getBearing());
}
public IInteractiveEvents getInteractiveEventListener() {
return null;
}
}
/**
* This is robot derived from AdvancedRobot.
* Only reason to use this inheritance and this class is that external robots are unable to call RobotPeer directly
*/
class Slave extends AdvancedRobot {
final MasterBase parent;
public Slave(MasterBase parent) {
this.parent = parent;
}
public void run() {
parent.run();
}
public void onScannedRobot(ScannedRobotEvent e) {
parent.onScannedRobot(e);
}
public void onHitByBullet(HitByBulletEvent e) {
parent.onHitByBullet(e);
}
}
/**
* Infrastructure base class, for helpers and boring implementation details
*/
abstract class MasterBase {
public MasterBase() {
helperRobot = new Slave(this);
}
private final AdvancedRobot helperRobot;
public IAdvancedEvents getAdvancedEventListener() {
return helperRobot;
}
public IInteractiveEvents getSystemEventListener() {
return helperRobot;
}
public Runnable getRobotRunnable() {
return helperRobot;
}
public IBasicEvents getBasicEventListener() {
return helperRobot;
}
public void setPeer(IBasicRobotPeer robotPeer) {
helperRobot.setPeer(robotPeer);
}
public void setOut(PrintStream printStream) {
helperRobot.setOut(printStream);
}
public void turnGunRight(double degrees) {
helperRobot.turnGunRight(degrees);
}
public void turnLeft(double degrees) {
helperRobot.turnLeft(degrees);
}
public void ahead(double distance) {
helperRobot.ahead(distance);
}
public void back(double distance) {
helperRobot.back(distance);
}
public void fire(double power) {
helperRobot.fire(power);
}
public void onScannedRobot(ScannedRobotEvent e) {}
public void onHitByBullet(HitByBulletEvent e) {}
public void run() {}
}
|