/* * GetOutDaWayDriver.java * * Created on August 23, 2001, 10:34 AM */ import java.io.*; import java.util.*; /** * * @author Chris Schmidt * @version */ public class GetOutDaWayDriver { /** Creates new GetOutDaWayDriver */ public GetOutDaWayDriver() { } /** * Runs the test driver for the 'Get out da way' program. * @param args an array of command-line arguments */ public static void main(java.lang.String[] args) { String temp; File testFile = null; BufferedReader in = null; String line; Vector tBulletData = new Vector(), tTargetData = new Vector(); // Vectors holding temp data for these elements Vector bulletData = new Vector(); // Vector holding the final (real) data Target target = null; Bullet bullet = null; Double x,y,z; Double targX = null, targY = null, targZ = null; boolean bHit = false; boolean bTargetVelocity = true; // vars for target statistics double height = 0.0; int width = 0; // Open the reader to get the data from the file try { in = new BufferedReader(new InputStreamReader(System.in)); line = "NULL"; while (line != null) { line = in.readLine(); if (line != null) { if (line.length() >= 5 && line.substring(0,5).compareTo("START") == 0) { // Start of a new dataset, reset the variables bTargetVelocity = true; height = 0.0; width = 0; targX = null; targY = null; targZ = null; } else if (line.trim().compareTo("END") != 0 && line.trim().compareTo("START") != 0) { // Determine if this line is a vector or for the target if (line.indexOf(",") != -1) { temp = line.substring(0, line.indexOf(",")); } else { temp = ""; } try { Double testInt = new Double(temp); // No exception means that the substring is a number, this should be a vector // - Split the vector into three pieces x = testInt; temp = line.substring(line.indexOf(",") + 1, line.lastIndexOf(",")).trim(); y = new Double(line.substring(line.indexOf(",") + 1, line.lastIndexOf(",")).trim()); z = new Double(line.substring(line.lastIndexOf(",") + 1, line.length()).trim()); // Add the vectors singly, we will handle putting them into the BulletVector after reading everything if (bTargetVelocity) { // This velocity is for the target, not a bullet targX = x; targY = y; targZ = z; bTargetVelocity = false; } else { tBulletData.add(x); tBulletData.add(y); tBulletData.add(z); } } catch (Exception NaN) { // This is not a number, thus it is a part of the target. Add the line to the target vector to store // update the max width if this line is longer than the previous one if (line.length() > width) { width = line.length(); } // Add the line to the vector tTargetData.add(line); // Each line signifies 10cm height += .1; } } else if (line.trim().compareTo("END") == 0) { // Received an 'END' line, process the current data and output the result // parse the data into Bullet and Target objects // 1. Bullets for (Enumeration eBullets = tBulletData.elements(); eBullets.hasMoreElements();) { x = (Double)eBullets.nextElement(); y = (Double)eBullets.nextElement(); z = (Double)eBullets.nextElement(); bullet = new Bullet(x.doubleValue(), y.doubleValue(), z.doubleValue(), (height/2.0)); bulletData.add(bullet); } // 2. Target (the width is in 10cm blocks, make into just cm) target = new Target(tTargetData,height,width*10,targX.doubleValue(),targY.doubleValue(),targZ.doubleValue()); // Go through each bullet and see if it will hit the target bHit = false; for (Enumeration bullets = bulletData.elements(); bullets.hasMoreElements();) { bullet = (Bullet)bullets.nextElement(); // See if the target will be hit at the moment that the bullet passes through the plane it inhabits if (target.isHit(bullet.getVector())) { // We have at least one hit bHit = true; } } // If there is a hit, print out the target if (bHit) { target.printTarget(); System.out.println(""); } else { System.out.println("Got Out Da Way!"); System.out.println(""); } // Clear the variables in case another data set is forthcoming tBulletData.clear(); tTargetData.clear(); bulletData.clear(); height = 0.0; width = 0; target = null; bullet = null; } } } } catch (IOException ioerr) { ioerr.printStackTrace(); } } } /** * Class encapsulating a single bullet. * Creation date: (6/17/01 6:40:32 PM) * @author: Chris Schmidt */ class Bullet { // 3-Dimensional vector (speed in m/s) private BulletVector bulletVector; // the height where the bullets start private double height; /** * BulletVector constructor */ public Bullet(double x, double y, double z, double height) { super(); // Initiate the vector that the bullet will travel bulletVector = new BulletVector(x,y,z); this.height = height; } // Return a position vector after a certain amount of seconds // based on the travel vector public BulletVector getPos(double dSeconds) { // Assume that the vector starts at <0,0,height> double x,y,z; BulletVector result = new BulletVector(0.0,0.0,height); // Calculate each component of the vector // - X x = 0.0 + dSeconds * bulletVector.getX(); // - Y y = 0.0 + dSeconds * bulletVector.getY(); // - Z z = height + dSeconds * bulletVector.getZ(); result.setVector(x,y,z); return result; } public BulletVector getVector() { return bulletVector; } } /** * Class encapsulating a single bullet vector. * Creation date: (6/17/01 6:40:32 PM) * @author: Chris Schmidt */ class BulletVector { // 3-Demensional vector private double x,y,z; /** * BulletVector constructor comment. */ public BulletVector(double x, double y, double z) { super(); this.x = x; this.y = y; this.z = z; } public double getX() { return this.x; } public double getY() { return this.y; } public double getZ() { return this.z; } public void setVector(double x,double y,double z) { this.x = x; this.y = y; this.z = z; } } /** * Insert the type's description here. * Creation date: (6/20/01 2:54:57 PM) * @author: Chris Schmidt */ class Target { private double height; private double width; private String [] mass; private double x,y,z; // The velocity of the target itself /** * Target constructor - create a target object */ public Target(Vector mass, double height, int width, double x, double y, double z) { super(); // The "mass" of the target is equal to the height (in 10.0 cm chunks) in meters // -- Which is (height * 100)/10 this.mass = new String[(int)Math.round(height * 10.0)]; // Add the lines into the string array Enumeration massLines = mass.elements(); for (int loop = 0; loop < this.mass.length; loop++) { this.mass[loop] = (String)massLines.nextElement(); } // Height already in meters this.height = height; // Width in cm! this.width = width; // Velocity in meters this.x = x; this.y = y; this.z = z; } /** * Returns true if the bullet vector crosses a 'piece of mass' of the target. * @return boolean * @param bulletVec com.ibm.matrix.BulletVector * @param time double */ public boolean isHit(BulletVector bulletVec) { boolean bHit = false; int zPos; double time; // Assume that the vector is at 10 meters north of the shot // First, check if the two object will ever intersect if (bulletVec.getX() - this.x <= 0) { // The target and bullet are travelling away from each other or standing still compared // to each other return false; } else { // Determine the time where the bullet will pass through the target time = (10.0 / (bulletVec.getX() - this.x)); } // Check to see if the bullet would be above or below the mass (z), // take into account the target's velocity and the height of the target double z = ((bulletVec.getZ() - this.z) * time) + (this.height/2); if (z < this.height && z > 0.0) { // Check the y axis // - Determine which line the bullet would hit zPos = (int)(z * 10.0); zPos = (mass.length-1) - zPos; StringBuffer yLine = new StringBuffer(mass[zPos]); double y = (bulletVec.getY() - this.y) * time; // in meters, convert to cm y = (y * 100.0); // The center of the target is at y == 0.0, see if the bullet will miss the target completely if (Math.abs(y) < (width / 2.0)) { // The bullet could possibly hit the target. Find where it will hit within this line int pos = (int)(((width/2.0) + y)/10.0); if (pos < yLine.length()) { char c = yLine.charAt(pos); if (c != ' ') { // We have a hit! You sunk my battleship... // - Update the string buffer and replace in the target array yLine = yLine.replace(pos,pos+1,"*"); mass[zPos] = yLine.toString(); bHit = true; } } } } return bHit; } // Print the target line by line public void printTarget() { for (int loop = 0; loop < mass.length; loop++) { System.out.println(mass[loop]); } } }