package cardPlay; public class card { public String color; public String num; public card(String color,String num){ this.color=color; this.num=num; } }
package cardPlay; import java.util.HashSet; import java.util.Set; public class player { public int id; public String name; public Set cards; public player(int id,String name){ this.id=id; this.name=name; this.cards=new HashSet(); } }
import java.util.Arrays; import java.util.Comparator; import java.util.List; /** * 比较规则 * @author jelly * */ public class Compare implements Comparator<card> { @Override public int compare(card o1,card 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.num) == nums.indexOf(o2.num)) return (((Integer) (color.indexOf(o1.color))) .compareTo((Integer) (color.indexOf(o2.color)))); else { return (((Integer) nums.indexOf(o1.color))) .compareTo(((Integer) nums.indexOf(o2.color))); } } }
package cardPlay; import java.util.*; public class Text { public List<card> cardList=new ArrayList<card>() ; public String[] nums={"2","3","4","5","6","7","8","9","10","J","Q","K","A"}; public String[] colors={"黑桃","红桃","梅花","方块"}; //创建卡牌 public void cardAdd(){ for(int i=0;i<4;i++){ for(int j=0;j<13;j++){ card c=new card((colors[i]),(nums[j])); cardList.add(c); } } System.out.println("创建卡牌成功"); for(card x:cardList) System.out.print(x.color+x.num+","); //for(int k=0;k<52;k++){ // //card temp=(card)cardList.get(k); //System.out.print(temp.color+temp.num+" "); //} System.out.println();} //洗牌 public void change(){ Random random=new Random(); for(int i=0;i<4;i++){ int k=cardList.size(); Integer j=random.nextInt(k); card temp=(card)cardList.get(j); cardList.remove(temp); cardList.add(i,temp); } System.out.println("洗牌结束"); } //创建玩家 public void playerAdd(){ int i=0; while(i<1){ try{ System.out.println("输入第一个玩家ID"); Scanner scan=new Scanner(System.in); int id1=scan.nextInt(); System.out.println("输入第一个玩家姓名"); String name1=scan.next(); player player1=new player(id1,name1); i++;}catch(Exception e){ System.out.println("请输入整数数字"); }} i=0; while(i<1){ try{ System.out.println("输入第二个玩家ID"); Scanner scan=new Scanner(System.in); int id2=scan.nextInt(); System.out.println("输入第二个玩家姓名"); String name2=scan.next(); player player2=new player(id2,name2); i++;}catch(Exception e){ System.out.println("请输入整数数字"); }} } //比较大小 public void comparator(){ Compare compare = new Compare(); List<card> com=new ArrayList<card>(); com.add(cardList.get(0)); com.add(cardList.get(1)); com.add(cardList.get(2)); com.add(cardList.get(3)); Collections.sort(com,compare); for(card x:com) System.out.println(x.num+x.color); } public static void main(String[] args){ Text text=new Text(); text.cardAdd(); text.change(); // text.playerAdd(); text.comparator(); }}
哪有闲工夫帮你写这种基础代码,你应该自己写,遇到问题再来问