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

2019/1/19

Leaderboard bug

ant ant

2019/1/19

#
Hey! So I figured out storing the scores in a file and putting them in an array. Then sorting it and displaying the top 5 on the screen. I am having a problem where it's not sorting through ALL from the file and everytime someone plays, their recent score gets added to the leaderboard and kicks out the top score or any other score. The sorting seems fine but not sure how to fix the problem. Any help is appreciated
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Write a description of class Highscores here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Highscores extends Actor
{
    public Highscores() {
        GreenfootImage img = new GreenfootImage(500, 500);
        setImage(img);
    }
    ArrayList <Integer> highscores = new ArrayList <Integer>();
    public int n;
    /**
     * Act - do whatever the Highscores wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        highscore(highscores);
        
        display(highscores);
    }

    public void highscore(ArrayList highscores) {
        try
        {
        Scanner output = new Scanner (new File ("highscores.txt"));

        
        for (int i = 0; i < 5; i++) {
            highscores.add(0); //initializes the array with 5 zero's
        }
        while (output.hasNext()) { //adds highscores to an arraylist from a file
            highscores.add(output.nextInt());
        }
        int i = 0;
        if (highscores.size() > 5) {
            while (highscores.size() != 5) { //while loop removes highscores if there are more than 5 highscores
                highscores.remove(i);
            }
        }
        sort(highscores, highscores.size());
        }
        catch(IOException e)
        {
            System.err.println("Caught IOException: " + e.getMessage());
        }
    }

    public static void sort(ArrayList <Integer> highscores, int n)
    {
        // Base case
        if (n <= 1) 
            return;
      
        // Sort first n-1 elements
        sort(highscores, n-1 );
        
        // Insert last element at its correct position
        // in sorted array.
        int last = highscores.get(n-1);
        int j = n-2;
      
         /*Move elements of arr[0..i-1], that are
          greater than key, to one position ahead
          of their current position */
        while ((j >= 0) && (highscores.get(j) < last))
        {
            highscores.set(j+1, highscores.get(j));
            j--;
        }
        highscores.set(j+1, last);
    }

    public void display(ArrayList highscores) {
        GreenfootImage img = getImage();
        img.clear();

        Font f = new Font("Invasion2000", false, false, 60);
        img.setFont(f);

        img.setColor(Color.WHITE);
        img.drawString("" + highscores.get(0), 250, 50);
        img.drawString("" + highscores.get(1), 250, 150);
        img.drawString("" + highscores.get(2), 250, 250);
        img.drawString("" + highscores.get(3), 250, 350);
        img.drawString("" + highscores.get(4), 250, 450);
        setImage(img);
    }
}
danpost danpost

2019/1/19

#
ant wrote...
it's not sorting through ALL from the file and everytime someone plays, their recent score gets added to the leaderboard and kicks out the top score or any other score.
Much improvement can be made with your code: * no need to pass the highscores array to the highscore method; * no need to write a sort algorithm (use java.util.Collections.sort); * should not use an act method in this class (use constructor or addedToWorld method); I cannot see where a recent score gets added (code required).
ant ant

2019/1/29

#
danpost wrote...
ant wrote...
it's not sorting through ALL from the file and everytime someone plays, their recent score gets added to the leaderboard and kicks out the top score or any other score.
Much improvement can be made with your code: * no need to pass the highscores array to the highscore method; * no need to write a sort algorithm (use java.util.Collections.sort); * should not use an act method in this class (use constructor or addedToWorld method); I cannot see where a recent score gets added (code required).
Thanks for the reply. Just a few things, I need to add a sort algorithm because my project requirements include recursively sorting them. I made a constructor, but I'm pretty sure I did it incorrectly, and a bit confused about it. What's happening is that when I play my game and the bird dies, I made it so a console opens up and shows the highscores array(https://imgur.com/a/lQSnyLQ) but when going to the leaderboard section of my game (I have a button), there's just this infinite loop that keeps going and shows the number 18 (which is the highest as of now). This is what it looks like:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Write a description of class Highscores here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Highscores extends Actor
{
    ArrayList <Integer> highscores = new ArrayList <Integer>();
    public int n;

    public Highscores(ArrayList <Integer> highscores) {
        this.highscores = highscores;

        GreenfootImage img = new GreenfootImage(500, 500);
        setImage(img);
    }

    /**
     * Act - do whatever the Highscores wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
    }

    public void highscore() {
        try
        {
            Scanner output = new Scanner (new File ("highscores.txt"));

        
            while (output.hasNext()) { //adds highscores to an arraylist from a file
                highscores.add(output.nextInt());

            }
            sort(highscores, highscores.size());
            int i = 0;
            if (highscores.size() > 5) {
                while (highscores.size() != 5) { //while loop removes highscores if there are more than 5 highscores
                    highscores.remove(i);
                }
            }

            System.out.println(highscores);

            GreenfootImage img = getImage();
            img.clear();

            Font f = new Font("Invasion2000", false, false, 60);
            img.setFont(f);

            img.setColor(Color.WHITE);
            img.drawString("" + highscores, 250, 50);

            setImage(img);
        }
        catch(IOException e)
        {
            System.err.println("Caught IOException: " + e.getMessage());
        }
    }

    public static void sort(ArrayList <Integer> highscores, int n)
    {
        // Base case
        if (n <= 1) 
            return;

        // Sort first n-1 elements
        sort(highscores, n-1 );

        // Insert last element at its correct position
        // in sorted array.
        int last = highscores.get(n-1);
        int j = n-2;

        /*Move elements of arr[0..i-1], that are
        greater than key, to one position ahead
        of their current position */
        while ((j >= 0) && (highscores.get(j) > last))
        {
            highscores.set(j+1, highscores.get(j));
            j--;
        }
        highscores.set(j+1, last);

    }

    /*public void display(ArrayList highscores) {
    GreenfootImage img = getImage();
    img.clear();

    Font f = new Font("Invasion2000", false, false, 60);
    img.setFont(f);

    img.setColor(Color.WHITE);
    img.drawString("" + highscores.get(0), 250, 50);
    img.drawString("" + highscores.get(1), 250, 150);
    img.drawString("" + highscores.get(2), 250, 250);
    img.drawString("" + highscores.get(3), 250, 350);
    img.drawString("" + highscores.get(4), 250, 450);
    setImage(img);
    }*/
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
/**
 * Write a description of class LeaderboardScreen here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LeaderboardScreen extends World
{
    //Highscores h1 = new Highscores(highscores);
    StartButton s1 = new StartButton();
    /**
     * Constructor for objects of class LeaderboardScreen.
     * 
     */
    public LeaderboardScreen()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1250, 750, 1, false);

        //addObject(h1, 600, 300);
    }
    //   File rate = new File("highscores.txt");

    /**
     * Act - do whatever the Rate wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act(int score) 
    {
        try
        {
            //    FileWriter create = new FileWriter(rate);
            //PrintWriter output = new PrintWriter(create);
            PrintWriter input = new PrintWriter(new BufferedWriter(new FileWriter("highscores.txt", true)));

            input.println(score);

            //display();
            input.close();

        }
        catch(IOException e)
        {
            System.err.println("Caught IOException: " + e.getMessage());
        }

    }

    /*public static void highscore() throws java.io.FileNotFoundException {

    Scanner file = new Scanner (new File ("highscores.txt"));

    ArrayList <Integer> highscores = new ArrayList <Integer>();
    for (int i = 0; i < 5; i++) {
    highscores.add(0); //initializes the array with 5 zero's
    }
    while (file.hasNext()) { //adds highscores to an arraylist from a file
    highscores.add(file.nextInt());
    }
    int r = 0;
    if (highscores.size() > 5) {
    while (highscores.size() != 5) { //while loop removes highscores if there are more than 5 highscores
    highscores.remove(r);
    }
    }
    System.out.println(highscores);
    }

    public void display(ArrayList <Integer> highscores) {
    GreenfootImage img = getImage();
    img.clear();

    Font f = new Font("Invasion2000", false, false, 50);
    img.setFont(f);

    img.setColor(Color.WHITE);
    img.drawString("" + highscores, 30, 30);
    setImage(img);
    }*/
}
This is the portion of code where it's adding the score to the file.
public void checkHit() {
        if((isTouching(BottomPipe.class)) || (isTouching(TopPipe.class) || (getY() > getWorld().getHeight()))) {
            Greenfoot.playSound("nani.mp3");  
            GameOver g1 = new GameOver(); 
            getWorld().addObject(g1,getWorld().getWidth() /2, getWorld().getHeight()/2);
            City c1 = new City();
            LeaderboardScreen l1 = new LeaderboardScreen();
            l1.act(City.score);

            City.score = 0;

            //StartButton s2 = new StartButton();
            //getWorld().addObject(s2,getWorld().getWidth() /2, getWorld().getHeight()/2 + 150);

            HomeScreen h1 = new HomeScreen();
            getWorld().removeObject(this);
            Greenfoot.delay(75);
            Greenfoot.setWorld(h1);
            //City c1 = new City();
            //LeaderboardScreen l1 = new LeaderboardScreen();
            //l1.act(c1.score);
            //Greenfoot.stop();

        }
This is the portion where the score is getting added
        if (counter >= firstPipe) {
            if (counter1 % 110 == 0) {
                score++;
                Greenfoot.playSound("ping.mp3");
                scoreText.setScore(score);

                //}
            }
            counter1++;
        }
Sorry for a lot of code snippets but I wasnt sure what you needed to understand the problem
danpost danpost

2019/1/29

#
When you move a value in your arraylist, you destroy what was previously in the position you set. Change line 88 in HighScores class to:
java.util.Collections.swap(highScores, j+1, j);
You need to login to post a reply.