Robocode & Math

We’ve used a open-source tool called robocode.  Basically it is a game where you program robots in Java (or .Net). The robots battle in real-time and on-screen. It was a closing project (last 4 weeks) to our Intro to Programming class, and it was excellent. They really got into battling each other’s robots and creating code to do better and better. We had a round-robin regular season, and then a double-elimination playoff bracket to see whose robot was the best. Awesome stuff, and my bot was in the bottom half!

Anyway, I promised math, and I’ll deliver:

double absoluteBearing = getHeadingRadians() + e.getBearingRadians();

double enemyX = getX() + e.getDistance() * Math.sin(absoluteBearing);

double enemyY = getY() + e.getDistance() * Math.cos(absoluteBearing);

Now don’t be scared non-CS people.  Essentially what they are doing is they are finding out the position of the enemy robot by only knowing in what cardinal direction they are coming from, and knowing their (x,y) position.

Now even though I’m a math teacher, I promise, I didn’t lead them to sine and cosine!  I refused to provide any real useful hints, I just plotted two points on a coordinate plane and they figured it out.  I had a couple of others use the Pythagorean theorem to find the distance to a corner.  One student ended up using the law of cosines to calculate the length of a side of a non-right triangle.

Good stuff, just thought you algebra II teachers would enjoy the use of those formulas in situation outside the math classroom.

Leave a Reply

Your email address will not be published. Required fields are marked *