Introduction
A long time ago, I wrote an article about game Matematico. This article is about using regular expression for a specific scenario.
Matematico for Android
The situation in the IT world is changing and I focused on new platform. First version of this article was written in Xamarin. Good platform, but I found it not so good to use it for Android. Instead, I assumed Java good enough, or better to say as good as C# is for Android. "For Android" are bold.
This tip is written to compare developing for Android using the same code for C#.
Using the Code
I will focus only on significant differences specific for Android. Java's syntax is very very close to C#.
Cards class is a specific class to hold all cards that have been dealed, all used cards and actual card to be set on grid. It is important that this class implements Serializable. Thanks to this, we can pack object into bundle and send to another activity (activity in web means another page). Bundle in this case is similar to viewstate. Specific for Android is switching between activities or even in single activity. When activity is resumed or screen rotation is changed, activity must react to change its state.
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putSerializable("cards",cards);
super.onSaveInstanceState(savedInstanceState);
}
The most important function is detection figure for each row, column and diagonals. As you can see, function is almost the same as in C#.
private int GetArrayValue(String[] Cards)
{
int retVal = 0;
int[] arr = new int[5];
String[] regexVari = { "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 = { 50,
160, 160,
80, 80,
20, 20,
40, 40, 40,
10,
200,
120,
150};
for (int i = 0; i < arr.length; i++) {
arr[i] = getCardValue(Cards[i].charAt(0));
}
Arrays.sort(Cards);
Arrays.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++) {
Pattern pattern = Pattern.compile(regexVari[i]);
Matcher matcher = pattern.matcher(S);
if (matcher.find()) {
retVal = regexVals[i];
break;
}
}
S = "";
for (int i = 0; i < 5; i++) S = S + Cards[i].charAt(0);
for (int i = 11; i < regexVari.length; i++) {
Pattern pattern = Pattern.compile(regexVari[i]);
Matcher matcher = pattern.matcher(S);
if (matcher.find()) {
retVal = regexVals[i];
break;
}
}
return retVal;
}
Points of Interest
So, why is this code worth being listed in CodeProject? In my opinion, it is good for comparing C# and Java usage in Android. There are too many similarities. For me, it is better to learn Java than using Xamarin C#. You can use many of the projects on Github, for example. If you learn some Android specific techniques like adapters, layouts, view holder patterns, you've got good enough knowledge to write useful Android applications.
To Do
Project is done.
History
This is the first version.