• Welcome to the arena
Wall Walker

From Marvin's Arena

Jump to: navigation, search
Name Wall Walker
Version 0.1
Author Sebastian Pech
Type Base

Description

Move to the nearest wall and rotate every time we hit a wall (Yes this hurts!).

Code (C#)

using MarvinsArena.Robot;
 
namespace WallWalker
{
	public class WallWalker: BaseRobot, IRobot
	{
		/// <summary>
		/// Initialize local variables
		/// </summary>
		public void Initialize()
		{
			ScannedRobot += new EventHandler<ScannedRobotEventArgs>(OnScannedRobot);
			HitWall += new EventHandler<EventArgs>(OnHitWall);
			HitRobot += new EventHandler<EventArgs>(OnHitRobot);
		}
 
		/// <summary>
		/// Run the robot
		/// </summary>
		public void Run()
		{
			// Move if not rotating
			if(RemainingRotation == 0)
			{
				MoveForward(32);
			}
		}
 
		void OnHitRobot(object sender, EventArgs e)
		{
			RotateRightDeg(90);
		}
 
		void OnHitWall(object sender, EventArgs e)
		{
			WalkWalls();
		}
 
		void OnScannedRobot(object sender, ScannedRobotEventArgs e)
		{
			FireMissile();
		}
 
		private void WalkWalls()
		{
			double rot = 90 - RotationDeg % 90;
			if(rot > 90)
				rot = 90;
 
			RotateRightDeg(rot);
			RotateGunRightDeg(RotationDeg - RotationGunDeg + 180);
			RotateRadarRightDeg(RotationDeg - RotationRadarDeg + 180);
		}
	}
}

Code (C++)

Please see remarks at [Setting_Up_Your_Robot#Standard_Robot:_C.2B.2].

WallWalker.h

#pragma once
 
using namespace System;
using namespace MarvinsArena::Robot;
 
namespace WallWalker {
 
	public ref class WallWalker : BaseRobot, IRobot
	{
	private:
		void WalkWalls();
 
	public:
		virtual void Initialize();
		virtual void Run();
 
		void OnScannedRobot(Object^ o, ScannedRobotEventArgs^ e);
		void OnHitWall(Object^ o, EventArgs^ e);
		void OnHitRobot(Object^ o, EventArgs^ e);
	};
}

WallWalker.cpp

#include "stdafx.h"
 
#include "WallWalker.h"
 
namespace WallWalker
{
	void WallWalker::Initialize()
	{
		ScannedRobot += gcnew EventHandler<ScannedRobotEventArgs^>(this, &WallWalker::OnScannedRobot);
		HitWall += gcnew EventHandler<EventArgs^>(this, &WallWalker::OnHitWall);
		HitRobot += gcnew EventHandler<EventArgs^>(this, &WallWalker::OnHitRobot);
	}
 
	void WallWalker::Run()
	{
		if(RemainingRotation == 0)
		{
			MoveForward(32);
		}
	}
 
	void WallWalker::OnScannedRobot(Object^ o, ScannedRobotEventArgs^ e)
	{
		FireMissile();
	}
 
	void WallWalker::OnHitWall(Object^ o, EventArgs^ e)
	{
		WalkWalls();
	}
 
	void WallWalker::OnHitRobot(Object^ o, EventArgs^ e)
	{
		RotateRightDeg(90);
	}
 
	void WallWalker::WalkWalls()
	{
		double rot = 90 - (int)RotationDeg % 90;
		if(rot > 90)
		{
			rot = 90;
		}
 
		RotateRightDeg(90);
		RotateGunRightDeg(RotationDeg - RotationGunDeg + 180);
		RotateRadarRightDeg(RotationDeg - RotationRadarDeg + 180);
	}
}