手记

java入门第三季-扑克游戏原创代码

package com.exer.playcard;

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

public class Card implements Comparable<Card> {
    private String color;// 扑克牌的颜色
    private String number;// 扑克牌的点数
    public static List<String> cards;// 存放52张扑克牌
    public static List<String> numList;// 存放2-10,J,Q,K,A。下标代表点数的大小
    public static List<String> colorList;// 存放颜色 黑红梅方 下标代表等级高低
    // 通过colorList集合返回纸牌颜色的下标(代表优先级)

    public int getColor() {
        return colorList.indexOf(color);
    }

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

    // 通过numList集合 返回number对应的下标用来比较点数大小
    public int getNumber() {
        return numList.indexOf(number);
    }

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

    /**
     * 静态初始化块 初始化颜色 黑红梅方集合 下标大代表优先级高 初始化数字集合 下标大 代表扑克点数大
     */
    static {
        System.out.println("-----------------创建扑克牌-----------------------");
        // 将52张牌放到cards集合中
        cards = new ArrayList<>();
        String str = "黑桃2,黑桃3,黑桃4,黑桃5,黑桃6,黑桃7,黑桃8,黑桃9,黑桃10,黑桃J,黑桃Q,黑桃K,黑桃A,"
                + "红桃2,红桃3,红桃4,红桃5,红桃6,红桃7,红桃8,红桃9,红桃10,红桃J,红桃Q,红桃K,红桃A,"
                + "梅花2,梅花3,梅花4,梅花5,梅花6,梅花7,梅花8,梅花9,梅花10,梅花J,梅花Q,梅花K,梅花A,"
                + "方片2,方片3,方片4,方片5,方片6,方片7,方片8,方片9,方片10,方片J,方片Q,方片K,方片A";
        String[] strs = str.split(",");
        cards.addAll(Arrays.asList(strs));
        // 初始化数字集合
        numList = new ArrayList<>();
        for (int i = 2; i <= 10; i++) {
            numList.add(Integer.toString(i));
        }
        numList.add("J");
        numList.add("Q");
        numList.add("K");
        numList.add("A");
        // 初始化颜色集合
        colorList = new ArrayList<>();
        colorList.add("方片");
        colorList.add("梅花");
        colorList.add("红桃");
        colorList.add("黑桃");
        System.out.println("------------------扑克牌创建成功-------------------");
        int count=0;
        for(int i=0;i<cards.size();i++){
            System.out.print(cards.get(i)+",");
            count++;
            if(count==13){
                System.out.println();
                count=0;
            }
        }
    }

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

    public Card() {
        super();
    }

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

    /**
     * 先按牌的点数比较 点数一样再按花色比较
     */
    @Override
    public int compareTo(Card o) {
        if (this.getNumber() > o.getNumber()) {
            return 1;
        } else if (this.getNumber() < o.getNumber()) {
            return -1;
        } else {
            return this.getColor()-o.getColor();
        }

    }

}
package com.exer.playcard;

import java.util.ArrayList;
import java.util.List;
/**
 * 扑克牌玩家
 * @author Administrator
 *@value ID 玩家ID
 *@value name  玩家姓名
 *@value cards  玩家的手牌
 */
public class Player {
    private Integer ID;//玩家ID
    private String name;//玩家姓名
    private List<Card> cards=new ArrayList<>();//玩家手牌
    public Player(Integer iD, String name) {
        super();
        ID = iD;
        this.name = name;
    }
    public Player() {
        super();
    }
    public Integer getID() {
        return ID;
    }
    public void setID(Integer iD) {
        ID = iD;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public List<Card> getCards() {
        return cards;
    }
    public void setCards(List<Card> cards) {
        this.cards = cards;
    }
    /**
     * 玩家信息如 Tom:[红桃A,黑桃2]
     */
    @Override
    public String toString() {
        StringBuilder  info=new StringBuilder(name+":[");
        for(int i=0;i<cards.size();i++){
            info=info.append(cards.get(i));
            if(i!=cards.size()-1){
                info.append(",");
            }
        }
        info.append("]");
        return info.toString();
    }

}
package com.exer.playcard;

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

public class PlayCardGame {
    // 给玩家随机发牌
    public void sendCard(Player p1, Player p2) {
        System.out.println("--------------开始发牌——------------");
        Collections.shuffle(Card.cards);
        List<Integer> indexList = new ArrayList<>();
        while (indexList.size() < 4) {
            Integer index = (int) (Math.random() * 52);
            if (indexList.contains(index)) {
                continue;
            } else
                indexList.add(index);
        }
        Collections.shuffle(indexList);
        for (int i = 0; i < indexList.size(); i++) {
            String card = Card.cards.get(indexList.get(i));
            String color = card.substring(0, 2);
            String number = card.substring(2);
            Card c = new Card(color, number);
            if (i % 2 == 0) {
                System.out.println("-----------玩家:"+p1.getName()+"-拿牌");
                p1.getCards().add(c);
            } else{
                System.out.println("-----------玩家:"+p2.getName()+"-拿牌");
                p2.getCards().add(c);
            }
        }
        Collections.sort(p1.getCards());//对p1玩家的手牌进行排序
        Collections.sort(p2.getCards());//对p2玩家的手牌进行排序
        System.out.println("-------------发牌结束-------------------");
    }
    //创建玩家
    public void creatPlayer(Player p,int i){
        Scanner input = new Scanner(System.in);
        boolean flag = true;
        while (flag) {
            System.out.println("请输入第"+i+"位玩家的ID和姓名:");
            System.out.println("请输入ID:");
            try {
                p.setID(input.nextInt());
                input.nextLine();//读取换行符,避免影响下次输入
                System.out.println("输入姓名:");
                p.setName(input.nextLine());
                flag=false;
            } catch (Exception e) {
                System.out.println("请输入整数类型的ID");
                if(input.hasNext()){
                    input.nextLine();
                }
            }
        }
    }
    public static void main(String[] args) {
        Card card = new Card();
        System.out.println("-------------开始洗牌-----------");
        Collections.shuffle(Card.cards);
        System.out.println("-------------洗牌结束-----------");
        System.out.println("-------------创建玩家-----------");
        int i=1;//表示第i个玩家
        PlayCardGame pcg=new PlayCardGame();
        Player p=new Player();
        pcg.creatPlayer(p, i++);
        Player p2=new Player();
        pcg.creatPlayer(p2, i++);
        System.out.println("-----------欢迎"+p.getName());
        System.out.println("-----------欢迎"+p2.getName());
        pcg.sendCard(p, p2);
        System.out.println("---------------开始发牌---------------");
        System.out.println("玩家:"+p.getName()+"最大的手牌为:"+p.getCards().get(1));
        System.out.println("玩家:"+p2.getName()+"最大的手牌为:"+p2.getCards().get(1));
        if(p.getCards().get(1).compareTo(p2.getCards().get(1))>0){
            System.out.println("----------------玩家"+p.getName()+"赢------------------");
        }else{
            System.out.println("----------------玩家"+p2.getName()+"赢------------------");
        }
        System.out.println("玩家各自的手牌为:");
        System.out.println(p);
        System.out.println(p2);
    }

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