Category: Uncategorized

Big Life Changes

Life changed this year. Wife realized she was unhappy but it was too late to fix. She left. Took the kids 45 minutes away.

On the bright side, she fixed up the townhouse we own, that she now lives in. It’s worth a bunch more now and she’s as motivated as I am to sell it. So that’ll happen in the spring, I think.

I no longer feel compelled to leave work as soon as my 8 hours are done. Previously I headed straight home after work every day to give her a break from housework and parenting duties. Now I hang around at work for board game nights and after-work activities. And I wake up later in the morning. Used to be I’d work 8-4, now it’s more like a 9 or 9:30 start. That’s nice.

I see the kids every two weekends. Sometimes I take them a couple weekends in a row. The 45 minute drive isn’t so bad. I get to listen to a lot of audiobooks during the drive. Lately I’ve been listening to “IT” by Stephen King.

I met someone. She’s a school teacher who recently moved here from Alberta. She was looking for work and only two days ago finally found something. It’s not teaching, but it’s still in-line with her career of choice.

So life changed. Now I’m looking for opportunities to make the most of those changes. It’s tough. I don’t wish it on anyone. But silver linings and all that.

Ranking Players and Declaring a Winner

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

Staypressed theme by Themocracy