问答详情
源自:7-1 简易扑克牌游戏

晒一下自己的,貌似比较玩家最大牌友点问题,有木有可以指出一下,是啥问题?

package com.imooc;
import java.util.ArrayList;
import java.util.List;
public class Person {
   public String id;
   public String name;
   public List<Poker> ownPoker = new ArrayList<Poker>();
   public Person(String id, String name){
       this.id = id;
       this.name = name;
       this.ownPoker = new ArrayList<Poker>();
   }
}


提问者:zzl7 2016-01-20 00:38

个回答

  • zzl7
    2016-01-20 00:50:12

    package com.imooc;
    
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.List;
    /**
     * 比较类
     *
     */
    public class ComparatorPoker implements Comparator<Poker> {
    
        @Override
        public int compare(Poker o1, Poker o2) {
            List<String> flowerColor =Arrays.asList("方块", "梅花", "红桃", "黑桃");
            List<String> cardNum = Arrays.asList("2", "3", "4", "5", "6", "7", "8",
                    "9", "J", "Q", "K", "A");
            if(cardNum.indexOf(o1.cardVal) == cardNum .indexOf(o2.cardVal)){
                return ((Integer)flowerColor.indexOf(o1.cardColor)).compareTo((Integer)flowerColor.indexOf(o2.cardColor));
            }else{
                return ((Integer)cardNum.indexOf(o1.cardVal)).compareTo((Integer)cardNum.indexOf(o2.cardVal));
            }
            // TODO Auto-generated method stub
            
        }
    
    }


  • zzl7
    2016-01-20 00:47:40

    package com.imooc;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.List;
    import java.util.Scanner;
    
    /**
     * 游戏类
     */
    public class PlayGame {
        public List<Poker> pokers = new ArrayList<Poker>();
        public List<Person> players= new ArrayList<Person>();
        private   String[] cardVals = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        private static String[] cardColors = {"红桃","黑桃","梅花","方块"}; 
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            PlayGame pg = new PlayGame();
            System.out.println("------创建扑克牌------");
            pg.createPoker();
            System.out.println("--------创建玩家---------");
            pg.createPlayer();
            pg.shuffer();
            System.out.println("-------开始发牌--------");
            pg.deal();
            pg.comparePoker();
        }
        
        /**
         * 创建扑克牌
         */
       public void createPoker(){
           for(int i=0; i<cardColors.length;i++){
               String cardColor = cardColors[i];
               for(int j=0; j<cardVals.length; j++){
                   String cardVal =  cardVals[j];
                   pokers.add(new Poker(cardVal,cardColor));
               }
          }
          System.out.print("为[");
          for(Poker poker:pokers){
          System.out.print(poker.cardColor+poker.cardVal+ ",");
          }
          System.out.print("]");
       }
       /**
        * 洗牌
        */
       public void shuffer(){
           System.out.println("-------开始洗牌--------");
           Collections.shuffle(pokers);
           System.out.println("-------结束洗牌--------");
           
           // 测试洗牌是否成功
          /* for(Poker poker:pokers){
               System.out.print(poker.cardColor+poker.cardVal);
           }*/
       }
       
       /**
        * 发牌
        */
       public void deal(){
         int j = 0;
         for(int i=0; i<2; i++){
             for(Person player:players){
                 System.out.println("玩家"+player.name+"拿牌");
                 Poker poker = pokers.get(j);
                 player.ownPoker.add(poker);
                 j++;
               } 
          }
         for(Person player:players){
             System.out.print("玩家"+player.name+"拥有");
             for(Poker poker:player.ownPoker){
                System.out.print(poker.cardColor+poker.cardVal);
             }
             System.out.println();
         }
       }
       
     /**
      *比较牌的大小
      */
       public void comparePoker(){
           ComparatorPoker cpr = new ComparatorPoker();
           
           //1.先比较玩家自己手里的牌;
             for(Person player:players){
               Collections.sort(player.ownPoker, cpr);
               System.out.println("玩家:"+player.name+"最大的牌为"+
                    player.ownPoker.get(player.ownPoker.size()-1).cardColor+
                    player.ownPoker.get(player.ownPoker.size()-1).cardVal);
             }
           //2.比较对手玩家最大的牌;
             System.out.println(cpr.compare(players.get(0).ownPoker.get(0), players.get(1).ownPoker.get(0)));
              if(cpr.compare(players.get(0).ownPoker.get(0), players.get(1).ownPoker.get(0))>0){
                 System.out.println("玩家"+players.get(0).name+"获胜"); 
              }else{
                  System.out.println("玩家"+players.get(1).name+"获胜"); 
              } 
       }
       
       /**
        * 创建玩家
        */
      public void createPlayer(){
         
         for(int i=0; i<2; i++){
             System.out.println("请输入"+(i+1)+"位玩家的ID");
             
             Boolean flag =false;
             
             while(true){
                 try{
                     Scanner sc = new Scanner(System.in);
                     String playerId;
                      playerId = sc.nextInt()+"";
                      System.out.println("请输入"+(i+1)+"位玩家的姓名");
                      String playerName = sc.next();
                      players.add(new Person(playerId,playerName));
                      break;
                     }catch(Exception e){
                     System.out.println("您输入的不是整数,请重新输入");
                     continue;
                 } 
             } 
         }
         System.out.println("成功添加");
         for(Person player:players){
             System.out.println("欢迎玩家:"+player.id+":"+player.name);
         }
      }
      
    }


  • zzl7
    2016-01-20 00:47:02

    package com.imooc;
    
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.List;
    
    public class ComparatorPoker implements Comparator<Poker> {
    
        @Override
        public int compare(Poker o1, Poker o2) {
            List<String> flowerColor =Arrays.asList("方块", "梅花", "红桃", "黑桃");
            List<String> cardNum = Arrays.asList("2", "3", "4", "5", "6", "7", "8",
                    "9", "J", "Q", "K", "A");
            if(cardNum.indexOf(o1.cardVal) == cardNum .indexOf(o2.cardVal)){
                return ((Integer)flowerColor.indexOf(o1.cardColor)).compareTo((Integer)flowerColor.indexOf(o2.cardColor));
            }else{
                return ((Integer)cardNum.indexOf(o1.cardVal)).compareTo((Integer)cardNum.indexOf(o2.cardVal));
            }
            // TODO Auto-generated method stub
            
        }
    
    }


  • zzl7
    2016-01-20 00:46:18

    package com.imooc;

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

    /**
     * 游戏类
     */
    public class PlayGame {
        public List<Poker> pokers = new ArrayList<Poker>();
        public List<Person> players= new ArrayList<Person>();
        private   String[] cardVals = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
        private static String[] cardColors = {"红桃","黑桃","梅花","方块"};
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            PlayGame pg = new PlayGame();
            System.out.println("------创建扑克牌------");
            pg.createPoker();
            System.out.println("--------创建玩家---------");
            pg.createPlayer();
            pg.shuffer();
            System.out.println("-------开始发牌--------");
            pg.deal();
            pg.comparePoker();
        }
        
        /**
         * 创建扑克牌
         */
       public void createPoker(){
           for(int i=0; i<cardColors.length;i++){
               String cardColor = cardColors[i];
               for(int j=0; j<cardVals.length; j++){
                   String cardVal =  cardVals[j];
                   pokers.add(new Poker(cardVal,cardColor));
               }
          }
          System.out.print("为[");
          for(Poker poker:pokers){
          System.out.print(poker.cardColor+poker.cardVal+ ",");
          }
          System.out.print("]");
       }
       /**
        * 洗牌
        */
       public void shuffer(){
           System.out.println("-------开始洗牌--------");
           Collections.shuffle(pokers);
           System.out.println("-------结束洗牌--------");
           
           // 测试洗牌是否成功
          /* for(Poker poker:pokers){
               System.out.print(poker.cardColor+poker.cardVal);
           }*/
       }
       
       /**
        * 发牌
        */
       public void deal(){
         int j = 0;
         for(int i=0; i<2; i++){
             for(Person player:players){
                 System.out.println("玩家"+player.name+"拿牌");
                 Poker poker = pokers.get(j);
                 player.ownPoker.add(poker);
                 j++;
               }
          }
         for(Person player:players){
             System.out.print("玩家"+player.name+"拥有");
             for(Poker poker:player.ownPoker){
                System.out.print(poker.cardColor+poker.cardVal);
             }
             System.out.println();
         }
       }
       
     /**
      *比较牌的大小
      */
       public void comparePoker(){
           ComparatorPoker cpr = new ComparatorPoker();
           
           //1.先比较玩家自己手里的牌;
             for(Person player:players){
               Collections.sort(player.ownPoker, cpr);
               System.out.println("玩家:"+player.name+"最大的牌为"+
                    player.ownPoker.get(player.ownPoker.size()-1).cardColor+
                    player.ownPoker.get(player.ownPoker.size()-1).cardVal);
             }
           //2.比较对手玩家最大的牌;
             System.out.println(cpr.compare(players.get(0).ownPoker.get(0), players.get(1).ownPoker.get(0)));
              if(cpr.compare(players.get(0).ownPoker.get(0), players.get(1).ownPoker.get(0))>0){
                 System.out.println("玩家"+players.get(0).name+"获胜");
              }else{
                  System.out.println("玩家"+players.get(1).name+"获胜");
              }
       }
       
       /**
        * 创建玩家
        */
      public void createPlayer(){
        
         for(int i=0; i<2; i++){
             System.out.println("请输入"+(i+1)+"位玩家的ID");
            
             Boolean flag =false;
            
             while(true){
                 try{
                     Scanner sc = new Scanner(System.in);
                     String playerId;
                      playerId = sc.nextInt()+"";
                      System.out.println("请输入"+(i+1)+"位玩家的姓名");
                      String playerName = sc.next();
                      players.add(new Person(playerId,playerName));
                      break;
                     }catch(Exception e){
                     System.out.println("您输入的不是整数,请重新输入");
                     continue;
                 }
             }
         }
         System.out.println("成功添加");
         for(Person player:players){
             System.out.println("欢迎玩家:"+player.id+":"+player.name);
         }
      }
     
    }

  • zzl7
    2016-01-20 00:41:58

    package com.imooc;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Scanner;/** * 游戏类 */public class PlayGame {    public List<Poker> pokers = new ArrayList<Poker>();    public List<Person> players= new ArrayList<Person>();    private   String[] cardVals = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};    private static String[] cardColors = {"红桃","黑桃","梅花","方块"};     public static void main(String[] args) {        // TODO Auto-generated method stub        PlayGame pg = new PlayGame();        System.out.println("------创建扑克牌------");        pg.createPoker();        System.out.println("--------创建玩家---------");        pg.createPlayer();        pg.shuffer();        System.out.println("-------开始发牌--------");        pg.deal();        pg.comparePoker();    }        /**     * 创建扑克牌     */   public void createPoker(){       for(int i=0; i<cardColors.length;i++){           String cardColor = cardColors[i];           for(int j=0; j<cardVals.length; j++){               String cardVal =  cardVals[j];               pokers.add(new Poker(cardVal,cardColor));           }      }      System.out.print("为[");      for(Poker poker:pokers){      System.out.print(poker.cardColor+poker.cardVal+ ",");      }      System.out.print("]");   }   /**    * 洗牌    */   public void shuffer(){       System.out.println("-------开始洗牌--------");       Collections.shuffle(pokers);       System.out.println("-------结束洗牌--------");              // 测试洗牌是否成功      /* for(Poker poker:pokers){           System.out.print(poker.cardColor+poker.cardVal);       }*/   }      /**    * 发牌    */   public void deal(){     int j = 0;     for(int i=0; i<2; i++){         for(Person player:players){             System.out.println("玩家"+player.name+"拿牌");             Poker poker = pokers.get(j);             player.ownPoker.add(poker);             j++;           }       }     for(Person player:players){         System.out.print("玩家"+player.name+"拥有");         for(Poker poker:player.ownPoker){            System.out.print(poker.cardColor+poker.cardVal);         }         System.out.println();     }   }    /**  *比较牌的大小  */   public void comparePoker(){       ComparatorPoker cpr = new ComparatorPoker();              //1.先比较玩家自己手里的牌;         for(Person player:players){           Collections.sort(player.ownPoker, cpr);           System.out.println("玩家:"+player.name+"最大的牌为"+                player.ownPoker.get(player.ownPoker.size()-1).cardColor+                player.ownPoker.get(player.ownPoker.size()-1).cardVal);          /* System.out.println("排序后");           for(Poker poker:player.ownPoker){               System.out.println("玩家"+player.name+"手中的牌"+poker.cardColor+poker.cardVal);           }*/         }       //2.比较对手玩家最大的牌;         System.out.println(cpr.compare(players.get(0).ownPoker.get(0), players.get(1).ownPoker.get(0)));          if(cpr.compare(players.get(0).ownPoker.get(0), players.get(1).ownPoker.get(0))>0){             System.out.println("玩家"+players.get(0).name+"获胜");           }else{              System.out.println("玩家"+players.get(1).name+"获胜");           }             }      /**    * 创建玩家    */  public void createPlayer(){          for(int i=0; i<2; i++){         System.out.println("请输入"+(i+1)+"位玩家的ID");                  Boolean flag =false;                  while(true){             try{                 Scanner sc = new Scanner(System.in);                 String playerId;                  playerId = sc.nextInt()+"";                  System.out.println("请输入"+(i+1)+"位玩家的姓名");                  String playerName = sc.next();                  players.add(new Person(playerId,playerName));                  break;                 }catch(Exception e){                 System.out.println("您输入的不是整数,请重新输入");                 continue;                              }                      }              }     System.out.println("成功添加");     for(Person player:players){         System.out.println("欢迎玩家:"+player.id+":"+player.name);     }  }}

  • zzl7
    2016-01-20 00:40:41

    package com.imooc;
    /**
     * 创建扑克牌类
     *
     */
    public class Poker {
       public String cardVal;
       public String cardColor;
       public Poker(String cardVal, String cardColor){
           this.cardVal = cardVal;
           this.cardColor = cardColor;
       }
    }