Java beginners make poker games in 4 days (2nd day)

Motivation and purpose

I started studying yesterday (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 (I wish I could do it) Day 4: Arrange the code according to object orientation (planned)

Implementation function (simple for simplicity)

** Players heads up only for themselves and their opponent (CPU) The CPU determines the action by random numbers, not by hand strength BTN is always on the other side with no blinds Finish when either chip runs out **

In implementation ** Creating a deck Deck shuffle Distribute your hand to the player ** There was a very easy-to-understand site for the processing of, so I used this as a reference. Must-see for anyone who wants to make something in Java! Creating a game for those who have read the introductory book

The functions that could be implemented in the second day ** Preflop sequence ** It just took quite a while to understand passing values by reference

Code commentary

public static void main(String[] args) {

System.out.println ("Start a session! Good luck!"); //デッキを作成

	List <Integer> deck = new ArrayList<>(52);

//デッキをシャッフル shuffleDeck(deck);

//プレイヤーの手札リストを生成 List hero = new ArrayList<>(); List enemy = new ArrayList<>();

//プレイヤーにカードを二枚ずつ配る hero.add(deck.get(0)); enemy.add(deck.get(1)); hero.add(deck.get(2)); enemy.add(deck.get(3));

//山札の進行状況を記録する変数deckCountを定義 int deckCount = 4;

//プレイヤーの持っているチップを定義(初期値100)potも定義(初期値0) int list[] = {100,100,0,0,0};

// [0] hero chip (self) / // [1] enemy chip / //[2] pot/ //[3] needChiptoCall //[4]foldFlag
//===foldFlag=== // 0 not folded // 1 hero is fold // 2 enemy is fold

	Scanner scan = new Scanner(System.in);

//プリフロップの処理 preFlop(hero, list, scan); //各プレイヤーのアクションの後にneedtocallが0の場合にそのストリートが終了したとできる while(true) { enemyAction(list); if(list[4] == 2) { System.out.println ("enemy got off hero wins"); break; }else if(list[3] == 0) { System.out.println ("Go to flop"); break; } preFlop(hero, list, scan);

    	if(list[4] == 1) {

System.out.println ("hero got off enemy wins"); break; }else if(list[3] == 0) { System.out.println ("Go to flop"); break; } }

//ターン、リバーの処理 //ショーダウン。勝敗の決定 //チップの移動 //チップがなくなったら終了

I don't know anything technical yet, so it's a dirty way to put the necessary data into an array list and pass it in its entirety.

Action of yourself (hero)

There are five basic actions in poker: check, call, bet, raise, and fold. If the opponent's bet (raise) is not included, there are two types of "check, bet" If the opponent's bet (raise) is included, there are three types of actions: "call, raise, fold". They are defined as "checkBet" and "callRaiseFold" methods, respectively. (There is no sense of naming) checkBet

 private static boolean checkBet(int list[], String str, Scanner scan){

if ("c" .equals (str)) {// Processing when checked //相手に回す System.out.println ("checked."); return true; } else if ("b" .equals (str)) {// When betting. Listen to the bet amount and move the chips, //ベット額を聞く while(true) { System.out.println ("How much do you want to bet? Maximum" + list [0] + "$"); int bet = scan.nextInt(); if (bet <= list [0] && bet> = list [2]) {// When the bet amount exceeds the amount you have and when it is less than the pot bet(list, bet); break; }else { System.out.println ("The bet amount is incorrect. Please enter the correct value"); } } return true; }else { return false; } }

callRaiseFold private static boolean callRaiseFold(int list[], String str, Scanner scan){ if ("c" .equals (str)) {// What to do when you call //相手に回す System.out.println ("called."); return true; } else if ("r" .equals (str)) {// When betting. Listen to the bet amount and move the chips, //ベット額を聞く while(true) { System.out.println ("How much do you raise? Maximum" + list [0] + "$"); int raise = scan.nextInt(); if (raise <= list [0] && raise> = list [3]) {// If the wraith amount exceeds the amount you have and if it is smaller than the previous bet (raise) raise(list, raise); break; }else { System.out.println ("The raise amount is incorrect. Please enter the correct value"); } } return true; }else if("f".equals(str)){ System.out.println ("folds"); list[4] = 1; return true; }else { return false; } }

Defined as boolean type. If the input from the command line is invalid, false is returned and a loop is performed. Also, when fold, list [4] is set as foldFlag, 1 when hero folds, and 2 when enemy folds. (Default is 0)

Action of the other party (enemy)

Random numbers are generated using the random library, and the actions are determined by dividing by the numbers. I really like the switch statement because it's easy to read.

private static void enemyAction (int list []) {// Enemy action (varies with random numbers) Random rand = new Random(); int randomValue = rand.nextInt (100); // Generate random numbers

	if(list[3] == 0) {//check or bet
		switch(randomValue%2) {

case 0: // Check System.out.println ("enemy checked."); break; case 1: // bet int bet = list [2]; // Bet amount is fixed at pot size System.out.println ("ememy bet" + bet + "."); //手持ちチップを減らし、ポットをふやす、needtoCallも増やす list[1] = list[1] - bet; list[2] = list[2] + bet; list[3] = bet; break; } }else {//call or raise or fold switch(randomValue % 3) { case 0: // call System.out.println ("enemy called."); //手持ちチップを減らしポットをふやす。needtocallをリセット list[1] = list[1] - list[3]; list[2] = list[2] + list[3]; list[3] = 0; break; case 1: int raise = list[3] * 2; if(raise >= list[1]) { raise = list[1]; System.out.println ("ememy is All-in."); }else { System.out.println ("ememy raised to" + raise + "."); } // needtocall-raise = next needtocall list[1] = list[1] - raise; list[2] = list[2] + raise; list[3] = list[3] - raise; break; case 2: System.out.println ("ememy has folded"); list[4] = 2; break; } } }

Preflop processing

Whether or not there is a raise (advance to the flop) is judged by whether or not list [3] is 0. It took me a while to decide what to do here. I came up with it in a public bath. If the input is in error, request the input again. If foldFlag is set, move to the process of determining the outcome.

private static void preFlop(List<Integer> hero, int list[], Scanner scan) {
	printData(hero, list);

if (list [3] == 0) {// When needchiptocall is 0, that is, when there is no bet raise while(true) { System.out.println ("Please select an action check: c or bet: b"); String str = scan.next(); boolean flag = checkBet(list, str, scan); if(flag == true) { break; }else { System.out.println ("Incorrect input!"); } } }else { while(true) { System.out.println ("Please select an action call: c or raise: r or fold: f"); String str = scan.next(); boolean flag = callRaiseFold(list, str, scan); if(flag == true) { break; }else { System.out.println ("Incorrect input!"); } } } }

Execution result

Start a session! Good Luck! Your hand is ♥ A♣K You have 100 chips The pot is 0 Select an action check: c or bet: b b How much do you want to bet? Up to 100 $ 3 I bet 3 $ The enemy raised to 6 $. Your hand is ♥ A♣K Your chips are 97 The pot is 9 Select an action call: c or raise: r or fold: f r How much do you raise? Up to 97 $ 30 Raised to 30 $ enemy called. Go to the flop

After tomorrow

Turn and river processing can be easily implemented by using action methods. I still don't know how to decide the outcome of the hand (role) It seems that it will take time My misplaced neck hurts (quite)

I don't usually write sentences + it's not for showing to people, so please forgive me for being difficult to read ...

Recommended Posts

Java beginners make poker games in 4 days (2nd day)
Java beginners make poker games in 4 days (3rd day)
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 (2nd day)
Easy to make Slack Bot in Java
[Java] Beginners want to make dating. 1st
Java Day 2018
Represents "next day" and "previous day" in Java / Android
[Java beginner's anguish] Hard-to-test code implemented in Junit