手记

Java--简易扑克牌游戏

package com.test.demo;

import java.util.*;

/*
 * 玩家类
 */

public class Player {
    private int ID;
    private String name;
    private List<PokerCard> handCards;     //有序 手牌List

    public Player(int ID,String name)
    {
        this.ID = ID;
        this.name = name;
        this.handCards = new ArrayList<PokerCard>();
    }

    public List<PokerCard> getHandCards()
    {
        return handCards;
    }

    public void setHandCards(List<PokerCard> handCards)
    {
        this.handCards = handCards;
    }

    public int getID()
    {
        return ID;
    }
    public String getName()
    {
        return name;
    }
}

package com.test.demo;

import java.util.*;

public class PokerCard implements Comparable<PokerCard> {
    /*
     * 扑克牌类
     */
    private String Color;
    private String Points;

    public PokerCard(String Color,String Points)
    {
        this.Color = Color;
        this.Points = Points;
    }
    public String getCardColor()
    {
        return Color;
    }
    public String getCardPoints()
    {
        return Points;
    }
    public String getCardInfo()
    {
        return Color + Points;
    }

    @Override
    public int compareTo(PokerCard o) {
        final String[] cardPointsArr = {"A","2","3","4","5","6","7","8","9","10"
                ,"J","Q","K"};
        final String[] cardColorArr = {"","","",""};
        List<String> cardPointsList = Arrays.asList(cardPointsArr);
        List<String> cardColorList = Arrays.asList(cardColorArr);
        int thisPointIndex = cardPointsList.indexOf(Points);
        int comparaPointIndex = cardPointsList.indexOf(o.Points);
        if(thisPointIndex == comparaPointIndex)
        {
            int thisColorIndex = cardColorList.indexOf(Color);
            int comparaColorIndex = cardColorList.indexOf(o.Color);
            return thisColorIndex - comparaColorIndex;
        }
        else
        {
            return thisPointIndex - comparaPointIndex;
        }
    }

}

package com.test.demo;

import java.util.*;

public class PlayCardDemo {

    private final String[] cardPoints = {"A","2","3","4","5","6","7","8","9","10"
            ,"J","Q","K"};
    private final String[] cardColor = {"","","",""};
    private List<PokerCard> pcs;//定义一副牌
    private List<Player> players;//定义一群玩家
    private List<Player> playersNewList;//定义一群手牌排序后的玩家
    private Scanner in;   

    public PlayCardDemo()
    {
        pcs = new ArrayList<PokerCard>();
        players = new ArrayList<Player>();
    }

    private void cratePokerCards()
    {
        for(int i = cardColor.length - 1;i >= 0;i--)
        {
            for(int j = 0;j < cardPoints.length;j++)
            {
                pcs.add(new PokerCard(cardColor[i],cardPoints[j]));
            }
        }
    }

    private void showPokerCards()
    {
        StringBuilder sbd = new StringBuilder();
        sbd.append("[");
        for(PokerCard pc:pcs)
        {
            sbd.append(pc.getCardInfo()+",");
        }
        System.out.println("洗牌前为:"+sbd.substring(0,sbd.length()-1)+"]");
    }

    private void shuffleCard()
    {
        Collections.shuffle(pcs);
        StringBuilder sbd = new StringBuilder();
        sbd.append("[");
        for(PokerCard pc : pcs)
        {
            sbd.append(pc.getCardInfo()+",");
        }
        System.out.println("洗牌后为:"+sbd.substring(0,sbd.length()-1)+"]");
    }

    /*
     * 判断输入的ID必须为int型
     * Scanner在发生InputMismatchException异常时,如果不作处理,后面的next会继续异常下去
     * 处理方法:1.手动换行 in.nextLine();
     *       2.重写输入 in = new Scanner(System.in);
     */
    private int checkInputID()
    {
        int inputID;
        do{
            System.out.print("输入ID:");
            try{
                inputID = in.nextInt();
            }catch(Exception e)
            {
                System.out.println("请输入整数类型的ID!");
                inputID = -1;
                in.next();
            }
        }while(inputID == -1);
        return inputID;
    }

    /*
     * 添加玩家
     */

    private void addPlayer(int ID,String name)
    {
        Player p = new Player(ID,name);
        players.add(p);

    }
    /*
     * 欢迎玩家
     */
    private void welcomePlayers() 
    { 
        for (Player p : players) 
        { 
            System.out.println("----欢迎玩家:" + p.getName()); 
        } 
    }

    /*
     * 发牌  一人一张一次发牌共发两张
     * 乱序 返回 牌序
     */
    private void dealCard()
    {
        for(int i = 0;i < 2;i++)
        {
            for(Player p : players)
            {
                PokerCard pc = pcs.get(0);
                p.getHandCards().add(pc);
                System.out.println("----玩家"+p.getName()+"拿牌----");
                pcs.remove(0);

                //每add一次  remove一次

            }
        }
    }

    /*
     * 开始游戏
     */

    private void play()
    {
        playersNewList = new ArrayList<Player>();
        for(Player p : players)
        {
            Player playNew = new Player(p.getID(),p.getName());
            List<PokerCard>pcNewList = new ArrayList<PokerCard>();
            for(PokerCard pc:p.getHandCards())
            {
                pcNewList.add(pc);
            }
            playNew.setHandCards(pcNewList);
            playersNewList.add(playNew);
        }
    }

    /*
     * 判断输赢
     */

    private void getWinner()
    {
        List<PokerCard> maxCards = new ArrayList<PokerCard>();
        for(Player p : playersNewList)
        {
            maxCards.add(p.getHandCards().get(0));
        }
        Collections.sort(maxCards);
        PokerCard maxCard = maxCards.get(0);
        System.out.println("最大的牌是"+maxCard.getCardInfo());

        String winner = "";
        for(Player p : playersNewList)
        {
            if(p.getHandCards().contains(maxCard))
            {
                winner = p.getName();
                break;
            }
        }
        System.out.println("玩家:"+winner+"获胜");
    }

    /*
     * 先是玩家手中的牌
     */
    private void showHandCards()
    {
        for(Player p:players)
        {
            StringBuilder sbd = new StringBuilder();
            sbd.append(p.getName()+":[");
            for(PokerCard pc:p.getHandCards())
            {
                sbd.append(pc.getCardInfo()+",");
            }
            System.out.println(sbd.substring(0,sbd.length()-1)+"]");
        }

    }

    public static void main(String[] args) {
        System.out.println("-----------------创建扑克牌-----------------");
        PlayCardDemo pcd = new PlayCardDemo();
        pcd.cratePokerCards();      //创建扑克牌
        System.out.println("-----------------扑克牌创建成功!-----------------");
        pcd.showPokerCards();       //显示所创建扑克牌
        System.out.println("-----------------开始洗牌-----------------");
        pcd.shuffleCard();          //洗牌
        System.out.println("-----------------洗牌结束!-----------------"); 
        System.out.println("-----------------创建玩家-----------------");
        pcd.in = new Scanner(System.in);
        for(int i = 1;i < 3;i++)
        {
            System.out.println("请输入第" + i + "位玩家的ID和姓名:"); 
            // 这里需要实现输入整数异常处理
            int ID = pcd.checkInputID();
            System.out.print("请输入姓名:");
            String name = pcd.in.next();
            pcd.addPlayer(ID,name);
        }
        pcd.welcomePlayers();
        System.out.println("-----------------开始发牌-----------------");
        pcd.dealCard();   //发牌
        System.out.println("-----------------发牌结束-----------------"); 
        System.out.println("-----------------开始游戏-----------------");
        System.out.println("玩家各自的手牌为:");
        pcd.showHandCards();  
        pcd.play();    //进行手牌大小比较
        pcd.getWinner();        //判断输赢

    }

}
1人推荐
随时随地看视频
慕课网APP