This site requires JavaScript, please enable it in your browser!
Greenfoot back
s1ice
s1ice wrote ...

2022/2/16

Determining who's in first place (with interesting conditions)

s1ice s1ice

2022/2/16

#
I've been thinking about this for a while, and while I have no code for this yet, since this is an entirely theoretical question, how would you be able to accurately portray who is in first place (probably by using an int variable) in a race of some sort? And further on, what if there were varying levels of complexity such as racers with different starting times and positions in a curved, non-linear track? I'm planning on working on some sort of determiner for this, and I'm wondering what the best method of performing this would be.
TheMuncher TheMuncher

2022/2/16

#
We need code and context behind this to properly answer your question.
danpost danpost

2022/2/16

#
s1ice wrote...
how would you be able to accurately portray who is in first place (probably by using an int variable) in a race of some sort?
Yes -- an int variable in the class of the racer could be used to hold that racers best time. However, it would not be best to sort out who has the current best time. As an alternative, a mapped array in the world class could be used (mapping times to racers). It would be easier to keep sorted that way.
what if there were varying levels of complexity such as racers with different starting times and positions in a curved, non-linear track?
That should not be an issue with either method.
I'm planning on working on some sort of determiner for this, and I'm wondering what the best method of performing this would be.
That really depends on how you want to use the information.
s1ice s1ice

2022/2/16

#
I guess my question is how to determine who is currently closest to the end of a pathway. One way of determining that is by measuring the distance of all racers to the end of the track, but that would only work if it were a straight (or relatively so) path. I could use the game Bloons TD 6 as an example for this, the towers are always able to target the enemy that's the closest to the end of the pathway, even though it's in a track that loops around itself twice, making the targeting method different than only measuring the distance to the end of the path. 00000 0 0 S000000000000 000000 S is the starting point where the enemies spawn 0 0 0 E is the point where they end 00000000000000000 0 0 0 0 00000 000000000000 0 E I'm thinking about making checkpoints along the path that would increase the value of an enemy by 1, and then the enemy with the highest value (and distance to the next checkpoint if there are more than 1) would be first, like so: #000# 0 0 S00#00#00#00# #000# S is the starting point where the enemies spawn 0 0 0 E is the point where they end #000#00#00#00#00# # # is a checkpoint 0 0 0 #000# #000#00#000# 0 E What are your thoughts on this?
danpost danpost

2022/2/16

#
They have different starting times, which necessitates different positions; but, they all travel the same route at the same speed. No? As such, an int field can be used to time the existence of any enemy (by counting act frames). So, at any time, of those enemies currently in the world, the one with the highest frame counter would be the one closest to the end.
s1ice s1ice

2022/2/16

#
That's a really good idea, thank you
s1ice s1ice

2022/2/16

#
I'm presuming in the Enemy class, you would have a field like this,
public int time = 0;
and then increase it by 1 in the act method:
public void act(){
    time++;
    //more code and such afterwards
then whichever enemy has the highest "time" value would be first
danpost danpost

2022/2/16

#
s1ice wrote...
I'm presuming in the Enemy class, you would have a field like this, << Code Omitted >> and then increase it by 1 in the act method: << Code Omitted >> then whichever enemy has the highest "time" value would be first
Precisely.
s1ice s1ice

2022/2/17

#
I've worked on a Tower class now, and I put this code in the determiner function, and it seems to be working well: Pie is the enemy class I talked about earlier.
    public void determine(){
        //attack Pie with highest distance
        List <Pie> prox = getObjectsInRange(range, Pie.class);
        if(prox.size() > 0) {
            Pie first = prox.get(0);
            for(Pie enemy : prox){
                if(enemy.dist > first.dist){
                    first = enemy;
                }
            }
            turnTowards(first.getX(), first.getY());
        }
        
    }
"dist" is the time variable I talked about in the previous comment
You need to login to post a reply.