Click here to Skip to main content
15,885,365 members
Articles / Mobile Apps / Android
Tip/Trick

Matematico, Android Card Game

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
7 Dec 2014CPOL2 min read 15.8K   769   8  
Logical card Android game

Screenshot from game

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.

Java
@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#.

Java
private int GetArrayValue(String[] Cards)
    {
        //to array
        int retVal = 0;
        int[] arr = new int[5];
        String[] regexVari = { "1111",                             //straight
            "000[1-9]", "[1-9]000",                                //four of kind, poker
            "0[1-9]00", "00[1-9]0",                                //full house
            "0[1-9]0[1-9]", "[1-9]0[1-9]0",                        //two pair
            "00[1-9][1-9]", "[1-9]00[1-9]", "[1-9][1-9]00",        //three of a kind
            "0",                                                   //one pair
            "aaaa",                                                //four aces
            "ajkqt",                                               //royal flush
            "aaakk" };                                             //ace full house
        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);

        // regular combinations
        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;
            }
        }
        // special combinations starting from index 11
        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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --