Java beginners make poker games in 4 days (3rd day)

Motivation and purpose

I started studying from 6/21 to know a little about Java. I learned the basics of Java with progate on 6/21, so I will output it. I could have made anything, but I would like to make the brain sport "Texas Hold'em" that I love.

For the purpose of increasing the output source for employment, I decided to make a note of the learning trajectory here in a diary sense.

plans

Day 1: Learn about Java with progate (omitted) Day 2 and 3: Implemented for the time being (done!) Day 4: Arrange the code according to object orientation (maybe impossible)

Implementation function (simple for simplicity)

Yesterday ** Deal cards and decide preflop action ** I was able to do it

Where to leave ** Flop, turn, river movement Hand strength, role determination Processing such as all-in, fold ** is

Hand strength

From the decision of the role of the hand that bothered me the most. For the sake of simplicity, I did not implement flash and straight this time. Also, the weakest card is A instead of 2, but please forgive me. I thought as follows. B751E5BA-69F5-4EFC-8BEE-5D71AE49D3AF.jpg

I managed to do it by stuffing the array with flags as I did yesterday.


  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, 5 cards are selected from the 7 cards so that they are the strongest cards and stored in the hand. I put numbers with roles such as pairs first, and packed 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 fold flag and all-in flag to int list [], skipping each street when these are set, and suddenly showdown.

        int list[] = {1000,1000,0,0,0,0};
        
        //[0] herochip(myself)/
        //[1] enemychip(Opponent)/
        //[2] pot/
        //[3] needChiptoCall
        //[4]foldFlag				       
        //===foldFlag===
    		//0 not folded
        	//1 hero is fold
        	//2 enemy is fold
        
        //[5]alInFlag
        //===allInFlag===
        	//0 not all in
    		//1 hero is all in
    		//2 enemy is allin

The street method contains the process of performing your own action and the action of the other party on the street.

	         //Preflop processing
	    	
	    	street(hero, list, scan);
	    	

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


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

	            System.out.println("The turn card is" + toDescription(holeCard.get(3)) + "is. The hole card"+ 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("River card" + toDescription(holeCard.get(4)) + "is. The hole card"+ 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 gets off
				System.out.println("enemy is"+list[2]+"$Won the 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 is"+list[2]+"$Won the chips" );
	    		list[0] = list[0] + list[2];
	    		list[2] = 0;
	    		list[3] = 0;
	    		list[4] = 0;
	    		
	    	}else {
	            int herorole[] = showdown(hero, holeCard);
	            int enemyrole[] = showdown(enemy, holeCard);
	            
	            System.out.println("It's a showdown. The hole card"+ 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 one of the chips becomes 0.

Start a session! Good Luck! ========== 1st hand =========== Your hand is ♣ 8 ♣ 4 You have 1000 chips. Your opponent has 1000 chips The pot is 0 Select an action check: c or bet: b b How much do you want to bet? Up to 1000 10 I bet 10 $ The enemy raised to $ 20. Your hand is ♣ 8 ♣ 4 Your chip is 990. The opponent's chip is 980 The pot is 30 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. The opponent's chip is 980 The pot is 40 Select an action check: c or bet: b b How much do you want to bet? Up to 980 980 All in I bet 980 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 Kicker is 8 hero won 2000 chips The game is over. Thank you for your hard work.

at the end

I couldn't retrieve the index from the element of the array, and I wondered if it wasn't easy to use. Should I give priority to the list? It was my 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 a month later, I don't think I can understand it at all ... However, even though it is considerably simplified, I am impressed that I was able to make something that works by myself, who was hello world the day before yesterday. Tomorrow, if I can successfully digest the university assignments, I will learn how to write object-oriented and rewrite it. My misplaced neck still hurts Good Good Good Good Good

Even so, quite a lot of quita is read ... I was surprised at the number of views. The source code is below https://github.com/hmck8625/poker/blob/master/poker.java

Recommended Posts

Java beginners make poker games in 4 days (3rd day)
Java beginners make poker games in 4 days (2nd day)
Make Blackjack in Java
Refactoring: Make Blackjack in Java
Rock-paper-scissors game for beginners in Java
[For beginners] Run Selenium in Java
How to learn JAVA in 7 days
New engineer who will be one serving in 100 days (3rd day)
Easy to make Slack Bot in Java
[Java] Beginners want to make dating. 1st
Java Day 2018
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
Represents "next day" and "previous day" in Java / Android
[Java beginner's anguish] Hard-to-test code implemented in Junit