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

用pkp类,players类,playgame类三步教你写扑克牌游戏

标签:
Java

public class pkp {
   private String color;
   private String number;

   public String getColor() {
      return color;
  }
  public void setColor(String color) {
      this.color = color;
  }

   public String getNumber() {
      return number; 
  }

   public void setNumber(String number) {
      this.number = number;
  }

   public pkp() {

   }

   public pkp(String newColor,String newNumber) {
       this.color = newColor;
       this.number = newNumber;
   }

   }
public class players {
  private int ID;
  private String name;
  public int getID() {
     return ID;
  }
  public void setID(int iD) {
     ID = iD;
  }
  public String getName() {
     return name;
  }
  public void setName(String name) {
     this.name = name;
  }

  public players(int newID,String newName) {
      this.ID = newID;
      this.name = newName;
  }
  public players() {

  }
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class playgame {
    public List<pkp> playerpkp;
    public List<players> playerlist;
    public List<pkp> playerone;
    public List<pkp> playertwo;
    Scanner input = new Scanner(System.in);
    Random rand = new Random();
    String[] pcolor = {"黑桃","红桃","梅花","方片"};
    String[] pnumber = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
    public void addpkp() {
        System.out.println("-------------创建扑克牌-------------");
        playerpkp = new ArrayList<pkp> ();
        for(int j=0;j<pcolor.length;j++) {
            for(int i=0;i<pnumber.length;i++) {
                pkp p = new pkp(pcolor[j],pnumber[i]);
                playerpkp.add(p);
            }
        }
        System.out.println("------------创建成功!-------------");
        Iterator it = playerpkp.iterator();
        while(it.hasNext()) {
            pkp p = (pkp)it.next();
            System.out.print(p.getColor()+p.getNumber()+",");
        }
        System.out.println("-----------开始洗牌-----------");
        System.out.println("-----------洗牌结束-----------");
    }
    public void addplayer() {
        playerlist = new ArrayList();
        System.out.println("-----------创建玩家-----------"); 
        int ID=0;
        for(int i=1;i<3;i++) {  
            try {
                input = new Scanner(System.in);
                System.out.println("请输入第"+i+"位玩家的ID:");
                ID = input.nextInt();
            }catch(Exception e) {
                System.out.println("请输入整数!");
                i--;
                continue;
            }   
            System.out.println("请输入第"+i+"位玩家的姓名:");
            String name = input.next();
            players p = new players(ID,name);
            playerlist.add(p);

        }
       int s = playerlist.size();
       for(int j=0;j<s;j++) {
           players p = playerlist.get(j);
           System.out.println("欢迎玩家:"+p.getName());
       }
    }   
    public void play() {
        playerone = new ArrayList();
        playertwo = new ArrayList();
        System.out.println("-----------开始发牌-----------");
        for(int i=0;i<2;i++) {
            int n1 = rand.nextInt(playerpkp.size()-1);
            int n2 = rand.nextInt(playerpkp.size()-1);
            System.out.println("玩家"+playerlist.get(0).getName()+"拿牌!");
            playerone.add(playerpkp.get(n1));
            System.out.println("玩家"+playerlist.get(1).getName()+"拿牌!");
            playertwo.add(playerpkp.get(n2));
        }
        System.out.println("-----------发牌结束-----------");
        System.out.println("-----------开始游戏------------");

        Collections.sort(playerone,new test());
        System.out.println("玩家"+playerlist.get(0).getName()+"最大手牌为:"+playerone.get(1).getColor()+playerone.get(1).getNumber());
        Collections.sort(playertwo,new test());
        System.out.println("玩家"+playerlist.get(1).getName()+"最大手牌为:"+playertwo.get(1).getColor()+playertwo.get(1).getNumber());
        System.out.println("--------------游戏结束----------------");
        if(playerone.get(1).getNumber().compareTo(playertwo.get(1).getNumber())>0) {
            System.out.println("玩家"+playerlist.get(0).getName()+"获胜!");
        }else if (playerone.get(1).getColor().compareTo(playertwo.get(1).getColor())>0) {
            System.out.println("玩家"+playerlist.get(0).getName()+"获胜!");
        }else {
            System.out.println("玩家"+playerlist.get(1).getName()+"获胜!");
        }
        for(int i=0;i<2;i++) {
            System.out.println("玩家"+playerlist.get(0).getName()+"手牌为:"+playerone.get(i).getColor()+playerone.get(i).getNumber());           
        }
        for(int j=0;j<2;j++) {
            System.out.println("玩家"+playerlist.get(1).getName()+"手牌为:"+playertwo.get(j).getColor()+playertwo.get(j).getNumber());
        }
    }
    public static void main(String[] args) {
        playgame pg = new playgame();
        pg.addpkp();
        pg.addplayer();
        pg.play();
    }

}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

public class test implements Comparator<pkp>{
    ArrayList<String> pcolor = new ArrayList<String>(Arrays.asList("黑桃","红桃","梅花","方片"));
    ArrayList<String> pnumber = new ArrayList<String>(Arrays.asList("A","2","3","4","5","6","7","8","9","10","J","Q","K"));

    @Override
    public int compare(pkp o1, pkp o2) {
        if(pnumber.indexOf(o1.getNumber())==(pnumber.indexOf(o2.getNumber()))) {
            return (pcolor.indexOf(o1.getNumber())==(pcolor.indexOf(o2.getNumber())))?1:-1;
        }

        return pnumber.indexOf(o1.getNumber())>(pnumber.indexOf(o2.getNumber()))?1:-1;
    }
}
点击查看更多内容
1人点赞

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

评论

作者其他优质文章

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

100积分直接送

付费专栏免费学

大额优惠券免费领

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

举报

0/150
提交
取消