earlsun
2017-07-16 13:52
package com.earl; public class Zhipai implements Comparable<Zhipai> { private String name;//纸牌花色+点数 private String value;//纸牌用于比较的值 //getter and setter public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } //构造方法,提取纸牌的值 public Zhipai(String name){ setName(name); StringBuilder zpvalue=new StringBuilder(); zpvalue.append(returnzp(name.substring(2, name.length()))); zpvalue.append(returnzp(name.substring(0,2))); setValue(zpvalue.toString()); } //纸牌花色大小返回char public char returnzp(String str){ switch(str){ case "3":return '0'; case "4":return '1'; case "5":return '2'; case "6":return '3'; case "7":return '4'; case "8":return '5'; case "9":return '6'; case "10":return '7'; case "J":return '8'; case "Q":return '9'; case "K":return 'a'; case "A":return 'b'; case "2":return 'c'; case "黑桃":return '0'; case "红心":return '1'; case "梅花":return '2'; default:return '3';//方块 } } @Override public int compareTo(Zhipai o) { // TODO Auto-generated method stub return -(this.value.compareTo(o.value)); } }
package com.earl; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Player { private int id; private String name; private List<Zhipai> playerzhipai=new ArrayList<>(); public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<Zhipai> getPlayerzhipai() { return playerzhipai; } public void setPlayerzhipai(Zhipai listvalue) { playerzhipai.add(listvalue); Collections.sort(playerzhipai); } }
package com.earl; import java.util.ArrayList; import java.util.Arrays; import java.util.InputMismatchException; import java.util.Iterator; import java.util.List; import java.util.Random; import java.util.Scanner; public class Test { //纸牌花色大小数组 Zhipai[] zparr={new Zhipai("黑桃A"),new Zhipai("黑桃2"),new Zhipai("黑桃3"),new Zhipai("黑桃4"),new Zhipai("黑桃5"),new Zhipai("黑桃6"),new Zhipai("黑桃7"), new Zhipai("黑桃8"),new Zhipai("黑桃9"),new Zhipai("黑桃10"),new Zhipai("黑桃J"),new Zhipai("黑桃Q"),new Zhipai("黑桃K"),new Zhipai("红心A"), new Zhipai("红心2"),new Zhipai("红心3"),new Zhipai("红心4"),new Zhipai("红心5"),new Zhipai("红心6"),new Zhipai("红心7"),new Zhipai("红心8"), new Zhipai("红心9"),new Zhipai("红心10"),new Zhipai("红心J"),new Zhipai("红心Q"),new Zhipai("红心K"),new Zhipai("梅花A"),new Zhipai("梅花2"), new Zhipai("梅花3"),new Zhipai("梅花4"),new Zhipai("梅花5"),new Zhipai("梅花6"),new Zhipai("梅花7"),new Zhipai("梅花8"),new Zhipai("梅花9"), new Zhipai("梅花10"),new Zhipai("梅花J"),new Zhipai("梅花Q"),new Zhipai("梅花K"),new Zhipai("方块A"),new Zhipai("方块2"),new Zhipai("方块3"), new Zhipai("方块4"),new Zhipai("方块5"),new Zhipai("方块6"),new Zhipai("方块7"),new Zhipai("方块8"),new Zhipai("方块9"),new Zhipai("方块10"), new Zhipai("方块J"),new Zhipai("方块Q"),new Zhipai("方块K")}; //存放纸牌 List<Zhipai> newlist=new ArrayList<>(); List<Zhipai> xipailist=new ArrayList<>(); //参与者id and name Player p1=new Player(); Player p2=new Player(); private Scanner s0; //加入纸牌至newlist public void addzp(){ newlist.addAll(Arrays.asList(zparr)); System.out.println("10元一包,拆包不退。开了一包牌!"); } //洗牌,随机打乱纸牌newlist->xipailist public void randomzp(){ while(newlist.size()>0){ Random random=new Random(); int rd0=random.nextInt(newlist.size()); xipailist.add(newlist.get(rd0)); newlist.remove(rd0); } System.out.println("-------------------------洗牌-------------------------"); for (Zhipai xp0 : xipailist) { System.out.print(xp0.getName()+";"); } System.out.println(); } //输入参与者姓名 public void addplayer(){ s0 = new Scanner(System.in); while(true){ try{ System.out.println("------------------------请输入------------------------"); System.out.println("输入第一个玩家id:"); p1.setId(s0.nextInt()); System.out.println("输入第二个玩家id:"); p2.setId(s0.nextInt()); if(p1.getId()==p2.getId()){ System.out.println("玩家id不能相同!"); continue; } break; }catch(InputMismatchException e){ System.out.println("请输入整数!"); continue; }catch(Exception e){ e.printStackTrace(); }finally{ s0=new Scanner(System.in); } } while(true){ System.out.println("输入第一个玩家name:"); p1.setName(s0.next()); System.out.println("输入第二个玩家name:"); p2.setName(s0.next()); if(p1.getName().equals(p2.getName())){ System.out.println("玩家name不能相同!"); continue; } break; } System.out.println("玩家ID:"+p1.getId()+" 玩家name:"+p1.getName()); System.out.println("玩家ID:"+p2.getId()+" 玩家name:"+p2.getName()); } //抽牌 public void getzhipai(){ Iterator<Zhipai> it=xipailist.iterator(); for(int i=0;i<3;i++){ p1.setPlayerzhipai(it.next()); p2.setPlayerzhipai(it.next()); } System.out.println("-----------------------发牌完成-----------------------"); System.out.print("玩家ID:"+p1.getId()+";"+p1.getName()+"手牌:"); for (Zhipai pz0 : p1.getPlayerzhipai()) { System.out.print(pz0.getName()+";"); } System.out.println(); System.out.print("玩家ID:"+p2.getId()+";"+p2.getName()+"手牌:"); for (Zhipai pz1 : p2.getPlayerzhipai()) { System.out.print(pz1.getName()+";"); } System.out.println(); } //获胜者判断 public void winner(){ System.out.println("-------------------------结果-------------------------"); if(p1.getPlayerzhipai().get(0).getValue().compareTo(p2.getPlayerzhipai().get(0).getValue())>0){ System.out.println("玩家ID:"+p1.getId()+";"+p1.getName()+"获胜!"); }else if(p1.getPlayerzhipai().get(0).getValue().compareTo(p2.getPlayerzhipai().get(0).getValue())<0){ System.out.println("玩家ID:"+p2.getId()+";"+p2.getName()+"获胜!"); }else{ System.out.println("平局!"); } } //main public static void main(String[] args) { // TODO Auto-generated method stub Test mc=new Test(); mc.addzp(); mc.randomzp(); mc.addplayer(); mc.getzhipai(); mc.winner(); } }
厉害啊
Java入门第三季
409792 学习 · 4340 问题
相似问题