为了账号安全,请及时绑定邮箱和手机立即绑定

Java入门第三季:简易扑克牌游戏

标签:
Java
游戏玩法
  1. 创建一副扑克牌,除大小王
  2. 登录两名玩家
  3. 随机洗牌 (打乱扑克牌的顺序)
  4. 发牌 (游戏开始,每名玩家发两张)
  5. 比较:先比点数,点数相等,再比花色(黑桃>红桃>梅花>方片)
分析

通过玩法,我们可以知道我们需要一个扑克牌集合来存放扑克牌,需要一个玩家集合来存放玩家,难点就在于扑克牌的比较,这个可以通过扑克牌这个对象实现Comparable接口,并重写compareTo()方法,自己定义默认的比较规则来解决这个难点。

代码
Card.java
import java.util.ArrayList;
import java.util.List;

/**
 * 扑克牌类
 * @author 杨萌 coder_sprout@163.com
 *
 */
public class Card implements Comparable<Card> {
    // 扑克牌花色
    public String color;
    // 扑克牌点数
    public String point;

    public Card() {

    }

    public Card(String color, String point) {
        this.color = color;
        this.point = point;
    }

    /**
     * 重写compareTo()方法,根据自己的规则
     */
    @Override
    public int compareTo(Card o) {
        if (this.point == o.point) {
            if (this.color.equals("黑桃")) {
                return 1;
            } else if (this.color.equals("红桃")) {
                if (o.color.equals("黑桃")) {
                    return -1;
                } else {
                    return 1;
                }
            } else if (this.color.equals("梅花")) {
                if (o.color.equals("黑桃") || o.color.equals("红桃")) {
                    return -1;
                } else {
                    return 1;
                }
            } else {
                return -1;
            }
        } else {
            if (this.point.equals("A")) {
                return 1;
            } else if (this.point.equals("K")) {
                if (o.point.equals("A")) {
                    return -1;
                } else {
                    return 1;
                }
            } else if (this.point.equals("Q")) {
                if (o.point.equals("A") || o.point.equals("K")) {
                    return -1;
                } else {
                    return 1;
                }
            } else if (this.point.equals("J")) {
                if (o.point.equals("A") || o.point.equals("K") || o.point.equals("Q")) {
                    return -1;
                } else {
                    return 1;
                }
            } else if (this.point.equals("10")) {
                if (o.point.equals("A") || o.point.equals("K") || o.point.equals("Q") || o.point.equals("J")) {
                    return -1;
                } else {
                    return 1;
                }
            } else {
                return this.point.compareTo(o.point);
            }
        }
    }

}
Player.java
/**
 * 玩家类
 * @author 杨萌 coder_sprout@163.com
 *
 */
public class Player {
    // 玩家id
    public int id;
    // 玩家姓名
    public String name;
    public List<Card> cards;

    public Player(){

    } 

    public Player(int id, String name){
        this.id = id;
        this.name = name;
        this.cards = new ArrayList<Card>();
    } 
}
GameDemo.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * 简易扑克牌游戏: 1.创建一副扑克牌,除大小王 2.登录两名玩家 3.随机洗牌 4.发牌 5.比较:先比点数,点数想等,再比花色
 * 
 * @author 杨萌 coder_sprout@163.com
 *
 */
public class GameDemo {
    // 保存扑克牌的集合
    private List<Card> cards = new ArrayList<Card>();
    // 保存玩家的集合
    private List<Player> players = new ArrayList<Player>();
    private Scanner scanner;

    public GameDemo() {
        scanner = new Scanner(System.in);
    }

    // 创建扑克牌
    private void createCard() {
        System.out.println("---------创建扑克牌---------");
        for (int i = 0; i < 4; i++) {
            for (int j = 2; j <= 14; j++) {
                switch (i) {
                case 0:
                    Card card = new Card("黑桃", setCardPoint(j));
                    cards.add(card);
                    break;
                case 1:
                    Card card2 = new Card("红桃", setCardPoint(j));
                    cards.add(card2);
                    break;
                case 2:
                    Card card3 = new Card("梅花", setCardPoint(j));
                    cards.add(card3);
                    break;
                case 3:
                    Card card4 = new Card("方片", setCardPoint(j));
                    cards.add(card4);
                    break;
                }
            }
        }
        System.out.println("---------扑克牌创建成功---------");
    }

    // 遍历扑克牌
    private void lookCardByForEach() {
        for (Card card : cards) {
            System.out.print(card.color + card.point + " ");
        }
    }

    private String setCardPoint(int j) {
        String point = null;
        if (j <= 10) {
            point = j + "";
        } else {
            switch (j) {
            case 11:
                point = "J";
                break;
            case 12:
                point = "Q";
                break;
            case 13:
                point = "K";
                break;
            case 14:
                point = "A";
                break;
            }
        }
        return point;
    }

    // 洗牌
    private void shuffle() {
        System.out.println();
        System.out.println("---------开始洗牌---------");
        // 随机打乱list顺序
        Collections.shuffle(cards);
        System.out.println("---------洗牌结束---------");
    }

    // 创建玩家
    private void createPlayer() {
        System.out.println("---------创建玩家---------");
        for (int i = 0; i < 2; i++) {
            System.out.println("请输入第" + (i + 1) + "位玩家的ID和姓名:");
            System.out.println("输入ID:");
            int id = 0;
            while(true){
                try{
                    /**
                     * nextInt方法在发生异常后,不再接受用户输入的任何数据,而是徘徊在异常区域,如果外部使用死循环,从而导致死循环发生。
                     * 使用trim()方法是以防万一的写法,为了降低错误的发生率,以免手误多打空格
                     */
                    id = Integer.parseInt(scanner.nextLine().trim());;
                    break;
                }catch (NumberFormatException e) {
                    System.out.println("请输入整数类型的ID!");
                                        System.out.println("重新输入ID:");
                    continue;
                } 
            }
            System.out.println("输入姓名:");
            String name = scanner.nextLine();
            Player player = new Player(id, name);
            players.add(player);    
        }
        System.out.println("----欢迎玩家:" + players.get(0).name);
        System.out.println("----欢迎玩家:" + players.get(1).name);
    }

    // 开始发牌
    private void getCard() {
        System.out.println("---------开始发牌---------");
        System.out.println("----玩家:" + players.get(0).name + "-拿牌");
        players.get(0).cards.add(cards.get(0));
        System.out.println("----玩家:" + players.get(1).name + "-拿牌");
        players.get(1).cards.add(cards.get(1));
        System.out.println("----玩家:" + players.get(0).name + "-拿牌");
        players.get(0).cards.add(cards.get(2));
        System.out.println("----玩家:" + players.get(1).name + "-拿牌");
        players.get(1).cards.add(cards.get(3));
        System.out.println("---------发牌结束---------");
    }

    // 游戏开始
    private void gameStart() {
        System.out.println("---------开始游戏---------");
        Collections.sort(players.get(0).cards);
        System.out.println("玩家:" + players.get(0).name + "最大的手牌为: " + players.get(0).cards.get(1).color
                + players.get(0).cards.get(1).point);
        Collections.sort(players.get(1).cards);
        System.out.println("玩家:" + players.get(1).name + "最大的手牌为: " + players.get(1).cards.get(1).color
                + players.get(1).cards.get(1).point);

        List<Card> gameCards = new ArrayList<Card>();
        gameCards.addAll(players.get(0).cards);
        gameCards.addAll(players.get(1).cards);
        Collections.sort(gameCards);
        if (players.get(0).cards.contains(gameCards.get(gameCards.size() - 1))) {
            System.out.println("---------玩家:" + players.get(0).name + "获胜---------");
        } else {
            System.out.println("---------玩家:" + players.get(1).name + "获胜---------");
        }
    }

    // 查看玩家手牌
    private void lookPlayerCard() {
        System.out.println("---------玩家各自的手牌为:---------");
        String player1Card1 = players.get(0).cards.get(0).color + players.get(0).cards.get(0).point;
        String player1Card2 = players.get(0).cards.get(1).color + players.get(0).cards.get(1).point;
        System.out.println(players.get(0).name + ":[" + player1Card1 + "," + player1Card2 + "]");
        String player2Card1 = players.get(1).cards.get(0).color + players.get(1).cards.get(0).point;
        String player2Card2 = players.get(1).cards.get(1).color + players.get(1).cards.get(1).point;
        System.out.println(players.get(1).name + ":[" + player2Card1 + "," + player2Card2 + "]");
    }

    public static void main(String[] args) {
        while (true) {
            GameDemo gd = new GameDemo();
            gd.createCard();
            gd.lookCardByForEach();
            gd.shuffle();
            gd.lookCardByForEach();
            gd.createPlayer();
            gd.getCard();
            gd.gameStart();
            gd.lookPlayerCard();
            System.out.println();
            System.out.println();
            System.out.println();
            System.out.println("---------是否继续游戏?---------");
            System.out.println("----1.继续游戏-----0.退出游戏----");
            if (gd.scanner.next().equals("1")) {
                continue;
            } else {
                break;
            }
        }
        System.out.println("---------游戏结束!---------");
    }
}
点击查看更多内容
35人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消