Regular straight hand calculation in poker - Using ASCII CODE

In another question, I found out how to calculate the direct hand in poker using regex ( here ).

Now, curiously, the question is: Is it possible to use a regular expression to evaluate the same using ASCII CODE?

Sort of:

regex: [C] [C + 1] [C + 2] [C + 3] [C + 4] being a C ASCII CODE (or something like this)

Matches: 45678,23456

Doesn't match: 45679or 23459(not in sequence)

+4
source share
4 answers

SOLVE!

See http://jsfiddle.net/g48K9/3

I decided to use closure in js.

String.prototype.isSequence = function () {
    If (this == "A2345") return true; // an exception
    return this.replace(/(\w)(\w)(\w)(\w)(\w)/, function (a, g1, g2, g3, g4, g5) {
        return    code(g1) == code(g2) -1 &&
                code(g2) == code(g3) -1 &&
                code(g3) == code(g4) -1 &&
                code(g4) == code(g5) -1;
    })
};

function code(card){
    switch(card){
        case "T": return 58;
        case "J": return 59;
        case "Q": return 60;
        case "K": return 61;
        case "A": return 62;
        default: return card.charCodeAt();
    }
}


test("23456");
test("23444");
test("789TJ");
test("TJQKA");
test("8JQKA");

function test(cards) {
    alert("cards " + cards + ": " + cards.isSequence())
}

Just to clarify ascii codes:

ASCII CODES:

2 = 50
3 = 51
4 = 52
5 = 53
6 = 54
7 = 55
8 = 56
9 = 57
T = 84 -> 58
J = 74 -> 59
Q = 81 -> 60
K = 75 -> 61
A = 65 -> 62
0

, ASCII- , , .

2345A, 23456, 34567, ..., 6789T, 789TJ, 89TJQ, 9TJQK TJQKA.

ASCII, , , A2345 TJQKA , A , , .

, :

(2345A|23456|34567|45678|56789|6789T|789TJ|89TJQ|9TJQK|TJQKA)

, .

+5

, , , , , , - , .

Java, , , , 5.

    String seq = "ABCDEFGHIJKLMNOP";
    System.out.printf("^(%s)$",
        seq.replaceAll(
            "(?=(.{5}).).",
            "$1|"
        )
    );

( ideone.com):

^(ABCDE|BCDEF|CDEFG|DEFGH|EFGHI|FGHIJ|GHIJK|HIJKL|IJKLM|JKLMN|KLMNO|LMNOP)$

, , , seq .


. metacharacter "" ( , ).

{5} . .{5} 5 ..

(?=…) ; , , , ( ) .

(…) . , , , , .

:

     match one char
        at a time
           |
(?=(.{5}).).
\_________/
 must be able to see 6 chars ahead
 (capture the first 5)

. . , , (?=…), 6 (.{5})., (…) .{5}. $1|, , 1, .

, , String seq = "ABCDEFG";. .

=== INPUT ===                                    === OUTPUT ===

 A B C D E F G                                   ABCDE|BCDEFG
↑
We can assert (?=(.{5}).), matching ABCDEF
in the lookahead. ABCDE is captured.
We now match A, and replace with ABCDE|

 A B C D E F G                                   ABCDE|BCDEF|CDEFG
We can assert (?=(.{5}).), matching BCDEFG
in the lookahead. BCDEF is captured.
We now match B, and replace with BCDEF|

 A B C D E F G                                   ABCDE|BCDEF|CDEFG
    ↑
Can't assert (?=(.{5}).), skip forward

 A B C D E F G                                   ABCDE|BCDEF|CDEFG
      ↑
Can't assert (?=(.{5}).), skip forward

 A B C D E F G                                   ABCDE|BCDEF|CDEFG
        ↑
Can't assert (?=(.{5}).), skip forward

       :
       :

 A B C D E F G                                   ABCDE|BCDEF|CDEFG
              ↑
Can't assert (?=(.{5}).), and we are at
the end of the string, so we're done.

, ABCDE|BCDEF|CDEFG, 5 seq.

+4

- regex: [C][C+1][C+2][C+3][C+4], C ASCII CODE ( )

- . , .

mainstream, , x ASCII.


...

(. ideone.com):

    String alpha = "ABCDEFGHIJKLMN";
    String p = alpha.replaceAll(".(?=(.))", "$0(?=$1|\\$)|") + "$";

    System.out.println(p);
    // A(?=B|$)|B(?=C|$)|C(?=D|$)|D(?=E|$)|E(?=F|$)|F(?=G|$)|G(?=H|$)|
    // H(?=I|$)|I(?=J|$)|J(?=K|$)|K(?=L|$)|L(?=M|$)|M(?=N|$)|N$

    String p5 = String.format("(?:%s){5}", p);

    String[] tests = {
        "ABCDE",    // true
        "JKLMN",    // true
        "AAAAA",    // false
        "ABCDEFGH", // false
        "ABCD",     // false
        "ACEGI",    // false
        "FGHIJ",    // true
    };
    for (String test : tests) {
        System.out.printf("[%s] : %s%n",
            test,
            test.matches(p5)
        );
    }

-regexing. , ( ), lookahead. -regexed 5 .

You can replace it alphawith your poker sequence if necessary.

Please note that this is an ABSOLUTELY IMPRACTIC solution. It is much more readable, for example, just check if alpha.contains(test) && (test.length() == 5).

Related Questions

+3
source

All Articles