# jfabre/TDD-workshop

1 parent eb29da5 commit b805e6802e5419567d4d83026a4a102e5e3d25ac Jeremy Fabre committed Mar 31, 2011
Showing with 439 additions and 25 deletions.
1. +439 −5 MonSuperRobot/Base/TestableRobot.cs
444 MonSuperRobot/Base/TestableRobot.cs
 @@ -26,11 +26,17 @@ public TestableRobot(ITestableRobot robot) _testableRobot = robot; } + /// + /// Setup the first moves of the robot before it starts doing things + /// public virtual void Init() { } + /// + /// This method is called infinitely, it is where you will do your things + /// public abstract void DoRobotThings(); public void Run() { @@ -278,6 +284,53 @@ public virtual void TurnLeft(double degree) _myRobot.TurnLeft(degree); } } + + + /// + /// Immediately fires a bullet. The bullet will travel in the direction the + /// gun is pointing. + ///

+ /// The specified bullet power is an amount of energy that will be taken from + /// the robot's energy. Hence, the more power you want to spend on the + /// bullet, the more energy is taken from your robot. + ///

+ /// The bullet will do (4 * power) damage if it hits another robot. If power + /// is greater than 1, it will do an additional 2 * (power - 1) damage. + /// You will get (3 * power) back if you hit the other robot. You can call + /// + /// for getting the damage that a + /// bullet with a specific bullet power will do. + ///

+ /// The specified bullet power should be between + /// and . + ///

+ /// Note that the gun cannot Fire if the gun is overheated, meaning that + /// + /// returns a value > 0. + ///

+ /// A event is generated when the bullet hits a robot (), + /// wall (), or another bullet + /// (). + ///

+ /// + /// + /// // Fire a bullet with maximum power if the gun is ready + /// if (GetGunHeat() == 0) + /// { + /// Fire(Rules.MAX_BULLET_POWER); + /// } + /// + /// + /// + /// + /// + /// + /// + /// + ///

+ /// + /// The amount of energy given to the bullet, and subtracted from the robot's energy. + /// public virtual void Fire(double power) { if (_onTest) @@ -289,6 +342,64 @@ public virtual void Fire(double power) _myRobot.Fire(power); } } + + /// + /// + /// Immediately fires a bullet. The bullet will travel in the direction the + /// gun is pointing. + ///

+ /// The specified bullet power is an amount of energy that will be taken from + /// the robot's energy. Hence, the more power you want to spend on the + /// bullet, the more energy is taken from your robot. + ///

+ /// The bullet will do (4 * power) damage if it hits another robot. If power + /// is greater than 1, it will do an additional 2 * (power - 1) damage. + /// You will get (3 * power) back if you hit the other robot. You can call + /// + /// for getting the damage that a + /// bullet with a specific bullet power will do. + ///

+ /// The specified bullet power should be between + /// and . + ///

+ /// Note that the gun cannot Fire if the gun is overheated, meaning that + /// returns a value > 0. + ///

+ /// A event is generated when the bullet hits a robot (), + /// wall (), or another bullet + /// (). + ///

+ /// + /// + /// // Fire a bullet with maximum power if the gun is ready + /// if (GetGunHeat() == 0) + /// { + /// Bullet bullet = FireBullet(Rules.MAX_BULLET_POWER); + /// + /// // Get the velocity of the bullet + /// if (bullet != null) + /// { + /// double bulletVelocity = bullet.Velocity; + /// } + /// } + /// + /// + /// Returns a + /// + /// That contains information about the bullet if it was actually fired, + /// which can be used for tracking the bullet after it has been fired. + /// If the bullet was not fired, null is returned. + /// + /// + /// + /// + /// + /// + /// + ///

+ /// + /// power the amount of energy given to the bullet, and subtracted from the robot's energy. + /// public virtual void FireBullet(double power) { if (_onTest) @@ -300,7 +411,35 @@ public virtual void FireBullet(double power) _myRobot.FireBullet(power); } } - + + /// + /// + /// Scans for other robots. This method is called automatically by the game, + /// as long as the robot is moving, turning its body, turning its gun, or + /// turning its radar. + ///

+ /// Scan will cause + /// to be called if you see a robot. + ///

+ /// There are 2 reasons to call manually: + ///

+ ///
1. + /// You want to scan after you stop moving. + ///
2. + ///
3. + /// You want to interrupt the + /// event. This is more likely. If you are in + /// and call + /// , and you still see a robot, then the system will interrupt your + /// event immediately and start it + /// from the top. + ///
4. + ///
+ ///

+ /// This call executes immediately. + /// + /// + ///

public virtual void Scan() { if (_onTest) @@ -312,6 +451,38 @@ public virtual void Scan() _myRobot.Scan(); } } + + /// + /// Sets all the robot's color to the same color in the same time, i.e. the + /// color of the body, gun, radar, bullet, and scan arc. + ///

+ /// You may only call this method one time per battle. A null + /// indicates the default (blue) color for the body, gun, radar, and scan + /// arc, but white for the bullet color. + ///

+ /// + /// + /// // Don't forget to using System.Drawing at the top... + /// using System.Drawing; + /// ... + /// + /// public void Run() + /// { + /// SetAllColors(Color.Red); + /// ... + /// } + /// + /// + /// + /// + /// + /// + /// + /// + /// + /// + ///

+ /// The new color for all the colors of the robot public virtual void SetAllColors(Color color) { if (_onTest) @@ -324,22 +495,22 @@ public virtual void SetAllColors(Color color) } } - public IRunnable GetRobotRunnable() + IRunnable IRunnable.GetRobotRunnable() { return this; } - public IBasicEvents GetBasicEventListener() + IBasicEvents IBasicRobot.GetBasicEventListener() { return this; } - public void SetPeer(IBasicRobotPeer peer) + void IBasicRobot.SetPeer(IBasicRobotPeer peer) { ((IBasicRobot) _myRobot).SetPeer(peer); } - public void SetOut(TextWriter output) + void IBasicRobot.SetOut(TextWriter output) { ((IBasicRobot)_myRobot).SetOut(output); } @@ -534,6 +705,39 @@ public virtual void TurnRadarLeft(double degrees) } } + /// + /// + /// Immediately turns the robot's gun to the right by degrees. + /// This call executes immediately, and does not return until it is complete, + /// i.e. when the angle remaining in the gun's turn is 0. + ///

+ /// Note that both positive and negative values can be given as input, + /// where negative values means that the robot's gun is set to turn left + /// instead of right. + ///

+ /// + /// + /// // Turn the robot's gun 180 degrees to the right + /// TurnGunRight(180); + /// + /// // Afterwards, turn the robot's gun 90 degrees to the left + /// TurnGunRight(-90); + /// + /// + /// + /// + /// + /// + /// + /// + /// + ///

+ /// + /// The amount of degrees to turn the robot's gun to the right. + /// If degrees > 0 the robot's gun will turn right. + /// If degrees < 0 the robot's gun will turn left. + /// If degrees = 0 the robot's gun will not turn, but execute. + /// public virtual void TurnGunRight(double degrees) { if (_onTest) @@ -546,6 +750,40 @@ public virtual void TurnGunRight(double degrees) } } + /// + /// + /// Immediately turns the robot's gun to the left by degrees. + ///

+ /// This call executes immediately, and does not return until it is complete, + /// i.e. when the angle remaining in the gun's turn is 0. + ///

+ /// Note that both positive and negative values can be given as input, + /// where negative values means that the robot's gun is set to turn right + /// instead of left. + ///

+ /// + /// + /// // Turn the robot's gun 180 degrees to the left + /// TurnGunLeft(180); + /// + /// // Afterwards, turn the robot's gun 90 degrees to the right + /// TurnGunLeft(-90); + /// + /// + /// + /// + /// + /// + /// + /// + /// + ///

+ /// + /// The amount of degrees to turn the robot's gun to the left. + /// If degrees > 0 the robot's gun will turn left. + /// If degrees < 0 the robot's gun will turn right. + /// If degrees = 0 the robot's gun will not turn, but execute. + /// public virtual void TurnGunLeft(double degrees) { if (_onTest) @@ -558,6 +796,15 @@ public virtual void TurnGunLeft(double degrees) } } + /// + /// + /// Immediately stops all movement, and saves it for a call to . + /// If there is already movement saved from a previous stop, this will have no effect. + ///

+ /// This method is equivalent to Stop(false). + /// + /// + ///

public virtual void Stop() { if (_onTest) @@ -651,6 +898,17 @@ public virtual double Energy return _onTest ? _testableRobot.Energy : _myRobot.Energy; } } + + /// + /// Returns the rate at which the gun will cool down, i.e. the amount of heat + /// the gun heat will drop per turn. + ///

+ /// The gun cooling rate is default 0.1 / turn, but can be changed by the + /// battle setup. So don't count on the cooling rate being 0.1! + /// + /// + /// + ///

public virtual double GunCoolingRate { get @@ -659,6 +917,15 @@ public virtual double GunCoolingRate } } + /// + /// Returns the direction that the robot's gun is facing, in degrees. + /// The value returned will be between 0 and 360 (is excluded). + ///

+ /// Note that the heading in Robocode is like a compass, where 0 means North, + /// 90 means East, 180 means South, and 270 means West. + /// + /// + ///

public virtual double GunHeading { get @@ -667,6 +934,20 @@ public virtual double GunHeading } } + /// + /// Returns the current heat of the gun. The gun cannot Fire unless this is + /// 0. (Calls to Fire will succeed, but will not actually Fire unless + /// GetGunHeat() == 0). + ///

+ /// The amount of gun heat generated when the gun is fired is 1 + (firePower / 5). + /// Each turn the gun heat drops by the amount returned by , + /// which is a battle setup. + ///

+ /// Note that all guns are "hot" at the start of each round, where the gun heat is 3. + /// + /// + /// + ///

public virtual double GunHeat { get @@ -675,6 +956,15 @@ public virtual double GunHeat } } + /// + /// Returns the direction that the robot's body is facing, in degrees. + /// The value returned will be between 0 and 360 (is excluded). + ///

+ /// Note that the heading in Robocode is like a compass, where 0 means North, + /// 90 means East, 180 means South, and 270 means West. + /// + /// + ///

public virtual double Heading { get @@ -690,15 +980,26 @@ public string Name return _onTest ? _testableRobot.Name : _myRobot.Name; } } + + /// + /// Returns the number of rounds in the current battle. + /// + /// public int NumRounds { get { return _onTest ? _testableRobot.NumRounds : _myRobot.NumRounds; } } + + + /// + /// Returns how many opponents that are left in the current round. + /// public int Others { get { return _onTest ? _testableRobot.Others : _myRobot.Others; } } + public Color RadarColor { get @@ -720,6 +1021,47 @@ public Color RadarColor public double Height { get { return _onTest ? _testableRobot.Height : _myRobot.Height; } } + + /// + /// Sets the gun to turn independent from the robot's turn. + ///

+ /// Ok, so this needs some explanation: The gun is mounted on the robot's + /// body. So, normally, if the robot turns 90 degrees to the right, then the + /// gun will turn with it as it is mounted on top of the robot's body. To + /// compensate for this, you can call + /// . + /// When this is set, the gun will turn independent from the robot's turn, + /// i.e. the gun will compensate for the robot's body turn. + ///

+ /// Note: This method is additive until you reach the maximum the gun can + /// turn. The "adjust" is added to the amount you set for turning the robot, + /// then capped by the physics of the game. If you turn infinite, then the + /// adjust is ignored (and hence overridden). + ///

+ /// + /// Assuming both the robot and gun start Out facing up (0 degrees): + /// + /// // Set gun to turn with the robot's turn + /// setAdjustGunForRobotTurn(false); // This is the default + /// TurnRight(90); + /// // At this point, both the robot and gun are facing right (90 degrees) + /// TurnLeft(90); + /// // Both are back to 0 degrees + /// + /// -- or -- + /// + /// // Set gun to turn independent from the robot's turn + /// setAdjustGunForRobotTurn(true); + /// TurnRight(90); + /// // At this point, the robot is facing right (90 degrees), but the gun is still facing up. + /// TurnLeft(90); + /// // Both are back to 0 degrees. + /// + /// + ///

+ /// Note: The gun compensating this way does count as "turning the gun". + /// + ///

public bool IsAdjustGunForRobotTurn { get @@ -739,6 +1081,44 @@ public bool IsAdjustGunForRobotTurn } } + /// + /// Sets the radar to turn independent from the gun's turn. + ///

+ /// Ok, so this needs some explanation: The radar is mounted on the robot's + /// gun. So, normally, if the gun turns 90 degrees to the right, then the + /// radar will turn with it as it is mounted on top of the gun. To compensate + /// for this, you can call = (true). + /// When this is set, the radar will turn independent from the robot's turn, + /// i.e. the radar will compensate for the gun's turn. + ///

+ /// Note: This method is additive until you reach the maximum the radar can + /// turn. The "adjust" is added to the amount you set for turning the gun, + /// then capped by the physics of the game. If you turn infinite, then the + /// adjust is ignored (and hence overridden). + ///

+ /// + /// Assuming both the gun and radar start Out facing up (0 degrees): + /// + /// // Set radar to turn with the gun's turn + /// SetAdjustRadarForGunTurn(false); // This is the default + /// TurnGunRight(90); + /// // At this point, both the radar and gun are facing right (90 degrees); + /// + /// -- or -- + /// + /// // Set radar to turn independent from the gun's turn + /// SetAdjustRadarForGunTurn(true); + /// TurnGunRight(90); + /// // At this point, the gun is facing right (90 degrees), but the radar is still facing up. + /// + /// + /// Note: Calling will automatically call + /// with the same value, unless you have + /// already called it earlier. This behavior is primarily for backward compatibility + /// with older Robocode robots. + /// + /// + ///

public bool IsAdjustRadarForGunTurn { get @@ -758,6 +1138,43 @@ public bool IsAdjustRadarForGunTurn } } + /// + /// Sets the radar to turn independent from the robot's turn. + ///

+ /// Ok, so this needs some explanation: The radar is mounted on the gun, and + /// the gun is mounted on the robot's body. So, normally, if the robot turns + /// 90 degrees to the right, the gun turns, as does the radar. Hence, if the + /// robot turns 90 degrees to the right, then the gun and radar will turn + /// with it as the radar is mounted on top of the gun. To compensate for + /// this, you can call = true. + /// When this is set, the radar will turn independent from the robot's turn, + /// i.e. the radar will compensate for the robot's turn. + ///

+ /// Note: This method is additive until you reach the maximum the radar can + /// turn. The "adjust" is added to the amount you set for turning the robot, + /// then capped by the physics of the game. If you turn infinite, then the + /// adjust is ignored (and hence overridden). + ///

+ /// + /// Assuming the robot, gun, and radar all start Out facing up (0 + /// degrees): + /// + /// // Set radar to turn with the robots's turn + /// setAdjustRadarForRobotTurn(false); // This is the default + /// TurnRight(90); + /// // At this point, the body, gun, and radar are all facing right (90 degrees); + /// + /// -- or -- + /// + /// // Set radar to turn independent from the robot's turn + /// setAdjustRadarForRobotTurn(true); + /// TurnRight(90); + /// // At this point, the robot and gun are facing right (90 degrees), but the radar is still facing up. + /// + /// + /// + /// + ///

public bool IsAdjustRadarForRobotTurn { get @@ -776,7 +1193,13 @@ public bool IsAdjustRadarForRobotTurn } } } + + /// + /// Returns the current round number (0 to - 1) of the battle. + /// + /// public int RoundNum { get { return _onTest ? _testableRobot.RoundNum : _myRobot.RoundNum; } } + public Color ScanColor { get { return _onTest ? _testableRobot.ScanColor : _myRobot.ScanColor; } @@ -792,9 +1215,20 @@ public Color ScanColor } } } + + /// + /// Returns the velocity of the robot measured in pixels/turn. + ///

+ /// The maximum velocity of a robot is defined by + /// (8 pixels / turn). + /// + ///

public double Velocity { get { return _onTest ? _testableRobot.Velocity : _myRobot.Velocity; } } + public double Width { get { return _onTest ? _testableRobot.Width : _myRobot.Width; } } + public double X { get { return _onTest ? _testableRobot.X : _myRobot.X; } } + public double Y { get { return _onTest ? _testableRobot.Y : _myRobot.Y; } } } }