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

2021/10/12

Saving 2 high scores seperately at a time

1
2
3
Roshan123 Roshan123

2021/10/14

#
If i score 2 then it will save everyone score as 2 I don't know how will i show the score individually. In csv file it stores seperately but i m not able to show it seperately I changed it a little bit and i think this may work but it gives me error void type not allowed here. drawString(Integer.toString(playerData.setScore(w1.Score())), x, y+18, Scorecolor,14) ; I don't even know wether the above one will show seperately or not but plz tell me how will i show it
danpost danpost

2021/10/14

#
playerData.setScore(w1.Score());
is a command on its own that has no returning value (setScore is a method void of a returning value). Maybe you meant to get the score:
drawString(Integer.toString(playerData.getScore()), x, y+18, Scorecolor, 14);
or
drawString(Integer.toString(w1.Score()), x, y+18, Scorecolor, 14);
Roshan123 Roshan123

2021/10/14

#
danpost wrote...
drawString(Integer.toString(playerData.getScore()), x, y+18, Scorecolor, 14);
Its not what i mean becaz if u score 11 in green project then the same score will be shown in foot project. I want them to save and show seperately
danpost wrote...
drawString(Integer.toString(w1.Score()), x, y+18, Scorecolor, 14);
Its aslo not what i mean to say becaz it saves score seperately in green project and foot project(which i want) but the problem is that in foot project if i score 2 then everyone score will be 2, no matter what they scored. If u score 1 then everyone score will be 1 even if i scored 0, my score will be 1 becaz of u But in csv file if u see, everyone score is saved seperately but i don't know how will i show the score seperately in scoreboard1 class just like in csv file
danpost danpost

2021/10/14

#
With Scoreboard1, you cannot use getRank or getScore (since Scoreboard uses those for game 1). You will need to use getInt and compile the required list yourself (by iterating through all user's data, figuring out which users have the top n ints and in the process, sort the list to acquire ranks).
Roshan123 Roshan123

2021/10/15

#
Would u plz tell me how will i show rank of others and download it again becaz i modified a little bit and now i m able to save and see everyone's score separately but i dont know how will i create rank mechanism By the way is 63 ur real age? Its ur choice if u wanna reveal or not
danpost danpost

2021/10/15

#
Roshan123 wrote...
Would u plz tell me how will i show rank of others and download it again becaz i modified a little bit and now i m able to save and see everyone's score separately but i dont know how will i create rank mechanism
Working on revising ScoreBoard class to use int values instead of score field.
By the way is 63 ur real age? Its ur choice if u wanna reveal or not
Actual and factual -- for real.
Roshan123 Roshan123

2021/10/15

#
danpost wrote...
Working on revising ScoreBoard class to use int values instead of score field
Is thier any need of method. If yes then which one is in ur mind. Plz never mind but i need more detailed info and i will try my best to make it.
danpost danpost

2021/10/15

#
Roshan123 wrote...
danpost wrote...
Working on revising ScoreBoard class to use int values instead of score field
Is thier any need of method. If yes then which one is in ur mind. Plz never mind but i need more detailed info and i will try my best to make it.
One can use getTop(1000) to acquire a list of users (probably good for ages). Sorting the list by int field values high to low will provide ranking (index in list). The one tricky thing is getting nearby scores -- determining where in the list to start and end for the sub-list to be displayed.
Roshan123 Roshan123

2021/10/15

#
I don't know what to say,,,,, but I quit!!! After reading the documentary my mind was like floating in space. I have been trying to understand since many months but whenever i start i quit becaz i barely understand anything. So,,,,, plz show me how m i supposed to do I have 1 more doubt i.e. if the rank mechanism is made, then also i don't think it will arrange the users in decreasing order. I think it will only show them thier rank and place them alphabetically no matter what's the score is(note that its just something stuck on my mind suddenly so i m not sure about it)
danpost danpost

2021/10/15

#
I added the following method to ScoreBoard class:
/** Takes a list of UserInfo objects and sorts them by value of an int field, given by int game, and returns a short list of the first so many UserInfo object, given by int numUsers.
 * 
 * @param game a zero value is game one and a one value is game two; also represents the UserInfo int field to sort by (0 or 1)
 * @param list the list of UserInfo objects to sort and return a sub-list of
 * @param numUsers the number of top users to return
 * 
 * @return a short list of UserInfo objects having the highest int values in said int field 
 */
private List<UserInfo> getTop(int game, List<UserInfo> list, int numUsers)
{
    UserInfo[] tops = new UserInfo[numUsers+1]; // create working list used for sorting and saving those with top scores
    // sort given list
    for (int i=0; i<list.size(); i++)
    {
        int index = numUsers+1;
        tops[index] = list.get(i); // place UserInfo object at end of sorting list
        // sort entry into list
        while (index > 0 && (tops[index-1] == null || tops[index].getInt(game) > tops[index-1].getInt(game)))
        {
            UserInfo hold = tops[index-1];
            tops[index-1] = tops[index];
            tops[index] = hold;
            index--;
        }
    }
    list.clear();
    for (int i=0; i<numUsers; i++) if (tops[i] != null) list.add(tops[i]); // create list of top numUsers users
    return list; // return top users for given game
}
Roshan123 Roshan123

2021/10/16

#
Where and how m i supposed to call this method? I don't know why but it says indexOutOfBound
danpost danpost

2021/10/16

#
Roshan123 wrote...
Where and how m i supposed to call this method? I don't know why but it says indexOutOfBound
I wasn't getting any errors with it. How did you set up the call to this method?
Roshan123 Roshan123

2021/10/16

#
drawString(Integer.toString(getTop(1, users, 1000)), x, y+18, Scorecolor, 14);
danpost danpost

2021/10/16

#
Roshan123 wrote...
drawString(Integer.toString(getTop(1, users, 1000)), x, y+18, Scorecolor, 14);
My appologies. Apparently I wrote the method and then decided not to use it, anyway. The following ScoreBoard class works for top users. I have yet to complete nearby users algorithm.
import greenfoot.*;
import java.util.List;

public class ScoreBoard extends Actor
{
    private static final int GAP = 10;
    private static final int HEADER_TEXT_HEIGHT = 25;
    private static final Color MAIN_COLOR = new Color(0x60, 0x60, 0x60); // dark grey
    private static final Color SCORE_COLOR = new Color(0xB0, 0x40, 0x40); // orange-y
    private static final Color BACKGROUND_COLOR = new Color(0xFF, 0xF0, 0xE0, 0xFF);
    private static final Color BACKGROUND_HIGHLIGHT_COLOR = new Color(180, 230, 255, 0xFF);
    private static final Color TRANSPARENT = new Color(0, 0, 0, 0);
    
    public ScoreBoard(int game, int width, int height)
    {    
        setImage(new GreenfootImage(Math.max(600, width), height)); 
        drawScores(game);
    }
    
    private void drawString(String text, int x, int y, Color color, int height)
    {
        getImage().drawImage(new GreenfootImage(text, height, color, TRANSPARENT), x, y);
    }
    
    private void drawScores(int game)
    {
        final int pixelsPerUser = 50+2*GAP;
        final int numUsers = ((getImage().getHeight()-(HEADER_TEXT_HEIGHT+10))/pixelsPerUser);
        final int topSpace = getImage().getHeight()-(numUsers*pixelsPerUser)-GAP;
        getImage().setColor(BACKGROUND_COLOR);
        getImage().fill();
        drawString("Top Players", 120, topSpace-HEADER_TEXT_HEIGHT-5, MAIN_COLOR, HEADER_TEXT_HEIGHT);
        drawString("Near You", 120 + getImage().getWidth()/2, topSpace-HEADER_TEXT_HEIGHT-5, MAIN_COLOR, HEADER_TEXT_HEIGHT);        
        List<UserInfo> list = UserInfo.getTop(200);
        for (int i=1, n=1; i<list.size(); i++, n=i)
        {
            UserInfo user = list.get(n);
            while (user.getInt(game) > list.get(n-1).getInt(game)) java.util.Collections.swap(list, n, n-1);
        }
        List<UserInfo> tops = list.subList(0, Math.min(numUsers, list.size()));
        drawUserPanel(game, GAP, topSpace, (getImage().getWidth()/2)-GAP, topSpace+numUsers*pixelsPerUser, tops, 0);
        
        /** have yet to complete nearby algorithm */
    }
    
    private void drawUserPanel(int game, int left, int top, int right, int bottom, List users, int startRank)
    {
        getImage().setColor(MAIN_COLOR);
        getImage().drawRect(left, top, right-left, bottom-top);
        if (users == null) return;
        UserInfo me = UserInfo.getMyInfo();
        int y = top + GAP;
        for (Object obj : users)
        {
            UserInfo user = (UserInfo)obj;
            if (user == null) continue;
            Color c = (me != null && user.getUserName().equals(me.getUserName())) ? BACKGROUND_HIGHLIGHT_COLOR : BACKGROUND_COLOR;
            getImage().setColor(c);
            getImage().fillRect(left+5, y-GAP+1, right-left-10, 50+2*GAP-1);
            int x = left + 10;
            drawString("#"+Integer.toString(users.indexOf(user)+startRank+1), x, y+18, MAIN_COLOR, 14);
            x += 50;
            drawString(Integer.toString(user.getInt(game)), x, y+18, SCORE_COLOR, 14);
            x += 80;
            getImage().drawImage(user.getUserImage(), x, y);
            x += 55;
            drawString(user.getUserName(), x, y+18, MAIN_COLOR, 14);
            y += 50+2*GAP;
        }
    }
}
Roshan123 Roshan123

2021/10/17

#
Is algorithms of top users completed? If no, ignore this message else i would like to share my prblm i.e. rank mechanism still doesn't work and its shows the previous score. In other words if i score 2 now, it will be shown in next match when i lose
There are more replies on the next page.
1
2
3