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

JAVA入门第三季 —— 扑克牌游戏

闰冬秋拾
关注TA
已关注
手记 2
粉丝 4
获赞 15
//Card类:
package com.bob;

public class Card {
    public String color;
    public String value;
    public double colorWeight;
    public double weight;

    public Card(String color, String value) {
        this.color = color;
        this.value = value;
    }
}
//Player类:
package com.bob;

import java.util.ArrayList;
import java.util.List;

public class Player {
    public String ID;
    public String name;
    List<Card> list = new ArrayList<Card>();
    List<Double> result = new ArrayList<Double>();
    double resultLast;

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((this.result == null) ? 0 : this.result.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Player other = (Player) obj;
        if (result == null) {
            if (other.result != null)
                return false;
        } else if (!result.equals(other.result))
            return false;
        return true;
    }

    public Player(String ID, String name) {
        this.ID = ID;
        this.name = name;
    }
}
//GamePlay类:
package com.bob;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class GamePlay {
    List<Card> cardList = new ArrayList<Card>();
    List<Player> playerList = new ArrayList<Player>();
    Set<Card> cardSet = new HashSet<Card>();
    Scanner console = new Scanner(System.in);
    int joinerNum = 0;

    public void cardCreate() {
        System.out.println("----------创建扑克----------");
        String colors[] = { "梅花", "黑桃", "方片", "红桃" };
        for (String string : colors) {
            for (int i = 2; i < 11; i++) {
                cardList.add(new Card(string, i + ""));
            }
            cardList.add(new Card(string, "J"));
            cardList.add(new Card(string, "Q"));
            cardList.add(new Card(string, "K"));
            cardList.add(new Card(string, "A"));
        }
        System.out.println("----------创建成功----------");
        for (Card card : cardList) {
            System.out.print(card.color + card.value + ",");
        }
        System.out.println();
    }

    public void playerIn() {
        System.out.println("----------用户注册----------");
        System.out.println("请输入参与游戏的人数:");
        joinerNum = console.nextInt();
        for (int i = 0; i < joinerNum; i++) {
            String ID = null;
            String name = null;
            System.out.println("请输入第" + (i + 1) + "位玩家编号(仅支持数字):");
            ID = console.next();
            while (!ID.matches("\\d+")) {
                System.out.println("输入编号有误,请输入整数!");
                ID = console.next();
            }
            System.out.println(ID + "号玩家," + "请设置您的昵称(仅支持数字/字母/下划线组合)");
            name = console.next();
            while (!name.matches("[a-zA-Z0-9_]+")) {
                System.out.println("不支持这样的昵称,请您重新输入(仅支持数字/字母/下划线组合,如hrq_123):");
                name = console.next();
            }
            System.out.println(ID + "号玩家" + name + ",欢迎您进入游戏!");
            playerList.add(new Player(ID, name));
        }
        System.out.println("----------注册完毕----------");
    }

    public void cardRearrange() {
        System.out.println("----------正在洗牌---------");
        for (Card card : cardList) {
            cardSet.add(card);
        }
        System.out.println("----------洗牌结束---------");
        for (Card card : cardSet) {
            System.out.print(card.color + card.value + ",");
        }
    }

    public void cardAllocate() {
        System.out.println("----------现在发牌----------");
        for (int j = 0; j < 2; j++) {
            for (int i = 0; i < joinerNum; i++) {
                Random random = new Random();
                int rIndex = 0;
                int flag = 0;
                do {
                    rIndex = random.nextInt(52);
                } while (rIndex == flag);
                flag = rIndex;
                System.out.println(playerList.get(i).ID + "号玩家,现在为你发第" + (j + 1) + "张牌");
                playerList.get(i).list.add(cardList.get(rIndex));
            }
        }
        System.out.println("----------发牌结束----------");
    }

    public void cardCompare() {
        System.out.println("----------大小比较----------");
        System.out.println("规则:黑红方梅,从大到小!");
        List<Double> rs = new ArrayList<Double>();
        int i = 0;
        for (Player player : playerList) {
            for (Card card : player.list) {
                if (card.color.equals("黑桃")) {
                    card.colorWeight = 0.4;
                }
                if (card.color.equals("红桃")) {
                    card.colorWeight = 0.3;
                }
                if (card.color.equals("梅花")) {
                    card.colorWeight = 0.2;
                }
                if (card.color.equals("方片")) {
                    card.colorWeight = 0.1;
                }
                if (card.value.matches("\\d+"))
                    card.weight = card.colorWeight + Integer.parseInt(card.value);
                if (card.value.equals("J")) {
                    card.weight = card.colorWeight + 11;
                }
                if (card.value.equals("Q")) {
                    card.weight = card.colorWeight + 12;
                }
                if (card.value.equals("K")) {
                    card.weight = card.colorWeight + 13;
                }
                if (card.value.equals("A")) {
                    card.weight = card.colorWeight + 1;
                }
                player.result.add(card.weight);
            }
            Collections.sort(player.result);
            player.resultLast = player.result.get(player.result.size() - 1);
            for (Card card : player.list) {
                if (card.weight == player.resultLast)
                    System.out.println(player.ID + "号玩家最大的牌是:" + card.color + card.value);
            }
            rs.add(player.resultLast);
            i++;
        }
        Collections.sort(rs);
        for (Player player : playerList) {
            if (player.resultLast == rs.get(rs.size() - 1)) {
                System.out.println(player.ID + "号玩家" + player.name + "获胜!");
                break;
            }
        }
        System.out.println(joinerNum + "位玩家的牌分别是:");
        for (Player player : playerList) {
            for (Card card : player.list) {
                System.out.println(player.ID + ":" + card.color + card.value);
            }
        }
        System.out.println("----------游戏结束----------");
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("-----------欢迎进入扑克比大小游戏----------");
        GamePlay gp = new GamePlay();
        gp.cardCreate();
        gp.playerIn();
        gp.cardRearrange();
        gp.cardAllocate();
        gp.cardCompare();
    }
}

遗憾的是,没有使用Map,所以根据比较结果找牌的名字的时候非常的吃力。还需要多熟悉,异常处理真的没有用上,还是使用了while的经典循环。

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

热门评论

可以有解释吗?我有点看不懂!我是新手!

查看全部评论