无限记录者
2019-11-07 15:38
public class Card implements Comparable{ String num; String color; Card(String color,String num){ this.num=num; this.color=color; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((num == null) ? 0 : num.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Card)) return false; Card other = (Card) obj; if (num == null) { if (other.num != null) return false; } else if (!num.equals(other.num)) return false; return true; } @Override public int compareTo(Object o) { //如果数字相同的话就按照花色来排序 if(this.num.compareTo(num)==0) { return this.color.compareTo(color); } return this.num.compareTo(num); } }
import java.util.ArrayList;import java.util.List; public class Player { int id; String name; List<Card>holdCard=new ArrayList<Card>(); Player(int id,String name){ this.id=id; this.name=name; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (!(obj instanceof Player)) return false; Player other = (Player) obj; if (name == null) { if (other.name != null) return false; //调用名字来比对 } else if (!name.equals(other.name)) return false; return true; }
import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.InputMismatchException; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; import java.util.Set; import java.util.Random; public class PukeTest { List<Card>puke=new ArrayList<Card>();//存放创建好的扑克牌 Map<Integer,Player>pls=new HashMap<Integer,Player>();//存放玩家的id 姓名 Set<Entry<Integer, Player>>entry=pls.entrySet();//用于接收玩家信息并方便调用 List<Card>orderPuke=new ArrayList<Card>();//存放打乱的扑克牌 List<Card>resultCard=new ArrayList<Card>();//存放最终比大小的两张牌 Random random=new Random(); // 一,创建一副扑克牌 //包括四种花色:黑桃,红桃,梅花,方块 //十三种点数:2-10,j,q,k,a,不考虑大小王 public void createCard() { System.out.println("--------------创建扑克牌--------------------"); String Element[]= {"2","3","4","5","6","7","8","9","10","j","Q","K","A"}; String[]huase= {"方块","梅花","红桃","黑桃"}; //外循环:花色 内循环:不同大小的卡片 for(int i=0;i<huase.length;i++) { for(int j=0;j<Element.length;j++) { puke.add(new Card(huase[i],Element[j])); } } System.out.println("--------------创建扑克牌成功--------------------"); //遍历创建的所有卡片 int k=0; for (int i=0;i<puke.size();i++) { System.out.print(puke.get(i).color+puke.get(i).num+","); k++; if(k==13) { System.out.println(); k=0; } } System.out.println("共"+puke.size()+"张牌!"); } // 二,创建两名玩家 // * 玩家至少要有ID、姓名、手牌等属性,手牌为扑克牌的集合 public void createp() { System.out.println("--------------创建玩家--------------------"); int i=0; while(i<2) { try{ Scanner input=new Scanner(System.in); System.out.println("请输入第"+(i+1)+"位玩家的ID和姓名"); System.out.println("输入ID:"); Integer id=input.nextInt(); if(pls.containsKey(id)){ System.out.println("请不要重复输入相同的ID:"); continue; } System.out.println("输入姓名:"); String name=input.next(); Player newPlayer=new Player(id,name); if(pls.containsValue(newPlayer)) { System.out.println("请不要重复输入同一个姓名"); continue; } pls.put(id,newPlayer); i++; }catch(InputMismatchException e) { System.out.println("请输入整数类型的ID"); continue; } } for (Entry<Integer, Player> entry2 : entry) { System.out.println("---------欢迎玩家"+entry2.getValue().name); } } // 三,洗牌 // * 将之前创建的“一副扑克牌”打乱顺序 public void order() { for(int i=0;i<52;) { Integer x=random.nextInt(52); Card j=puke.get(x); List<Integer>list=new ArrayList<Integer>(); list.add(random.nextInt()); while(list.contains(x)) { continue; } orderPuke.add(j); i++; } System.out.println("--------------洗牌完毕--------------------"); int k=0; for (int i=0;i<orderPuke.size();i++) { System.out.print(orderPuke.get(i).color+orderPuke.get(i).num+","); k++; if(k==13) { System.out.println(); k=0; } System.out.println("共"+orderPuke.size()+"张牌!"); } } // 四,发牌 // * 将洗牌的扑克牌集合,从第一张开始,发给两名玩家,按照一人一张的方式,每人发2张 public void pass() { System.out.println("--------------开始发牌--------------------------------"); for(int i=0;i<4;) { for (Entry<Integer, Player> entry2 : entry) { Card x=orderPuke.get(0); System.out.println("----------- "+entry2.getValue().name+"拿牌"); entry2.getValue().holdCard.add(0, x); resultCard.add(0, x); orderPuke.remove(0); i++; } } Collections.sort(resultCard); System.out.println("----------发牌结束!-----------------"); } // * 五,游戏 // * 比较两名玩家手中的扑克牌点数最大的牌,大的赢,如果点数相同,则按照花色比较。 public void pk() { System.out.println("-------------开始游戏--------------"); System.out.println("比赛结果:"); Card resultCard; List result =new ArrayList(); for (Entry<Integer, Player> entry2 : entry) { Player pl=entry2.getValue(); Collections.sort(pl.holdCard); Card maxCard=pl.holdCard.get(1); Card minCard=pl.holdCard.get(0); System.out.println(pl.name+"最大的牌是"+maxCard.color+maxCard.num); System.out.println(pl.name+"最小的牌是"+minCard.color+minCard.num); result.add(maxCard); } Collections.sort(result); for (Entry<Integer, Player> entry2 : entry) { Player pl=entry2.getValue(); if(pl.holdCard.contains(result.get(result.size()-1))) { System.out.println("获胜者:"+pl.name); } } } public static void main(String[] args) { PukeTest pt=new PukeTest(); pt.createCard(); pt.createp(); pt.order(); pt.pass(); pt.pk(); }}
Java入门第三季
409792 学习 · 4340 问题
相似问题