继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

简易扑克牌游戏(java)

miao2015
关注TA
已关注
手记 2
粉丝 1
获赞 6
//创建扑克牌类
public class PokerCard {
    public String shapeColor;
    public String number;

}

//创建玩家类
import java.util.HashSet;
import java.util.Set;

public class Player {
    public int id;
    public String name;
    public Set<PokerCard> handCards;
    public Player(){
        handCards = new HashSet<PokerCard>();
    }
}

//创建游戏
import java.util.*;

public class TestPoker {

    private List<PokerCard> pokerCardsList;
    public Map<String,Integer> nums;
    public Map<String,Integer> huase;

    public TestPoker(){
        pokerCardsList = new ArrayList<PokerCard>();
        nums = new HashMap<String, Integer>();
        huase = new HashMap<String, Integer>();
    }
//获取玩家的ID并且处理可能的异常
    public void getID(Player player){
     while (true) {
         Scanner console = new Scanner(System.in);
         try {
             System.out.println("输入ID");
             int n = console.nextInt();
             player.id = n;
             break;
         } catch (Exception e) {
             System.out.println("请输入整数型的ID");
             continue;
         }
     }
    }

    //创建方法比较两张扑克牌的大小
    public PokerCard compare(PokerCard p1,PokerCard p2){
        if(nums.get(p1.number)>nums.get(p2.number) ){
            return p1;
        }if(nums.get(p1.number)<nums.get(p2.number) ){
            return p2;
        }else {
            if(huase.get(p1.shapeColor)>huase.get(p2.shapeColor) ){
                return p1;
            }
            else{
                return p2;
            }
        }

    }

//主函数
    public static void main(String[] args){

        Scanner console = new Scanner(System.in);

        System.out.println("------------------创建扑克牌...------------------");
        TestPoker tp = new TestPoker();
        String[] huase = {"方片","梅花","红桃","黑桃"};
        String[] num = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

        //初始化点数、花色与其大小的映射
        for(String str:huase){
            tp.huase.put(str, Arrays.asList(huase).indexOf(str));
        }
        for(String str:num){
            tp.nums.put(str,Arrays.asList(num).indexOf(str));
        }

  //创建扑克牌
        for(String str:huase){
            for(String string:num){
                PokerCard pc = new PokerCard();
                pc.shapeColor = str;
                pc.number = string;
                tp.pokerCardsList.add(pc);
            }
        }

        System.out.println("------------------扑克牌创建成功!-----------------");
        System.out.print("为:");
        for(PokerCard pokerCard:tp.pokerCardsList){
            int k =tp.pokerCardsList.indexOf(pokerCard);
            //System.out.print("为:");
            switch (k){
                case 0:
                    System.out.print("["+pokerCard.shapeColor+pokerCard.number+",");
                    break;
                case 51:
                    System.out.println(pokerCard.shapeColor+pokerCard.number+"]");
                    break;
                default:
                    System.out.print(pokerCard.shapeColor+pokerCard.number+",");
                    break;
            }
            //System.out.print(str+",");
        }

        System.out.println("-------------------开始洗牌...--------------------");

        Collections.shuffle(tp.pokerCardsList);

        System.out.println("--------------------洗牌结束---------------------");

        System.out.println("--------------------创建玩家...--------------------");
        Player[] players = {new Player(),new Player()};
        for(int i=0;i<2;i++){
            System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名");
            tp.getID(players[i]);
            System.out.println("输入姓名");
            String name = console.next();
            players[i].name = name;
        }
        System.out.println("----欢迎玩家:"+players[0].name);
        System.out.println("----欢迎玩家:"+players[1].name);

        System.out.println("-------------------开始发牌...----------------------");
        int k =0;
        for(int i=0;i<2;i++){
            for(Player player:players){
                System.out.println("----玩家:"+player.name+"-拿牌");
                player.handCards.add(tp.pokerCardsList.get(k));
                k++;
            }
        }

        System.out.println("---------------------开始游戏...------------------------");

        //创建玩家和最大手牌的映射关系
        Map<PokerCard,Player> playerMaxPokerCardMaps = new HashMap<PokerCard,Player>();

        for(int i =0;i<2;i++) {
            PokerCard maxHandCard = tp.compare((PokerCard) players[i].handCards.toArray()[0], (PokerCard) players[i].handCards.toArray()[1]);
            System.out.println("玩家:" + players[i].name + "最大的手牌为:" + maxHandCard.shapeColor + maxHandCard.number);
            playerMaxPokerCardMaps.put(maxHandCard,players[i]);
        }

        System.out.println("-----------玩家:"+playerMaxPokerCardMaps.get(
                tp.compare((PokerCard) playerMaxPokerCardMaps.keySet().toArray()[0]
                        ,(PokerCard) playerMaxPokerCardMaps.keySet().toArray()[1])).name
                +"获胜!-------------------");

        System.out.println("玩家各自的手牌为:");
        int j =0;
        for(Player player:players){
            System.out.println(player.name+":"+"["+((PokerCard)player.handCards.toArray()[0]).shapeColor
                    +((PokerCard)player.handCards.toArray()[0]).number
                    +","+((PokerCard) player.handCards.toArray()[1]).shapeColor
                    +((PokerCard)player.handCards.toArray()[1]).number+"]");
            j++;
        }

    }
}

结果
------------------创建扑克牌...------------------
------------------扑克牌创建成功!-----------------
为:[方片2,方片3,方片4,方片5,方片6,方片7,方片8,方片9,方片10,方片J,方片Q,方片K,方片A,梅花2,梅花3,梅花4,梅花5,梅花6,梅花7,梅花8,梅花9,梅花10,梅花J,梅花Q,梅花K,梅花A,红桃2,红桃3,红桃4,红桃5,红桃6,红桃7,红桃8,红桃9,红桃10,红桃J,红桃Q,红桃K,红桃A,黑桃2,黑桃3,黑桃4,黑桃5,黑桃6,黑桃7,黑桃8,黑桃9,黑桃10,黑桃J,黑桃Q,黑桃K,黑桃A]
-------------------开始洗牌...--------------------
[黑桃9,方片6,黑桃J,黑桃K,红桃J,方片8,红桃6,红桃A,黑桃3,梅花9,红桃3,梅花4,梅花2,方片10,方片3,方片2,红桃10,方片5,黑桃5,黑桃8,梅花A,红桃2,方片Q,红桃9,梅花6,红桃K,梅花3,黑桃Q,黑桃7,方片4,方片J,梅花Q,方片7,梅花7,方片9,梅花10,黑桃6,黑桃A,方片K,红桃8,红桃Q,梅花8,红桃7,黑桃10,红桃5,梅花K,黑桃4,方片A,梅花5,黑桃2,红桃4,梅花J]
--------------------洗牌结束---------------------
--------------------创建玩家...--------------------
请输入第1位玩家的ID和姓名
输入ID
1
输入姓名
Tom
请输入第2位玩家的ID和姓名
输入ID
2
输入姓名
Jack
----欢迎玩家:Tom
----欢迎玩家:Jack
-------------------开始发牌...----------------------
----玩家:Tom-拿牌
----玩家:Jack-拿牌
----玩家:Tom-拿牌
----玩家:Jack-拿牌
---------------------开始游戏...------------------------
玩家:Tom最大的手牌为:黑桃J
玩家:Jack最大的手牌为:黑桃K
-----------玩家:Jack获胜!-------------------
玩家各自的手牌为:
Tom:[黑桃J,黑桃9]
Jack:[黑桃K,方片6]

打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP