继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

List扑克牌游戏

慕粉15858822902
关注TA
已关注
手记 2
粉丝 0
获赞 4
package edu.eurasia.homework.poker;

import java.util.Comparator;

/**
 * 类名: Compara<br>
 * 功能:(对扑克牌大小比较的排序规则)<br>
 * 作者:黄振敏 <br>
 * 日期:2018/7/17
 */
public class Compara implements Comparator<Pokerentity> {
    @Override
    public int compare(Pokerentity o1, Pokerentity o2) {
        String color="黑桃  红桃 梅花 方块";
        String numbers="2 3 4 5 6 7 8 9 10 J Q K A";

        int result=numbers.indexOf(o1.number)-numbers.indexOf(o2.number);

        if(result<0){
            return -1;
        }else if(result>0){
            return 1;
        }else {
            int result2=color.indexOf(o1.color)-color.indexOf(o2.color);
            if(result2>0){
                return 1;
            }else if(result<0){
                return -1;
            }else{
                return 0;
            }
        }
    }
    }
    package edu.eurasia.homework.poker;

/**
 * 类名: Mypoker<br>
 * 功能:(玩家手中的扑克牌实体类)<br>
 * 作者:黄振敏 <br>
 * 日期:2018/7/16
 */
public class Mypoker {

    public String number;
    public String color;
    public Mypoker(){

    }
    public Mypoker(String number, String color){

        this.color = color;

        this.number = number;
    }

}
 package edu.eurasia.homework.poker;



/**
 * 类名: Pokerentity<br>
 * 功能:(扑克牌实体类)<br>
 * 作者:黄振敏 <br>
 * 日期:2018/7/16
 */
public class Pokerentity {
    public String number;
    public String color;


    public Pokerentity(){

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

    }


}   
package edu.eurasia.homework.poker;

import java.util.*;

/**
 * 类名: Test<br>
 * 功能:(测试用例)<br>
 * 作者:黄振敏 <br>
 * 日期:2018/7/16
 */
public class Test {
    public Scanner scanner;
    public List<Pokerentity> pokerentityList;
    public List<Mypoker> mypokers;


    public Test(){
        this.pokerentityList = new ArrayList<>();
        this.mypokers = new ArrayList<>();
        scanner = new Scanner(System.in);
    }

    /**
     * 初始化扑克牌
     */
    public void initPoker(){
        System.out.println("------------创建扑克牌----------");
        int n = -1;
       String[] colors = {"黑桃","红桃","梅花","方块"};
       String[] numbers = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

       for (int i = 0; i < numbers.length; i++){
           for (int j = 0; j < colors.length; j++){
               pokerentityList.add(new Pokerentity(numbers[i],colors[j]));
           }

       }

        for (Pokerentity poker: pokerentityList
             ) {
           if (n%10==9){
               System.out.println();
           }
           n++;
            System.out.print("[" + poker.color + poker.number + "]");
        }
    }

    /**
     * 洗牌并取牌
     */
    public void randomPoker(){
        System.out.println();
        System.out.println("------------------开始洗牌--------------");
        for (int i = 0;i < 4;i++) {
            Pokerentity putout = new Pokerentity();
            Random random = new Random();
            do {
                putout = pokerentityList.get(random.nextInt(pokerentityList.size()));

            } while (mypokers.contains(putout.color) && mypokers.contains(putout.number));
            Mypoker mypoker = new Mypoker(putout.number,putout.color);
            mypokers.add(mypoker);
        }
        System.out.println("-------------------洗牌结束----------------");
//        for (Mypoker my:mypokers
//             ) {
//            System.out.println("[" + my.color + my.number + "]");
//        }

    }

    /**
     * 输入玩家对象
     */
    public int p1;
    public int p2;
    public String pName1;
    public String pName2;
    public void putPlay(){
        System.out.println("-----------------创建玩家--------------");

        while (true) {
            try {
                //玩家1
            System.out.println("请输入玩家1的ID");
            int playId1 = Integer.parseInt(scanner.nextLine().trim());
            System.out.println("请输入玩家1的姓名");
            String playName1 = scanner.next();
            p1 = playId1;
            pName1 = playName1;
            break;

            }catch (NumberFormatException e){
                System.out.println("请输入整数ID!!!!");
                System.out.println("----------------------");
                continue;
            }
        }
        while (true){
            try {
                //玩家2
                System.out.println("请输入玩家2的ID");
                int playId2 = Integer.parseInt(scanner.nextLine().trim());
                System.out.println("请输入玩家2的姓名");
                String playName2 = scanner.nextLine();
                p2 = playId2;
                pName2 = playName2;
                break;
            }catch (NumberFormatException e){

                System.out.println("请输入整数ID!!!!");
                System.out.println("----------------------");
                continue;
            }

        }
        System.out.println("-------------欢迎玩家" + pName1 + "------------");
        System.out.println("-------------欢迎玩家" + pName2 + "------------");
    }
        /**
         * 给玩家1和玩家2传入洗牌后的扑克牌
         */
    public void toPlay(){
        System.out.println("---------------开始发牌---------------");
        int i=1;
        List<Pokerentity> play1 = new ArrayList<>();
        List<Pokerentity> play2 = new ArrayList<>();
        for (Mypoker my:mypokers
                ) {
            if(i%2!=0) {

                System.out.println("玩家:"+pName1+"-拿牌");
                play1.add(new Pokerentity(my.number,my.color));
            }else {

                System.out.println("玩家:"+pName2+"-拿牌");
                play2.add(new Pokerentity(my.number,my.color));
            }
            i++;

        }
        System.out.println("---------------发牌结束------------");
        System.out.println("---------------开始游戏------------");


        List<Pokerentity> lastPoker=new ArrayList<>();

        Collections.sort(play1,new Compara());
        Collections.sort(play2,new Compara());

        System.out.println("玩家" + pName1 + "最大的手牌为:[" + play1.get(1).color + play1.get(1).number + "]");
        System.out.println("玩家" + pName2 + "最大的手牌为:[" + play2.get(1).color + play2.get(1).number + "]");

        lastPoker.add(play1.get(1));
        lastPoker.add(play2.get(1));

        Collections.sort(lastPoker,new Compara());

        System.out.println("最大的牌是[" + lastPoker.get(1).color + lastPoker.get(1).number + "]");

        if(lastPoker.get(1).equals(play1.get(1))){
            System.out.println("----------------玩家:" + pName1 + "获胜----------------");
        }else {
            System.out.println("----------------玩家:" + pName2 + "获胜----------------");

        }
        //输出各个玩家的手牌
        System.out.println("---------------玩家各自的手牌为--------------");
        System.out.println(pName1 + ":[" + play1.get(0).color + play1.get(0).number + "," + play1.get(1).color +
                play1.get(1).number + "]");
        System.out.println(pName2 + ":[" + play2.get(0).color + play2.get(0).number + "," + play2.get(1).color +
                play2.get(1).number + "]");

    }
    

    public static void main(String[] args){
        Test test = new Test();

        test.initPoker();
        test.randomPoker();
        test.putPlay();
        test.toPlay();
    }
}


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP