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

供参考 有问题请指出

纸牌类

package imooc.imoocGame;


import java.util.Objects;

public class Card implements Comparable<Card>{
    private String flower;
    private String num;
    private int id;

    public void setInd(int ind) {
        this.ind = ind;
    }

    public int getInd() {
        return ind;
    }

    private int ind;

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

    public void setFlower(String flower) {
        this.flower = flower;
    }


    public void setNum(String num) {
        this.num = num;
    }

    public String getNum() {
        return num;
    }

    public String getFlower() {
        return flower;
    }

    public int getId() {
        return id;
    }

    @Override
    public int compareTo(Card o) {
        if(this.num.equals(o.num)){
            return this.id-o.id;
        }else{
            return this.ind-o.ind;
        }

    }

}

玩家类

package imooc.imoocGame;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Player {
    private int id;
    private String name;
    private List<Card> handCard;
    public Player() {
        this.handCard = new ArrayList<>();
    }
    public Player(int id,String name) {
        this.handCard = new ArrayList<>();
        this.id=id;
        this.name=name;
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<Card> getHandCard() {
        return handCard;
    }

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

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

    public void setHandCard(List<Card> handCard) {
        this.handCard = handCard;
    }

}

游戏类


package imooc.imoocGame;

import com.Car;

import java.util.*;

public class Game {
    private List<Card> cards;
    private Scanner console;
    private List<Player> players;

    private Game() {
        this.cards = new ArrayList<>();
        this.console = new Scanner(System.in);
        this.players = new ArrayList<>();
    }

    //    创建扑克牌
    private void EstablishCards() {
        System.out.println("------创建扑克牌------");
        String[] flower = {"方块", "梅花", "黑桃", "红桃"};
        int id = 0;
        for (String val : flower) {
            switch (val) {
                case "方块":
                    id = 1;
                    break;
                case "梅花":
                    id = 2;
                    break;
                case "黑桃":
                    id = 3;
                    break;
                case "红桃":
                    id = 4;
                    break;
            }
            for (int i = 2; i <= 14; i++) {
                String num = String.valueOf(i);
                Card card = new Card();
                card.setInd(i);
                card.setFlower(val);
                card.setId(id);
                switch (num) {
                    case "11":
                        card.setNum("J");
                        break;
                    case "12":
                        card.setNum("Q");
                        break;
                    case "13":
                        card.setNum("K");
                        break;
                    case "14":
                        card.setNum("A");
                        break;
                    default:
                        card.setNum(num);
                }
                cards.add(card);
            }
        }
        System.out.println("------扑克牌创建成功------");
        System.out.print("扑克牌为:");
        for (Card val : cards) {
            System.out.print(val.getFlower() + val.getNum() + ",");
        }
        System.out.println();
    }

    //洗牌
    private void Shuffle() {
        System.out.println("------开始洗牌------");
        List<Integer> num = new ArrayList<>();
        Random random = new Random();
        Integer k;
        for (int i = 0; i < 52; i++) {
            Card resetCard = cards.get(i);
            do {
                k = random.nextInt(52);
            } while (num.contains(k));
            num.add(k);
            cards.set(k, resetCard);
        }
        System.out.println("------洗牌结束------");
    }

    //    创建玩家
    private void EstablishPlayer() {
        System.out.println("------创建玩家------");
        System.out.println("------请输入第一位玩家的id和姓名------");
        int player1Id = CatchInput();
        System.out.println("------输入姓名------");
        String player1Name = console.next();
        System.out.println("------请输入第二位玩家的id和姓名------");
        int player2Id = CatchInput();
        System.out.println("------输入姓名------");
        String player2Name = console.next();
        System.out.println(player1Id + ":" + player2Id);
        Player player1 = new Player(player1Id, player1Name);
        Player player2 = new Player(player2Id, player2Name);
        players.add(player1);
        players.add(player2);
        System.out.println("---欢迎玩家:" + player1.getName());
        System.out.println("---欢迎玩家:" + player2.getName());
    }

    //    捕捉输入id错误的异常
    private int CatchInput() {
        while (true) {
            Scanner console = new Scanner(System.in);
            try {
                System.out.println("------输入id------");
                return console.nextInt();
            } catch (InputMismatchException e) {
                System.out.println("------输入错误------");
            }
        }
    }

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

    private void gameStart() {
        System.out.println("------开始游戏------"); 
        Collections.sort(players.get(0).getHandCard());
        Collections.sort(players.get(1).getHandCard());
        System.out.println("玩家:" + players.get(0).getName() + "最大的手牌为:" +
                players.get(0).getHandCard().get(1).getFlower() + players.get(0).getHandCard().get(1).getNum());
        System.out.println("玩家:" + players.get(1).getName() + "最大的手牌为:" +
                players.get(1).getHandCard().get(1).getFlower() + players.get(1).getHandCard().get(1).getNum());
        List<Card> lc = new ArrayList<>();
        lc.add(players.get(0).getHandCard().get(1));
        lc.add(players.get(1).getHandCard().get(1));
        Collections.sort(lc);

        Player winner = new Player();
        for (Player pl : players) {
            if (pl.getHandCard().contains(lc.get(1))) {
                winner = pl;
            }
        }
        System.out.println("------玩家:" + winner.getName() + "获胜------");

        System.out.println("------玩家各自的手牌为------");
        System.out.print(players.get(0).getName() + ":[");
        for (Card ca : players.get(0).getHandCard()) {
            System.out.print(ca.getFlower() + ca.getNum() + " ");
        }
        System.out.print("]");
        System.out.println();
        System.out.print(players.get(1).getName() + ":[");
        for (Card ca : players.get(1).getHandCard()) {
            System.out.print(ca.getFlower() + ca.getNum() + " ");
        }
        System.out.print("]");

    }

    public static void main(String[] args) {
        Game game = new Game();
//        创建扑克牌
        game.EstablishCards();
//        洗牌
        game.Shuffle();
//        创建玩家
        game.EstablishPlayer();
//        发牌
        game.Licensing();
//        开始游戏
        game.gameStart();
    }

}


正在回答

举报

0/150
提交
取消
Java入门第三季
  • 参与学习       409791    人
  • 解答问题       4340    个

Java中你必须懂得常用技能,不容错过的精彩,快来加入吧

进入课程

供参考 有问题请指出

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信