Halite Home

Java: How do you store more than 1 direction in a variable/location?


#1

So far I have been only factoring for the next move/location for each cell but I want to expand...
for (Direction f : Direction.CARDINALS) {
fNew = f;
numEnemyCount += 1;
while(gameMap.getSite(new Location(x, y), fNew).owner == myID) {
fNew += f; //adding 1 more move in that direction...?
numToEnemy += 1;
}
if (numEnemyCount < 1) {
lastNumEnemy = numToEnemy; //factoring in for the very first instance of the fNew
}
if (numEnemyCount >= 1 && numToEnemy < lastNumEnemy) {
lastNumEnemy = numToEnemy; //making sure the variable will be at it's lowest for the next loop
bestF = f; //setting the direction
}
}
Above, I attempted to create a program that, while you are surrounded by friendly squares, calculates the direction in which it will take the least moves to reach an enemy square but the one problem I have (i think), is that in the second for loop, I don't know how to add a move to the location. I tried adding the same direction to the previous direction but that didn't work. Do I have to do something with the location function?


#2

I don't really read Java, but the CARDINALS are just the numbers 1,2,3,4 each representing a direction. 1 is UP, 2 is RIGHT, 3 is DOWN, and 4 is LEFT. So adding 1 to a direction just gets you a different (possibly invalid) direction.

You probably just want to keep the direction the same during your loop as you search for the border. Bear in mind, this could lead to an infinite loop if you control a whole column or row of the map.


#3

Yeah, I get what you're saying but my intention wasn't to change the direction itself but to change the "amount" of said direction. I ended up making the fNew an array of direction which helped me a little bit but I still don't know how to get the new location of the cell via that array


#4

Maybe this? I'm not a Java person so use with caution...

[Edit: typo corrected.]

lastNumEnemy = 999;    // Some impossible score we're trying to beat
bestF = STILL;

for (Direction f : Direction.CARDINALS) {

    test_loc = new Location(x, y);
    numToEnemy = 0;
    
    while (1) {
        numToEnemy += 1;

        if (numToEnemy > 50) {      // Escape from infinite loops
            break;
        }

        test_loc = gameMap.getSite(test_loc, f);
        if (test_loc.owner != myID) {
            break;
        }
    }

    if (numToEnemy < lastNumEnemy) {
        bestF = f;
    }
}

Of course, this is not at all efficient if you're doing it for every piece.


#5

AmbiturnerBot basically implements similar logic: