/*
* Number One: Whenever I attempt to add the newly created "NumberBots" to my ArrayList (on line 70 specifically)
* it throws an out of bounds exception at me. I am totally unsure as to why this is happening!!
* When I initialize the Collection list, it is of size 9, so I don't understand why it is saying it
* is going out of bounds. MAybe you can shed some light on the situation after looking at the code!
*
* Number Two: I have now organized everything into classes, so that the main is looking much better. However, I am pretty confused as to
* how I can get the Xbot's location passed into my Collection class, so that I can execute the sorting algorithm and then print.
* Specifically on lines 127 - 143.
*
* I have methods that allow the Xbot to retrieve it's own location once I have it created, but I am unsure where to go from here.
* Hopefully you have a few guiding words that will help push me towards finishing this. Thanks!!!
*/
import java.util.Random;
import java.util.*;
class Array2DExampleAndrewBrooks1 {
public static void main(String args[]) {
Array2DCreation numbers = new Array2DCreation();
numbers.setBots();
numbers.print();
//sort the ArrayList
//print the ArrayList
}
}
class Array2DCreation {
private static final int MAX_X = 19;
private static final int MAX_Y = 10;
Random random = new Random();
int randomInt1 = random.nextInt(MAX_X);
int randomInt2 = random.nextInt(MAX_Y);
Array2D<Bot> numbers;
public Array2DCreation() {
numbers = new Array2D<Bot>(MAX_X, MAX_Y);
numbers.initialize(null);
numbers.start(0,0, Array2D.DOWN);
}
public void setBots() {
setXbot();
setNumberBots();
}
public void setXbot() {
Xbot bot1 = new Xbot();
bot1.setCoordinates(randomInt1, randomInt2);
numbers.set(randomInt1,randomInt2,bot1);
}
public void setNumberBots() {
Collection list = new Collection();
for(int k = 0; k < 9 ; k++) {
boolean isUnique = false;
while(isUnique == false) {
int randoX = random.nextInt(MAX_X);
int randoY = random.nextInt(MAX_Y);
if(numbers.get(randoX, randoY) == null) {
NumberBot bot = new NumberBot();
bot.setCoordinates(randoX, randoY);
bot.setCounter(bot.getCounter());
//list.add(bot.getCounter(), bot); // THIS is the line of code that is throwing the out of bounds exception
numbers.set(randoX, randoY , bot);
isUnique = true;
} else {
randoX = random.nextInt(MAX_X);
randoY = random.nextInt(MAX_Y);
}
}
}
}
public void print() {
for(int i = 0; i < MAX_Y; i++) {
numbers.start(0, i, Array2D.RIGHT);
if(numbers.get() == null) {
System.out.print(" ");
} else {
System.out.print(numbers.get() + " ");
}
while (numbers.hasNext()) {
numbers.next();
if(numbers.get() == null) {
System.out.print(" ");
} else {
System.out.print(numbers.get() + " ");
}
}
System.out.println();
}
}
}
class Collection {
List<Bot> list;
public Collection() {
list = new ArrayList<Bot>(9);
}
public void add(int index, NumberBot bot) {
list.set(index, bot);
}
/*public static void sortList(List<Bot> list) { //This is my failed sort algorithm as I cannot figure out how to access xbot
Bot tempBot;
for(int k = 0; k < list.size(); k++) {
if (calculateDistance(bot, list.get(k)) > calculateDistance(bot, list.get(k+1))) {
tempBot = list.get(k);
list.set(k, list.get(k+1));
list.set((k+1), tempBot);
}
}
print(list);
}*/
public static void print(List<Bot> list) {
for(int i = 0; i < list.size(); i++ ) {
System.out.println("Numbered Bot " + (list.get(i)).toString());
}
}
}
abstract class Bot {
private int xCoordinate;
private int yCoordinate;
abstract public String toString();
public void setCoordinates(int xCoordinate, int yCoordinate) {
this.xCoordinate = xCoordinate;
this.yCoordinate = yCoordinate;
}
public int getX() {
return xCoordinate;
}
public int getY() {
return yCoordinate;
}
public double calculateDistance(Bot b) {
double totalDistance = 0;
int xDifference;
int yDifference;
if(xCoordinate > b.getX()) {
if(yCoordinate > b.getY()) {
xDifference = (xCoordinate - b.getX());
yDifference = (yCoordinate - b.getY());
} else {
xDifference = (xCoordinate - b.getX());
yDifference = (b.getY() - yCoordinate);
}
} else {
if(yCoordinate > b.getY()) {
xDifference = (b.getX() - xCoordinate);
yDifference = (yCoordinate - b.getY());
} else {
xDifference = (b.getX() - xCoordinate);
yDifference = (b.getY() - yCoordinate);
}
}
totalDistance = Math.sqrt(Math.pow(xDifference, 2) + Math.pow(yDifference, 2));
return totalDistance;
}
}
class Xbot extends Bot{
public String toString() {
return "X";
}
}
class NumberBot extends Bot{
private static int botCounter = 0;
private int botNumber;
public NumberBot() {
super();
botCounter = botCounter + 1;
}
public static int getCounter() {
return botCounter;
}
public void setCounter (int botCounter) {
botNumber = botCounter;
}
public String toString() {
return Integer.toString(botNumber);
}
}