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

扑克牌比较大小Demo,大家看看就好

标签:
Java

Player类

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Player {
    public String id;
    public String pid;
    public String name;
    public List<Pukepai> shoupai;

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

    }

    public Player(String pid, List<Pukepai>shoupai) {
        // TODO Auto-generated constructor stub
        this.pid = pid;
        this.shoupai = new ArrayList<Pukepai>();
    }

}

TestGeneric类

import java.io.Console;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.Set;

public class TestGeneric {
    static TestGeneric tg =new TestGeneric();
    public String h;
/**
 * 带有泛型的——Pukepai 的List类型属性
 */
    public List<Pukepai> pukepai; 
    public Map<String,Player> player;

    public TestGeneric(){

        this.pukepai = new ArrayList<Pukepai>();
        this.player = new HashMap<String, Player>();

    }

    /**
     * 
     * 添加2-10号牌
     */

    public void NumAdd(){

        for(int i=2;i<=10;i++)
        {
             h = Integer.toString(i);
            pukepai.add(new Pukepai("黑桃", h));
            pukepai.add(new Pukepai("红杏", h));
            pukepai.add(new Pukepai("梅花", h));
            pukepai.add(new Pukepai("方块", h));

        }

    }

    /**
     * 添加A-K号牌
     */
    public void CharAdd(){

        for(int i=0;i<=3;i++)
        {   
        if(i== 0){h = "A";}
        if(i== 1){h = "J";}
        if(i== 2){h = "Q";}
        if(i== 3){h = "K";}

            pukepai.add(new Pukepai("黑桃", h));
            pukepai.add(new Pukepai("红杏", h));
            pukepai.add(new Pukepai("梅花", h));
            pukepai.add(new Pukepai("方块", h));

        }

    }

    /**
     * 添加玩家
     * 若玩家Id未占用,则输入玩家姓名,并且添加到player中
     */
    public  void playeradd(){
        //获取输入
        Scanner sc = new Scanner(System.in);
        int i = 0;
        while(i<2){
            System.out.println("请输入玩家ID:");
            String ID = sc.next();
            //判断ID是否被占用
            Player pl = player.get(ID);
            if(pl == null){
                System.out.println("请输入玩家姓名:");
                String name = sc.next();
                //创建新的玩家对象
                Player newPlayer = new Player(ID,name);
                //通过调用player的put方法,添加ID-姓名映射
                player.put(ID, newPlayer);
                System.out.println(player.get(ID).name+"该玩家成功加入~!");
                i++;
            }else{
                System.out.println("该玩家ID已被占用!");
                continue;
            }

        }

    }

    /**
     * 
     * 循环遍历
     */
    public void foreach(){
        for(Pukepai h:pukepai){
            System.out.print(h.huase+":"+h.dianshu+" ");    }   
    }

    /**
     * KeySet方法返回Map中所有“键”的Set集合
     */
    public void KeySet(){
        Set<String> keySet =player.keySet();
        //遍历keyset,取得每一个键,再调用get方法取得每一个键对应的value
        for(String pid:keySet){
            Player pl =player.get(pid);
            if(pl!=null)
                System.out.println("玩家:"+pl.name+"准备开始");
        }

    }
    //打乱牌的顺序
    public void xipai(){
     Collections.shuffle(pukepai);    
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println("欢迎来到比大小游戏~~!");
        System.out.println("请添加玩家信息~~!");
        tg.playeradd();
        System.out.println("玩家添加成功~!");
        tg.KeySet();
        System.out.println("--------------输入start开始游戏--------------");
        Scanner input = new Scanner(System.in);
        String str = input.next();
        System.out.println("--------------正在创建初始牌组--------------");
        try{
            Thread.sleep(1000);
            }catch(Exception e){
            System.exit(0);//退出程序
            }
        tg.NumAdd();
        tg.CharAdd();
        tg.foreach();
        System.out.println(" ");
        System.out.println("--------------正在洗牌--------------");
        try{
            Thread.sleep(1000);
            tg.xipai();
            tg.foreach();
            }catch(Exception e){
            System.exit(0);//退出程序
            }
        System.out.println("--------------正在发牌--------------");
        try{
            Thread.sleep(1000);
            tg.fapai();
            }catch(Exception e){
            System.exit(0);//退出程序
            }
        System.out.println("结束");
    }

    //比较大小
    public void bijiao(){
        int amax;
        int bmax;

    }

//发牌
    public void fapai(){
        int count =0;
        Set<String> keySet =player.keySet();
        //遍历keyset,取得每一个键,再调用get方法取得每一个键对应的value
        for(String pid:keySet){

            Player pl = player.get(pid);
            Pukepai pk = (Pukepai)pukepai.get(0);
            Pukepai pk1 = (Pukepai)pukepai.get(2);
            Pukepai pk2 = (Pukepai)pukepai.get(1);
            Pukepai pk3 = (Pukepai)pukepai.get(3);
            if(pl!=null)
            {   if (count == 0){

                    System.out.println("ID为"+pl.id+"的玩家的手牌为:"+pk.huase.toString()+":"+pk.dianshu.toString()
                            +"  "+pk1.huase.toString()+":"+pk1.dianshu.toString());     
            }
            if(count == 1){ 

                    System.out.println("ID为"+pl.id+"的玩家的手牌为:"+pk2.huase.toString()+":"+pk2.dianshu.toString()
                            +"  "+pk3.huase.toString()+":"+pk3.dianshu.toString());
                    }
                count++;    

            }  

            //比较

            String Dmaxa,Hmaxa;
            String Dmaxb,Hmaxb;

        }           

    }

}

Pukepai类


public class Pukepai {
public String huase;
public String dianshu;

public Pukepai(String huase, String dianshu) {
    this.huase = huase;
    this.dianshu = dianshu;
    }
}
点击查看更多内容
7人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消