三个人扎金花,显示出最大的那个人 博客分类: 代码交流 游戏
程序员文章站
2024-03-14 13:03:28
...
今天突然想到比较三个单张的时候可以用三个排序后的整型数组合成字符串再转换成整型来比较大小。三个单张的牌,整型的数越大,牌型也就越大。 //刚刚又测试了下,发现这样的话大多数情况都是对的,但是还是有很高的几率错的。比如 一个人牌是12 10 5另一个人的牌是13 2 3,整型的数肯定是前者的大,但是牌型是后者的大,所以这个方法还是不行。 import java.lang.reflect.Array; import java.util.Arrays; public class zhajinhua { public static void main(String[] args) { // TODO Auto-generated method stub Shuffle.xipai(); Shuffle.fapai(); Shuffle a=new Shuffle(); Shuffle b=new Shuffle(); Shuffle c=new Shuffle(); a.x(a.player1,"玩家一"); a.zhuanhuan(a.playerpuke); a.paixing(a.playerhuase, a.playerpukeint1); b.x(b.player2, "玩家二"); b.zhuanhuan(b.playerpuke); b.paixing(b.playerhuase, b.playerpukeint1); c.x(c.player3, "玩家三"); c.zhuanhuan(c.playerpuke); c.paixing(c.playerhuase, c.playerpukeint1); Shuffle.bijiao(a.paixing, b.paixing, c.paixing, a.playerpukeint1, b.playerpukeint1, c.playerpukeint1,a.maxcard,b.maxcard,c.maxcard); } } class Shuffle{ static int xipai; static String temp; static String [] allCard=new String[52]; static String [] huase={"红心","方块","黑桃","梅花"}; static String [] puke={"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; static int k=0; static String [] player1=new String[3];//玩家手牌 static String [] player2=new String[3]; static String [] player3=new String[3]; String [] playerhuase=new String[3];//玩家手牌每张的花色 String [] playerpuke=new String[3];//玩家手牌每张的点数 int[] playerpukeint1=new int[3]; int one,two; int paixing; int maxcard; static void xipai(){ for(int i=0;i<huase.length;i++){ for(int j=0;j<puke.length;j++){ allCard[k]=huase[i]+puke[j]; k++; } } for(int i=0;i<52;i++){ xipai=(int)(Math.random()*52); temp=allCard[i]; allCard[i]=allCard[xipai]; allCard[xipai]=temp; } } static void fapai(){ int i=0; for(int j=0;j<3;j++){ player1[j]=allCard[i]; player2[j]=allCard[i+1]; player3[j]=allCard[i+2]; i=i+3; } } void x(String[] player,String play){ System.out.print(play+"的牌:"); for(int j=0;j<3;j++){ System.out.print(player[j]+" "); if(player[j].length()==4){ playerpuke[j]=player[j].substring(2, 4); }else{ playerpuke[j]=player[j].substring(2, 3); } playerhuase[j]=player[j].substring(0, 2); } System.out.println(); } void zhuanhuan(String[] puke){ for(int i=0;i<3;i++){ switch(puke[i]){ case "2": playerpukeint1[i]=2; break; case "3": playerpukeint1[i]=3; break; case "4": playerpukeint1[i]=4; break; case "5": playerpukeint1[i]=5; break; case "6": playerpukeint1[i]=6; break; case "7": playerpukeint1[i]=7; break; case "8": playerpukeint1[i]=8; break; case "9": playerpukeint1[i]=9; break; case "10": playerpukeint1[i]=10; break; case "J": playerpukeint1[i]=11; break; case "Q": playerpukeint1[i]=12; break; case "K": playerpukeint1[i]=13; break; case "A": playerpukeint1[i]=14; break; } } } void paixing(String[] playerhuase,int[] playerhuaseint1){ Arrays.sort(playerhuaseint1); if(playerhuaseint1[0]==playerhuaseint1[1]&&playerhuaseint1[1]==playerhuaseint1[2]){ paixing=6; maxcard=playerhuaseint1[0]; }else if(playerhuaseint1[0]==playerhuaseint1[1]||playerhuaseint1[1]==playerhuaseint1[2]||playerhuaseint1[0]==playerhuaseint1[2]){ paixing=2; for(int i=0;i<3;i++){//如果是一对,两两相减结果为0的就是那对 for(int j=i+1;j<3;j++){ if(playerhuaseint1[i]-playerhuaseint1[j]==0){ maxcard=playerhuaseint1[i]; } } } } if(playerhuase[0].equals(playerhuase[1])&&playerhuase[0].equals(playerhuase[2])&&playerhuaseint1[2]-playerhuaseint1[1]==1&&playerhuaseint1[1]-playerhuaseint1[0]==1){ paixing=5; maxcard=playerhuaseint1[2]; }else if(playerhuase[0].equals(playerhuase[1])&&playerhuase[0].equals(playerhuase[2])){ paixing=4; maxcard=playerhuaseint1[2]; }else if(playerhuaseint1[2]-playerhuaseint1[1]==1&&playerhuaseint1[1]-playerhuaseint1[0]==1){ paixing=3; maxcard=playerhuaseint1[2]; }else if(playerhuaseint1[0]!=playerhuaseint1[1]&&playerhuaseint1[1]!=playerhuaseint1[2]){ paixing=1; maxcard=playerhuaseint1[2]; } } static void bijiao(int p1,int p2,int p3,int[] max1,int[] max2,int[] max3,int maxcard1,int maxcard2,int maxcard3){ int[] paixing=new int[3]; int a1,a2,a3; paixing[0]=p1; paixing[1]=p2; paixing[2]=p3; Arrays.sort(paixing); Arrays.sort(max1); a1=Integer.parseInt(String.valueOf(max1[2])+ String.valueOf(max1[1])+ String.valueOf(max1[0])); //把三张整型的数组合成一个字符串再变成一个整型的数,根据这个数的大小来判断三人都是单张时牌的大小 Arrays.sort(max2); a2=Integer.parseInt(String.valueOf(max2[2])+ String.valueOf(max2[1])+ String.valueOf(max2[0])); Arrays.sort(max3); a3=Integer.parseInt(String.valueOf(max3[2])+ String.valueOf(max3[1])+ String.valueOf(max3[0])); if(paixing[2]!=paixing[1]){ if(p1==paixing[2]){ System.out.println("玩家一赢!"); }else if(p2==paixing[2]){ System.out.println("玩家二赢!"); }else{ System.out.println("玩家三赢!"); } } if(paixing[2]==paixing[1]&&paixing[1]!=paixing[0]){ if(p1==paixing[0]){ if(maxcard2>maxcard3){ System.out.println("玩家二赢!"); }else if(maxcard3>maxcard2){ System.out.println("玩家三赢!"); }else if(max2[1]>max3[1]){ System.out.println("玩家二赢!"); }else if(max3[1]>max2[1]){ System.out.println("玩家三赢!"); }else if(max2[0]>max3[0]){ System.out.println("玩家二赢!"); }else if(max2[0]<max3[0]){ System.out.println("玩家三赢!"); }else{ System.out.println("两个人的牌相同"); } }else if(p2==paixing[0]){ if(maxcard1>maxcard3){ System.out.println("玩家一赢!"); }else if(maxcard3>maxcard1){ System.out.println("玩家三赢!"); }else if(max1[1]>max3[1]){ System.out.println("玩家一赢!"); }else if(max3[1]>max1[1]){ System.out.println("玩家三赢!"); }else if(max1[0]>max3[0]){ System.out.println("玩家一赢!"); }else if(max1[0]<max3[0]){ System.out.println("玩家三赢!"); }else{ System.out.println("两个人的牌相同"); } }else{ if(maxcard1>maxcard2){ System.out.println("玩家一赢!"); }else if(maxcard2>maxcard1){ System.out.println("玩家二赢!"); }else if(max2[1]>max1[1]){ System.out.println("玩家二赢!"); }else if(max1[1]>max2[1]){ System.out.println("玩家一赢!"); }else if(max2[0]>max1[0]){ System.out.println("玩家二赢!"); }else if(max2[0]<max1[0]){ System.out.println("玩家一赢!"); }else{ System.out.println("两个人的牌相同"); } } } if(paixing[0]==1&&paixing[1]==1&&paixing[2]==1){ if(a1>a2&&a1>a3){ System.out.println("玩家一赢!"); }else if(a2>a1&&a2>a3){ System.out.println("玩家二赢!"); }else if(a3>a2&&a3>a1){ System.out.println("玩家三赢!"); }else if(a1==a2||a1==a3||a2==a3){ System.out.println("这两个人的牌一样大,不算重来"); } } } }