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

运行时牌大小判断会出错...检查不出来5555,诚心求助,谢谢!

第一个类,player

package PokerGame;


import java.util.ArrayList;

import java.util.List;


public class Player {

Integer id;

String name;

List<Poker> hisPoker=new ArrayList<Poker>();

public Player(Integer id,String name){

this.id=id;

this.name=name;

}

public Player(){

}

}

第二个类,poker

package PokerGame;


public class Poker implements Comparable<Poker> {

String flower;

String number;

Integer numCompare;

Integer floCompare;

public Poker(String flower,String number,Integer numCompare,Integer floCompare){

this.flower=flower;

this.number=number;

this.numCompare=numCompare;

this.floCompare=floCompare;

}

public Poker(){

}

@Override

public int compareTo(Poker o) {

// TODO Auto-generated method stub

if(this.numCompare==o.numCompare){

return this.numCompare.compareTo(o.numCompare);

}else

return this.floCompare.compareTo(o.floCompare);

}


@Override

public boolean equals(Object obj) {

if (this == obj)

return true;

if (obj == null)

return false;

if (getClass() != obj.getClass())

return false;

Poker other = (Poker) obj;

if (flower == null) {

if (other.flower != null)

return false;

} else if (!flower.equals(other.flower))

return false;

if (number == null) {

if (other.number != null)

return false;

} else if (!number.equals(other.number))

return false;

return true;

}


}

主程序

package PokerGame;


import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

import java.util.Scanner;


public class Major {

//把list定义在这里,使用方法后list得知将会随方法改变

List<Poker> pokerList=new ArrayList<Poker>();

//导入scanner对象来接受

Scanner console=new Scanner(System.in);

    

//创建玩家

Player player1=new Player();

Player player2=new Player();

//创建扑克牌

public void buildPoker(){

String[] flowerS={"方块","梅花","红桃","黑桃"};

        Integer[] flowerCompareS={1,2,3,4};

        String[] numberS={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};

        Integer[] numberCompareS={1,2,3,4,5,6,7,8,9,10,11,12,13};

for(int i=0;i<=12;i++){

for(int j=0;j<=3;j++){

         Poker poker=new Poker();

         poker.number=numberS[i];

         poker.numCompare=numberCompareS[i];

         poker.flower=flowerS[j];

         poker.floCompare=flowerCompareS[j];

         pokerList.add(poker);

         }

}

System.out.println("扑克牌建立完成啦~~~建立的牌为:");

for(Poker poker:pokerList){

System.out.print(poker.flower+poker.number+" ");

}

}

//洗牌

public void washPoker(){

System.out.println("洗牌时间~~~");

Collections.shuffle(pokerList);//shuffle方法用于打乱顺序

System.out.println("洗牌完成~~~");

}

//输入玩家信息

public void buildPlayer(){

   System.out.println("请输入玩家一ID");

Integer id1=console.nextInt();

System.out.println("请输入玩家一姓名");

String name1=console.next();

player1.name=name1;

player1.id=id1;

System.out.println("请输入玩家二ID");

Integer id2=console.nextInt();

System.out.println("请输入玩家二姓名");

String name2=console.next();

player2.name=name2;

player2.id=id2;

System.out.println("欢迎玩家:"+player1.name+"和"+player2.name);

}

//发牌

public void playerGetPoker(){

player1.hisPoker.add(pokerList.get(0));

System.out.println("玩家"+player1.name+"拿牌啦~~~"+pokerList.get(0).flower+pokerList.get(0).number);

player2.hisPoker.add(pokerList.get(1));

System.out.println("玩家"+player2.name+"拿牌啦~~~");

player1.hisPoker.add(pokerList.get(2));

System.out.println("玩家"+player1.name+"拿牌啦~~~"+pokerList.get(2).flower+pokerList.get(2).number);

player2.hisPoker.add(pokerList.get(3));

System.out.println("玩家"+player2.name+"拿牌啦~~~");

}

//判断两个玩家最大的手牌

//用重新定义的compare来进行sort排序,再打印出第一个就可以啦,排序为从小到大

//本游戏精华部分

public void judgeBigOne(){

Collections.sort(player1.hisPoker);

System.out.println("玩家:"+player1.name+"小的手牌为:"+player1.hisPoker.get(0).flower+

player1.hisPoker.get(0).number+",大的手牌为:"+player1.hisPoker.get(1).flower+

player1.hisPoker.get(1).number);

Collections.sort(player2.hisPoker);

System.out.println("玩家:"+player2.name+"小的手牌为:"+player2.hisPoker.get(0).flower+

player2.hisPoker.get(0).number+",大的手牌为:"+player2.hisPoker.get(1).flower+

player2.hisPoker.get(1).number);

}

//判断胜利

public void judgeWinner(){

List<Poker> temp=new ArrayList<Poker>();

temp.add(player1.hisPoker.get(1));

temp.add(player2.hisPoker.get(1));

Collections.sort(temp);

if(temp.get(1).equals(player1.hisPoker.get(1))){

System.out.println("获胜者为"+player1.name+"!~~恭喜");

}else{

System.out.println("获胜者为"+player2.name+"!~~恭喜");

}

System.out.println("~~~~~游戏结束~~~~~");

}

public static void main(String[] args) {

Major mj=new Major();

mj.buildPoker();

mj.washPoker();

mj.buildPlayer();

mj.playerGetPoker();

mj.judgeBigOne();

mj.judgeWinner();

}

}



提问者:白白哟 2016-03-04 00:03

个回答

  • moyemoji
    2016-04-01 17:27:43

    你的判断大小方法中的get(1)应该改为get(0)吧?

  • moyemoji
    2016-04-01 17:25:24

    //你的代码的比较函数有以下两句
    temp.add(player1.hisPoker.get(1));
    temp.add(player2.hisPoker.get(1));
    //比较最大的不应该排序以后序列的第一个元素么?应该是get(0)吧!?


  • yanghao123
    2016-03-31 17:24:05

    我也写了这麽一个,请指教:

    /*
     * 扑克牌类
     */
    public class poke {
     public String number;
     public int color; //1-红桃 2-方片 3-黑桃 4-梅花
     public int value;
     
     public poke(String number, int color, int value){
      this.number = number;
      this.color = color;
      this.value = value;
     }
    }

    //////////////////////////////////////////

    /*
     * 玩家类
     */
    public class player implements Comparable<player>{
     private String id;
     private String name;
     private Set<poke> playerPokes;
     private poke pkMaxValue;
     
     public poke getPkMaxValue() {
      return pkMaxValue;
     }

     public void setPkMaxValue(poke pkMaxValue) {
      this.pkMaxValue = pkMaxValue;
     }

     /*
      * 默认构造器
      */
     public player(){
      playerPokes = new HashSet<poke>();
     }
     
     /*
      * 操作手牌方法
      */
     public void addPoke(poke pk){
      this.playerPokes.add(pk);
     }
     public Set<poke> getPoke(){
      return playerPokes;
     }


     public String getId() {
      return id;
     }


     public void setId(String id) {
      this.id = id;
     }

     public String getName() {
      return name;
     }

     public void setName(String name) {
      this.name = name;
     }

     @Override
     public int compareTo(player o) {
      // TODO Auto-generated method stub
      return this.id.compareTo(o.id);
     }
    }

    //////////////////////////////////////////

    /*
     * 简单扑克牌游戏
     */
    public class pokeGame {
     public List<poke> pokeList;  //扑克牌列表
     public List<player> playerList; //玩家列表
     
     /*
      * 默认构造器
      */
     public pokeGame(){
      pokeList = new ArrayList<poke>();
      playerList = new ArrayList<player>();
     }
     /*
      * 新建一手牌
      */
     public void newCreatePokes(){
      //数字
      for (int i=2; i<=10; i++){
       for (int j=1; j<=4; j++){
        poke pkt = new poke(i+"", j, i);
        pokeList.add(pkt); 
       }
      }
      //字母
      for (int k=1; k<=4; k++){
       poke pk1 = new poke("A", k, 14);
       pokeList.add(pk1); 
       poke pk2 = new poke("J", k, 11);
       pokeList.add(pk2); 
       poke pk3 = new poke("Q", k, 12);
       pokeList.add(pk3); 
       poke pk4 = new poke("K", k, 13);
       pokeList.add(pk4); 
      }
     }
     /*
      * 洗牌
      */
     public void resetPoke(){
      Random rad = new Random();
      for (int i=0; i<pokeList.size(); i++){
       int v1 = rad.nextInt(pokeList.size());
       int v2 = rad.nextInt(pokeList.size());
       //任意交换随机的两个位置的元素
       Collections.swap(pokeList, v1, v2);
      }
     }
     /*
      * 打印扑克牌
      */
     public void printPoke(){
      for (int i=0; i<pokeList.size(); i++){
       if (1 == pokeList.get(i).color){
        System.out.print("红桃"+pokeList.get(i).number+" ");
       }
       if (2 == pokeList.get(i).color){
        System.out.print("方片"+pokeList.get(i).number+" ");
       }
       if (3 == pokeList.get(i).color){
        System.out.print("黑桃"+pokeList.get(i).number+" ");
       }
       if (4 == pokeList.get(i).color){
        System.out.print("梅花"+pokeList.get(i).number+" ");
       }
      }
      System.out.println("");
     }
     /*
      * 打印玩家信息
      */
     public void printPlayerInfo(){
      for (player p: playerList){
       System.out.println(p.getId() + ":" + p.getName());
      }
     }
     /*
      * 打印玩家手中的牌
      */
     public void printPlayerPokeInfo(){
      for (int i=0; i<playerList.size(); i++){
       System.out.print("玩家" +playerList.get(i).getName() + ":");
       for (poke p:playerList.get(i).getPoke()){
        if (1 == p.color){
         System.out.print("红桃"+p.number+" ");
        }
        if (2 == p.color){
         System.out.print("方片"+p.number+" ");
        }
        if (3 == p.color){
         System.out.print("黑桃"+p.number+" ");
        }
        if (4 == p.color){
         System.out.print("梅花"+p.number+" ");
        }
       }
       System.out.println("");
      }
     }
     
     /*
      * 返回集合中最大扑克牌
      */
     public poke returnMaxPoke(Set<poke> pks){
      poke ptemp = new poke("", 4, 0);
      for (poke pt : pks){
       if (pt.value>ptemp.value){
        ptemp= pt;
       }else if(pt.value == ptemp.value){
        if (pt.color<=ptemp.color){
         ptemp= pt;
        } 
       }
      }
      return ptemp;
     }
     /*
      * 评判比赛结果
      */
     public void printResult(poke p1, poke p2){
      if (p1.value>p2.value){
       System.out.println("玩家"+playerList.get(0).getName()+"获胜!");
      }else if(p1.value == p2.value){
       if (p1.color<=p2.color){
        System.out.println("玩家"+playerList.get(0).getName()+"获胜!");
       }else{
        System.out.println("玩家"+playerList.get(1).getName()+"获胜!");
       } 
      }else{
       System.out.println("玩家"+playerList.get(1).getName()+"获胜!");
      }
     }
     /*
      * 返回一组玩家中的赢家
      */
     public void printResult(List<player> pList){
      if(pList.size()<=0){
       return;
      }
      player plyTemp = pList.get(0);
      for (int i=0; i<pList.size(); i++){
       if (pList.get(i).getPkMaxValue().value > plyTemp.getPkMaxValue().value){
        plyTemp= pList.get(i);
       }else if(pList.get(i).getPkMaxValue().value == plyTemp.getPkMaxValue().value){
        if (pList.get(i).getPkMaxValue().color <= plyTemp.getPkMaxValue().color){
         plyTemp= pList.get(i);
        } 
       } 
      }
      System.out.println("玩家"+plyTemp.getName()+"获胜!");
     }
     /*
      * 打印玩家最大手牌
      */
     public void printMaxPoke(int color, String number, String name){
      if (1 == color){
       System.out.println("玩家"+ name+"的最大手牌为:"+"红桃"+number);
      }
      if (2 == color){
       System.out.println("玩家"+name+"的最大手牌为:"+"方片"+number);
      }
      if (3 == color){
       System.out.println("玩家"+name+"的最大手牌为:"+"黑桃"+number);
      }
      if (4 == color){
       System.out.println("玩家"+name+"的最大手牌为:"+"梅花"+number);
      }
     }
     /**
      * @param args
      */
     public static void main(String[] args) {
      // TODO Auto-generated method stub
      pokeGame pg = new pokeGame();
      pg.newCreatePokes();
      System.out.println("新建一幅扑克牌:");
      pg.printPoke();
      
      System.out.println("------------------------------");
      //洗牌次数可自己定,当然是越多洗的越开
      for (int i=0; i<5; i++){
       pg.resetPoke();
      }
      System.out.println("洗牌后:");
      pg.printPoke();
      
     // System.out.println(pg.pokeList.size());
      System.out.println("----------创建玩家--------------");
      Scanner cos = new Scanner(System.in);
      
      System.out.println("请输入需要创建的玩家个数(正整数):");
      int nPlayerNum = 0;
      try{
       nPlayerNum = cos.nextInt();
      }catch(InputMismatchException e){
       System.out.println("你太笨了!输入的值有问题!游戏还没开始就已经结束了!");
       return;
      }
      //创建两个玩家
      for (int i=0; i<nPlayerNum; i++){
       String idt = "";
       boolean bt=false;
       boolean g = false;
       do{
        System.out.println("请输入第" + (i+1)+ "个玩家ID:");
        idt = cos.next();
        try{
         int it = Integer.valueOf(idt);
         bt = false;
        }catch(NumberFormatException e){
         System.out.println("提示:请输入合规的玩家ID!");
         bt = true;
        }
        
        g = pg.playerList.contains(idt);
       }while(!g && bt);
       player pt = new player();
       pt.setId(idt);
       System.out.println("请输入合规的玩家姓名:");
       pt.setName(cos.next());
       pg.playerList.add(pt);
      }
      
      //显示玩家信息
      System.out.println("-----------玩家信息---------------");
      pg.printPlayerInfo();
      
      System.out.println("------------发牌----------------");
      System.out.println("每个人发牌数:");
      int nPokeNum = 0;
      try{
       nPokeNum = cos.nextInt();
      }catch(InputMismatchException e){
       System.out.println("你太笨了!输入的值有问题!将默认为每个用户发2张牌!");
       nPokeNum = 2;
       return;
      }
      for (int i=0, j=0; i<nPokeNum; i++){
       for (int k=0; k<nPlayerNum; k++){
        if(j>=52){
         System.out.println("牌发光啦!!!");
         break;
        }
        poke pt = pg.pokeList.get(j);
        pg.playerList.get(k).addPoke(pt);
        j++;
       }
       if(j>=52){
        break;
       }
      }
      
      //通用玩法
      for (int i=0; i<nPlayerNum; i++){
       Set<poke> p = pg.playerList.get(i).getPoke();
       poke ptMax = pg.returnMaxPoke(p);
       pg.playerList.get(i).setPkMaxValue(ptMax);
      }
      
      
      //比赛结果
     // pg.printResult(ptMax1, ptMax2);  //两个玩家的比较,一组玩家比较的话待定
      pg.printResult(pg.playerList);
      
      //打印玩家手中的牌
      pg.printPlayerPokeInfo();
     }

    }

  • yanghao123
    2016-03-31 17:20:59

    不好意思,代码实在是没有看下去,不过有点建议:写代码有一定的通用性,方便在需求更改的时候改很少的代码或者不改代码!

  • hm_amwje
    2016-03-30 10:25:14

    你compareTO 写反了

    public int compareTo(Poker o) {


    // TODO Auto-generated method stub


    if(this.numCompare==o.numCompare){


     return this.floCompare.compareTo(o.floCompare);


    }else



        return this.numCompare.compareTo(o.numCompare);
    }


  • Uauslman
    2016-03-05 15:52:30

    花色大小没有定义