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

JAVA入门第三季实战:简易扑克牌

标签:
Java

本程序使用了ArrayList 、HashMap,简单应用了Java第三季的集合框架

首先创建一个卡牌类

/**
 * 扑克类
 */
public class Poker {
    public Poker(String size,String number){
        this.size=size;
        this.number=number;
    }
    public String getSize(){
        return this.size;
    }
    public String getNumber(){
        return this.number;
    }
    private String size;
    private String number;
}

然后创建一个卡牌玩家类

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

/**
 * 打牌人员类
 */
public class PokerPlayer {
    public PokerPlayer(String id,String name){
        this.id=id;
        this.name=name;
        this.playerpoker = new ArrayList<>();
    }

    public String getId(){
         return this.id;
    }
    public String getName(){
        return this.name;
    }

    private String id;
    private String name;
    public List<Poker> playerpoker;
    public String a;
}

最后一个就大杂烩了,流程如下:创建卡牌--遍历卡牌--创建玩家--发牌--比较卡牌;
创建卡牌用ArrayList
遍历卡牌用foreach方法
创建玩家用到了HashMap
发牌过程较简单,仅为随机不重复四个数字
卡牌比较可以扩展一下,我写的比较简单了

import java.util.*;

public class PokerList {
    private List<Poker> poker;
    private Map<Integer,PokerPlayer> player ;
    private int a1,a2,b1,b2;//用来比较
    Scanner in = new Scanner(System.in);
    public PokerList (){
        this.poker = new ArrayList<>();
        this.player = new HashMap<>();
    }
    public void addPoker(){
        System.out.println("正在创建卡牌......");
        String[] size={"黑桃","红桃","梅花","方块"};
        String[] number={"A","2","3","4","5","6","7","8","9","10","J","Q","k"};
        for (String a:size) {
            for (String b:number) {
                Poker apoker = new Poker(a,b);
                poker.add(apoker);
            }
        }

    }
    public void pokerforeach(){
        System.out.print("已创建卡牌: ");
        for (Poker pok:poker) {
            System.out.print(pok.getSize()+pok.getNumber()+", ");
        }
        System.out.println("\n正在洗牌......");
    }

    public void creatPlayer(){
        int i=0;
        while (i<2)
        {   System.out.println("输入玩家id");
            String ID = in.next();
            PokerPlayer pper=  player.get(i);//返回指定键映射的值这里为Student
            if (pper==null)
            {   System.out.println("输入玩家姓名");
                String NAME= in.next();
                PokerPlayer newpper = new  PokerPlayer(ID,NAME);
                player.put(i,newpper);
                System.out.println(player.get(i).getName());
                i++;
            }
            else{ System.out.println("该ID已被占用");}

        }

    }
    public void shuttle(){

        System.out.println("正在发牌...");
        Random rd = new Random();
        //System.out.println(rd.nextInt(52));
        int[] four = {-1,-2,-3,-4};
        for(int i=0;i<4;i++){
            do{
                four[i]=rd.nextInt(52);
            }while( (four[0]==four[1])&&
                    (four[0]==four[2])&&
                    four[0]==four[3]&&
                    four[1]==four[2]&&
                    four[1]==four[3]&&
                    four[2]==four[3]);
            //System.out.println("this is "+four[i]);
            //String a = String.valueOf(four[1]};

        }
        a1=four[0]%13;
        a2=four[1]%13;
        b1=four[2]%13;
        b2=four[3]%13;

        PokerPlayer pp1=player.get(0);
        pp1.playerpoker.add((Poker)poker.get(four[0]));
        pp1.playerpoker.add((Poker)poker.get(four[3]));
        System.out.println("玩家:"+pp1.getName()+"的手牌:"+
            pp1.playerpoker.get(0).getSize()+
            pp1.playerpoker.get(0).getNumber()+
            pp1.playerpoker.get(1).getSize()+
            pp1.playerpoker.get(1).getNumber());

        PokerPlayer pp2=player.get(1);
        pp2.playerpoker.add((Poker)poker.get(four[1]));
        pp2.playerpoker.add((Poker)poker.get(four[2]));
        System.out.println("玩家:"+pp2.getName()+"的手牌:"+
            pp2.playerpoker.get(0).getSize()+
            pp2.playerpoker.get(0).getNumber()+
            pp2.playerpoker.get(1).getSize()+
            pp2.playerpoker.get(1).getNumber());

    }

    public void comparison( )
    {
        int amax,bmax;
        String max;
        if (a1>a2)
        amax=a1;
        else amax=a2;
        if (b1>b2)
        bmax=b1;
        else bmax=b2;
        if (amax>bmax)
        System.out.println("玩家一胜利");
        else System.out.println("玩家二胜利");
    }

    public static void main(String[] args){
        PokerList pl= new PokerList();
        pl.addPoker();//创建卡牌
        pl.pokerforeach();//遍历卡牌
        pl.creatPlayer();//创建玩家
        pl.shuttle();//发牌
        pl.comparison();

    }

}
点击查看更多内容
4人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消