package com.imooc; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Scanner; import java.util.Set; /** * 控制器,主函数 * @author jelly * */ public class Controller { List<Poker> poker = new ArrayList<Poker>(); List<Player> player = new ArrayList<Player>(); Set<Poker> newPoker; //创建poker牌 public void build() { System.out.println("-----------开始创建扑克牌-----------"); String[] suits = { "方片", "梅花", "红桃", "黑桃" }; String[] numbers = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", }; System.out.println("-----------扑克创建成功----------"); System.out.print("为:["); for (int i = 0; i < 4; i++) { for (int j = 0; j < 13; j++) { poker.add(new Poker(suits[i], numbers[j])); } } for (Poker pk : poker) { System.out.print(pk + ","); } System.out.println("]"); } // 洗牌 public void shuffle() { System.out.println("----------开始洗牌----------"); this.newPoker = new HashSet<>(poker); System.out.println("----------洗牌结束----------"); } // 创建玩家 public void buildPlayer() { System.out.println("----------创建玩家----------"); int i = 0; while (i < 2) { System.out.println("请输入第" + (i + 1) + "位玩家的ID和姓名"); try { Scanner in = new Scanner(System.in); System.out.println("输入ID:"); int id = in.nextInt(); System.out.println("输入姓名:"); String name = in.next(); player.add(new Player(id, name)); i++; } catch (Exception e) { System.out.println("请输入整数类型的ID"); } } for (int k = 0; k < player.size(); k++) { System.out.println("欢迎玩家:" + player.get(k).getName()); } } // 发牌 public void deal() { System.out.println("----------开始发牌----------"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { Iterator<Poker> it = newPoker.iterator(); Poker poker = it.next(); player.get(j).handPoker.add(poker); System.out.println("玩家" + player.get(j).getName() + "拿牌"); newPoker.remove(poker); } } System.out.println("----------发牌结束----------"); } public void game() { System.out.println("----------开始游戏----------"); Compare compare = new Compare(); for (int i = 0; i < 2; i++) { Collections.sort(player.get(i).handPoker, compare); System.out.println("玩家" + player.get(i).getName() + "最大的手牌为" + player.get(i).handPoker.get(1)); } if (compare.compare(player.get(0).handPoker.get(0), player.get(1).handPoker.get(1)) > 0) { System.out.println("玩家" + player.get(0).getName() + "获胜"); } else { System.out.println("玩家" + player.get(1).getName() + "获胜"); } System.out.println("玩家各自的手牌为"); for (int j = 0; j < 2; j++) { System.out.println("玩家"+player.get(j).getName()); for (Poker poker : player.get(j).handPoker) { System.out.println(poker+" "); } } } public static void main(String[] args) { Controller cr = new Controller(); cr.build(); cr.shuffle(); cr.buildPlayer(); cr.deal(); cr.game(); } }
恩,有一些漏洞,应该用collection.shuffle(),方法,不过我当时不知道这个方法,谢谢各位了,多多学习
然后 game方法 中对比的应该是2个较大的手牌么?,
if (compare.compare(player.get(0).handPoker.get(0),
player.get(1).handPoker.get(1)) > 0)
此句第一个handPoker.get(0),应该是较小那一个,是否该改为handPoker(1) ?
洗牌部分真的洗牌了吗?
这样多次运行结果应该不会变,因为并没有洗牌,只是把newpoker赋值了poker,然而前面应该加一句 Collections.shuffle(poker);
才可做到洗牌效果吧?
可能不对,考虑考虑
Controller 中 创建Poker 语句
for (Poker pk : poker) {
System.out.print(pk + ",");
}
为什么我输出的是地址符?
代码无懈可击,但我觉得有个地方可能有问题:
老师要求随机洗牌后按顺序从第一张扑克开始发牌,用Set(无序的)集合,用迭代器取牌并不符合发牌规则,这更像从扑克牌中随机取牌,如果洗牌后的排序不变,你的发牌出现的结果是随机的:
// 发牌 public void deal() { System.out.println("----------开始发牌----------"); for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { Iterator<Poker> it = newPoker.iterator(); Poker poker = it.next(); player.get(j).handPoker.add(poker); System.out.println("玩家" + player.get(j).getName() + "拿牌"); newPoker.remove(poker); } } System.out.println("----------发牌结束----------"); }
Set<Poker> newPoker; //代码中洗牌后的扑克牌的集合
厉害啊
package com.imooc; import java.util.Arrays; import java.util.Comparator; import java.util.List; /** * 比较规则 * @author jelly * */ public class Compare implements Comparator<Poker> { @Override public int compare(Poker o1, Poker o2) { List<String> nums = Arrays.asList("2", "3", "4", "5", "6", "7", "8", "9", "J", "Q", "K", "A"); List<String> color = Arrays.asList("方块", "梅花", "红桃", "黑桃"); if (nums.indexOf(o1.getNum()) == nums.indexOf(o2.getNum())) return (((Integer) (color.indexOf(o1.getSuit()))) .compareTo((Integer) (color.indexOf(o2.getSuit())))); else { return (((Integer) nums.indexOf(o1.getNum()))) .compareTo(((Integer) nums.indexOf(o2.getNum()))); } } }
package com.imooc; /** * poker class * @author jelly * */ public class Poker { private String suit;//扑克牌的花色 private String num;//poker's number public Poker(String suit,String num){ this.suit = suit; this.num = num; } public String getSuit() { return suit; } public void setSuit(String suit) { this.suit = suit; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String toString() { return this.suit+this.num; } }
package com.imooc; import java.util.ArrayList; import java.util.List; /** * 玩家类 * @author jelly * */ public class Player { private int ID;//万家的ID private String name; List<Poker> handPoker = new ArrayList<Poker>(); public Player(int ID,String name){ this.ID = ID; this.name = name; } public int getID() { return ID; } public void setID(int iD) { ID = iD; } public String getName() { return name; } public void setName(String name) { this.name = name; } }