package zhajinhua;
import Java.util.Comparator;
public class Card implements Comparator<Card> {
 /** 花色 */
 private int suit;// 实例变量, 每个扑克牌实例都有自己的花色
 /** 点数 0代表3, 1代表4, 2代表5 */
 private int rank;// 实例变量, 每个扑克牌实例都有自己的点数
 public static final int DIAMOND = 0; // 方块(钻石)
 public final static int CLUB = 1; // 梅花
 public static final int HEART = 2; // 红桃(红心)
 public static final int SPADE = 3; // 黑桃(花锄)
 public final static int DEUCE = 0; // 2
 public final static int THREE = 1;
 public final static int FOUR = 2;
 public final static int FIVE = 3;
 public final static int SIX = 4;
 public final static int SEVEN = 5;
 public final static int EIGHT = 6;
 public final static int NINE = 7;
 public final static int TEN = 8;
 public final static int JACK = 9;// J
 public final static int QUEEN = 10;// Q
 public final static int KING = 11;// K
 public final static int ACE = 12;// A
 public Card() {
 }
 public Card(int suit, int rank) {
  setRank(rank);
  setSuit(suit);
 }
 public int getRank() {
  return rank;
 }
 /** 更新点数 */
 public void setRank(int rank) {
  if (rank < DEUCE || rank > ACE) {
   throw new RuntimeException("超了!");
  }
  this.rank = rank;
 }
 public int getSuit() {
  return suit;
 }
 public void setSuit(int suit) {
  if (suit < DIAMOND || suit > SPADE) {
   throw new RuntimeException("超了!");
  }
  this.suit = suit;
 }
 private final static String[] SUIT_NAMES = { "方块", "梅花", "红桃", "黑桃" };
 private final static String[] RANK_NAMES = { "2", "3", "4", "5", "6", "7",
   "8", "9", "10", "J", "Q", "K", "A" };
 public String toString() {
  return SUIT_NAMES[suit] + RANK_NAMES[rank];
 }
 public boolean equals(Object obj) {
  if (obj == null) // 与null比较返回false
   return false;
  if (this == obj) // obj对象就是当前(this)对象, 返回true
   return true;
  if (obj instanceof Card) {// 检查obj引用的对象是否是Card类型
   Card other = (Card) obj;// 引用类型转换,方便访问属性
   return this.rank == other.rank && this.suit == other.suit;
  }
  return false;// 类型不一致返回false
 }
 public int hashCode() {
  return (suit << 16) | rank;
 }
 //比较器  按大到小顺序
 public int compare(Card o1, Card o2) {
  if (o1.rank != o2.rank) {
   return o2.rank - o1.rank;
  } else {
   return o2.suit - o1.suit;
  }
 }
}
 
package zhajinhua;
import java.util.ArrayList;
import java.util.Random;
/**
 * 符合扎金花要求的一副牌以及扎金花的一些规则
 * @author Administrator
 *
 */
public class CardsOfZJH {
 public ArrayList<Card> cardsOfZJH = new ArrayList<Card>();
 public CardsOfZJH(){
  cardsOfZJH = createCards();
 }
 //生成一副符合扎金花要求的打乱的扑克牌
 public ArrayList<Card> createCards(){
  ArrayList<Card> cards = new ArrayList<Card>();
  for (int i = 0; i < 4; i++) {
   for (int j = 0; j < 13; j++) {
    Card c = new Card(i, j);
    cards.add(c);
   }
  }
     Random r = new Random();
     for(int i=cards.size() - 1; i>=1;i --){
       int j = r.nextInt(i);
       Card t=cards.get(i);
       cards.set(i,cards.get(j));
       cards.set(j,t);
     }
  return cards;
 }
 public String toString() {
  return cardsOfZJH.toString();
 }
}
 
 
package zhajinhua;
/**
 *该类定义了所有关于扎金花的比牌规则 
 */
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
public class CardsOfPlayer implements Comparator<CardsOfPlayer>{
 //所有玩家共用一副牌,但是每次重新开始应该生成不同顺序的牌,所以此处不能初始化,而应该设置一个set方法。
 private static CardsOfZJH cardsOfZJH;
 private Player player;
 private Card firstCard;
 private Card secondCard;
 private Card thirdCard;
 //每次重新开始可以调用该方法重新生成一副新牌
 public static void setCardsOfZJH(CardsOfZJH cardsOfZJH) {
  CardsOfPlayer.cardsOfZJH = cardsOfZJH;
 }
 public CardsOfPlayer(){
  
 }
 //从一幅牌的剩余牌中随即抽取三张
 public CardsOfPlayer(Player p){
  this.player = p;
  if(null == cardsOfZJH){
   throw new RuntimeException("提示:该局游戏的牌还没创建,不能进行游戏!");
  }
  Card[] cards = getCards();
  Arrays.sort(cards,new Card());//让三张牌按大到小的顺序排序
  firstCard = cards[0];
  secondCard = cards[1];
  thirdCard = cards[2];
 }
 public Player getPlayer() {
  return player;
 }
 //获得抽取的那三张牌
 private Card[] getCards(){
  Card[] cards = new Card[3];
  Random ran = new Random();
  int j = 0;
  for(int i = 0;i < 3;i ++){
   j = ran.nextInt(cardsOfZJH.cardsOfZJH.size());
   cards[i] = cardsOfZJH.cardsOfZJH.remove(j);//获取随即抽取的那张牌并删除它。
  }
  return cards;
 }
 //这个需要完善。
 public int hashCode() {
  return firstCard.hashCode() + secondCard.hashCode() + thirdCard.hashCode();
 }
 public boolean equals(Object obj) {
  if(null == obj){
   return false;
  }
  if(this == obj){
   return true;
  }
  if(obj instanceof CardsOfPlayer){
   CardsOfPlayer cardsOfPlayer = (CardsOfPlayer)obj;
   return this.firstCard.equals(cardsOfPlayer.firstCard) && this.secondCard.equals(cardsOfPlayer.secondCard) && this.thirdCard.equals(cardsOfPlayer.thirdCard);
  }
  return false;
 }
 public String toString() {
  return player.getName() + " : \t" + firstCard + "," + secondCard + "," + thirdCard;
 }
 //判断牌型
  public int Type(){
   //豹子 
   if(firstCard.getRank() == secondCard.getRank() && firstCard.getRank() == thirdCard.getRank()){
    return 1;
   }
   //同花顺 金花
   if(firstCard.getSuit() == secondCard.getSuit() && firstCard.getSuit() == thirdCard.getSuit()){
    if(firstCard.getRank() - secondCard.getRank() == 1 && secondCard.getRank() - thirdCard.getRank() == 1){
     return 2;
    }else{
     return 4;
    }
   }
   //顺子
   if(firstCard.getRank() - secondCard.getRank() == 1 && secondCard.getRank() - thirdCard.getRank() == 1){
    return 3;
   }
   //对子
   if(firstCard.getRank() == secondCard.getRank() || secondCard.getRank() == thirdCard.getRank()){
    return 5;
   }
   //特殊235
   if(firstCard.getRank() == 0 && secondCard.getRank() == 3 && thirdCard.getRank() == 5){
    return 7;
   }else{//散牌
    return 6;
   }
  }
  //比较两手牌的大小  升序
 public int compare(CardsOfPlayer o1, CardsOfPlayer o2) {
  //特殊情况7
  if(o1.Type() == 7){
   if(o2.Type() == 1){
    return 1;
   }else{
    return -1;
   }
  }
  if(o2.Type() == 7){
   if(o1.Type() == 1){
    return -1;
   }else{
    return 1;
   }
  }
  //两手牌牌型相同时  先比较各张牌的大小  再比较花色
  if(o1.Type() == o2.Type()){
   if(o1.firstCard.getRank() == o2.firstCard.getRank()){
    if(o1.secondCard.getRank() == o2.secondCard.getRank()){
     if(o1.thirdCard.getRank() == o2.thirdCard.getRank()){
      if(o1.firstCard.getSuit() > o2.firstCard.getSuit()){//牌的点数都相同时比较最大那张牌的花色
       return 1;
      }else{
       return -1;
      }
     }else{
      return o1.thirdCard.getRank() - o2.thirdCard.getRank();
     }
    }else{
     return o1.secondCard.getRank() - o2.secondCard.getRank();
    }
   }else{
    return o1.firstCard.getRank() - o2.firstCard.getRank();
   }
  }
  //牌型不同时只需比较牌型大小即可(前面已经出去了特殊情况)
  return o2.Type() - o1.Type();
 }
}
 
 
package zhajinhua;
import java.util.Random;
import java.util.Scanner;
import java.util.TreeSet;
public class StartGame {
 public static void main(String[] args) {
  Random ran = new Random();
  int flag;//用于接收产生的庄家序号
  Scanner con = new Scanner(System.in);
  Player[] players;
  while(true){
   //每次重新开始都要生成一幅牌
   CardsOfPlayer.setCardsOfZJH(new CardsOfZJH());
   String name;//用于接收输入的玩家名
   System.out.println("输入玩家人数(2 - 6):");
   int number = Integer.valueOf(con.nextLine());
   if(number < 2 || number > 6){
    System.out.println("提示:输入的玩家人数不正确!");
    continue;
   }
   //生成玩家数组
   players = new Player[number];
   for(int i = 0;i < number;i ++){
    System.out.println("输入第" + (i + 1) + "位玩家姓名:");
    name = con.nextLine();
    players[i] = new Player(name);
    //打印下每个玩家的牌
    System.out.println("只有选择看牌了才能看到自己的牌哦!");
   }
   Player.setPlayer_Number(players.length);//初始化有效玩家个数。
   //随机产生庄家
   flag = ran.nextInt(players.length);
   players[flag].setZhuangjia();
   //将所有玩家按操作顺序排好序。  双向链表或者数组。庄家 0 1 2 3 、、、定轮流规则(将庄家调为第一个)
   if(flag != 0){
    Player p = players[0];
    players[0] = players[flag];
    players[flag] = p;
   }
   //庄家蒙牌
   System.out.println("庄家是:" + players[0].getName());
   System.out.println("-------------------庄家蒙牌-------------------");
   players[0].mengpai();
   boolean b = true;
   int c = 1;//为了用来区分是不是第一轮操作
   while(b){
    //出庄家外各位玩家轮流选择操作 --跟牌、加注、弃牌、看牌、比牌、开牌。switch()
    for(int i = c;i < players.length;i ++){
     //如果有效玩家只剩下一个时就停止循环直接判断赢家
     if(Player.getPlayer_Number() == 1){
      b = false;break;
     }
     if(players[i].isQipai()){//判断是否弃牌,弃牌了则轮下一个玩家。
      continue;
     }
     System.out.println(players[i].getName() + "的操作:");
     //系统是否强制要求某玩家开牌判断
     if(kaipai(players[i],players)){
      b = false;break;
     }
     if(players[i].isKanpai()){
      caozuojiemian(players,i,0,1,1,1,1);//看了牌者的选择界面
     }else{
      caozuojiemian(players,i,1,1,1,1,1);
     }
    }
    c = 0;//下一轮循环从庄家开始。
   }
  }
 }
 //计时读秒    //超时则调用弃权
 public static void dumiao(){
  
 }
 //开牌  不管加注还是跟注之前都要判断是否必须开牌。
 public static boolean kaipai(Player p,Player[] players){
  //先判断是否看了牌
  if((p.isKanpai() && (p.getCion() < 6 * Player.getGenzhuFlag())) || (!p.isKanpai() && (p.getCion() < 3 * Player.getGenzhuFlag()))){
    //满足以上条件则需要开牌,显示坚持下来的所有玩家牌信息 并判定赢家
   TreeSet<CardsOfPlayer> set = new TreeSet<CardsOfPlayer>(new CardsOfPlayer());
   for(int i = 0;i < players.length;i ++){
    set.add(players[i].getCards());
   }
   System.out.println("以下输出的顺序是从小到大");
   System.out.println(set);
   set.last().getPlayer().setCion(set.last().getPlayer().getCion() + Player.getGuodi());
   return true;
  }else{
   return false;
  }
 }
 // 比牌   
 public static void bipai(Player[] players,int i){
  //也得判断是否看了牌然后更改锅底和自己的金额
  if(players[i].isKanpai()){
   players[i].setCion(players[i].getCion() - 2 * Player.getGenzhuFlag());Player.setGuodi(Player.getGuodi() + 2 * Player.getGenzhuFlag());
  }else{
   players[i].setCion(players[i].getCion() - Player.getGenzhuFlag());Player.setGuodi(Player.getGuodi() + Player.getGenzhuFlag());
  }
  int [] biaohaoshuzu = new int[players.length - 1];
  int biaohao =-1;//用于标记你要选择比牌的对象
  int k = 0;
  System.out.println("你可以选择的比牌对象有:");
  for(int j = 0;j < players.length;j ++){
   if(players[j] == players[i] || players[j].isQipai()){//弃牌了的玩家和自己不是比牌对象
    continue;
   }else{
    System.out.println("标号:" + k + "," + players[j].getName());
    biaohaoshuzu[k ++] = j;
   }
  }
  boolean bool = true;
  //确定比牌对象的索引biaohao
  while(bool){
   System.out.println("输入你的选择标号");
   Scanner con = new Scanner(System.in);
   biaohao = con.nextInt();
   for(int j = 0;j < biaohaoshuzu.length;j ++){
    if(biaohao == biaohaoshuzu[j])
    {
     bool = false;
     break;
    }
   }
   if(bool == true){
    System.out.print("输入有误请重新");
   }
  }
  //比较players[i]和players[biaohao]的牌的大小 
  System.out.println("测试");
  System.out.println(i);
  System.out.println(biaohao);
  System.out.println(players.length);
  System.out.println("测试");
  System.out.println(biaohaoshuzu[biaohao]);
  System.out.println(biaohao);
  System.out.println("测试");
  if(players[i].getCards().compare(players[i].getCards(),players[biaohaoshuzu[biaohao]].getCards()) < 0){
   players[i].qipai();
   if(Player.getPlayer_Number() == 1){
    players[i].setCion(players[i].getCion() + Player.getGuodi());
    System.out.println("第一轮游戏结束,打印所有玩家牌信息揭晓大家心中的疑问");
    for(int m = 0;m < players.length;m ++){
     System.out.println(players[m]);
    }
   }
  }else{
   players[biaohaoshuzu[biaohao]].qipai();
   if(Player.getPlayer_Number() == 1){
    players[biaohaoshuzu[biaohao]].setCion(players[biaohaoshuzu[biaohao]].getCion() + Player.getGuodi());
    System.out.println("第一轮游戏结束,打印所有玩家牌信息揭晓大家心中的疑问");
    for(int m = 0;m < players.length;m ++){
     System.out.println(players[m]);
    }
   }
  }
 }
 /**
  * 当你选择看牌操作时你有第二次操作选择机会,这时你将不再用于看牌操作权限,当你输入的选择不在范围内时系统将给你重新选择的机会
  * */
 public static void caozuojiemian(Player[] players,int i,int a,int b,int c,int d,int e){//abcde用来标记各个选项有没有必要出现
  System.out.println("---------------------------------操作选择-------------------------------");
  if(a  == 1){
   System.out.println("-----------------------------1.======看牌-------------------------------");
  }
  if(b == 1){
   System.out.println("-----------------------------2.======跟注-------------------------------"); 
  }
  if(c == 1){
   System.out.println("-----------------------------3.======加注-------------------------------"); 
  }
  if(d == 1){
   System.out.println("-----------------------------4.======比牌-------------------------------");
  }
  if(e == 1){
   System.out.println("-----------------------------5.======弃牌-------------------------------");
  }
  System.out.println("---------------------------------操作选择-------------------------------");
  Scanner con = new Scanner(System.in);
  int number;
  System.out.println("输入你的选择:");
  if(players[i].isKanpai()){
   while(true){
    System.out.println("你看过牌只能选择2、3、4、5:");
    number = con.nextInt();
    if(number == 2 || number == 3 || number == 3 || number == 4 || number == 5){
     break;
    }else{
     System.out.print("你的选择有误!");
    }
   }
  }else{
   number = con.nextInt();
  }
  //如果选择的是看牌的话可以重新选择其他的。
  switch(number){
  case 1:players[i].kanpai();caozuojiemian(players,i,0,1,1,1,1);break;
  case 2:players[i].genzhu();break;
  case 3:players[i].jiazhu();break;
  case 4:bipai(players,i);break;
  case 5:players[i].qipai();break;
  default:System.out.println("你的选择不在范围内,请重新选择");caozuojiemian(players,i,a,b,c,d,e);
  }
 }
}
 
 
package zhajinhua;
/**
 * 没看牌的话最大额为20看了牌才能到达40
 * 加注  只有当跟注 <= 10 才可以加注,看了牌之后加注 翻倍//////////////////////////////////////////////////需要一个禁用
 *  //跟注  看牌之后跟注翻倍
 */
import java.util.Scanner;
public class Player {
 private String name;
 @SuppressWarnings("unused")
 private boolean zhuangjia = false;// 标记是否是庄家
 private static int genzhuFlag;//跟注金额
 private static int guodi = 0;
 private boolean kanpai = false;//用于标记是否看了牌
 private boolean qipai = false;//用于标记玩家是否弃牌
 private CardsOfPlayer cards;
 private static int player_Number;//有效玩家数
 // 每个玩家所拥有的金币
 private int coin = 2000;
 public Player(String name) {
  this.name = name;
  this.cards = new CardsOfPlayer(this);
 }
 // 设置庄家
 public void setZhuangjia() {
  this.zhuangjia = true;
 }
 // 蒙牌
 public void mengpai() {
  Scanner con = new Scanner(System.in);
  int number;
  boolean bool = true;
  while (bool) {
   System.out.println("---------------蒙牌的选择项-------------------");
   System.out.println("---------1.----1金币------------------------");
   System.out.println("---------2.----5金币------------------------");
   System.out.println("---------3.----10金币------------------------");
   System.out.println("---------4.----20金币------------------------");
   System.out.println("---------------蒙牌的选择项-------------------");
   System.out.println("选择你要蒙牌项为:");
   number = Integer.valueOf(con.nextLine());
   switch (number) {
   case 1:coin -= 1;bool = false;genzhuFlag = 1;guodi = 1;break;
   case 2:coin -= 5;bool = false;genzhuFlag = 5;guodi = 5;break;
   case 3:coin -= 10;bool = false;genzhuFlag = 10;guodi = 10;break;
   case 4:coin -= 20;bool = false;genzhuFlag = 20;guodi = 20;break;
   default:System.out.println("输入选项无效,重新选择:");
   }
  }
 }
 //跟注  看牌之后跟注翻倍
 public void genzhu(){
  if(kanpai){
   coin = coin - 2 * genzhuFlag;
   guodi = guodi + 2 * genzhuFlag;
  }else{
   coin -=genzhuFlag;
   guodi +=genzhuFlag;
  }
 }
 //加注  只有当跟注 <= 10 才可以加注,看了牌之后加注 翻倍
 public void jiazhu(){
  Scanner con = new Scanner(System.in);
  int number;//加注金额
  boolean bool = true;//输入不符合规则重新输入标签
  System.out.println("目前跟注价额为:" + genzhuFlag);
  while(bool){
   if(genzhuFlag == 1){
    System.out.println("你可以选择的加注金额有:5,10,20");
    System.out.print("输入你的跟注金额:");
    number = Integer.valueOf(con.nextLine());
    if(number == 5 || number == 10 || number == 20){
     bool = false;
     if(kanpai){//判断是否看了牌  看了牌要加倍
      coin = coin -2 * number;genzhuFlag = number;guodi = guodi + 2 * number;
     }else{
      coin -= number;genzhuFlag = number;guodi = guodi + number;
     }
    }else{
     System.out.println("输入不符合规则,请重新输入:");
    }
   }else if(genzhuFlag == 5){
    System.out.println("你可以选择的加注金额有:10,20");
    System.out.print("输入你的跟注金额:");
    number = Integer.valueOf(con.nextLine());
    if(number == 10 || number == 20){
     bool = false;
     if(kanpai){//判断是否看了牌  看了牌要加倍
      coin = coin -2 * number;genzhuFlag = number;guodi = guodi + 2 * number;
     }else{
      coin -= number;genzhuFlag = number;guodi = guodi + number;
     }
    }else{
     System.out.println("输入不符合规则,请重新输入:");
    }
   }else if(genzhuFlag == 10){
    bool = false;
    System.out.println("你可以选择的加注金额只有:20");
    if(kanpai){//判断是否看了牌  看了牌要加倍
     coin = coin -2 * 20;genzhuFlag = 20;guodi = guodi + 2 * 20;
    }else{
     coin -= 20;bool = false;genzhuFlag = 20;guodi = guodi + 20;
    }
   }
  }
 }
 //看牌
 public void kanpai(){
  this.kanpai = true;
  //显示牌信息
  showpai();
 }
 //弃牌
 public void qipai(){
  Player.setPlayer_Number(--Player.player_Number);
  qipai = true;
 }
 //显示牌 
 public void showpai(){
  System.out.println(cards);
 }
 public static int getGuodi() {
  return guodi;
 }
 public static void setGuodi(int guodi) {
  Player.guodi = guodi;
 }
 public static int getGenzhuFlag() {
  return genzhuFlag;
 }
 public boolean isKanpai() {
  return kanpai;
 }
 public boolean isQipai() {
  return qipai;
 }
 public static int getPlayer_Number() {
  return player_Number;
 }
 public static void setPlayer_Number(int player_Number) {
  Player.player_Number = player_Number;
 }
 public String getName() {
  return name;
 }
 public CardsOfPlayer getCards() {
  return cards;
 }
 public int getCion() {
  return coin;
 }
 public void setCion(int cion) {
  this.coin = cion;
 }
 public String toString() {
  return cards.toString() + "  \n金币:" + coin;// /////////////////////////
 }
}

作者:星辰 时间:2016-10-02 浏览 685评论 0 赞 0砸 0 标签: 代码
评论
还可以再输入500个字

请您注意

·自觉遵守:爱国、守法、自律、真实、文明的原则
·尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法规
·严禁发表危害国家安全,破坏民族团结、国家宗教政策和社会稳定,含侮辱、诽谤、教唆、淫秽等内容的作品
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
·您在NoteShare上发表的作品,NoteShare有权在网站内保留、转载、引用或者删除
·参与本评论即表明您已经阅读并接受上述条款