THKP

View Original

Beat Your Grandma at Cards - Chapter 3 - Modelling Euchre

I know we’re trying to build an AI that can beat grandmas and not a fully-fledged card game app, but I think it helps a great deal to have some basic visualizations when working on a difficult technical problem.

There are a few more game concepts we haven’t discussed yet. Most of them are pretty simple, so let’s do some rapidfire modelling:

We need a way to refer to the four players. One common way of doing that in a four person game is to use the four cardinal directions. I’ll call it TablePosition:

See this content in the original post

Once again, I added some useful extension methods for naming and opposite and next and such. They’re pretty simple, so I’ll leave them out of this tutorial, but you can see them here.

Team is also just a simple enum:

See this content in the original post

Yes, I really like enums.

Next, let’s model two concepts I’ve alluded to a few times called Trick and Kitty:

See this content in the original post

You notice Kitty has a place where the dealer discard card is stored. We could’ve put this elsewhere, but this is mostly a throwback to when I played as a kid and we’d put the discard back into the kitty to keep it hidden.

Okay, and finally: Trick. Trick needs a method to determine the winner of the trick. In order to do that, I’m going to introduce card scoring. Card scoring is simply assigning higher int values to higher value cards. So the right bauer gets the highest value, the left is next, then the ace of trump and so on. There are also several contexts where cards can be scored. For example, if you’re in a Trick, there is a lead suit, and every card of that suit is more valuable than cards of other suits (except trump, of course, which is always the most valuable suit). So you’ll see here in Trick that I use a method called ‘trickCardScore’. You can see all the scoring here. It’s pretty straightforward, and very verbose, so I won’t include the code in this tutorial, but click the link so you get the idea.

Once scoring is figured out, we can write our Trick class:

See this content in the original post

Alright, that’s all the necessary modelling for Euchre. Let’s render a nice looking euchre table with our four players. We need some widget that can render the players on the four sides of the table. Luckily, with flutter, layout like this is a cinch. Here’s a widget that will do the job:

See this content in the original post

Most of this is simple, hopefully. The cards will be rendered at the sides of the table. I’ve used RotatedBox to make it look like the cards are facing the player. I also provided a “tableCenter” widget. This can be some UI element that gets passed in. The center is a reasonable place to put a UI for a simple AI visualizer.

Let’s throw this in our main. We can just center it inside the same container we were using before, like so:

See this content in the original post

If we run that code, it looks like this:

I think this is a suitable look and feel for our Euchre AI visualizer. It’s simple, readable, and not completely atrocious. In the next chapter, we’ll add interactivity and a basic random AI to make the game playable!