一共3个class pai , ren , selectPai
如下pai.class
public class pai { //数据成员 String huase ; String dianshu ; //函数 public pai(String huase, String dianshu) { this.huase = huase; this.dianshu = dianshu; } }
ren.class
import java.util.ArrayList; import java.util.HashSet; import java.util.List; import java.util.Set; public class ren { //数据成员 public String id; public String name; public List<pai> poker; //成员函数 public ren(String id,String name) { this.id = id; this.name = name; this.poker = new ArrayList<pai>(); } }
selectPai.class
package com.company; import java.util.*; public class selectPai { // List类型的数据 public List<pai> Bepais; public List<pai> pais; public List<ren> a = new ArrayList<ren>(); //花色 public String[] h = {"黑桃","红桃","梅花","方块"}; public String[] d = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"}; //构造函数 public selectPai() { this.Bepais = new ArrayList<pai>(); this.pais = new ArrayList<pai>(); } //把牌都添加到List集合中去 public void paiAdd() { for(int i = 0;i < 4; i++) { for(int j = 0; j < 13; j++) { pai temp = new pai(h[i],d[j]); Bepais.add(temp); } } } //遍历牌中含有的元素 public void displayPai() { for(pai temp: Bepais) { System.out.println("花色:"+temp.huase+"点数:"+temp.dianshu); } } //洗牌 public void xiPai() { int k; Random random = new Random(); while(pais.size()<52) { k = random.nextInt(52); pai temp = Bepais.get(k); if(!pais.contains(temp)) { pais.add(temp); } } } //洗牌后展示 public void displayPai2() { for(pai temp: pais) { System.out.println("花色:"+temp.huase+"点数:"+temp.dianshu); } } //创建人物 public void createPeo() { Scanner console = new Scanner(System.in); int i = 0; int j = 0; while(i < 2) { System.out.println("请输入人物ID"); String ID = console.next(); System.out.println("请输入人物姓名"); String name = console.next(); //创建新的人物 ren one = new ren(ID,name); a.add(one); i++; } } //获取索引 然后比较大小 public int indexDian(String dianshu) { int index2 = 0; for(int j = 0;j < 13; j++) { if(dianshu == d[j]) { index2 = j; break; } } return index2; } //获取索引 然后比较大小 public int indexHua(String huase) { int index1 = 0; for(int j = 0;j < 4; j++) { if(huase == h[j]) { index1 = j; break; } } return index1; } //发牌 public void fapai() { int j = 0; for(ren one:a) { int k = 0; while(k < 2) { System.out.println("玩家"+one.id+" "+one.name+"拿牌 - "+pais.get(j).huase+" "+pais.get(j).dianshu); pai temp = new pai(pais.get(j).huase,pais.get(j).dianshu); one.poker.add(temp); j++;k++; } } } //比较发牌 public void bijiao() { int num1 = indexDian(a.get(0).poker.get(0).dianshu); System.out.println(a.get(0).poker.get(0).huase+" "+a.get(0).poker.get(0).dianshu); int num2 = indexDian(a.get(0).poker.get(1).dianshu); System.out.println(a.get(0).poker.get(1).huase+" "+a.get(0).poker.get(1).dianshu); int num3 = indexDian(a.get(1).poker.get(0).dianshu); System.out.println(a.get(1).poker.get(0).huase+" "+a.get(1).poker.get(0).dianshu); int num4 = indexDian(a.get(1).poker.get(1).dianshu); System.out.println(a.get(1).poker.get(1).huase+" "+a.get(1).poker.get(1).dianshu); int max1 ,max2 ; if(num1 == num2) { int q = indexHua(a.get(0).poker.get(0).huase); int q1 = indexHua(a.get(0).poker.get(1).huase); if(q>q1) { max1 = num1; System.out.println("玩家1的最大手牌为"+a.get(0).poker.get(0).huase+" "+a.get(0).poker.get(0).dianshu); } else { max1 = num2; System.out.println("玩家1的最大手牌为"+a.get(0).poker.get(1).huase+" "+a.get(0).poker.get(1).dianshu); } } else if( num1 > num2) { max1 =num1; System.out.println("玩家1的最大手牌为"+a.get(0).poker.get(0).huase+" "+a.get(0).poker.get(0).dianshu); } else { max1 = num2; System.out.println("玩家1的最大手牌为"+a.get(0).poker.get(1).huase+" "+a.get(0).poker.get(1).dianshu); } //玩家2 if(num3 == num4) { int q2 = indexHua(a.get(1).poker.get(0).huase); int q3 = indexHua(a.get(1).poker.get(1).huase); if(q2>q3) { max2 = num3; System.out.println("玩家2的最大手牌为"+a.get(1).poker.get(0).huase+" "+a.get(1).poker.get(0).dianshu); } else { max2 = num4; System.out.println("玩家2的最大手牌为"+a.get(1).poker.get(1).huase+" "+a.get(1).poker.get(1).dianshu); } } else if( num2 > num4) { max2 =num3; System.out.println("玩家2的最大手牌为"+a.get(1).poker.get(0).huase+" "+a.get(1).poker.get(0).dianshu); } else { max2 = num4; System.out.println("玩家2的最大手牌为"+a.get(1).poker.get(1).huase+" "+a.get(1).poker.get(1).dianshu); } if(max1>max2) { System.out.println("玩家"+a.get(0).id+" "+a.get(0).name+" 获胜!!"); } else { System.out.println("玩家"+a.get(1).id+" "+a.get(1).name+" 获胜!!"); } } //主函数 public static void main(String[] args) { selectPai temp = new selectPai(); //调用添加牌的函数 temp.paiAdd(); System.out.println("开始牌"); //调用展示牌的函数 temp.displayPai(); System.out.println("长度为"+temp.Bepais.size()); //洗牌后 temp.xiPai(); System.out.println("洗牌后"); //调用洗牌后的函数 temp.displayPai2(); System.out.println("长度为"+temp.pais.size()); //创建人物 temp.createPeo(); temp.fapai(); temp.bijiao(); } }
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦