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

2014/3/12

Updating an ordered fixed list(fixed)

JasonZhu JasonZhu

2014/3/12

#
    public void addPlayer(Player pl)
    {
        if(score[score.length-1]!=null&&pl.getScore()>score[score.length-1].getScore()){
            score[score.length-1]=null;
        }        
        for(int i=score.length-2;i>-1;i--){
            if(score[i]!=null&&pl.getScore()>score[i].getScore()){
                score[i]=score[i+1];
            }else
            if(score[i]!=null&&pl.getScore()<score[i].getScore()){
                score[i]=pl;
            }
        }
    }
I am trying to order a list based off score when I add players. My above code does not work and I cant seem to figure out why. Help appreciated! My approach is basically starting from the bottom of the list and working my way upwards while placing each player with lower score 1 cell lower with the player at the last cell being removed as its a list.
danpost danpost

2014/3/12

#
I think the 'for' loop, as you have written it, will replace all higher entries with the player (basically eliminating all players with higher scores). All in all, it is not far from what you needed. I re-wrote the method two different ways. The second one below is, I believe closer to what you intended above. Both ways, btw, start with the same two statements which locates where the new entry should be placed. The first way uses two 'while' loops
public void addPlayer(Player pl)
{
    int index = 0;
    while (index < score.length && score[index] != null && pl.getScore() < score[index].getScore()) index++;
    Player hold = null;
    while (index < score.length)
    {
        hold = score[index];
        score[index++] = pl;
        pl = hold;
    }
}
This second way uses one 'while' loop and one 'for' loop followed by an 'if' statement:
public void addPlayer(Player pl)
{
    int index = 0;
    while (index < score.length && score[index] != null && pl.getScore() < score[index].getScore()) index++;
    for (int i=score.length-1; i>index; i--) score[i] = score[i-1];
    if (index < score.length) score[index] = pl;
}
danpost danpost

2014/3/12

#
A third way -- and even closer to what you had attempted originally, I think:
public void addPlayer(Player pl)
{
    int index = score.length-1;
    if (score[index] == null || score[index].getScore() < pl.getScore())
    {
        while (index > 0 && (score[index-1]==null || score[index-1].getScore()<pl.getScore())) score[index] = score[(index--)-1];
        score[index] = pl;
    }
}
This way, using a 'for' loop, would be:
public void addPlayer(Player pl)
{
    int index = score.length-1;
    if (score[index] == null || score[index].getScore() < pl.getScore())
    {
        for ( ; index>0 && (score[index-1] == null || score[index-1].getScore() < pl.getScore()); index--) score[index] = score[index-1];
        score[index] = pl;
    }
}
JasonZhu JasonZhu

2014/3/12

#
Thank you very much for sharing the different approaches. My previous method made all the entries null. I think I was too concerned with getting an out of bounds exception when bumping the other score down.
danpost danpost

2014/3/12

#
JasonZhu wrote...
Thank you very much for sharing the different approaches. My previous method made all the entries null. I think I was too concerned with getting an out of bounds exception when bumping the other score down.
After you said this, I looked at your original post and realized the you are not doing anything unless an entry was not null to begin with; so, they stay null. All three 'if' statements require that an element in the array have something other than 'null' before doing anything; and nothing is done outside of those 'if' blocks.
JasonZhu JasonZhu

2014/3/12

#
I guess this is an example of where order matters. The "&& !=null" should be the last element in the statement.
danpost danpost

2014/3/12

#
JasonZhu wrote...
I guess this is an example of where order matters. The "&& !=null" should be the last element in the statement.
Yes -- order matters. But, the '&& != null' must go before 'getScore' on the item from the array list or you might catch a 'NullPoinerException'.
JasonZhu JasonZhu

2014/3/13

#
I will take time to review the options for this task, I once again thank you for you guidance.
danpost danpost

2014/3/13

#
Here is one more option:
public void addPlayer(Player pl)
{
    for (int i=score.length-1; i>=0; i--)
    {
        if (score[i] != null && score[i].getScore() >= pl.getScore())
        {
            if (i < score.length-1) score[i+1] = pl;
            return;
        }
        else
        {
            if (i < score.length-1)  score[i+1] = score[i];
            if (i == 0) score[0] = pl;
        }
    }
}
As you can tell, it is a single loop with a bunch of 'if' statements (which is not so bad, considering the number of conditions placed upon the loops in previous examples). I have found that scores of equal values are ordered differently for the first two than for the others. The first two order them from newest to oldest; the others list them from oldest to newest. Again, I would go with method 4 (or 3) over 1 or 2. I would prefer the first one to reach a score retain the higher ranking among the scores.
JasonZhu JasonZhu

2014/3/13

#
I think it would make sense that if a high score was already under a person's name and another person achieved the exact same score that the first person to obtain that score to retain that ranking. So yes, I concur with what you stated.
danpost danpost

2014/3/13

#
In the first two methods, you can change the score comparison from '<' to '<=' to fix the ranking of equal scores.
if (score[index] != null && pl.getScore() <= score[index].getScore())
You need to login to post a reply.