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

Java入门第三季—7.1扑克牌小游戏

散华礼之弥
关注TA
已关注
手记 4
粉丝 2
获赞 15
PokerCard.java
package 扑克牌游戏;

public class PokerCard {

    private String color;
    private String point;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getPoint() {
        return point;
    }

    public void setPoint(String point) {
        this.point = point;
    }

    public PokerCard(){
        super();
    }

    public PokerCard(String color, String point) {
        super();
        this.color = color;
        this.point = point;
    }
}
Player.java
package 扑克牌游戏;

import java.util.HashSet;
import java.util.Set;

public class Player {

    private int id;
    private String name;
    private Set<PokerCard> pokerCard;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Set<PokerCard> getPokerCard() {
        return pokerCard;
    }

    public void setPokerCard(Set<PokerCard> pokerCard) {
        this.pokerCard = pokerCard;
    }

    public Player(int id, String name) {
        super();
        this.id = id;
        this.name = name;
        this.pokerCard = new HashSet<>();
    }

}
PokerGame.java
package 扑克牌游戏;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class PokerGame {

    private static Scanner scanner;
    // 每个玩家的拿牌数
    private static final int CARDSNUM = 2;
    // 玩家数量
    private static final int PLAYERNUM = 2;

    // 花色数组
    private static final String[] COLORS = new String[] { "方片", "梅花", "红桃", "黑桃" };
    // 花色Map,以便于查找index,并通过index比较大小
    private static Map<String, Integer> colorMap = new HashMap<>();
    // 点数数组
    private static final String[] POINTS = new String[] { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K",
            "A" };
    // 点数Map,以便于查找index,并通过index比较大小
    private static Map<String, Integer> pointMap = new HashMap<>();

    // 玩家数组
    private static final Player[] PLAYERS = new Player[PLAYERNUM];
    // 扑克牌List
    private static List<PokerCard> pokerCards;

    // 玩家1的卡牌组
    private static PokerCard[] pokerCards0 = new PokerCard[CARDSNUM];
    // 玩家2的卡牌组
    private static PokerCard[] pokerCards1 = new PokerCard[CARDSNUM];

    public static void main(String[] args) {
        pokerCards = new ArrayList<>();
        scanner = new Scanner(System.in);
        // 开始游戏
        initGame();
        // 洗牌
        shuffle();
        // 创建玩家
        initPlayers();
        // 开始发牌
        dealCards();
        // 开始游戏
        playGame();
        // 显示玩家各自的手牌
        showPlayersCards();
        scanner.close();
    }

    private static void showPlayersCards() {
        System.out.println("----------玩家各自的手牌为----------");
        // 玩家1
        System.out.print("玩家" + PLAYERS[0].getName() + ":[");
        System.out.print(pokerCards0[0].getColor() + pokerCards0[0].getPoint()+", "+pokerCards0[1].getColor() + pokerCards0[1].getPoint());
        System.out.print("]\n");
        // 玩家2
        System.out.print("玩家" + PLAYERS[1].getName() + ":[");
        System.out.print(pokerCards1[0].getColor() + pokerCards1[0].getPoint()+", "+pokerCards1[1].getColor() + pokerCards1[1].getPoint());
        System.out.print("]");

    }

    /**
     * 开始游戏
     */
    private static void playGame() {
        System.out.println("----------开始游戏----------");
        PokerCard pokerCard0;
        PokerCard pokerCard1;
        // 找到玩家1最大的牌
        Iterator<PokerCard> iterator = PLAYERS[0].getPokerCard().iterator();
        for (int j = 0; j < CARDSNUM && iterator.hasNext(); j++) {
            pokerCards0[j] = (PokerCard) iterator.next();
        }
        pokerCard0 = getMaxPointCard(pokerCards0[0], pokerCards0[1]);
        System.out.println("玩家" + PLAYERS[0].getName() + "最大的手牌为:" + pokerCard0.getColor() + pokerCard0.getPoint());
        // 找到玩家2最大的牌
        Iterator<PokerCard> iterator1 = PLAYERS[1].getPokerCard().iterator();
        for (int j = 0; j < CARDSNUM && iterator1.hasNext(); j++) {
            pokerCards1[j] = (PokerCard) iterator1.next();
        }
        pokerCard1 = getMaxPointCard(pokerCards1[0], pokerCards1[1]);
        System.out.println("玩家" + PLAYERS[1].getName() + "最大的手牌为:" + pokerCard1.getColor() + pokerCard1.getPoint());
        // 然后找到最大的牌
        // 再判断这张牌是谁的。。。
        PokerCard maxPokerCard = getMaxPointCard(pokerCard0, pokerCard1);
        if (PLAYERS[0].getPokerCard().contains(maxPokerCard)) {
            System.out.println("----------玩家" + PLAYERS[0].getName() + "获胜!----------");
        } else {
            System.out.println("----------玩家" + PLAYERS[1].getName() + "获胜!----------");
        }
    }

    /**
     * 比较两张牌的大小 通过在数组中的下标进行比较 牌越大,下标越大
     * 
     * @param p1
     * @param p2
     * @return
     */
    private static PokerCard getMaxPointCard(PokerCard p1, PokerCard p2) {
        // 黑桃,红心,梅花,方块
        // 首先比较点数
        if (pointMap.get(p1.getPoint()) == pointMap.get(p2.getPoint())) {
            if (colorMap.get(p1.getColor()) == colorMap.get(p2.getColor())) {
                // 花色和点数都相等的情况是不存在的所有返回null
                return null;
            } else {
                return colorMap.get(p1.getColor()) > colorMap.get(p2.getColor()) ? p1 : p2;
            }
        } else {
            return pointMap.get(p1.getPoint()) > pointMap.get(p2.getPoint()) ? p1 : p2;
        }
    }

    /**
     * 发牌
     */
    private static void dealCards() {
        System.out.println("----------开始发牌----------");
        for (int i = 0; i < CARDSNUM; i++) {
            for (int j = 0; j < PLAYERS.length; j++) {
                if ((i == 1 && j == 0) || (i == 1 && j == 1)) {
                    PLAYERS[j].getPokerCard().add(pokerCards.get(i + j + 1));
                } else {
                    PLAYERS[j].getPokerCard().add(pokerCards.get(i + j));
                }
                System.out.println("----------玩家" + PLAYERS[j].getName() + "拿牌----------");
            }
        }
        System.out.println("----------发牌结束----------");
    }

    /**
     * 创建玩家
     */
    private static void initPlayers() {
        for (int i = 0; i < PLAYERNUM; i++) {
            System.out.println("请输入第" + (i + 1) + "位玩家的ID和姓名:");
            System.out.println("输入ID:");
            int id = inputInt();
            System.out.println("输入姓名:");
            scanner.nextLine();
            String name = scanner.nextLine();
            PLAYERS[i] = new Player(id, name);
        }
        // 玩家欢迎
        for (int i = 0; i < PLAYERNUM; i++) {
            System.out.println("-----欢迎玩家:" + PLAYERS[i].getName() + "-----");
        }
    }

    /**
     * 洗牌
     */
    private static void shuffle() {
        System.out.println("----------开始洗牌----------");
        // 洗牌--打乱List中元素的顺序
        Collections.shuffle(pokerCards);
        System.out.println("----------洗牌结束----------");
    }

    /**
     * 开始游戏
     */
    private static void initGame() {
        System.out.println("----------创建扑克牌----------");
        // 创建扑克牌
        createPokerCards();
        System.out.println("----------扑克牌创建成功----------");
        // 显示
        showData();
    }

    /**
     * 创建52张扑克牌
     */
    private static void createPokerCards() {
        /*
         *  初始化花色和点数Map
         *  之所有要用到Map是为了更快速的比较牌的大小
         */
        for (int i = 0; i < COLORS.length; i++) {
            colorMap.put(COLORS[i], i);
        }
        for (int i = 0; i < POINTS.length; i++) {
            pointMap.put(POINTS[i], i);
        }
        // 生成扑克
        for (int i = 0; i < COLORS.length; i++) {
            for (int j = 0; j < POINTS.length; j++) {
                pokerCards.add(new PokerCard(COLORS[i], POINTS[j]));
            }
        }
    }

    /**
     * 显示List数据
     */
    private static void showData() {
        System.out.print("[");
        for (int i = 0; i < 51; i++) {
            System.out.print(pokerCards.get(i).getColor() + pokerCards.get(i).getPoint() + ", ");
            //纯粹为了显示好看点儿的
            //每隔13个元素换行一次
            if (i > 0 && (i + 1) % 13 == 0) {
                System.out.println();
            }
        }
        System.out.print(pokerCards.get(51).getColor() + pokerCards.get(51).getPoint());
        System.out.print("]");
    }

    /**
     * 输入判断--如果输入为非数字,则报错并重新输入
     * 
     * @return
     */
    private static int inputInt() {
        int result = 0;
        while (true) {
            try {
                scanner = new Scanner(System.in);
                // 这里如果输入非int类型则会出现异常
                result = scanner.nextInt();
                break;
            } catch (Exception e) {
                // 捕获异常并进行提示
                System.out.println("请输入整型数字!");
                continue;
            }
        }
        return result;
    }
}
打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP