[Java] Java beginners make poker game in 4 days (3rd day)

5 minute read

Motivation and purpose

I wanted to know a little bit about Java and started studying on June 21st. Since I learned the basics of Java with progate on 6/21, I will output it. Anything could have been made, but I’m going to try to make the brain sports I love, Texas Hold’em.

For the purpose of increasing output sources for employment, I decided to write down the course of learning here with a diary meaning.

plans

Day 1: Learn about Java with progate (Omitted) Days 2 and 3: Implemented for the time being (it was done!) Day 4: Prepare the code following the object orientation (may not be possible)

Implementation function (Simplified for simplicity)

Yesterday Hand out cards and decide pre-flop action I was able to do it

Where to leave Flop, turn, river movement Strength of hand, determination of role Processing of all-in, fold, etc. is

Hand strength

From the determination of the part of the hand that was most troublesome. For the sake of simplicity, I didn’t install flash and straight this time. Also, please bear in mind that the weakest card is A instead of 2. I thought as follows. B751E5BA-69F5-4EFC-8BEE-5D71AE49D3AF.jpg

Yesterday I managed to do it by continuing to stuff the array with flags.


Public static int[] handRank(int array[]) {

Int count[] = {0,0,0,0,0,0,0,0,0,0,0,0,0};

for(int i: array) {
count[i-1] = count[i-1] + 1;
}
int hand[] = new int[6];
int newCount[] = count.clone();
int pairNumber1;
int pairNumber2;
Arrays.sort(newCount);
if(newCount[12] == 1) {
//System.out.println("hicard");
Arrays.sort(array);
System.arraycopy(array, 2, hand, 0, 5);
return hand;
}else if(newCount[12] == 2 && newCount[11] == 1) {
//System.out.println("pair");
pairNumber1 = handNumber(count, 2).get(0);
pairHand(array, pairNumber1, hand);
return hand;
}else if(newCount[12] == 2 && newCount[11] == 2) {
//System.out.println("two-pair");
pairNumber1 = handNumber(count, 2).get(0);
pairNumber2 = handNumber(count, 2).get(1);
twoPairHand(array, pairNumber1,pairNumber2, hand);
return hand;
}else if(newCount[12] == 3 && newCount[11] == 1) {
//System.out.println("three-card");
pairNumber1 = handNumber(count, 3).get(0);
threePairHand(array, pairNumber1, hand);
return hand;
}else if(newCount[12] == 3 && newCount[11] == 2 || newCount[11] == 3) {
//System.out.println("full-house");
pairNumber1 = handNumber(count, 3).get(0);
pairNumber2 = handNumber(count, 2).get(0);
fullHouseHand(pairNumber1, pairNumber2, hand);
return hand;
}else if(newCount[12] == 4) {
//System.out.println("four-card");
pairNumber1 = handNumber(count, 4).get(0);
fourCard(array, pairNumber1, hand);
return hand;
}
return hand;
}

In the method in each branch, the process of selecting 5 cards from 7 cards and storing it in hand is performed. I put the numbers with roles such as pairs first and fill the rest in descending order of numbers.

public static void twoPairHand(int array[], int pairNumber1, int pairNumber2, int hand[]) {
Arrays.sort(array);
int handNum = 0;
int i = 6;
while(handNum <1) {
if(array[i] != pairNumber1 && array[i] != pairNumber2) {
hand[handNum] = array[i];
i --;
handNum ++;
}else {
i --;
}
}

Flop, turn, river movement

Added a fold flag and an all-in flag to int list[], and skips each street when these are standing, and suddenly shows down.

        int list[] = {1000,1000,0,0,0,0};
        
        //[0] herochip (self)/
        //[1] enemy chip/
        //[2] pot/
        //[3] needChiptoCall
        //[4]foldFlag
        //===foldFlag===
    // not 0 fold
        // 1 hero is fold
        // 2 enemy fold
        
        //[5]alInFlag
        //===allInFlag===
        // 0 not allin
    // 1 hero is allin
    // 2 enemy is allin

The street method contains processing to perform your own actions and the actions of the other party on the street.

// pre-flop processing
It's a sequel.
street(hero, list, scan);
It's a sequel.

//Turn and river processing
It's a sequel.
// Add 3 flops
if(list[4] == 0 && list[5] == 0) {
System.out.println("Flop is" + toDescription(holeCard.get(0)) + toDescription(holeCard.get(1)) + toDescription(holeCard.get(2)) + "is");
street(hero, list, scan);
}


It's a sequel.
// turn
if(list[4] == 0 && list[5] == 0) {

System.out.println("Turn card is "+ toDescription(holeCard.get(3)) + ". Hole card is "+ toDescription(holeCard.get(0)) + toDescription(holeCard.get(1)) + toDescription(holeCard.get(2)) + toDescription(holeCard.get(3)) + "is");
street(hero, list, scan);
}

// river
if(list[4] == 0 && list[5] == 0) {

System.out.println("The river card is "+ toDescription(holeCard.get(4)) + ". The hole card is "+ toDescription(holeCard.get(0)) + toDescription(holeCard.get(1)) + toDescription(holeCard.get(2)) + toDescription(holeCard.get(3)) + toDescription(holeCard.get(4)) + "is");
street(hero, list, scan);
}

// Showdown. Win/Loss Decision
if(list[4] == 1) {// when hero descends
System.out.println("enemy got "+list[2]+"$ chips" );
list[1] = list[1] + list[2];
list[2] = 0;
list[3] = 0;
list[4] = 0;
}else if(list[4] == 2) {
System.out.println("hero won "+list[2]+"$ chips" );
list[0] = list[0] + list[2];
list[2] = 0;
list[3] = 0;
list[4] = 0;
It's a sequel.
}else {
int herorole[] = showdown(hero, holeCard);
int enemyrole[] = showdown(enemy, holeCard);System.out.println("Showdown. Holecard is" + toDescription(holeCard.get(0)) + toDescription(holeCard.get(1)) +toDescription(holeCard.get(2)) +toDescription(holeCard. get(3)) + toDescription(holeCard.get(4)) + "is");
System.out.println("hero :" + toDescription(hero.get(0)) + toDescription(hero.get(1)) + role(herorole[5]));
System.out.println("enemy :" + toDescription(enemy.get(0)) + toDescription(enemy.get(1)) + role(enemyrole[5]));
checkWinner(herorole, enemyrole, list);
}

Execution result

It ends with one hand, but the game continues until either chip reaches 0.

Start the session! Good Luck! ==========1 Hand eye =========== Your hand is ♣8♣4 You have 1000 chips. The opponent has 1000 chips The pot is 0 Please select an action check :c or bet :b b How much do you want to bet? Up to 1000 Ten Bet 10$ ememy raised to 20$. Your hand is ♣8♣4 Your chip is 990. Opponent’s possession chip is 980 The pot is 30 Please select an action call: c or raise :r or fold :f c I called. Proceed to the next street The flop is ♥2♦8♥3 Your hand is ♣8♣4 Your chip is 980. Opponent’s possession chip is 980 The pot is 40 Please select an action check :c or bet :b b How much do you want to bet? Up to 980 980 I’m Allin Bet 980 The enemy called. All-in Proceed to the next street It’s a showdown. The hole card is ♥2♦8♥3♣10♥J hero :♣8♣4 one pair enemy :♦7♠7 one pair The winner is hero! The role is one pair The kicker is 8 hero won 2000 chips The game is over. Thank you for your hard work.

at the end

I can’t get the index of an array from an element, so I thought it would be awkward to use. Should I use the list first? It was the first time to write more than 600 lines of code at once, so I was very tired. The commentary has become super appropriate. Even if I look back one month later, I don’t understand at all… But even though it’s a lot simpler, I’m impressed by the fact that I was able to make something that moved hello world yesterday. Tomorrow, if the university’s tasks can be digested smoothly, I will learn how to write object-oriented and rewrite it. The neck I slept over still hurts good good good good good good

Even so, quite often it is read as quita… I was surprised at the number of views. Source code is below https://github.com/hmck8625/poker/blob/master/poker.java

Tags:

Updated: