Introduction
This article is about Matematico, a logical game I played when I was 11 years old. I never forgot this game and my teacher who was a very clever woman. Now, my daughter Nina is able to play with me and I create this page for her just for fun. And for you, of course. You can play this game with your children, parents, and friends. IMHO this game is better than bingo, and poker.
Matematico - Rules
Mix 52 cards and turn the first one on the front side. Into a 5x5 grid, write a card (value is enough; color has no meaning in this game). Turn the next card and so on until the 25th card is reached. You must write each card into a grid when it is turned on the next card. You must write a card to create a figure: straight, four of a kind (or four aces), full house (full house with three aces), two pairs, one pair, three of a kind, royal flush. Each figure has a value. Don't care about the card color; straight does not have to be sorted. Figures can be created in rows, columns and both diagonals (bonus +10 points).
Points |
Figures (cards do not need to be sorted) |
10 |
One pair |
20 |
Two Pairs |
40 |
Three of a kind |
50 |
Straight |
80 |
Full house |
100 |
Three aces and two kings |
150 |
Royal flush (straight to ace) |
160 |
Four of a kind |
200 |
Four aces |
+10 |
Additional points for figure in diagonal |
Using the Code
This is the most significant routine (behavior is described below) for finding the figure in the array and calculating points for this figure. As you can see, the array is sorted in the first step. In the next step, the difference between two nearby cards is computed. In the next step, the result is tested for a regular expression for common variants and for special variants such as four aces.
private int GetArrayValue(string[] Cards)
{
int retval = 0;
int[] arr = new int[5];
string[] RegexVari = new String[14]
{ "1111",
"000[1-9]", "[1-9]000",
"0[1-9]00", "00[1-9]0",
"0[1-9]0[1-9]", "[1-9]0[1-9]0",
"00[1-9][1-9]", "[1-9]00[1-9]", "[1-9][1-9]00",
"0",
"AAAA",
"AJKQT",
"AAAKK" };
int[] RegexVals = new int[14] { 50,
160, 160,
80, 80,
20, 20,
40, 40, 40,
10,
200,
150,
100};
for (int i = 0; i < arr.Length; i++) {
arr[i] = GetCardValue(Cards[i][0]);
}
Array.Sort(Cards);
Array.Sort(arr);
String S = "";
for (int i = 0; i < 4; i++) S = S +
(Math.Abs(arr[i] - arr[i + 1]) > 1 ? 9 : Math.Abs(arr[i] - arr[i + 1]));
for (int i = 0; i < RegexVari.Length; i++) {
Regex rx = new Regex(RegexVari[i]);
if (rx.Match(S).Success) {
retval = RegexVals[i];
break;
}
}
S = "";
for (int i = 0; i < 5; i++) S = S + Cards[i][0];
for (int i = 0; i < RegexVari.Length; i++) {
Regex rx = new Regex(RegexVari[i]);
if (rx.Match(S).Success) {
retval = RegexVals[i];
break;
}
}
return retval;
}
Points of Interest
So, why is this code worth being listed on The Code Project? There is a problem. How do we count figures in rows, columns and diagonals? Use brute force? Or something smarter? The solution for this problem gives me… regular expressions. I love this tool. How to do it? Imagine figure 4 – 6 – 7- 7 – 8. as one pair. How to find it? The first thing I must to do is sort the cards. The second thing is to compute differences between cards. In this case, is it 2-1-0-1. (4 subtracted from 6 is two, 6 subtracted from 7 is one, 7 subtracted from 7 is zero, 7 subtracted from 8 is one.. of course in absolute values). Well, now I must enumerate all combinations for regular expressions to find this pair. Exactly I must find zero. Zero means there are two cards with no difference between them. Two same cards! For a better understanding, there are full house figures: K – K – K – 8 – 8. The only allowed combination for regex are "0[1-9]00", "00[1-9]0". This means.. two cards with zero differences and three cards or three cards then two cards. In this case are 00[1-9]0. You must also consider priority, because one pair (“0“) can also be found in three of a kind, so I must test for this combination before one pair.
To Do
There are two things to do, the first one is “tune” points values for each combination and add other figures. In my opinion, +10 points for diagonal is not sufficient because to create a figure in diagonal intact all columns and rows and you must obey this in the following figures. The second one is to create an algorithm to compute the best variant from all dealt cards to get the best score. Moreover, create an algorithm for hint, as to where to place a card during a game.
History
- 2nd July, 2008: Initial post