Youngguava
2015-04-15 22:04
package pokergame; import java.util.ArrayList; import java.util.InputMismatchException; import java.util.Iterator; import java.util.Random; import java.util.Scanner; //使用int来表示扑克,点数为2-14,花色为100-400,如104即为方片4 class Poker{ private int color; private int num; private int poker; private String colorString; private String numString; //设置扑克的花色和点数 public Poker(int color,int num) { this.color = color; this.num = num; this.poker = color*100+num; } public Poker(){ } //将扑克的花色与点数提取出来 public Poker(int pokerint){ this.num = pokerint%100; this.color = pokerint/100; } public int getPoker() { return poker; } //将int表示的扑克转换为字符串 public String toStringPoker(int pokerint){ num = pokerint%100; color = pokerint/100; if (num == 11) { this.numString = "J"; } else if (num == 12) { this.numString = "Q"; }else if (num == 13) { this.numString = "K"; }else if (num == 14) { this.numString = "A"; }else { this.numString = num+""; } switch (color) { case 1: this.colorString = "方片"; break; case 2: this.colorString = "梅花"; break; case 3: this.colorString = "红桃"; break; case 4: this.colorString = "黑桃"; break; } return (colorString+numString); } } //玩家类,用来表示玩家 class Player{ private int ID; private String name; //玩家的手牌集合,用ArrayList来表示 ArrayList<Integer> poker = new ArrayList<>(); Poker pk = new Poker(); //用来存储玩家最大的手牌 private int maxPoker; 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; } public ArrayList<Integer> getPoker() { return poker; } public void setPoker(ArrayList<Integer> poker) { this.poker = poker; } //将玩家的手牌转换为字符串并输出,index代表玩家的第几张手牌 public String getPokerString(int index){ return pk.toStringPoker(poker.get(index)); } public int getMaxPoker() { return maxPoker; } public void setMaxPoker(int maxPoker) { this.maxPoker = maxPoker; } } public class PlayGame { ArrayList<Integer> poker; //用来存放洗牌后的扑克 int poker1[]; //创建两个玩家的对象 Player player1; Player player2; public PlayGame(){ this.poker = new ArrayList<>(); this.poker1 = new int[52]; this.player1 = new Player(); this.player2 = new Player(); } //创建一副扑克,将其放在ArrayList集合中 public void makePoker(){ for (int i = 1; i <= 4; i++) { for (int j = 2; j <= 14; j++) { poker.add(new Poker(i,j).getPoker()); } } } //将创建好的扑克输出到屏幕上 public void getPoker(){ Poker pk = new Poker(); System.out.print("为:["); for (Iterator<Integer> iterator = poker.iterator(); iterator.hasNext();) { Integer integer = (Integer) iterator.next(); System.out.print(pk.toStringPoker(integer)+" "); } System.out.println("]"); } //用来将排序后的ArrayList的扑克打乱顺序并放在poker1数组中 public void shuffle(){ //将ArrayList的扑克先全部复制到poker1数组中 for (int i = 0; i < poker1.length; i++) { poker1[i] = poker.get(i); } //打乱poker1中扑克 Random rd = new Random(); for (int i = 0; i < 52; i++) { int rdnum = rd.nextInt(52); poker1[i] = poker1[i] + poker1[rdnum]; poker1[rdnum] = poker1[i] - poker1[rdnum]; poker1[i] = poker1[i] - poker1[rdnum]; } } //用来创建玩家的方法 public void makePlayer(Player player){ int ID; System.out.print("输入ID:"); @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); while (true) { try { if (scanner.hasNext()) { ID = scanner.nextInt(); player.setID(ID); } break; } catch (InputMismatchException e) { scanner.next(); System.out.println("ID应该为整数"); } } System.out.print("输入姓名:"); if (scanner.hasNext()) { player.setName(scanner.next()); } } //找出一个玩家手中最大的牌 public String comparPoker(Player player){ if (player.poker.get(0)%100 > player.poker.get(1)%100) { player.setMaxPoker(player.poker.get(0)); return player.pk.toStringPoker(player.poker.get(0)); }else if (player.poker.get(0)%100 < player.poker.get(1)%100) { player.setMaxPoker(player.poker.get(1)); return player.pk.toStringPoker(player.poker.get(1)); }else if (player.poker.get(0)/100 > player.poker.get(1)/100) { player.setMaxPoker(player.poker.get(0)); return player.pk.toStringPoker(player.poker.get(0)); }else if (player.poker.get(0)/100 < player.poker.get(1)/100) { player.setMaxPoker(player.poker.get(1)); return player.pk.toStringPoker(player.poker.get(1)); }else { player.setMaxPoker(player.poker.get(0)); return player.pk.toStringPoker(player.poker.get(0)); } } //将两个玩家手中最大的牌做比较,找出其中的赢家 public String comparPoker(Player player1,Player player2){ if (player1.getMaxPoker()%100 > player2.getMaxPoker()%100) { return player1.getName(); }else if (player1.getMaxPoker()%100 < player2.getMaxPoker()%100) { return player2.getName(); }else if (player1.getMaxPoker()/100 > player2.getMaxPoker()/100) { return player1.getName(); }else if (player1.getMaxPoker()/100 < player2.getMaxPoker()/100) { return player2.getName(); }else { return "平手"; } } public static void main(String[] args) { PlayGame pg = new PlayGame(); System.out.println("----------创建一副扑克牌----------"); pg.makePoker(); System.out.println("----------扑克牌创建成功---------"); pg.getPoker(); System.out.println("--------开始洗牌---------"); pg.shuffle(); System.out.println("--------洗牌结束---------"); System.out.println("--------创建玩家---------"); System.out.println("请输入第一位玩家的ID和姓名:"); pg.makePlayer(pg.player1); System.out.println("请输入第二位玩家的ID和姓名:"); pg.makePlayer(pg.player2); System.out.println("\n欢迎玩家"+pg.player1.getName()); System.out.println("欢迎玩家"+pg.player2.getName()); System.out.println("\n--------开始发牌---------"); pg.player1.poker.add(pg.poker1[0]); System.out.println("玩家"+pg.player1.getName()+"拿牌"); pg.player2.poker.add(pg.poker1[1]); System.out.println("玩家"+pg.player2.getName()+"拿牌"); pg.player1.poker.add(pg.poker1[2]); System.out.println("玩家"+pg.player1.getName()+"拿牌"); pg.player2.poker.add(pg.poker1[3]); System.out.println("玩家"+pg.player2.getName()+"拿牌"); System.out.println("--------发牌结束----------"); System.out.println("--------开始游戏----------"); System.out.println("玩家"+pg.player1.getName()+"的最大的手牌为"+pg.comparPoker(pg.player1)); System.out.println("玩家"+pg.player2.getName()+"的最大的手牌为"+pg.comparPoker(pg.player2)); String resultString = pg.comparPoker(pg.player1, pg.player2); if (resultString != "平手") { System.out.println("----------玩家"+resultString+"获胜----------"); }else { System.out.println("------------平手-------------"); } System.out.println("玩家各自的手牌为:"); System.out.println(pg.player1.getName()+"["+pg.player1.pk.toStringPoker(pg.player1.poker.get(0))+","+pg.player1.pk.toStringPoker(pg.player1.poker.get(1))+"]"); System.out.println(pg.player2.getName()+"["+pg.player2.pk.toStringPoker(pg.player2.poker.get(0))+","+pg.player2.pk.toStringPoker(pg.player2.poker.get(1))+"]"); } }
for (int i = 0; i < 4; i++) { if(i%2==0){ System.out.println("玩家"+p1.getName()+"拿牌!"); p1.setMyPoker(pokerList.get(i)); }else{ System.out.println("玩家"+p2.getName()+"拿牌!"); p2.setMyPoker(pokerList.get(i)); } }
拿牌那里我觉得这样写好点,可扩展性大些,比如要拿10张牌,只需要改 for循环中 i<10就可以了。 还有 那个异常处理,直接scanner.next()不会报错? 我试了一下,我的要报错。
Java入门第三季
409792 学习 · 4340 问题
相似问题