Object_is_null
2015-07-27 00:07
//Poker.java package com.imooc; public class Poker implements Comparable<Poker>{ private String num; private String color; @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((color == null) ? 0 : color.hashCode()); result = prime * result + ((num == null) ? 0 : num.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Poker)) return false; Poker other = (Poker) obj; if (color == null) { if (other.color != null) return false; } else if (!color.equals(other.color)) return false; if (num == null) { if (other.num != null) return false; } else if (!num.equals(other.num)) return false; return true; } public Poker(String num, String color){ this.num = num; this.color = color; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public int compareTo(Poker o) { // TODO 自动生成的方法存根 String pokerNum = "2,3,4,5,6,7,8,9,10,J,Q,K,A"; String pokerColor = "方块,梅花,红桃,黑桃"; int flag; flag = pokerNum.indexOf(this.getNum())-pokerNum.indexOf(o.getNum()); if(flag == 0){ flag = pokerColor.indexOf(this.getColor())-pokerColor.indexOf(o.getColor()); } return flag; } } //Player.java package com.imooc; import java.util.ArrayList; import java.util.List; public class Player { private int id; private String name; private List<Poker> handPoker; public Player(int id, String name){ this.id = id; this.name = name; handPoker = new ArrayList<Poker>(); } 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<Poker> getHandPoker() { return handPoker; } public void setHandPoker(List<Poker> handPoker) { this.handPoker = handPoker; } } //MakePoker.java package com.imooc; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; public class MakePoker { private List<Poker> pokerList; public List<Poker> getPokerList() { return pokerList; } public void setPokerList(List<Poker> pokerList) { this.pokerList = pokerList; } public MakePoker(){ pokerList = new ArrayList<Poker>(); String[] nums = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; String[] colors = {"黑桃","红桃","梅花","方块"}; for(int i=0; i<colors.length; i++){ for(int j=0; j<nums.length; j++){ pokerList.add(new Poker(nums[j],colors[i])); } } System.out.println("扑克牌有:"); for (Poker poker : pokerList) { System.out.print(poker.getColor()+poker.getNum()+" "); } System.out.println(); } public static void main(String[] args) { System.out.println("--------------扑克牌游戏开始--------------"); System.out.println("--------------创建扑克牌---------------"); MakePoker mp = new MakePoker(); System.out.println("扑克牌创建成功!"); Scanner input = new Scanner(System.in); List<Player> playerList = new ArrayList<Player>(); System.out.println("---------------创建玩家----------------"); for(int i=0; i<2; i++){ try{ System.out.println("请输入玩家的id:"); int id = input.nextInt(); System.out.println("请输入玩家的名字:"); String name = input.next(); playerList.add(new Player(id,name)); System.out.println("创建玩家"+name+"成功!"); }catch(Exception e){ System.out.println("输入的id必须为整型!"); } } System.out.println("--------------开始洗牌---------------"); Collections.shuffle(mp.getPokerList()); System.out.println("--------------开始发牌---------------"); for(int j=0; j<2; j++){ for (Player player : playerList) { player.getHandPoker().add(mp.getPokerList().get(0)); mp.getPokerList().remove(0); } } System.out.println("-----------发牌结束开始比较大小----------"); Collections.sort(playerList.get(0).getHandPoker()); Collections.sort(playerList.get(1).getHandPoker()); List<Poker> compareList = new ArrayList<Poker>(); compareList.add(playerList.get(0).getHandPoker().get(1)); System.out.println("玩家"+playerList.get(0).getName()+"最大的牌是"+playerList.get(0).getHandPoker().get(1).getColor()+playerList.get(0).getHandPoker().get(1).getNum()); compareList.add(playerList.get(1).getHandPoker().get(1)); System.out.println("玩家"+playerList.get(1).getName()+"最大的牌是"+playerList.get(1).getHandPoker().get(1).getColor()+playerList.get(1).getHandPoker().get(1).getNum()); Collections.sort(compareList); for (Player player : playerList) { if(player.getHandPoker().contains(compareList.get(1))){ System.out.println("-----------"+player.getName()+"获胜------------"); } } for (Player player : playerList) { System.out.print("玩家"+player.getName()+"的手牌是:"); for (Poker poker : player.getHandPoker()) { System.out.print(poker.getColor()+poker.getNum()+" "); } System.out.println(""); } } }
666666666666666
Java入门第三季
409792 学习 · 4340 问题
相似问题