Home > Uncategorized > Ranking Players and Declaring a Winner

Ranking Players and Declaring a Winner

September 19th, 2013 Leave a comment Go to comments

I’ve written about comparators once before but only just realized that the usefulness of that post hinges on the availability of the site it links to. Not wanting to rely on a third party site and the owner’s ability to pay hosting fees, here’s my quick blurb on how comparators work in Java.

But first, why do I need a comparator in the first place? Simply put, to declare a winner at the end of a round of Board Rogue. When a player reaches the board’s exit, the game is over. Scores are tallied, players are ranked, and the player with the highest score wins.

So you know how to sort numbers. You probably know how to sort alphabetically. There are functions for that sort of stuff built into most every programming language. But how do you take something like a PLAYER and sort them? You use a custom comparator, and the Collections class.

So you’ve got a player.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Player {
 
	private int score;
	private String name;
 
	public Player(String name) {
		this.name = name;
	}
 
	public int getScore() {
		return score;
	}
 
	public void setScore(int score) {
		this.score = score;
	}
}

And the game itself has a list of Players.

List<Player> playerList = new ArrayList<Player>();

And through the course of the game, each player accumulates a varied amount of treasure and items. Each piece of loot has a value associated with it, and the player with the highest loot value wins.

You could just loop through all of the players, look for the highest score, and declare a winner. But what’s the fun in that? You can’t easily tell who’s in second, third, or last place. But you can if you sort them! So create a comparator class that we’ll use to compare scores.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class PlayerScoreComparator implements Comparator<Player> {
 
	public int compare(Player p1, Player p2) {
 
		int p1score = p1.getScore();
		int p2score = p2.getScore();
 
		if (p1score > p2score)
			return -1;
		else if (p1score < p2score)
			return 1;
		else
			return 0;
	}
 
}

And now, to sort the playerList, simply apply the comparator to the Collection.sort function.

Collections.sort(playerList, new PlayerScoreComparator());

Finally, putting it all together you get the following.

Code.

1
2
3
4
5
6
7
8
9
for (Player player : playerList) {
	System.out.println ("Unsorted " + player.getName() + " has " + player.getScore() + " points");
}
 
Collections.sort(playerList, new PlayerScoreComparator());
 
for (Player player : playerList) {
	System.out.println ("Sorted " + player.getName() + " has " + player.getScore() + " points");
}

Output.

Unsorted Player One has 116 points
Unsorted Player Two has 616 points
Unsorted Player Three has 129 points
Sorted Player Two has 616 points
Sorted Player Three has 129 points
Sorted Player One has 116 points
Categories: Uncategorized Tags:
  1. Ernest Pazera
    September 20th, 2013 at 06:22 | #1

    This is one of those areas where Java lags behind slightly, at least until SE8.

    At that point, lambdas will be along, and things like:

    playerList.orderBy(player->player.getScore());

    Will get the job done without a need for a comparator, which everyone will agree seems like an unnecessary bit of work for something as trivial as it does.

  1. No trackbacks yet.