手记

简易扑克牌游戏,在shoumananxun基础上优化修改版

扑克牌类

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 
 * @author sun
 *  扑克牌
 *  包括四种花色:黑桃、红桃、梅花、方块、
 *  十三中点数:2~10,J、Q、K、A,不包括大小王
 */
public class Pocker implements Comparable<Pocker>{
    private String color;  //扑克牌花色
    private String value;  //扑克牌面值
    public static final int CARDNUM = 52;   //扑克牌总量
    private static final int SINGLECOLORNUM = 13; //每种颜色扑克牌数量

    public Pocker(){
    }

    public Pocker(String color, String value) {
        this.color = color;
        this.value = value;
    }

    public String getColor() {
        return color;
    }

    public String getValue() {
        return value;
    }

    /*
     * 产生52张扑克牌
     * return List<Pocker>
     */

    public static List<Pocker> createPocker(){
        List<Pocker> pockers = new ArrayList<Pocker>();
        for(int i = 0; i < CARDNUM; i++){
            String newValue = null;
            int temp = i % SINGLECOLORNUM;
            switch (temp) {
            case 11:
                newValue = "J";
                break;
            case 12:
                newValue = "Q";
                break;
            case 0:
                newValue = "K";
                break;
            case 1:
                newValue = "A";
                break;
            default:
                newValue = String.valueOf(temp); //i为2-10,转成字符串
            }

            String newColor = null;
            switch (i / SINGLECOLORNUM) {
            case 0:
                newColor = "黑桃";
                break;
            case 1:
                newColor = "红桃";
                break;
            case 2:
                newColor = "梅花";
                break;
            case 3:
                newColor = "方块";
            }
            pockers.add(new Pocker(newColor,newValue));
        }
        return pockers;
    }

    /*
     * Collections.shuffle进行洗牌
     */
    public static List<Pocker> shuffle(List<Pocker> pockers){
        Collections.shuffle(pockers);
        return pockers;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Comparable#compareTo(java.lang.Object)
     * 重写了copmareTo方法,实现了Comparable接口
     * 根据牌值和花色在列表中的顺序进行比较
     */
    @Override
    public int compareTo(Pocker o) {
        List<String> valueBase = Arrays.asList("2","3","4","5","6","7","8","9","10","J","Q","K","A");
        List<String> colorBase = Arrays.asList("方块","梅花","红桃","黑桃");
        if(valueBase.indexOf(this.value) == valueBase.indexOf(o.value))
            return Integer.valueOf(colorBase.indexOf(this.color)).compareTo(Integer.valueOf(colorBase.indexOf(o.getColor())));
        else 
            return Integer.valueOf(valueBase.indexOf(this.value)).compareTo(Integer.valueOf(valueBase.indexOf(o.getValue())));

    }

    @Override
    public String toString() {
        return color+value;
    }

}

玩家类

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

public class Player {
    /*
     * 玩家
     * 属性:ID,姓名,手牌
     * 手牌为扑克牌的集合
     */
    private int id;
    private String name;
    private List<Pocker> handPockers;

    public Player(int id, String name) {
        this.id = id;
        this.name = name;
        this.handPockers = new ArrayList<Pocker>();
    }

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<Pocker> getHandPockers() {
        return handPockers;
    }

}

游戏类

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

public class PlayPockers {
    private List<Pocker> pockers;  //扑克牌列表
    private List<Player> players;   //玩家列表
    private Scanner console;    
    private int playerNum;  //玩家数量
    private int handCardNum;    //手牌数量

    public PlayPockers() {
        super();
        this.pockers = new ArrayList<Pocker>();
        this.players = new ArrayList<Player>();
        this.console = new Scanner(System.in);
        this.playerNum = 0;
        this.handCardNum = 3;
    }

    public PlayPockers(List<Pocker> pockers,int handCardNum) {
        super();
        this.pockers = pockers;
        this.players = new ArrayList<Player>();
        this.console = new Scanner(System.in);
        this.playerNum = 0;
        this.handCardNum = handCardNum;
    }

    public List<Pocker> getCards() {
        return pockers;
    }

    public void setCards(List<Pocker> cards) {
        this.pockers = cards;
    }

    public List<Player> getPlayers() {
        return players;
    }

    public void setPlayers(List<Player> players) {
        this.players = players;
    }

    public Scanner getConsole() {
        return console;
    }

    public void setConsole(Scanner console) {
        this.console = console;
    }

    public int getPlayerNum() {
        return playerNum;
    }

    public void setPlayerNum(int playerNum) {
        this.playerNum = playerNum;
    }

    public int getHandCardNum() {
        return handCardNum;
    }

    public void setHandCardNum(int handCardNum) {
        this.handCardNum = handCardNum;
    }

    public void createPlayers() {
        while (true) {
            try {
                int maxPlayerNum = Pocker.CARDNUM/this.handCardNum;
                System.out.println("请输入大于1并且小于"+(maxPlayerNum+1)+"的玩家数量:");
                this.playerNum = this.console.nextInt();
                if(this.playerNum<=1this.playerNum>maxPlayerNum){
                    continue;
                }
                int id;
                String name;
                for (int i = 0; i < this.playerNum; i++) {
                    System.out.println("创建第" + (i + 1) + "位玩家的ID和姓名:");
                    while (true) {
                        try {
                            System.out.println("输入玩家id(int): ");
                            id = this.console.nextInt();
                            break;
                        } catch (Exception e) {
                            // TODO: handle exception
                            System.out.println("请输入整数类型的ID!");
                            this.console.next();
                        }
                    }
                    System.out.println("输入玩家姓名:");
                    name = this.console.next();
                    this.players.add(new Player(id, name));
                }
                for (int i = 0; i < this.playerNum; i++) {
                    System.out.println("欢迎玩家:" + this.players.get(i).getName());
                }
                break;
            } catch (Exception e) {
                // TODO: handle exception
                System.out.println("请输入整数类型的数量!");
                this.console.next();
            }
        }
    }

    public void sendPocker() {
        System.out.println("--------开始发牌--------");
        int cardNums = this.handCardNum * this.playerNum;
        Pocker pocker;
        for (int i = 0; i < cardNums; i++) {
            pocker = this.pockers.get(i);
            this.players.get(i % this.playerNum).getHandPockers().add(pocker);
        }
        System.out.println("--------发牌结束--------");
    }

    public void playGame() {
        System.out.println("--------开始游戏--------");
        Pocker maxPocker = new Pocker("方块", "2");
        int winPlayer = 0;
        for (int i = 0; i < this.playerNum; i++) {
            Collections.sort(this.players.get(i).getHandPockers()); // 对手牌进行排序
            Collections.reverse(this.players.get(i).getHandPockers()); // 排序后的手牌进行翻转,第一位为最大值
            System.out.println("玩家" + this.players.get(i).getName() + "最大手牌为:"
                    + this.players.get(i).getHandPockers().get(0).toString());
            if (maxPocker.compareTo(this.players.get(i).getHandPockers().get(0)) < 0) {
                maxPocker = this.players.get(i).getHandPockers().get(0);
                winPlayer = i;
            }
        }
        System.out.println("玩家" + this.players.get(winPlayer).getName() + "获胜");
        System.out.println("玩家各自的手牌为:");
        for (int i = 0; i < this.playerNum; i++) {
            System.out.println(this.players.get(i).getName() + ": " + this.players.get(i).getHandPockers().toString());
        }
    }

    public static void main(String[] args) {
        System.out.println("--------产生一副扑克牌--------");
        List<Pocker> pockers = Pocker.createPocker();
        System.out.println("--------扑克牌创建成功--------");
        System.out.println(pockers.toString());
        System.out.println("--------开始洗牌--------");
        Collections.shuffle(pockers);
        System.out.println("--------洗牌结束--------");
        System.out.println(pockers.toString());
        PlayPockers playPocker = new PlayPockers(pockers,3);
        System.out.println("--------创建玩家--------");
        playPocker.createPlayers();
        // 开始发牌
        playPocker.sendPocker();
        // 开始游戏
        playPocker.playGame();
    }
}

shoumananxun版本链接:链接描述

15人推荐
随时随地看视频
慕课网APP