都是已存在
2022-02-24 18:32
package com.imooc.poker; /** * 扑克牌类 */ import java.util.Comparator; public class Poker implements Comparator<Poker> { public String color; public String point; public Poker(){ } public Poker(String color,String point){ this.color=color; this.point=point; } @Override public int compare(Poker o1, Poker o2) { String colors="方片,梅花,红桃,黑桃"; String points="2,3,4,5,6,7,8,9,10,J,Q,K,A"; int x=points.indexOf(o1.point); int y=points.indexOf(o2.point); if(x>y){ return -1; }else if(x<y){ return 1; }else{ int m=colors.indexOf(o1.color); int n=colors.indexOf(o2.color); if(m>n){ return -1; }else if(m<n){ return 1; }else{ return 0; } } } @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Poker poker = (Poker) o; return color.equals(poker.color) && point.equals(poker.point); } }
package com.imooc.poker; /** *玩家类 */ import java.util.ArrayList; import java.util.List; public class Player { public int id; public String name; public List<Poker> handList; public Player(){ } public Player(int id,String name){ this.id=id; this.name=name; this.handList=new ArrayList<Poker>(); } }
package com.imooc.poker; import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.InputMismatchException; import java.util.List; import java.util.Random; import java.util.Scanner; import java.util.Set; public class PokerGame { public List<Poker> pokerList; public String[] colors=new String[]{"方片","梅花","红桃","黑桃"}; public String[] points=new String[]{"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; public Set<Poker> pokerSet; public List<Player> playerList; public Player player1; public Player player2; public void createPoker(){ System.out.println("----------创建扑克牌...----------"); pokerList=new ArrayList<Poker>(); Poker poker=new Poker(); for(int i=0;i<colors.length;i++){ String color=colors[i]; for(int j=0;j<points.length;j++){ String point=points[j]; pokerList.add(new Poker(colors[i],points[j])); } } System.out.println("----------扑克牌创建成功!----------"); for(int i=0;i<pokerList.size();i++){ Poker pk=pokerList.get(i); if(i==0){ System.out.print("为:["+pk.color+pk.point+","); }else if(i==pokerList.size()-1){ System.out.print(pk.color+pk.point+"]\n"); }else{ System.out.print(pk.color+pk.point+","); } } } public void shuffleCards(){ System.out.println("----------开始洗牌...----------"); pokerSet=new HashSet<Poker>(); Random random=new Random(); while(pokerSet.size()<52){ pokerSet.add(pokerList.get(random.nextInt(52))); } System.out.println("----------洗牌结束!----------"); } public void createPlayers() { System.out.println("----------创建玩家...----------"); playerList=new ArrayList<Player>(); Scanner scanner = new Scanner(System.in); for (int i = 1; i <= 2; i++) { System.out.println("请输入第" + i + "位玩家的ID和姓名:"); System.out.println("输入ID:"); int id; while(true) { try { id = scanner.nextInt(); break; } catch (InputMismatchException e) { System.out.println("请输入整数类型的ID!"); System.out.println("输入ID:"); scanner=new Scanner(System.in); } } System.out.println("输入姓名:"); String name = scanner.next(); playerList.add(new Player(id,name)); } for (Player player:playerList) { System.out.println("----欢迎玩家:"+player.name); } } public void dealCards(){ System.out.println("----------开始发牌...----------"); Poker[] pokers=new Poker[pokerSet.size()]; pokerSet.toArray(pokers); player1=playerList.get(0); player2=playerList.get(1); System.out.println("玩家"+player1.name+"-拿牌"); player1.handList.add(pokers[0]); System.out.println("玩家"+player2.name+"-拿牌"); player2.handList.add(pokers[1]); System.out.println("玩家"+player1.name+"-拿牌"); player1.handList.add(pokers[2]); System.out.println("玩家"+player2.name+"-拿牌"); player2.handList.add(pokers[3]); System.out.println("----------发牌结束!----------"); } public void StartGame(){ System.out.println("----------开始游戏...----------"); Collections.sort(player1.handList,new Poker()); Poker poker1=new Poker(player1.handList.get(0).color, player1.handList.get(0).point); System.out.println("玩家:"+player1.name+"最大的手牌为:"+poker1.color+poker1.point); Collections.sort(player2.handList,new Poker()); Poker poker2=new Poker(player2.handList.get(0).color, player2.handList.get(0).point); System.out.println("玩家:"+player2.name+"最大的手牌为:"+poker2.color+poker2.point); List<Poker> vs=new ArrayList<Poker>(); vs.add(poker1); vs.add(poker2); Collections.sort(vs,new Poker()); Poker poker3=vs.get(0); if(player1.handList.contains(poker3)){ System.out.println("----------玩家:"+player1.name+"获胜!----------"); }else{ System.out.println("----------玩家:"+player2.name+"获胜!----------"); } System.out.println("玩家各自的手牌为:"); for(int i=0;i<player1.handList.size();i++){ Poker hd1=player1.handList.get(i); if(i==0){ System.out.print(player1.name+":["+hd1.color+hd1.point+","); }else { System.out.print(hd1.color+hd1.point+"]\n"); } } for(int i=0;i<player2.handList.size();i++){ Poker hd2=player2.handList.get(i); if(i==0){ System.out.print(player2.name+":["+hd2.color+hd2.point+","); }else { System.out.print(hd2.color+hd2.point+"]\n"); } } } public static void main(String[] args) { PokerGame pokerGame=new PokerGame(); pokerGame.createPoker(); pokerGame.shuffleCards(); pokerGame.createPlayers(); pokerGame.dealCards(); pokerGame.StartGame(); } }
还没有人回答问题,可以看看其他问题
Java入门第三季
409787 学习 · 4340 问题
相似问题