ITPub博客

首页 > IT职业 > IT图书 > List扑克牌游戏

List扑克牌游戏

IT图书 作者:shinez 时间:2021-09-09 09:33:25 0 删除 编辑
package edu.eurasia.homework.poker;

import java.util.Comparator;

/**
 * 类名: Compara
 * 功能:(对扑克牌大小比较的排序规则)
 * 作者:黄振敏 
 * 日期:2018/7/17  */ public class Compara implements Comparator {     @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(result0){             return 1;         }else {             int result2=color.indexOf(o1.color)-color.indexOf(o2.color);             if(result2>0){                 return 1;             }else if(result  * 功能:(玩家手中的扑克牌实体类)
 * 作者:黄振敏 
 * 日期: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
 * 功能:(扑克牌实体类)
 * 作者:黄振敏 
 * 日期: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
 * 功能:(测试用例)
 * 作者:黄振敏 
 * 日期:2018/7/16  */ public class Test {     public Scanner scanner;     public List pokerentityList;     public List 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  play1 = new ArrayList();         List 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 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();     } }


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3127/viewspace-2804652/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: 使用Admin监控
请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    0
  • 访问量
    932