Thursday, August 29, 2019

Java Example Compare Poker Card Value

Compare Poker Card Value

Introduction

To demonstrate the power of object oriented programming, I write a Java program to compare the value of 2 poker cards, using inheritance.

For normal card, from small to large 2 3 4 ... 10 K Q K Ace, eg. 2 of diamond < 3 of diamond For big2 game, 2 is the biggest value, 3 is the smallest. 3 4 5 ... Q K A 2, eg. 2 of diamond > 3 of diamond

I do not create 2 different classes for normal card and big2 card, but inherited the big2 card from normal card (standard card). Many of the properties of the 2 classes are the same. New big2 card comparison can be created with little code added to the new class.

To simplify the program, I do not shuffle the cards, just use the first 2 cards for comparison. You can modified to compare 2 random cards.

class Card { … }, class CompareNormalCard use Card.

class BigTwoCard extends Card { … }, class CompareBigTwoCard use BigTwoCard.

Card.java


$ edit Card.java
class Card {
    private int id; // 0-51, id and value are the same for normal card
    protected int f, v; // f is diamond, club etc, v is 0-9TJKQA
    // use protected because the value was referred inside sub class

    Card() {} // constructor

    Card(int n) { this.setId(n); }

    void setId(int n) {
        this.id= n%52; // id not >= 52
        f= this.id/13; // pre-calculated for getValue(), to save time
        v= this.id%13;
    }

    int getId() { return this.id; }

    // value and id are the same for normal card    
        int getValue() {
        return this.id;
    }

    // return same value==0, this card smaller -1, this card bigger 1
    int isCompare(Card card) {
        if (this.getValue()>card.getValue())
            return 1;
        else if (this.getValue()<card.getValue())
            return -1;

        return 0;
    }

    // for display as D2 DJ DQ DK DA, C2 CJ ...
    // D2 is 2 of diamond
    String getString() {
        int f; // suit 0-Diamond, 1-Club, 2-Heart, 3-Spade
        int v; // 0-2, 1=3... 8=T 9=J 10=Q 11=K 12=A
        String pattern, number;

        f= id/13; // duplicate in setId(). f is protected, can be changed, but id is private
        v= id%13;

        switch(f) {
            case 0: pattern= "D"; break;
            case 1: pattern= "C"; break;
            case 2: pattern= "H"; break;
            case 3: pattern= "S"; break;
            default: pattern= "x";
        }

        if (v>=0 && v<8)
            number= "" + (v+2);
        else if (v==8)
            number= "T";
        else if (v==9)
            number= "J";
        else if (v==10)
            number= "Q";
        else if (v==11)
            number= "K";
        else if (v==12)
            number= "A";
        else
            number= "0";

        return pattern+number;
    }
}



CompareNormalCard.java

$ edit CompareNormalCard.java
public class CompareNormalCard {
    public static void main(String[] args) {
        Card card1= new Card(0),
        card2= new Card(1);
        String symbol;

        if (card1.isCompare(card2)>0)
            symbol= ">";
        else
            symbol= "<";

        System.out.println(card1.getString() + symbol +
            card2.getString());
    }
}


$ javac CompareNormalCard.java && java CompareNormalCard
==>D2<D3
Note: Diamond 2 is smaller than diamond 3

to be continue...CompareBigTwoCard.java

Wednesday, August 28, 2019

Raspberry Pi Desktop Connect Bluetooth Mobile Internet

Is easy to connect bluetooth mouse to Raspberry Pi Desktop, but connect to mobile phone Internet via Bluetooth keep failing. I am using an old Nokia phone for testing.


Choose Bluetooth > Devices...


I am not sure why I have 2 Bluetooth icon on the taskbar. I have added another Bluetooth applet program. These 2 Bluetooth program works slightly different. (note: I suspect is lxplug-bluetooth?)



Select Bluetooth phone



Select (x)Network Access Point



But connect to the service fail!

I am sure the phone and Bluetooth service is working, as it works on other OS. After search on the Internet and read a few tutorial, still can't make it works.

Finally is just a little setting that I miss out. This is the solution:


Choose Bluetooth > Local services...





Enable (x) Network Access Point(NAP)



Select Nokia phone and (x)Network Access Point


Now it works!!

Just the Bluetooth on my device is too slow. It takes much longer time to load.

Friday, August 23, 2019

Install PostgreSQL 10.2

Install PostgreSQL 10.2


Introduction

Even I know much better of MySQL than PostgreSQL, I still prefer PostgreSQL. PostgreSQL is easier to be compiled and less dependency issue. I have successfully compile PostgreSQL on older system, as old as Mac OS X 10.6 Snow Leopard.

Compile and install

The easiest for me is use homebrew. PostgreSQL 10 data format is not compatible with version 9, so backup before you install PostgreSQL 10. The data may be in /usr/local/var/postgres

$ brew install postgres
...
$ cat /usr/local/var/postgres/PG_VERSION
9.4

If installed before backup or upgrade the old database, try
$ brew postgresql-upgrade-database

Setup and startup

To start PostgreSQL
$ pg_ctl -D /usr/local/var/postgres [-l /tmp/postgresql10.log] start
$ pg_ctl -D /usr/local/var/postgres stop

Note: I will prefer to use /usr/local/data/postgres directory.

You can use environment PGDATA=/usr/local/var/postgres
$ pg_ctl -l /tmp/postgresql10.log start

If there is no database in PGDATA directory
$ initdb -D /usr/local/var/postgres
$ cat /usr/local/var/postgres/PG_VERSION
10

Using postgresql

$ psql [ <database> ] [-U <user>]
$ psql postgresql
psql (10.2, server 10.1)
Type "help" for help.

postgres=#\q (quit)

Note: Seems like my database is created by PostgreSQL 10.1? To check version => select version();

Default database is postgres. You should create another user and database for development or testing purpose. The progress is not covered here.