交作业!~~~
package gameEzPoker; /** * 此为扑克牌类,定义扑克牌的属性: * 1.四种花色:黑桃、红桃、梅花、方片 * 2.13种点数:2-10,J、Q、K、A,不考虑大小王 * @author sys * */ public class Poker { //定义扑克牌属性 //花色 private String color; //点数大小 private String num; //构造方法初始化属性 public Poker(String color,String num){ //初始化类属性 this.color = color; this.num = num; } //无参构造方法 public Poker(){ } //属性的getter和setter方法 //获取color属性 public String getColor() { return color; } //修改color属性 public void setColor(String color) { this.color = color; } //获取num属性 public String getNum() { return num; } //修改num属性 public void setNum(String num) { this.num = num; } }
package gameEzPoker; import java.util.ArrayList; import java.util.List; /** * 玩家类需要具备的属性: * 1.玩家ID * 2.玩家姓名 * 3.玩家所持有的的手牌 * @author sys */ public class Player { //创建玩家ID属性 private String ID; //创建玩家名称属性 private String name; //创建玩家所持手牌集合 public List<Poker> playerPoker; //构造方法初始化属性 public Player(String ID,String name){ this.ID = ID; this.name = name; } public Player(){ this.playerPoker = new ArrayList<Poker>(); } //getter和setter方法 //获取ID属性 public String getID() { return ID; } //为ID赋值 public void setID(String iD) { ID = iD; } //获取name属性 public String getName() { return name; } //为name赋值 public void setName(String name) { this.name = name; } }
package gameEzPoker; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.InputMismatchException; import java.util.List; import java.util.Random; import java.util.Scanner; /** * 开始游戏 */ public class GameStarter implements Comparator<Poker>{ //创建玩家集合 private List<Player> player; //扑克牌集合 private static List<Poker> poker; //洗牌后的扑克牌集合 private List<Poker> pokerAfterShuffle; //发牌时临时存放Poker private Poker getPoker; //创建Scanner对象 private static Scanner input; static{//静态初始化块,代码只需要初始化一次 input = new Scanner(System.in); //创建一副扑克牌 poker = new ArrayList<Poker>(); //花色 final String[] color ={"方片","梅花","红桃","黑桃"}; //创建扑克牌点数字符串 String s = "2345678910JQKA"; //根据4种不同花色创建点数 System.out.println("--------扑克牌创建中...----------"); for(int j = 0;j<4;j++){ for(int i = 0;i < 14;i++){ StringBuilder s2 = new StringBuilder();//接收点数 if(i==8){ s2.append(s.charAt(i)); s2.append(s.charAt(i+1)); poker.add(new Poker(color[j],s2.toString())); continue; } if(i==9) continue; s2.append(s.charAt(i)); poker.add(new Poker(color[j],s2.toString())); } } //扑克牌集合的另一种创建方法foreach方法 ////点数 //final String[] num = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; ////花色 //final String[] color ={"方片","梅花","红桃","黑桃"}; //for (String str1 : color) { //for (String str2 : num) { //Poker p = new Poker(str1,str2); //poker.add(p); //} //} System.out.println("--------扑克牌创建成功!----------"); System.out.print("牌面为:"+"\n"+"[ "); for (Poker p : poker) { System.out.print(p.getColor()+p.getNum()+" "); } System.out.println("]"); } /** * 构造方法初始化属性 */ public GameStarter(){ this.player = new ArrayList<Player>(); this.pokerAfterShuffle = new ArrayList<Poker>(); this.getPoker = new Poker(); } /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub GameStarter gs = new GameStarter(); gs.shuffle();//洗牌 gs.printList(); gs.createPlayers();//添加玩家 gs.dealPoker(); } /** * 洗牌方法原理: * 利用遍历,使用生成的随机数字位置,将元素从有序的扑克集合中取出 * 再放入pokerAfterShuffle集合中即可 * 要求:元素不可重复添加 */ public void shuffle(){ //创建Random对象 Random random = new Random(); Poker p = new Poker(); System.out.println("---------开始洗牌...---------"); //取出poker中的元素 for(int i = 0;i < poker.size();i++){ do{ p = poker.get(random.nextInt(poker.size())); }while(pokerAfterShuffle.contains(p)); pokerAfterShuffle.add(p); } System.out.println("---------洗牌结束...---------"); } /** * 打印输出List方法 */ public void printList(){ System.out.print("洗牌后:"+"\n"+"[ "); for(Poker p:pokerAfterShuffle){ System.out.print(p.getColor()+p.getNum()+" "); } System.out.println("]"); } /** * 创建玩家(两个) * */ public void createPlayers(){ try{ for(int i = 0;i < 2; i++){ Player p = new Player(); //提示输入玩家ID System.out.println("请输入第"+(i+1)+"位玩家的ID:"); System.out.println("请输入玩家ID:"); p.setID(input.next()); if(p.getID().charAt(0)<'0'||p.getID().charAt(0)>'9') throw new InputMismatchException("输入有误!请输入10以内的整数!!"); System.out.println("请输入玩家姓名:"); p.setName(input.next()); //System.out.println(player.contains(new Player(p.getID(),p.getName()))); player.add(p); } }catch(InputMismatchException e){ System.out.println(e.getMessage()); createPlayers(); } } /** * 发牌并比较牌大小 */ public void dealPoker(){ //提示开始发牌 System.out.println("---------开始发牌...---------"); for(int i = 0;i < 2;i++){//控制取牌数 for(int j = 0;j < 2;j++){//控制玩家数 System.out.println("玩家"+player.get(j).getName()+"取牌"); getPoker = pokerAfterShuffle.get(2*i+j); player.get(j).playerPoker.add(getPoker); System.out.println("牌为:"+player.get(j).playerPoker.get(i).getColor()+ player.get(j).playerPoker.get(i).getNum()); } } for(int i = 0;i < 2;i++){//玩家 for(int j = 0;j < 2;j++){//牌数 System.out.println(player.get(i).getName()+":"+ player.get(i).playerPoker.get(j).getColor()+ player.get(i).playerPoker.get(j).getNum()); } } System.out.println("---------发牌结束!---------"); //开始比较牌面大小 Collections.sort(player.get(0).playerPoker,new GameStarter()); Collections.sort(player.get(1).playerPoker,new GameStarter()); System.out.println("各自最大的手牌为:"+"\n"+player.get(0).getName()+":"+ player.get(0).playerPoker.get(1).getColor() +player.get(0).playerPoker.get(1).getNum()); System.out.println(player.get(1).getName()+":"+player.get(1).playerPoker.get(1).getColor() +player.get(1).playerPoker.get(1).getNum()); System.out.println("---------两位玩家各持的手牌为---------"); for(int i = 0;i < 2;i++){//玩家 for(int j = 0;j < 2;j++){//牌数 System.out.println(player.get(i).getName()+":"+ player.get(i).playerPoker.get(j).getColor()+ player.get(i).playerPoker.get(j).getNum()); } } System.out.println("-------------比较大小------------"); if(compare(player.get(0).playerPoker.get(1),player.get(1).playerPoker.get(1))==1) System.out.println("玩家"+player.get(0).getName()+"获胜!"); else System.out.println("玩家"+player.get(1).getName()+"获胜!"); System.out.println("---------------游戏结束--------------"); } @Override /** * 用到Collections工具类的sort方法 * 需要重写comparator方法,对泛型为Poker的List进行排序 */ public int compare(Poker o1, Poker o2) { String[] s = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; if(Arrays.asList(s).indexOf(o1.getNum()) > Arrays.asList(s).indexOf(o2.getNum())) return 1; else if(Arrays.asList(s).indexOf(o1.getNum()) == Arrays.asList(s).indexOf(o2.getNum())){ if(poker.indexOf(o1) > poker.indexOf(o2)) return 1; else return -1; } else return -1; } }
getPoker = pokerAfterShuffle.get(
2
*i+j);怎么理解??
运行结果: