亲宝软件园·资讯

展开

java实现多人多牌数比较游戏 java实现多人多牌数比较游戏

DekingChen 人气:0

具体实现步骤如下:

Card.java

/**
 * 
 */
package dk.card;
 
/**
 * @author Administrator
 * 扑克类
 */
public class Card implements Comparable<Card>{
 private int number;//牌的点数--1到13
 private int type;//牌的花色--4种花色:♠(黑桃):4 ♥(红桃):3 ♣(梅花):2 方块:1
 
 public Card() {
  super();
 }
 public Card(int number, int type){
  if(number==1)
   number=14;
  this.number = number;
  this.type = type;
 }
 public int getNumber() {
  return number;
 }
 public void setNumber(int number) {
  this.number = number;
 }
 public int getType() {
  return type;
 }
 public void setType(int type) {
  this.type = type;
 }
 
 @Override
 public int hashCode() {
  final int prime = 31;
  int result = 1;
  result = prime * result + number;
  result = prime * result + type;
  return result;
 }
 @Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  Card other = (Card) obj;
  if (number != other.number)
   return false;
  if (type != other.type)
   return false;
  return true;
 }
 @Override
 public int compareTo(Card o) {
  if(this.number>o.number)
   return -1;
  else if(this.number<o.number)
   return 1;
  else
   if(this.type>o.type)
    return -1;
   else if(this.type<o.type)
    return 1;
 
  return 0;
 }
 
 public String toString(){
  String str ="";
  switch(this.type){
  case 1: str = "方块";break;
  case 2: str = "梅花";break;
  case 3: str = "红桃";break;
  case 4: str = "黑桃";break;
  }
  String n="";
  switch(this.number){
  case 14:n="A";break;
  case 11:n="J";break;
  case 12:n="Q";break;
  case 13:n="K";break;
  default:n=this.number+"";
  }
  return str+n;
 }
 
}

CardList.java

package dk.card;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
/**
 * @author Administrator
 * 牌表操作类
 */
public class CardList {
 public static List<Card> cardlist = new ArrayList<Card>(52);//储存扑克牌
 public static List<ArrayList<Card>> poker;//储存发牌结果
 
 //生成一副牌
 public static void generateCard() {
  for(int i=1;i<=13;i++){
   for(int j=1;j<=4;j++){
    Card c = new Card(i,j);
    cardlist.add(c);
   }
  }
 
 }
 
 //打印一副牌
 public static void printCardList(){
  System.out.print("[");
  for(Card c:cardlist){
   System.out.print(c.toString()+" ");
  }
  System.out.println("]");
 }
 
 //洗牌
 public static void shuffleCard(){
  List<Card> scard = new ArrayList<Card>(52);
  Card c;
  Random random = new Random();
  for(int i=1;i<=52;i++){
 
   do{
     c = cardlist.get(random.nextInt(52));
   }while(scard.contains(c));
   scard.add(c);
  }
  cardlist.clear();
  cardlist = scard;
  printCardList();
 }
 
 /**
  * 发牌
  * @param i number of player
  * @param j number of card
  * @return
  */
 public static List<ArrayList<Card>> dealCard(int i,int j){ 
  try{
   if(i<=1){
    throw new Exception("发牌规则有误:玩家至少为2人,请重新定义规则!");
   }
   if(i*j>52){
    throw new Exception("发牌规则有误:牌数超出扑克张数,请重新定义规则!");
   }
 
   poker = new ArrayList<ArrayList<Card>>(i);//储存玩家发到的牌
 
   //生成玩家手中的牌列表
   for(int h=0;h<i;h++){
    ArrayList<Card> player_card_list = new ArrayList<Card>(j);
    poker.add(player_card_list);
 
   }
   //给每个玩家发牌
   for(int k=0;k<j;k++){
    for(int d=0;d<i;d++){
     ArrayList<Card> player_card_list = poker.get(d);
     Card card = getCard();     
     player_card_list.add(card);
    }
   }
 
  }catch(Exception e){
   e.printStackTrace();
  }
  return poker;
 }
 
 //抓取一张牌
 public static Card getCard(){
  return cardlist.remove(0);
 }
 
 //获取一位玩家的牌
 public static ArrayList<Card> getPoker(){
  return poker.remove(0);
 }
 
 public static void clear(){
  cardlist.clear();
  poker.clear();
  printCardList();
 
 }
 
}

Player.java

/**
 * 
 */
package dk.card;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * @author Administrator
 * 玩家类
 */
public class Player implements Comparable<Player>{
 private String id;//玩家id
 private String name;//玩家姓名
 private List<Card> poker;//玩家手牌
 private Card max;//玩家最大的牌
 public Player(String id ,String name){
  this.id = id;
  this.name = name;
  this.setPoker(new ArrayList<Card>());
 }
 public String getId() {
  return id;
 }
 public void setId(String id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public List<Card> getPoker() {
  return poker;
 }
 
 public Card getMax() {
  return max;
 }
 public void setMax(Card max) {
  this.max = max;
 }
 public void setPoker(List<Card> poker) {
  this.poker = poker;
  Collections.sort(this.poker);
  for(Card c :this.poker){
   this.setMax(c);
   break;
  }
 }
 public String toString(){
  return "玩家Id:"+this.id+"\t玩家姓名:"+this.name;
 }
 
 //玩家摊牌
 public String showCard(){
  String msg="";
  for(Card c:this.poker){
   msg+=c.toString()+" ";
  }
  return this.toString()+"\t\n"+"玩家牌信息:\t\n"+msg;
 }
 @Override
 public int compareTo(Player o) {
  return this.max.compareTo(o.getMax());
 }
 
}

PlayerList.java

package dk.card;
 
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
 
/**
 * @author Administrator
 * 储存玩家列表
 */
public class PlayerList {
 
 public static List<Player> playerlist = new ArrayList<Player>();//储存扑玩家列表
 public static int sum;
 public static List<String> rules = new ArrayList<String>();
 public PlayerList(){
 
 }
 
 public static void setRule(String rule){
  rules.add(rule);
 }
 
 public static String getRules(){
  String str="";
  for(String s:rules){
   str+=s+"\t\n";
  }
  return str;
 }
 
 public static void printRules(){
  show(getRules());
 }
 
 //添加玩家
 public static boolean addplayer(Player player){
  show("添加玩家:"+player.toString());
  show("----------------------------------------");
  sum++;
  return playerlist.add(player);
 }
 
 //给所有玩家发牌
 public static void shuffleCardToPlayer(){
   for(Player player:playerlist){
    player.setPoker(CardList.getPoker());
   }
 } 
 
 //显示发牌结果
 public static void showCard(){
  for(Player player:playerlist){
   show(player.showCard());
   show("----------------------------------------");
  }
 }
 
 //比较牌点大小并且显示赢家信息
 public static void showWinner(){
  Collections.sort(playerlist);
  Player p = playerlist.get(0);
  show(p.showCard());
  show("本局赢家是:"+p.getName());
 }
 
 //玩家人数
 public static int size(){
  return sum;
 }
 
 public static void show(Object obj){
  System.out.println(obj.toString());
 }
 
 public static void clear(){
  playerlist.clear();
  sum = 0 ;
 }
 
}

Index05.java

/**
 * 
 */
package dk.main;
 
import java.util.Scanner;
 
import dk.card.CardList;
import dk.card.Player;
import dk.card.PlayerList;
 
/**
 * @author Administrator
 *
 */
public class Index05 extends Show {
 
 public static Scanner scanner = new Scanner(System.in);
 
 /**
  * 调用的主方法
  * @param args
  */
 public static void main(String[] args) {
  show("是否开始游戏?0/1");
  int isStart = scanner.nextInt();
  if(isStart==1){
   startGame();
  } 
  else{
   show("退出游戏!");
  }
 
 }
 
 //启动扑克游戏
 public static void startGame(){
  show("游戏规则如下:");
  PlayerList.setRule("1.至少需要两名玩家");
  PlayerList.setRule("2.所有玩家的牌数加起来不超过52张牌");
  PlayerList.setRule("3.比较所有玩家手中最大的牌点,最大的牌点数的玩家获胜");
  PlayerList.setRule("4.总的牌数只有52张,没有大小王");
  PlayerList.setRule("5.黑桃牌A最大,方块3最小,点数相同时,比较花色,黑桃较大。");
  PlayerList.printRules();
  show("请输入玩家人数:");
  int players = 2;//默认玩家数目
  int cardofplay = 2;//默认每个玩家的手牌数目
  try {
    players = scanner.nextInt();
    while(players<2){
     show("玩家人数至少是两人");
     show("请重新输入人数:");
     players = scanner.nextInt();
    }
 
    show("请输入每个玩家持有的牌数:"); 
    cardofplay = scanner.nextInt();
    while(cardofplay*players>52){
     show("玩家*牌数超出范围!请重新输入牌数");
     cardofplay = scanner.nextInt();
    }
 
    show("开始输入玩家信息:");
    for(int i=0;i<players;i++){
     show("请输入第"+(i+1)+"个玩家姓名:");
     String name = scanner.next();
     Player p =new Player("00"+i,name);
     PlayerList.addplayer(p);
    }
    show("一副没有洗的牌:");
    CardList.generateCard();//生成一副扑克
    CardList.printCardList();//打印扑克
    show("========================================");
    show("开始洗牌:");
    CardList.shuffleCard();//洗牌
    show("========================================");
    show("开始发牌:........发牌中......");
    CardList.dealCard(players,cardofplay);//发牌
    PlayerList.shuffleCardToPlayer();//给玩家发牌
    show("发牌结果:");
    PlayerList.showCard();//显示所有玩家牌信息
    show("========================================");
    show("比较结果:比较牌点大小并且显示赢家信息");
    PlayerList.showWinner();//显示本轮赢家
    show("========================================");
  } catch (Exception e) {
 
   e.printStackTrace();
  }
 }
 
 //清空游戏
 public static void clearGame(){
   CardList.clear();
   PlayerList.clear();
 }
 
 //测试扑克游戏
 public static void testGame(){
  try {
   show("一副没有洗的牌:");
   CardList.generateCard();
   CardList.printCardList();
   show("========================================");
   show("开始添加玩家:");
   Player p =new Player("001","DekingChen");
   Player p1 =new Player("002","chenzhenquan");
   Player p2 =new Player("003","czq");
   PlayerList.addplayer(p);
   PlayerList.addplayer(p1);
   PlayerList.addplayer(p2);
   show("开始洗牌:");
   CardList.shuffleCard();
   show("========================================");
   show("开始发牌:........发牌中......");
   int players = PlayerList.size();//玩家数目
   int cardofsum = 3;//每个玩家的手牌数目
   CardList.dealCard(players,cardofsum);
   PlayerList.shuffleCardToPlayer();
   show("发牌结果:");
   PlayerList.showCard();
   show("========================================");
   show("比较结果:比较牌点大小并且显示赢家信息");
   PlayerList.showWinner();
   show("========================================");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 
}

Show.java(辅助显示类)

package dk.main;
 
public abstract class Show {
 
 public static void show(Object obj){
  System.out.println(obj.toString());
 }
}

实验结果:

是否开始游戏?0/1
1
游戏规则如下:
1.至少需要两名玩家 
2.所有玩家的牌数加起来不超过52张牌 
3.比较所有玩家手中最大的牌点,最大的牌点数的玩家获胜 
4.总的牌数只有52张,没有大小王 
5.黑桃牌A最大,方块3最小,点数相同时,比较花色,黑桃较大。 
 
请输入玩家人数:
5
请输入每个玩家持有的牌数:
5(<a href="http://www.dztcsd.com/">资质代办</a>)
开始输入玩家信息:
请输入第1个玩家姓名:
DekingChen
添加玩家:玩家Id:000 玩家姓名:DekingChen
----------------------------------------
请输入第2个玩家姓名:
czq
添加玩家:玩家Id:001 玩家姓名:czq
----------------------------------------
请输入第3个玩家姓名:
chenzhenquan
添加玩家:玩家Id:002 玩家姓名:chenzhenquan
----------------------------------------
请输入第4个玩家姓名:

加载全部内容

相关教程
猜你喜欢
用户评论