Poker Hand Evaluator, Second Attempt

Update: This grew beyond an easy project, and I have written a own page about the issue. In a nutshell, the algorithm proposed below is a) a bit flawed (a fix would be easy, though) and b) not as fast as I had hoped. Which is, in turn, because my old algorithm was not that bad – and I spend most of my time improving it, getting quite competitive results.


It’s been a while since I posted something, and the thing I can post now is from a complete different area than the rest… it is about a Poker Hand Evaluator! I have written one during my PhD time (here it is) which got me a little fame.

This dag-based poker hand evaluator is nice, but it has one design flaw that makes it slower than the competition: for each card it visits, a lookup in a memory table is done. Since that table is big, it is bound to fail second-level caches, and that is where the time is spend. So one day, for a reason I no longer know, I came up with a new idea, which is less beautiful, but can do a hand comparison with just one table lookup. And since I will be programming a lot C++ in the near future, I thought it would make a good exercise to try it out.

So what is a hand evaluator? Basically, an algorithm that, given two poker hands (in our case, we only consider Texas Hold’em, best 5 out of 7 cards) will tell you which one is better. For determining the winner of a round, a very basic algorithm will do (e.g., using a series of “detector” algorithms that look for straight flushes, then four-of-a-kind, etc.). It gets more interesting when, given partial hands (i.e., some known cards with others yet to come), we want to know the likelihood that one hand wins. Like on TV, when they show you the cards the players have, and how likely it is that one hand will win. There is no clever formula – it is done by comparing all possible hands, or doing Monte-Carlo-testing (if there are too many). Hence, a hand evaluator needs to compare a lot of hands in very little time. Think of millions of hands per seconds – that only leaves something in the region of thousand cycles on a modern CPU. Sorting the cards, repeated scans, all that is too slow. The goal is to go through the cards once (maybe twice), and then have the result.

An important observation was done by Cactus Kev (or rather, put in writing so I could build on that) – many distinct hands actually have the same value. Order does not matter, nor the color of cards (unless it is a Flush, in which case the exact color is not important). He wrote a hand evaluator based on that, but for 5-card Poker variants. Here, we do seven cards, which just puts more hands into each equivalence class (e.g., for the Royal flush, the color does not matter, nor do the two cards not being part of the Ace-high straight). So what I did in my last evaluator, and do again, is to find a fast way to calculate the index of the Cactus Kev equivalence class . The lower the equivalence class number, the better the hand.

The approach I take is very straight-forward: let’s ignore the suit for now, so we have 13 cards, with each card being present 0 to 4 times in a hand. We can just create a bitfield that represents just that – how often each of the 13 cards is present – and look into a big, precalculated table to see which class it gives us. However, since we need to store 5 different values, we need 3 bits per card… so, in total, 549 billion entries in a lookup table. Too big. But why consider the case where we have 4 cards at all? That should be very infrequent, so we can detect it and revert to some special routine… and just use two bits per rank, which gives us just 67 million. But while developing it, I recognized that it works if we just ignore the four-of-a-kind case, and here is why:

Since we allocate two bits per rank, adding the fourth card will produce an overflow. It will hence appear as if we got no cards of that particular rank at all, and instead one more of the next-higher rank. Thus, instead of looking at 7 cards, we look at four – and this will produce a number that no other hand will produce (their cross-sum will always be 7). It could even overflow twice – e.g. with four kings, three aces – but then we just get one card. We can even clip that overflow, and get 0 cards for that particular case – the important thing is that it will not collide with another hand, and that it will be the same during creation and use of the lookup table. So we can just use two bits per rank, and ignore overflows (other than clipping the 26-bit field in the end).

What about flushes? Well, the easiest way is to just generate another number for each color, including just the cards that have this color. Then, we can do a lookup in a special Flush-only table (which, in practice, is merged with the normal lookup table) and see if it gets a good result. But it turns out to be faster if we just count the colors at first, and check if there is a Flush (it is, after all, quite infrequent), and then do a second pass and just include the cards that have the Flush suit.

The important thing is that we only have one table lookup (if there is a Flush, it will always rule out better hand rankings – i.e., if you have five cards of the same color, you cannot have a four-of-a-kind nor a full house). That should make this algorithm much faster than my previous one, and also algorithms like 2plus2, which do one lookup per card. Measurements are to follow here soon; for now, this is just the idea. I have implemented this and found it very fast, but then I hear fabulous times from 2plus2…

Facebook: A workaround for the missing Application settings

<edit>As “JDG” points out in the comments, the application settings page has been moved. You can use this URL: http://www.facebook.com/settings/?tab=applications to go directly to the page in question, or use the path JDG describes.</edit>

Facebook has removed the “Application settings” from the account menu. Maybe this is a mishap and they will add it again soon, but in the meantime, here is a workaround to access/remove an application:

  • If you are a developer, you can use the search for “Developers” and access that application, which offers links to the pages of your own applications.
  • Otherwise, you need to access the settings page of the application, which can be found at http://www.facebook.com/apps/application.php?id=[appid]. If the app sits on a tab, you can click on that tab and pick the parameter from the end of the URL, which is “v=app_[appid]“. For canvas pages (i.e., apps that are not in a tab), you can find a (grey-colored) link with the application name at the left-bottom corner of the page.
  • On the application page, on the left panel, you can click on “Remove application” to delete your authorization grants to the application. Or you can edit the settings, like/unlike it, whatever.

As I need to remove applications over and over again (debugging the application authentication flow), this is probably even better, as I can bookmark the app page.

Proposal: Quality of software can be measured by the quality of the workarounds

For a proof, please visit http://bugs.developers.facebook.net/show_bug.cgi?id=12014. Facebook recently deprecated (or rather, silently removed) a setting for Facebook apps which is still in use by software such as ours, and required for some functionality by Facebook. The workaround now suggests just replicating the app settings form and adding the missing field.

I rest my case.

Best before Nov 2010: “Next is not owned by the application” – solution for IE 8

Facebook is notorious for its frequent API changes. However, some error codes are rather persistent, in this case error 100: “next is not owned by the application”. I got it for the stream.publish popup (only in IE8, Chrome and Firefox work fine). You will find tons of suggestions that all tell you to change settings long removed by Facebook. So, here is a pointer: try setting the “Site Domain” on the “Web Site” tab to a meaningful value. This solved the issue for me, on Oct 13, 2010.

And a more general pointer: when Facebook tells you that a setting is optional and defaults to something, it can still help to add some value. Most likely the “defaults” was true some time ago, but no longer is.

Wicket: a convenient shortcut

When using Apache Wicket, much code is spent on building the component tree. You instantiate the components, and add them to their parent component. Then you take these new components, and add further components. This often looks like this:

Link<Void> l = new Link<Void>("linkid") {...};
add(l);
l.add(new Label("linklabelid", linktext));

Often, I forget the second line… so I want to have a shorter version. add returns the this component, which allows for chaining multiple component additions (as you chain calls to StringBuilder.append), but you cannot get a reference to the added child component directly.

So here is a small utility method that will allow you to do just that:

public class WicketUtils {
	public static <T extends Component> T add(MarkupContainer target, T object) {
		target.add(object);
		return object;
	}
}

Small and simple.. but you can shorten the code above to two lines:

Link<Void> l = WicketUtils.add(this, new Link<Void>("linkid") {...});
l.add(new Label("linklabelid", linktext));

This is more convenient, and tidies up the code. I have to admit, this is almost too trivial, but once I started using it consistently, it helped quite a bit.

Fun with Java Constants

I was interviewed yesterday, and was asked to fill out a test to prove that I know some Java. It was a test just like we gave to freshmen at the university after the first semester. One of the question was:

“What is the output of the following program?”

String x = "abc";
String y = "abc";
if (x == y) System.out.println("1") else System.out.println("0");

And of course, the expected answer was “0″. But this is wrong, the program will indeed return “1″.

The funny thing about this story is that the answer “0″ seems so obvious, that no-one ever checks it out. I did not, either, when I wanted to show that == does not work as expected on String during a Java introduction lecture. And got the answer “1″ in real-time, in front of a hundred students.

Java classes maintain a constant pool that stores the constants defined in the source code – in this example, “abc”, “1″, “Ljava.lang.String” and more (36 entries in total, most of them relating to class information). But (at least for the Sun/Oracle JDK compiler) every constant is just stored once. So when initializing x and y, they are given the same constant – and point to the same object. Hence the
== works.

You can check it out using javap -c, a Java bytecode disassembler. The first two lines go like this:

   0:	ldc	#2; //String 123
   2:	astore_1
   3:	ldc	#2; //String 123
   5:	astore_2

The ldc opcodes load a constant from the constant pool onto the stack, and astore puts them into a register for further use. You can see that the same constant is loaded twice.

You need to trick the compiler a little to get the desired results, e.g. using

String x = "123";
String z = "3";
String y = "12" + c;
...

(Using "12" + "3" won’t help, the compiler is too smart…)

Morale of the story: it is important not to rely on “==” for String comparison, but it is important not to rely on obvious semantics for educational purposes, too.

Without comment

http://bugs.developers.facebook.net/show_bug.cgi?id=12388

Using JQuery’s .fadeOut() with Wicket AJAX

I really learn a lot from Wicket and its way of being extensible.

For a project I need to integrate JQuery with Wicket, and being a little stubborn I do it without a lib, all by just extending standard Wicket components. I want to do AJAX calls, and the results should be beautified with things like .fadeIn(). For adding components, this is easy, using the appendJavascript() method of AjaxRequestTarget. For removing components, it is a little harder, because standard JQuery effects will not block the execution of the Wicket code (when using prependJavascript(), therefore Wicket will remove the component before any visual effect has been shown.

The solution: use the function parameter to .fadeOut() to effect the changes of the AJAX response to the DOM only after the effect has run. And now Wicket truly excels: Using an IAjaxCallDecorator, we can rewrite the Javascript in an AjaxLink – and have it follow the proposed scheme. All this by just overloading a method in AjaxLink:

final String CLOSE_JS_TEMPLATE = "$('#%s').fadeOut(300, function(){ %s });";

add(new AjaxLink<Void>("closelink"){
	@Override
	public void onClick(AjaxRequestTarget t2) {
		container.addOrReplace(new WebMarkupContainer("editor"));
		t2.addComponent(container);
	}

	@Override
	protected IAjaxCallDecorator getAjaxCallDecorator() {
		return new AjaxCallDecorator() {
			@Override
			public CharSequence decorateScript(CharSequence script) {
				return String.format(CLOSE_JS_TEMPLATE, editor.getMarkupId(), script);
			}
		};
	}
});

How cool is that!

The science of Mo

In order to figure out if racing in the relay of Challenge Roth 2010 has been a good idea, I went to the Munich Olympiazentrum and did a Spiroergonometrie. If you are interested in the results, and if you are not Norbert Romen, here are the results:


If, by chance, you are Norbert Romen, please click here.

“Only for fans” – navigating Facebook’s obstacles

We often get the request that a contest is only visible to fans of a page. However, when integrating the contest on a Facebook tab, we have the problem that Facebook does not provide us with information about the user unless the user clicks a link. Hence, we first have to guide the user to click on a link, and then tell the user to become a fan.

A better way can be implemented with FBML: Using the <fb:visible-to-connection> and the <fb:else>-tag, we can implement a case distinction, showing one content for fans and another content for the rest.

However, for a reason that is not entirely clear to me, Facebook will always render both cases (for fans and non-fans) and annotate the one not applicable with a style “visibility:hidden“. This leads to nasty white spots in the page design, as the non-matching content is only hidden, but the space remains reserved. However, using a CSS-attribute-matcher, we can match the offending tag and hide it completely:

<style>
span[style~="hidden;"] {display : none;}
</style>

Here is the complete code, ready to be used in your FBML:

<style>
span[style~="hidden;"] {display : none;}
</style>

<fb:fbml version="1.1">
    <fb:visible-to-connection>
         content for fans...
    <fb:else>
         content for those that have yet to become fans...
    </fb:else>
    </fb:visible-to-connection>
</fb:fbml>