http://blog.csdn.net/HanKin_/article/details/52145874
1 /* 2 * HanKin 3 * 炸金花 4 */ 5 package test20160801; 6 public class test20160801 { 7 public static void main(String[] args) { 8 // TODO 自动生成的方法存根 9 Person p1 = new Person(); 10 Person p2 = new Person(); 11 p1.name = "老沈"; 12 p2.name = "老黄"; 13 p1.card.random( p1 ); 14 p2.card.random( p2 ); 15 CardSet.rank( p1 ); 16 CardSet.rank( p2 ); 17 CardSet.judge( p1 ); 18 CardSet.judge( p2 ); 19 Success.success(p1, p2); 20 } 21 } 22 class Person{//玩家 23 Card card = new Card();//人有牌 24 int win = 0;//赢的人win为1 25 String name;//名字 26 } 27 class Card{//牌类 28 int[] card3 = new int[3];//用于存储玩家的3张牌 29 String[] card_3 = new String[3]; 30 int flag = 0;//用于存储不同类型牌的等级,用于判断 31 void random( Person p){//随机获得三张牌 32 for( int i = 0; i < card3.length; i++ ){ 33 card3[i] = (int)(Math.random()*13+2); 34 } 35 for( int i = 0; i <card3.length; i++ ){ 36 switch( card3[i] ){ 37 case 2:card_3[i] = "2";break; 38 case 3:card_3[i] = "3";break; 39 case 4:card_3[i] = "4";break; 40 case 5:card_3[i] = "5";break; 41 case 6:card_3[i] = "6";break; 42 case 7:card_3[i] = "7";break; 43 case 8:card_3[i] = "8";break; 44 case 9:card_3[i] = "9";break; 45 case 10:card_3[i] = "10";break; 46 case 11:card_3[i] = "J";break; 47 case 12:card_3[i] = "Q";break; 48 case 13:card_3[i] = "K";break; 49 case 14:card_3[i] = "A";break; 50 } 51 } 52 System.out.print( p.name + "的牌为:"); 53 for( int k = 0; k < card3.length; k++ ){ 54 System.out.print( " " + card_3[k] ); 55 } 56 System.out.println(""); 57 } 58 } 59 class CardSet{//用于比较前的牌的排序和属性设置 60 static void rank( Person p ){//对3张牌进行大小的排序 61 for( int i = 0; i < p.card.card3.length - 1; i++ ){ 62 for( int j = 0; j < p.card.card3.length - 1 -i; j++ ){ 63 if( p.card.card3[j] < p.card.card3[j+1] ){ 64 int temp = p.card.card3[j]; 65 p.card.card3[j] = p.card.card3[j+1]; 66 p.card.card3[j+1] = temp; 67 } 68 } 69 } 70 } 71 static void judge( Person p ){//设置牌的属性 72 if( p.card.card3[0]==p.card.card3[1] && p.card.card3[1]==p.card.card3[2] 73 && p.card.card3[0]==p.card.card3[2]){ 74 p.card.flag = 4; 75 }else if( p.card.card3[0]==(p.card.card3[1]+1) 76 && p.card.card3[0]==(p.card.card3[2]+2) ){ 77 p.card.flag = 3; 78 }else if( p.card.card3[0]==p.card.card3[1] || p.card.card3[0]==p.card.card3[2] 79 || p.card.card3[1]==p.card.card3[2] ){ 80 p.card.flag = 2; 81 if( p.card.card3[0]==p.card.card3[2] ){ 82 int temp = p.card.card3[1]; 83 p.card.card3[1] = p.card.card3[2]; 84 p.card.card3[2] = temp; 85 }else if( p.card.card3[1]==p.card.card3[2] ){ 86 int temp = p.card.card3[0]; 87 p.card.card3[0] = p.card.card3[2]; 88 p.card.card3[2] = temp; 89 } 90 }else{ 91 p.card.flag = 1; 92 } 93 } 94 } 95 class Success{//决定最终胜者 96 static void success( Person p1, Person p2){ 97 if( p1.card.flag > p2.card.flag ){ 98 p1.win = 1; 99 }else if( p1.card.flag == p2.card.flag ){ 100 if( p1.card.card3[0] > p2.card.card3[0] ){ 101 p1.win = 1; 102 }else if( p1.card.card3[0] == p2.card.card3[0] ){ 103 if( p1.card.card3[2] > p2.card.card3[2] ){ 104 p1.win = 1; 105 }else{ 106 p2.win = 1; 107 } 108 }else{ 109 p2.win = 1; 110 } 111 }else{ 112 p2.win = 1; 113 } 114 if( p1.win == 1 ){ 115 System.out.println( p1.name + "赢" ); 116 }else{ 117 System.out.println( p2.name + "赢" ); 118 } 119 } 120 }