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

Java入门 第三季 7-1 简易扑克牌游戏

标签:
Java 设计

欢迎交流 (⊙o⊙)

效果图:

图片描述

/**
 * 扑克牌类
 * @author Anders
 *
 */
public class Poker implements Comparable<Poker>{
    public int num;
    public int style;

    public Poker(int num,int style){
        this.num = num;
        this.style = style;
    }
    @Override
    public int compareTo(Poker o) {
        if(num>o.num){
            return 1;
        }else if(num==o.num){
            return style > o.style ? 1:(style==o.style?0:-1);
        }else{
            return -1;
        }
    }

    @Override
    public String toString() {
        String sty = null;
        String n = null;
        switch (style) {
        case 0:
            sty = "方片";
            break;
        case 1:
            sty = "梅花";
            break;
        case 2:
            sty = "红桃";
            break;
        case 3:
            sty = "黑桃";
            break;
        }
        switch (num) {
        case 9:
            n = "J";
            break;
        case 10:
            n = "Q";
            break;
        case 11:
            n = "K";
            break;
        case 12:
            n = "A";
            break;

        default:
            n = String.valueOf(num+2);
            break;
        }
        return sty + n;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + num;
        result = prime * result + style;
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Poker other = (Poker) obj;
        if (num != other.num)
            return false;
        if (style != other.style)
            return false;
        return true;
    }
}
import java.util.ArrayList;
import java.util.List;

/**
 * 扑克玩家
 * @author Anders
 *
 */
public class PokerMan {
    public int id;
    public String name;
    public List<Poker> list = new ArrayList<Poker>();

    public PokerMan(){}
    public PokerMan(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;
    }

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

/**
 * 扑克游戏入口
 * @author Anders
 *
 */
public class PokerGame {
    public static void main(String[] args) {
        List<Poker> pList = new ArrayList<Poker>();
        List<Poker> pmList = new ArrayList<Poker>();
        PokerMan[] pms = new PokerMan[2];
        createPoker(pList);
        System.out.println("==========牌已创建=======");
        System.out.println("==========开始洗牌===========");
        shuffle(pList);
        System.out.println("==========洗牌结束===========");
        for (Poker poker : pList) {
            if(pList.indexOf(poker)!=0&&pList.indexOf(poker)%8==7){
                System.out.println(poker + "\t");
            }else{
                System.out.print(poker+"\t");
            }
        }
        System.out.println();
        PokerMan pm = null;
        Scanner input = new Scanner(System.in);
        for (int i = 0; i < 2; i++) {
            pm = new PokerMan();
            System.out.println("请输入玩家"+(i+1)+"的id:");
            try {
                pm.setId(input.nextInt());
            } catch (Exception e) {
                System.out.println("输入错误,请输入整数类型的id:");
                input.next();
                i--;
                continue;
            }
            System.out.println("请输入玩家"+(i+1)+"的名字:");
            pm.setName(input.next());
            pms[i] = pm;
        }
        System.out.println("==========开始发牌===========");
        dispatch(pList,pms[0], pms[1], pmList);
        System.out.println("==========发牌结束===========");
        Collections.sort(pmList);
        for (PokerMan pokerMan : pms) {
            if(pokerMan.list.contains(pmList.get(pmList.size()-1))){
                System.out.println(pokerMan.name+"赢了,手牌最大是:"+pmList.get(pmList.size()-1));
            }
        }
        for (PokerMan pokerMan : pms) {
            System.out.println(pokerMan.name+"的手牌是:");
            for (Poker poker : pokerMan.list) {
                System.out.print(poker+"\t");
            }
            System.out.println();
        }
        input.close();
    }
    /**
     * 创建手牌
     * @param list
     */
    public static void createPoker(List<Poker> list){
        Poker p = null;
        for(int i=0;i<13;i++){
            for(int j=0;j<4;j++){
                p = new Poker(i,j);
                list.add(p);
            }
            p=null;
        }
    }
    /**
     * 洗牌
     * @param list
     */
    public static void shuffle(List<Poker> list){
        int i = list.size()-1;
        Random rd = new Random();
        while (i-->1) {
            Collections.swap(list, i, rd.nextInt(i));
        }
    }
    /**
     * 发牌
     * @param list
     * @param pm1
     * @param pm2
     * @param pmList
     */
    public static void dispatch(List<Poker> list,PokerMan pm1,PokerMan pm2,List<Poker> pmList){
        for (int i = 0; i < 2; i++) {
            pm1.list.add(list.get(i));
            pm2.list.add(list.get(i+2));
            pmList.add(list.get(i));
            pmList.add(list.get(i+2));
        }
    }
}
点击查看更多内容
8人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消