Tag:java stud game
<span style= "FONT-SIZE:18PX;" > Below this class encapsulates a stud game, specific ideas see Code package Lkl1;import java.util.arrays;import Java.util.scanner;import java.util.Random; public class Suohagame {//Player hand in the hands of the cards with a number of combinations, the first digit 4--1 represents//cards of the suit (respectively, Black Mei Fang), the following one or two digits//the size of the card from the 1--14 respectively represents A--k, A may be 1 or 14 indicated) public Suohagame () {for (int i=1;i<=5;i++) {score[i]=1000;xiazhu[i]=0;}} Initialize global variable public static void Init () {cnt=1;vis[0]=1;win=0;max=0;} The following are the global record variables private string[] dx={"", "a", "2", "3", "4", "5", "6", "7", "8", "9", "Ten", "J", "Q", "K", "a"};//to represent the size of the card private String[] hs={"" "," block "," Plum "," Red Peach "," spades "};//the color of the card is the private static int max;//record the maximum score of bets in the front wheel private static int win; The last winning player's number is private static int cnt; Current to the first few rounds of private static int[] vis= new int[500]; Used to mark those cards that have been used//the following to record the player's information public int [] score =new int[7]; Record the score on each player's hand public int[] Xiazhu=new int[7]; Record players in this round betting situation public int[] Flag=new int[7]; Record each player's game state public int [] card[] =new int[7][10]; Record the hands of each player///The following is a system behavior function//custom sort function, to achieve the sort from large to small public void sort (int[] a) {for (int i=1;i<=5;i+ +) for (int j=i+1;j<=5;j++) {if (a[i]<a[j]) {int t=a[i];a[i]=a[j];a[j]=t;}}} Pass in two numbered players, and return the number of the player with the big card public int Bijiao (int x1,int x2,int num) {int[] t1 = new int [20];//record player 1 card size int[] t2 = new int [10]; Record Player 1-card suit int[] t3= new int [20]; Record Player 2 card size int[] T4 = new int[10]; Record Player 2-card suit for (int j=1;j<=5;j++) {T1[j]=daxiao (card[x1][j],1); T3[j]=daxiao (card[x2][j],1); T2[j]=huase (Card[x1][j]); T4[j]=huase (Card[x2][j]);} if (num==9| | num==5) {//in straightening the case int tt1=0,tt2=0;//record the largest card corresponding to the suit int j1=-1,j2=-1; for (int i=1;i<=5;i++) {if (t1[i]>j1) {j1=t1[i]; Tt1=t2[i]; } if (t3[i]>j2) {j2=t3[i]; Tt2=t4[i]; }}//arrays.sort (t1); Call system Quick Sort (T1); sort (T3), if (t1[1]>t3[1])//Only the largest card return X1;else if (t1[1]<t3[1]) r Eturn x2;else{//The size is exactly the same color, but also only need to compare the largest card if (TT1>TT2) return X1;elsereturn x2;} else{for (int j=1;j<=5;j++) {//re-specify the size of a t1[j]=daxiao (card[x1][j],0); T3[j]=daxiao (card[x2][j],0);} int tt1=0,tt2=0;//record the largest card corresponding to the suit int j1=-1,j2=-1; For (int i=1;i<=5;i++) {if (t1[i]>j1) {j1=t1[i]; Tt1=t2[i]; } if (t3[i]>j2) {j2=t3[i]; Tt2=t4[i]; }}sort (t1); sort (T3);///The following is a different judgment on the classification of the cards if (num==8| |num==7| | num==4) {//For 4 and 3 identical case order comparison size if (T1[3]>t2[3]) return x1; else return x2;} if (num==6) {//Handle the same flower case//Compare size first, then compare suit int i=0;for (i=1;i<=5;i++) {if (T1[i]>t3[i]) return X1;else if (T1[i]<t3[i]) return x2;} if (i>5) {if (TT1>TT2) return X1;elsereturn x2;}} When dealing with 2 pairs and 1 pairs, you need to know the pair//So first preprocess int[] vis1=new int[22];int[] vis2 =new int[22];for (int i=0;i<=20;i++) {vis1[i]=0; vis2[i]=0;} for (int i=1;i<=5;i++) {vis1[t1[i]]++;vis2[t3[i]]++;} if (num==3) {//handles 2 pairs of times, compare two pairs in turn, then compare the size of the single card with the color int t11=0,t12=0,t13=0;//The local variable must be initialized after the int t21=0,t22=0,t23=0 is defined; int cnt1=0,cnt2=0; for (int d=14;d>=1;d--) {if (vis1[d]==2&&cnt1==0) {t11=d; cnt1++; } if (vis1[d]==2&&cnt1==1) {t12=d; cnt1++; } if (vis1[d]==1) T13=d; if (vis2[d]==2&&cnt2==0) {t21=d; cnt2++; } if (VIS2[D]==2&Amp;&cnt2==1) {t22=d; cnt2++; } if (vis2[d]==1) T23=d; } if (t11>t21) return x1; else if (t11<t21) return x2; else if (t12>t22) return x1; else if (t21<t22) return x2; else if (t13>t23) return x1; else if (t13<t23) return x2; else if (TT1>TT2) return x1; else return x2;} if (num==2) {//handle a pair of cases int xx1=0,xx2=0; for (int i=1;i<=5;i++) {if (vis1[t1[i]]==2) {xx1=t1[i]; T1[i]=-1; } if (vis2[t3[i]]==2) {xx2=t3[i]; T3[i]=-1; }}//System.out.println (xx1+ "" +xx2); if (XX1>XX2) return x1; else if (xx1<xx2) return x2; Sort (t1); Sort (T3); int i=0; for (i=1;i<5;i++) {if (T1[i]>t3[i]) return x1; else if (T1[i]<t3[i]) return x2; } if (i>=5) {if (TT1>TT2) return x1; Else return x2; }} if (num==1) {//handle all the cases of the hash//compare the size before comparing the largest card's suit int i=0;for (i=1;i<=5;i++) {if (T1[i]>t3[i]) return X1;else if (T1[i] <t3[i]) return x2;} if (i>5) {if (TT1>TT2) return X1;elsereturn x2;}}} return 0;} Obtain the player hand number//incoming need to deal with the player's number, return the player hand card corresponding to the number of public int getnum (int i) {////Compare the first preprocessing to remove the basic information (whether the suit is the same, three cards have the same////, the two cards have a few cards, Whether there is a CIS) and then use this information to compare the Boolean is_tonghua=false; Are the colors the same? Boolean Is_shunzi=false; Whether there is a cis int num_four=0; The number of four identical cards int num_three=0; Three cards of the same number int num_two=0; The number of two cards int[] t1 = new int [10]; Record player's size int[] t2 = new int [10]; The color of the player card is recorded int j;for (j=1;j<=5;j++) {T1[j]=daxiao (card[i][j],1); T2[j]=huase (Card[i][j]);} Color judging for (j=2;j<=5;j++) {if (t2[j]!=t2[j-1]) break;} if (j>5) {is_tonghua=true;} Whether there is a CIS judge sort (T1); for (j=2;j<=5;j++) {if (t1[j]!=t1[j-1]-1) break;} if (j>5) {is_shunzi=true;} The same number of cards is recorded for (j=1;j<=5;) {if (j+3<=5&&t1[j]==t1[j+1]&&t1[j]==t1[j+2]&&t1[j]==t1[j+3 ] {num_four++; break;} if (j+2<=5&&t1[j]==t1[j+1]&&t1[j]==t1[j+2]) {num_three++; j+=3;} else if (j+1<=5&&t1[j]==t1[j+1] {num_two++; j+=2;} j + +; }///is judged by the basic information obtained if (Is_tonghua&&is_shunzi)//flush return 9; if (num_four==1) return 8; if (num_three==1&&num_two==1)//Forhaus return 7; if (Is_tonghua)//flush return 6; if (Is_shunzi)//CIS return 5; if (num_three==1)//three return 4; if (num_two==2)//Two (two teams) return 3; if (num_two==1)//single pair return 2; return 1; The hash card}//is obtained by the combination of two random numbers to get the corresponding card public int cal (int t1,int T2) {if (t1>=10) return T2*100+t1;elsereturn t2*10+t1;} Returns the size of a card///For A to be treated separately, in a single comparison as the largest (14)///In the composition of the CIS as the smallest. The specific return value returned by FG control///FG to 0 o'clock returns 14 (single card case); FG is 1 o'clock returns 1 (CIS case) public int Daxiao (int x,int FG) {if (x>100) x=x%100;elsex=x%10;if (fg==0&&x==1) return 14;if (FG ==1&&x==1) return 1;return x;} Returns the color of a card public int huase (int x) {if (x>100) return X/100;elsereturn X/10;} After the game, the player's cards are sorted from large to small, then the public void print1 (int num) {int[] xx1=new int[20] is exported; Xx1 the size of the record card for (int i=0;i<20;i++) {xx1[i]=0;}for (int i=1;i<=5;i++) {int T=daxiao (card[num][i],0); xx1[t]++;} System.out.print (num+ "player's hand is:"); for (int d=4;d>=1;d--) {for (int i=14;i>=1;i--) {if (Xx1[i]==d) { j=1;j<=5;j++) {if (Daxiao (card[num][j],0) ==i) {System.out.print (Hs[huase (card[num][j])]+dx[i]+ ""); }}}}}system.out.println ("");} Print out the cards that each player displays on the desktop public void print (int num,int j) {if (j==2) System.out.print (num+ "Player's current card on the desktop:"); else System.out.print (num+ "player hands are:"); for (int i=j;i<=cnt;i++) {System.out.print (Hs[huase (Card[num][i])]); System.out.print (Dx[daxiao (card[num][i],0)]+ "");} System.out.println ();} The player chooses the function//gives the player a different choice, and the player's choice to modify the player's state public int choice (int num) {Scanner sc = new Scanner (system.in); if (Score[num]-xiazhu[num]<max) {//If the player is able to wager a score less than the minimum required score, the player exits the game System.out.println (num+ ") the player has insufficient score on your hand! Automatic exit "); Score[num]-=xiazhu[num]; Flag[num]=1; return 0; } System.out.println (num+ "Number of players please enter a number to select:"); int t; T=sc.nextint(); if (cnt!=5) {while (t>2| | t<0) {System.out.println ("wrong input! Please re-enter:"); T=sc.nextint (); }} else{while (t>3| | t<0) {System.out.println ("wrong input! Please re-enter:"); T=sc.nextint (); }} if (t==2) {//If the player abandons, subtract the score of his bet and Mark Score[num]-=xiazhu[num]; Flag[num]=1; return 0; if (t==1) {//player chooses to wager the score System.out.println (num+ "Number of players please select the BET score (" +max+ "---" + (Score[num]-xiazhu[num]) + "):"); int Fenshu; Fenshu=sc.nextint (); while (fenshu<max| | Fenshu> (Score[num]-xiazhu[num])) {System.out.println ("Input Error! Please re-enter:"); Fenshu=sc.nextint (); } if (Max<fenshu) {Max=fenshu; } Xiazhu[num]+=fenshu; return 0; } if (t==3) {Xiazhu[num]=score[num]; System.out.println (num+ "player chooses stud! Other players Please select"); System.out.println ("1: Stud 2: Abort"); int j=num+1; while (J!=num) {if (j>5) j-=5; if (flag[j]==0) {SYSTEM.OUt.println (j+ "Number of players please enter the number to select:"); int ch; Ch=sc.nextint (); while (ch<1| | CH>2) {System.out.println ("Input error Please re-enter:"); Ch=sc.nextint (); } if (ch==1) {xiazhu[j]=score[j]; } else{Score[j]-=xiazhu[j]; Flag[j]=1; }} j + +; if (j>5) j-=5; } return 1; Returns 1} return 0 when there is a player stud;} In addition to the last round, check if all players make a selection, whether or not to produce the winner://If only one player does not give up is the winner//If all players give up, it is a draw public void check () {int cnt=0;int temp=0;for (int i= 1;i<=5;i++) {if (flag[i]==0) {cnt++;temp=i;}} if (cnt==1) {win=temp;} else if (cnt==0) {win=-1;}} function public int judge () {if (cnt<5) {//////////////////To compare one card at a time to determine the size of the player's hand////And then compare the color int tmp=0,j=0;for (int i=1;i<=5;i++ ) {if (Flag[i]==0&&tmp<daxiao (card[i][cnt],0)) Tmp=daxiao (card[i][cnt],0); J=i;} for (int i=1;i<=5;i++) {if (Flag[i]==0&&daxiao (card[i][cnt],0) ==tmp) {if (Huase (card[i][cnt]) >Huase ( CARD[J][CNT])) {j=i;}}} return J;} ELSE {/////////Compare 5 cards at a time, it is not possible to compare directly, so we first divided the cards into several categories///respectively with 9--1, four, Forhaus (3 as + a pair), with flowers, CIS///Three, two (2 pairs), scattered cards. Each player's hand is then mapped to a number, so that if the number is different, it can be compared directly. If the number is the same, you need to do a further///comparison: Compare the size and then if the size is exactly the same color, where Forhaus///and three only need to compare the size of the cards that make up three cards, for two to simplify to three cards to compare// For other types, you need to sort and then compare the comparison size and then the color comparison int[] num =new int[10]; The number of each player hand card corresponding to the initial -1for (int i=1;i<=5;i++) num[i]=-1;for (int i=1;i<=5;i++) {if (flag[i]==0) {num[i]=getnum (i); }}/*//test statement for (int i=1;i<=5;i++) {System.out.print (num[i]+ "");} System.out.println (); */int tmp1=0,tmp2=-1; TMP1 record the largest player number on hand, TMP2 record the current maximum sequence number for (int i=1;i<=5;i++) {if (NUM[I]>TMP2) {tmp2=num[i];tmp1=i;} else if (NUM[I]==TMP2) {Tmp1=bijiao (TMP1,I,TMP2);}} return TMP1;} }//Licensing function//implementation to a player licensing public void Fapai (int num,int i) {if (flag[num]==1)//If you have exited the game, do not send a card return; Random rand =new random (); int t1=0,t2=0,t=0; while (vis[t]==1) {//generates 1--13 random number as the size of the card T1=rand.nextint (12) +1; Generate 1--4 random number as the color of the card T2=rand.nextint (3) +1; T=cal (T1,T2); if (vis[t]==0) {vis[t]=1; card[num][i]=t; return;} }}//Game procedure function public void Play () {cnt<=5) {//cnt denotes the first round of games if (cnt==1) {////////////////////////////-the first round of cards, 2 cards per player, one for the cards, and the rest for the 1;j<=5;j++) {for (int i1=1;i1<=2;i1++) {Fapai (J,I1);}} cnt++; After the first round, print out the cards that each player should display on the table for (int i1=1;i1<=5;i1++) {print (i1,2);} }else{//System.out.println ("The current" +cnt+ "Round"), or//after each wheel is the first to give the players to choose, when all players have made a choice//and then from the desktop cards the largest players start clockwise licensing for (int i1=1;i1< =5;i1++) {if (flag[i1]==1) continue; System.out.println ("1: Bet 2: Give up"); if (cnt==5) {System.out.println ("3: Stud"); } if (choice (i1) ==1) break; } check (); int J=judge (); The number if (cnt==5) {win=j) of the largest player on the current desktop card is obtained; if (win!=0) {///If the winner has been generated, output its number, all cards that did not exit the player at the End if (win==-1) {System.out.println ("Unfortunately, all players have given up"); Return } System.out.println ("The final winner is:" +win+ "player"); for (int i=1;i<=5;i++) {if (I==win) continue; Score[win]+=xiazhu[i]; } System.out.println ("This council has earned a score of:" + (score[win]-1000)); for (int i=1;i<=5;i++) {if (Flag[i]==0&&i!=win) {Score[i]-=xiazhu[i]; }} for (int i=1;i<=5;i++) {if (flag[i]==0) {print1 (i); Print (i,1); Print out all the cards sticking to the last player//System.out.println ("");//Test statement}}//Print out the end of each player's hand score for (int i=1;i<=5;i++) {Syste M.out.println (i+ "Number of players in the hands of the score is:" +score[i]); } return; }//Give J cards first, then turn the cards clockwise Fapai (j,cnt+1); int i=j+1; System.out.println (i); while (I!=J) {if (i>5) i-=5; Fapai (i,cnt+1); i++; if (i>5) i-=5; } cnt++; After the card is issued, you should increase the for (int i1=1;i1<=5;i1++) {/////after each round of the card to print out the player's card on the table if (flag[i1]==0) {print (i1,2); }}}//max=0; }}public static void Main (string[] args) {suohagame sg= new suohagame (); Init (); Sg.play ();}} </span>
java--Console Stud Games