Have started watching the MIT lecture series: Structure & Interpretation of Computer Programs
Lecture 01: http://www.youtube.com/watch?v=2Op3QLzMgSY
This should tie in nicely with my desire to learn LISP.
This was for a group networking protocol assignment in my data networks class.
It was challenging, although time consuming, and later I found a much more elegant solution on the internet. But this my ugly bastard child and I'm keeping it.
/**
* Determines the host machine's endianness.
*
* @return 1 if Little Endian, 0 if Big Endian
*/
int checkEndian () {
void * p;
unsigned short * shortP;
unsigned char buffer[2];
p = (void * ) buffer;
shortP = (unsigned short * ) p;
* shortP = 0x01;
if (buffer[0] == 0x00){
if (debug) {
printf("Big Endian\n");
}
return false;
}
else {
if (debug) {
printf("Little Endian\n");
}
return true;
}
}
/**
* Given a long long number reverse the order of the bytes
* if the machine is Little Endian. Otherwise do nothing and
* return the number.
*
* @param input - network value to be converted to host order
*/
unsigned long long ntohll (unsigned long long input) {
if (checkEndian()){
unsigned long long result = 0;
unsigned long long mask = 0xFF;
if (debug){
// Print INPUT
void * p;
unsigned char * buffer;
p = (void *) &input;
buffer = (unsigned char * ) p;
int j;
for (j = 7; j >= 0; j--) {
printf("%x ", buffer[j]);
}
printf("\n");
}
// Input will be reversed
// Need to reverse all 8 bytes
int i;
for ( i = 0; i < 8 ; i ++){
if (i < 4) {
result = result | ((input & ( mask << (i * 8) )) << (8 * (7 - 2 * i) ));
}
else {
result = result | ((input & ( mask << (i * 8) )) >> (8 * ( 2 * (i - 4) + 1) ));
}
}
// Print resulting value
if (debug) {
void * p;
unsigned char * bufferResult;
p = (void *) &result;
bufferResult = (unsigned char * ) p;
int j;
for (j = 7; j >= 0; j--) {
printf("%x ", bufferResult[j]);
}
printf("\n");
}
return result;
}
// If Big Endian, we can just return value without swapping bytes
else {
return input;
}
}
package ant;
public class HelloAnt {
public static void main(String[] args) {
System.out.println("Hello Ant!");
}
}
<project name="HelloAnt" default="run">
<description>
Simple Ant build file for Hello Ant
</description>
<property name="src.dir" location="src" />
<property name="build.dir" location="build" />
<target name="clean">
<delete dir="${build.dir}" />
</target>
<target name="makedir">
<mkdir dir="${build.dir}" />
</target>
<target name="compile" depends="clean, makedir">
<javac srcdir="${src.dir}" destdir="${build.dir}" />
</target>
<target name="run" depends="compile">
<java classname="ant.HelloAnt" fork="true">
<classpath>
<path location="${build.dir}" />
</classpath>
</java>
</target>
</project>
Re-did the methods in Position04. There now exists a method that has a coordinate as a parameter that moves the robot to that coordinate.
Also, I minimized the spin rotation. My previous code initially spins the robot to point to 0 degrees. This made for simpler code but lengthened the spin time. The code below now spins in direction that will produce the shortest rotation to point towards a destination coordinate.
Then, using Pythagorean, the robot moves from it's current position to the destination coordinate.
*************************************************************
package kgl;
import robocode.Robot;
/**
* Position 04 Robot
*
* Move to the center of the playing field, spin around in a circle, and stop.
*
* @class ICS 413
* @author Kevin Leong
* @date 9/2/10
*/
public class Position04 extends Robot{
/*
* Run Method
*/
public void run(){
//Get battlefield dimensions
double width = getBattleFieldWidth();
double height = getBattleFieldHeight();
//Assign coordinates for center and starting position 'current'
Position center = new Position(width/2, height/2);
//Go to the Center
goToCoordinate(center);
//Spin - We're Done!
double heading = getHeading();
turnRight(360 + 360 - heading);
}
/*
* Method that allows input of a coordinate and robot will go to
* that coordinate and stop
*
* @param Position p - x and y coordinate of destination
* @return Void
*/
private void goToCoordinate(Position p){
//Get Current Position
Position current = new Position(getX(), getY());
//Get current heading
double currentHeading = getHeading();
//Variable for recalibrating heading to -180 < 0 < 180
//based on turnRight()
double currentHeadingNormalized;
//Calculate the Distance and angle from the current point
//to the destination point
DistanceAngle ad = current.angleDistanceBetweenTwoPoints(p);
//Convert Heading to be between -180 and 180 only
if(currentHeading > 180){
currentHeadingNormalized = (-1) * (360 - currentHeading);
}
else
currentHeadingNormalized = currentHeading;
//Get the absolute difference between normalized heading and
//angle to destination point
double angleDeltaAbsolute = Math.abs(Math.abs(currentHeadingNormalized)
- Math.abs(ad.angle));
//Following Statements minimize spinning to point robot in position of
//destination point
//If current heading (normalized) is negative spin in the appropriate
//direction and the necessary rotation.
if (currentHeadingNormalized < 0){
//If angle and heading are both negative and angle is larger negative
if (ad.angle < currentHeadingNormalized){
turnLeft(angleDeltaAbsolute);
}
else if (ad.angle < 0){
turnRight(angleDeltaAbsolute);
}
else{
//If angle is positive then spin left or right depending on shortest
//rotation.
if (360 + ad.angle - currentHeading > 180){
turnLeft(currentHeading - ad.angle);
}
else{
turnRight(360 + ad.angle - currentHeading);
}
}
}
//Similar conditions for positive heading (normalized)
else{
if(ad.angle > currentHeadingNormalized){
turnRight(angleDeltaAbsolute);
}
else if(ad.angle > 0){
turnLeft(angleDeltaAbsolute);
}
else{
if (360 - currentHeading + ad.angle > 180){
turnLeft(currentHeading - ad.angle);
}
else{
turnRight(360 + currentHeading + ad.angle);
}
}
}
//At this point, the robot should be positioned to point towards
//the destiation point.
//Move Robot ahead the correct distance to the destination point
ahead(ad.distance);
}
}
/**
* Position Class to store position of center and starting position
* as coordinates. Also calculates:
*
* - Distance between two points
* - Angle between two points in degrees
*/
class Position{
double x;
double y;
public Position(double x, double y){
this.x = x;
this.y = y;
}
DistanceAngle angleDistanceBetweenTwoPoints(Position p){
double a = this.x - p.x;
double b = this.y - p.y;
//Calculate distance using Pythagorean Theorem
double distance = Math.sqrt(Math.pow(a, 2) + Math.pow(b,2));
//Angle variables for converting angle to degrees
double angleRadians, angleDegrees;
//Get angle in radians to center
angleRadians = Math.atan(a/b);
//Convert radians to degrees. This represents angle
//if robot starts in quadrant III
angleDegrees = 180 * angleRadians/Math.PI;
//If robot starts in quadrant I or II
if(b > 0){
angleDegrees = (180 - Math.abs(angleDegrees));
//If we are in quadrant I
if(a > 0){
angleDegrees *= -1;
}
}
return new DistanceAngle(distance, angleDegrees);
}
}
/**
* Object to hold calculations on Distance and Angle
* Calculated in the Position Class
*/
class DistanceAngle{
double distance;
double angle;
public DistanceAngle(double distance, double angle){
this.distance = distance;
this.angle = angle;
}
}