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

Java入门第三季项目实战——简易扑克游戏

MikaMika
关注TA
已关注
手记 2
粉丝 1
获赞 192

创建Card类

package com.imooc.mika.cardsplay;

public class Card implements Comparable<Card> {
    private int color;
    private int number;

    public Card() {
    }

    public Card(int color, int number) {
        super();
        this.color = color;
        this.number = number;
    }

    public int getColor() {
        return color;
    }

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

    public int getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    @Override
    public String toString() {
        String colorString = "";
        String numberString = "";
        switch (color) {
        case 0:
            colorString = "♠";
            break;
        case 13:
            colorString = "♥";
            break;
        case 26:
            colorString = "♣";
            break;
        case 39:
            colorString = "♦";
            break;
        }
        switch (number) {
        case 11:
            numberString = "J";
            break;
        case 12:
            numberString = "Q";
            break;
        case 13:
            numberString = "K";
            break;
        case 14:
            numberString = "A";
            break;
        default:
            numberString = number + "";
            break;
        }
        return colorString + numberString;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + color;
        result = prime * result + number;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Card other = (Card) obj;
        if (color != other.color)
            return false;
        if (number != other.number)
            return false;
        return true;
    }

    @Override
    public int compareTo(Card o) {
        if (this.number > o.number)
            return 1;
        else if (this.number < o.number)
            return -1;
        else if (this.color > o.color)
            return 1;
        else if (this.color < o.color)
            return -1;
        else
            return 0;
    }
}

创建player类

package com.imooc.mika.cardsplay;

import java.util.ArrayList;

public class Player {
    private int id;
    private String name;
    public ArrayList<Card> handCards = new ArrayList<Card>();

    public Player(int id, String name) {
        this.id = id;
        this.name = name;
    }

    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;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + id;
        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 (id != other.id)
            return false;
        return true;
    }

    @Override
    public String toString() {
        return "ID:" + id + ", Name:" + name;
    }

}

主程序

package com.imooc.mika.cardsplay;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class CardsPlayEasy {
    private static Scanner sn = new Scanner(System.in);
    private static ArrayList<Player> players = new ArrayList<Player>();
    private static ArrayList<Card> cards = new ArrayList<Card>();

    public static void main(String[] args) {
        // 创建扑克牌,一副牌52张,可创建多副
        cardsInitial();
        // 洗牌
        shuffleCards();
        // 创建玩家,可创建多人
        createPlayers();
        // 发牌
        getHandCards();
        // 游戏
        playGame();
    }

    private static void cardsInitial() {
        System.out.println("---------------创建扑克牌--------------");
        int fu = 0;
        while (true) {
            System.out.println("输入创建几副牌(1副52张):");
            fu = inputCommand();
            if (fu < 1)
                System.out.println("至少1副牌");
            else
                break;
        }

        for (int k = 0; k < fu; k++) {
            for (int i = 0; i < 52; i++)
                cards.add(new Card(i / 13 * 13, i % 13 + 2));
        }
        System.out.println("---------------扑克牌创建成功!-----------------");
        System.out.println(cards);
    }

    private static void shuffleCards() {
        System.out.println("---------------开始洗牌-----------------");
        Collections.shuffle(cards);
        System.out.println("---------------洗牌完成!-----------------");
    }

    private static void createPlayers() {
        System.out.println("---------------创建玩家:-----------------");
        int count = 0;
        while (true) {
            System.out.println("输入创建几个玩家:");
            count = inputCommand();
            if (count < 1)
                System.out.println("至少1个玩家");
            else
                break;
        }

        for (int i = 0; i < count; i++) {
            System.out.println("请输入第" + (i + 1) + "玩家的ID和姓名:");

            int playerId = 0;

            while (true) {
                System.out.println("输入ID(1~" + count + "):");
                playerId = inputCommand();
                if (playerId < 1 || playerId > count)
                    System.out.println("不在ID范围内");
                else if (players.contains(new Player(playerId, "")))
                    System.out.println("该ID玩家已存在");
                else
                    break;
            }

            System.out.println("输入姓名:");
            String playerName = sn.next();
            players.add(new Player(playerId, playerName));
        }

        for (Player player : players)
            System.out.println("-----欢迎玩家:" + player);
    }

    private static void playGame() {
        ArrayList<Card> maxHandCards = new ArrayList<Card>();
        for (Player player : players) {
            System.out.println("玩家:" + player.getName() + "手牌为:" + player.handCards);
            Collections.sort(player.handCards, Collections.reverseOrder());
            if (!player.handCards.isEmpty()) {
                maxHandCards.add(player.handCards.get(0));
                System.out.println("玩家:" + player.getName() + "最大手牌为:" + player.handCards.get(0));
            }
        }

        Collections.sort(maxHandCards, Collections.reverseOrder());
        if (!maxHandCards.isEmpty()) {
            for (Player player : players) {
                if (maxHandCards.get(0).equals(player.handCards.get(0))) {
                    System.out.println("-------玩家" + player.getName() + "获胜!-----");
                }
            }
        } else
            System.out.println("-------没人有牌!-----");
    }

    private static void getHandCards() {
        System.out.println("---------------开始发牌-----------------");
        int everyMaxCards = 0;
        while (true) {
            System.out.println("输入每个玩家最多抓取几张牌:");
            everyMaxCards = inputCommand();
            if (everyMaxCards < 1)
                System.out.println("至少1张牌");
            else
                break;
        }

        if (players.isEmpty())
            return;
        for (int i = 0; i < cards.size(); i++) {
            Player player = players.get((i % players.size()));
            if (player.handCards.size() >= everyMaxCards)
                break;
            player.handCards.add(cards.get(i));
            System.out.println("玩家:" + player.getName() + "-拿牌");
            System.out.println("----------------发牌结束!-------------------");
        }
    }

    private static int inputCommand() {
        try {
            return sn.nextInt();
        } catch (Exception e) {
            sn = new Scanner(System.in);
            return -1;
        }
    }

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

热门评论

这个直接拷贝过去就可以用了吗?

玩家:c手牌为:[方块2, 方块3, 黑桃2, 方块J, 梅花9, 黑桃10, 梅花3, 梅花4, 方块9, 方块A, 梅花7, 红桃5, 红桃Q] 玩家:c最大手牌为:方块A 玩家:d手牌为:[黑桃K, 梅花8, 方块4, 红桃10, 红桃K, 梅花Q, 方块7, 黑桃8, 黑桃5, 梅花6, 黑桃7, 方块8, 黑桃A] 玩家:d最大手牌为:黑桃A -------玩家c获胜!-----

玩家:b手牌为:[梅花K, 红桃3, 红桃7, 红桃J, 梅花5, 红桃8, 红桃4, 梅花10, 方块Q, 黑桃4, 梅花A, 红桃A, 红桃9] 玩家:b最大手牌为:梅花A

查看全部评论