Archived
1
0

-Major overhaul of design. Now using interfaces rather than extending

-Added DroneTools to allow a Drone to find out necessary information
-Prevented drone from accessing information it shouldn't get
-Changed moving again
-More stuff that I have forgotten by now
This commit is contained in:
Tony Grosinger 2011-04-22 19:46:44 -07:00
parent a2bb46503c
commit 70a933ac09
19 changed files with 208 additions and 235 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

4
sql_info.txt Normal file
View File

@ -0,0 +1,4 @@
redundan_nomadswiki
redundan_nomads
QQ[1_)gT5ST_!RZC^.

View File

@ -3,9 +3,10 @@ package net.grosinger.nomads;
/* /*
* Anything that is on the theWorld that is not a drone of some sort is a building * Anything that is on the theWorld that is not a drone of some sort is a building
*/ */
public class Building extends GameObject { public class Building implements GameObject {
Structure structure; Structure structure;
String name;
// Can't decide if I want to add Drone Houses as another enum item or as a // Can't decide if I want to add Drone Houses as another enum item or as a
// separate class that extends Building // separate class that extends Building
public enum Structure { public enum Structure {
@ -16,6 +17,16 @@ public class Building extends GameObject {
structure = thisBuilding; structure = thisBuilding;
} }
@Override
public String getName() {
return name;
}
@Override
public void setName(String newName) {
name = newName;
}
/* /*
* Information about how to use the switch command with enums * Information about how to use the switch command with enums
* http://download.oracle.com/javase/tutorial/java/javaOO/enum.html * http://download.oracle.com/javase/tutorial/java/javaOO/enum.html

View File

@ -6,134 +6,21 @@ import net.grosinger.nomads.DroneListItem.EnumMove;
* All drones will extend this class. Gives them access to basic set of tools * All drones will extend this class. Gives them access to basic set of tools
* they need such as movement, discovery, and world interaction. * they need such as movement, discovery, and world interaction.
*/ */
public class Drone extends GameObject { public interface Drone extends GameObject {
private int age;
/** /**
* Class constructor All custom drones should call their super constructor * This will give you a set of tools that will provide you will all the
* at the end of their own. * basic info your drone needs.
*/ */
public Drone() { public void setDroneTools(DroneTools yourTools);
if (Nomads.DEBUGSTATUS)
System.out.println("Drone initialized");
}
// Moving // Moving
// When moving, North is positive Y axis and East is positive X axis // When moving, North is positive Y axis and East is positive X axis
/**
* Starts the move, can be used to perform maintenance operations before the actual move
*/
public final EnumMove getMove() {
if (Nomads.DEBUGMOVES)
System.out.println("Drone " + name + " moving");
return move();
}
/** /**
* The main move method for all Drones. Custom Drones should override this * The main move method for all Drones. Custom Drones should override this
* with their own. * with their own.
*/ */
public EnumMove move() { public EnumMove move();
return EnumMove.NoMove;
}
/**
* Checks to see if the drone can move North
*
* @param amount
* of Distance to travel
* @return True if the move is possible, false if is invalid
*/
public final boolean canMoveNorth() {
return canMoveHelper(1, 0);
}
/**
* Checks to see if the drone can move South
*
* @param amount
* of Distance to travel
* @return True if the move is possible, false if is invalid
*/
public final boolean canMoveSouth() {
return canMoveHelper(-1, 0);
}
/**
* Checks to see if the drone can move East
*
* @param amount
* of Distance to travel
* @return True if the move is possible, false if is invalid
*/
public final boolean canMoveEast() {
return canMoveHelper(0, 1);
}
/**
* Checks to see if the drone can move West
*
* @param amount
* of Distance to travel
* @return True if the move is possible, false if is invalid
*/
public final boolean canMoveWest() {
return canMoveHelper(0, -1);
}
/**
* @param amountN
* The amount of desired travel North (negative will go South)
* @param amountE
* The amount of desired travel East (negative will go West)
* @return True if the move is possible, false if is invalid
*/
private final boolean canMoveHelper(int amountN, int amountE) {
int size = Nomads.awesomeWorld.getWorldSize();
if (getY() + amountN >= size || getY() + amountN < 0 || getX() + amountE >= size || getX() + amountE < 0) {
if (Nomads.DEBUGMOVES)
System.out.println("Cannot move, out of range");
return false;
} else if (Nomads.awesomeWorld.getObjectAt(getX() + amountE, getY() + amountN) != null) {
if (Nomads.DEBUGMOVES)
System.out.println("Cannot move, space occupied");
return false;
} else {
if (Nomads.DEBUGMOVES)
System.out.println("Can move");
return true;
}
}
// Getters and Setters
/**
* Returns how many turns this drone has been alive
*
* @return How many turns this drone has been alive
*/
public final int getAge() {
return age;
}
// TODO - find a way to prevent the drone from accessing this
/**
* Do not use under normal circumstances
*
* @param newAge
* Age to set as the Drone's current age
*/
public final void setAge(int newAge) {
age = newAge;
}
/**
* Increases the age of the drone by 1 turn
*/
public final void incrementAge() {
age++;
}
} }

View File

@ -17,8 +17,17 @@ public class DroneListItem {
private DroneListItem previous; private DroneListItem previous;
private Drone current; private Drone current;
/**
* The DroneTools for this Drone
*/
private DroneTools yourTools;
public enum EnumMove { public enum EnumMove {
NoMove, North, South, East, West, Upgrade, Attack NoMove, North, South, East, West, Upgrade, Attack, Steal
}
public enum Direction {
N, S, E, W
} }
// Stats about this robot // Stats about this robot
@ -31,6 +40,13 @@ public class DroneListItem {
private int speed; // Reflected in movements per turn private int speed; // Reflected in movements per turn
private int turning; private int turning;
private int cargoSpace; private int cargoSpace;
private int theft;
// Info about this robot
private int age;
private int x;
private int y;
/* /*
* Default constructor, includes all references * Default constructor, includes all references
@ -40,6 +56,10 @@ public class DroneListItem {
previous = thePrevious; previous = thePrevious;
current = theCurrent; current = theCurrent;
visibleDistance = 15; visibleDistance = 15;
// Give the Drone it's tools
yourTools = new DroneTools(current, this);
current.setDroneTools(yourTools);
} }
// Getters and Setters // Getters and Setters
@ -136,6 +156,7 @@ public class DroneListItem {
/** /**
* Retrieve the total space in the cargo hold of this drone. Does include * Retrieve the total space in the cargo hold of this drone. Does include
*
* space that is currently occupied. * space that is currently occupied.
* *
* @return <code>int</code> * @return <code>int</code>
@ -144,6 +165,42 @@ public class DroneListItem {
return cargoSpace; return cargoSpace;
} }
/**
* Retrieve the level of this drone in theiving
*
* @return <code>int</code>
*/
public int getTheft() {
return theft;
}
/**
* Returns how many turns this drone has been alive
*
* @return How many turns this drone has been alive
*/
public int getAge() {
return age;
}
/**
* Returns the x index of this drone
*
* @return <code>int</code>
*/
public int getX() {
return x;
}
/**
* Returns the y index of this drone
*
* @return <code>int</code>
*/
public int getY() {
return y;
}
/** /**
* Sets the next DroneListItem in the Linked List * Sets the next DroneListItem in the Linked List
* *
@ -170,7 +227,7 @@ public class DroneListItem {
* @param newDistance * @param newDistance
* <code>int</code> New Distance * <code>int</code> New Distance
*/ */
private void setVisibleDistance(int newDistance) { public void setVisibleDistance(int newDistance) {
visibleDistance = newDistance; visibleDistance = newDistance;
} }
@ -180,20 +237,27 @@ public class DroneListItem {
* @param amount * @param amount
* <code>int</code> How much to increase the distance * <code>int</code> How much to increase the distance
*/ */
private void increaseVisibleDistance(int amount) { public void increaseVisibleDistance(int amount) {
visibleDistance += amount; visibleDistance += amount;
} }
/**
* Increases the age of the drone by 1 turn
*/
public final void incrementAge() {
age++;
}
// Actions // Actions
/** /**
* Will ask the Drone what direction it would like to move * Will ask the Drone what direction it would like to move
* *
* @return * @return <code>boolean</code> if a move was made
*/ */
public boolean makeMove() { public boolean makeMove() {
// Call the Drone's Move method // Call the Drone's Move method
EnumMove move = current.getMove(); EnumMove move = current.move();
switch (move) { switch (move) {
case NoMove: { case NoMove: {
@ -201,27 +265,31 @@ public class DroneListItem {
return true; return true;
} }
case North: { case North: {
moveNorth(); moveDrone(Direction.N);
return true; return true;
} }
case South: { case South: {
moveSouth(); moveDrone(Direction.S);
return true; return true;
} }
case East: { case East: {
moveEast(); moveDrone(Direction.E);
return true; return true;
} }
case West: { case West: {
moveWest(); moveDrone(Direction.W);
return true; return true;
} }
case Upgrade: { case Upgrade: {
//TODO - Implement upgrade // TODO - Implement upgrade
return true; return true;
} }
case Attack: { case Attack: {
//TODO - Implement attack // TODO - Implement attack
return true;
}
case Steal: {
// TODO - Implement steal
return true; return true;
} }
default: { default: {
@ -233,79 +301,27 @@ public class DroneListItem {
// Movement // Movement
/** public void moveDrone(Direction direction) {
* Tests if a move North is possible. Will make the requested move if int amountN = 0;
* possible. int amountE = 0;
* switch (direction) {
* @param amount case N: {
* of distance to move North amountN = 1;
* @return True if the move was made, false if is invalid break;
*/ }
public final boolean moveNorth() { case S: {
if (current.canMoveNorth()) { amountN = -1;
moveHelper(1, 0); break;
return true; }
} else case E: {
return false; amountE = 1;
} break;
}
/** case W: {
* Tests if a move South is possible. Will make the requested move if amountE = -1;
* possible. break;
* }
* @param amount }
* of distance to move South Nomads.awesomeWorld.moveObjectAt(getX(), getY(), amountN, amountE);
* @return True if the move was made, false if is invalid
*/
public final boolean moveSouth() {
if (current.canMoveSouth()) {
moveHelper(-1, 0);
return true;
} else
return false;
}
/**
* Tests if a move East is possible. Will make the requested move if
* possible.
*
* @param amount
* of distance to move East
* @return True if the move was made, false if is invalid
*/
public final boolean moveEast() {
if (current.canMoveEast()) {
moveHelper(0, 1);
return true;
} else
return false;
}
/**
* Tests if a move West is possible. Will make the requested move if
* possible.
*
* @param amount
* of distance to move West
* @return True if the move was made, false if is invalid
*/
public final boolean moveWest() {
if (current.canMoveWest()) {
moveHelper(0,-1);
return true;
} else
return false;
}
/**
* Performs a move in the specified amounts
*
* @param amountN
* Amount of distance to move North (negative will go South)
* @param amountE
* Amount of distance to move East (negative will go West)
*/
public final void moveHelper(int amountN, int amountE) {
Nomads.awesomeWorld.moveObjectAt(current.getX(), current.getY(), amountN, amountE);
} }
} }

View File

@ -36,7 +36,7 @@ public class DroneTeam {
public DroneTeam(DroneListItem firstList) { public DroneTeam(DroneListItem firstList) {
first = firstList; first = firstList;
last = firstList; last = firstList;
teamName = firstList.getCurrent().name; teamName = firstList.getCurrent().getName();
currentBalance = 0; currentBalance = 0;
} }
@ -50,7 +50,7 @@ public class DroneTeam {
DroneListItem firstList = new DroneListItem(null, null, (Drone) firstDrone); DroneListItem firstList = new DroneListItem(null, null, (Drone) firstDrone);
first = firstList; first = firstList;
last = firstList; last = firstList;
teamName = firstDrone.name; teamName = firstDrone.getName();
currentBalance = 0; currentBalance = 0;
} }
@ -172,7 +172,24 @@ public class DroneTeam {
public void getMoves() { public void getMoves() {
DroneListItem current = first; DroneListItem current = first;
while (current != null) { while (current != null) {
current.getCurrent().getMove(); // Increment the drone's age
current.incrementAge();
// Have it perform it's moves
int turns = current.getSpeed();
int currentTurn = 1;
int currentTry = 1;
while (currentTurn <= turns) {
boolean moved = current.makeMove();
if (moved)
currentTurn++;
else
currentTry++;
// Give the drone 3 tries to make a valid move before skipping
// them
if (currentTry > 3)
break;
}
current = current.getNext(); current = current.getNext();
} }
} }

View File

@ -0,0 +1,61 @@
package net.grosinger.nomads;
/**
* Tools for the Drone to use. Only place methods in here that you want the
* drone to have access to.
*/
public class DroneTools {
private Drone referredDrone;
private DroneListItem listItem;
public DroneTools(Drone aDrone, DroneListItem droneParent) {
referredDrone = aDrone;
listItem = droneParent;
}
/**
* Returns the X value of your Drone
*
* @return <code>int</code> - X value
*/
public int getX() {
return listItem.getX();
}
/**
* Returns the Y value of your Drone
*
* @return <code>int</code> - Y value
*/
public int getY() {
return listItem.getY();
}
/**
* Returns the age of this Drone
*
* @return <code>int</code> - Age
*/
public int getAge() {
return listItem.getAge();
}
// Movement
public boolean canMoveNorth() {
return Nomads.awesomeWorld.getWorldGrid()[getY() + 1][getX()] == null;
}
public boolean canMoveSouth() {
return Nomads.awesomeWorld.getWorldGrid()[getY() - 1][getX()] == null;
}
public boolean canMoveEast() {
return Nomads.awesomeWorld.getWorldGrid()[getY()][getX() + 1] == null;
}
public boolean canMoveWest() {
return Nomads.awesomeWorld.getWorldGrid()[getY()][getX() - 1] == null;
}
}

View File

@ -3,29 +3,8 @@ package net.grosinger.nomads;
/* /*
* Everything that is found on the world will be a type of game object * Everything that is found on the world will be a type of game object
*/ */
public class GameObject { public interface GameObject {
private int x;
private int y; public String getName();
protected String name; public void setName(String newName);
// Getters and Setters
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setX(int newX) {
x = newX;
}
public void setY(int newY) {
y = newY;
}
public void setName(String newName) {
name = newName;
}
} }

View File

@ -85,8 +85,6 @@ public class World {
return WORLDSIZE; return WORLDSIZE;
} }
// Why would we need a setter for theWorld?
/** /**
* Used to set a new GameObject at a given location Not used for moving a * Used to set a new GameObject at a given location Not used for moving a
* GameObject * GameObject