package cardPlay;
import java.util.*;
public class Text {
public List<card> cardList=new ArrayList<card>() ;
List<player> player = new ArrayList<player>();
//创建卡牌
public void cardAdd(){
String[] nums={"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
String[] colors={"黑桃","红桃","梅花","方块"};
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+",");
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("洗牌结束");
for(card x:cardList)
System.out.print(x.color+x.num+",");
}
//创建玩家
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++;
player.add(player1);}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++;
player.add(player2);}catch(Exception e){
System.out.println("请输入整数数字");
}}
}
//比较大小
public void comparator(){
Compare compare = new Compare();
List<card> com=new ArrayList<card>();
List<card> max=new ArrayList<card>();
com.add(cardList.get(0));
com.add(cardList.get(1));
com.add(cardList.get(2));
com.add(cardList.get(3));
System.out.println(player.get(0).name+"有卡牌"+com.get(0).num+com.get(0).color+com.get(2).num+com.get(2).color);
System.out.println(player.get(1).name+"有卡牌"+com.get(1).num+com.get(1).color+com.get(3).num+com.get(3).color);
max.add(com.get(0));
Collections.sort(com,compare);//这句话是什么意思???
for(int i=1;i<4;i++){
if(compare.compare(com.get(i),max.get(0))>0)
max.add(0,com.get(i));
}
if(max.get(0).equals(cardList.get(0))||max.get(0).equals(cardList.get(2)))
System.out.println(player.get(0).name+"获胜");
else
System.out.println(player.get(1).name+"获胜");
}
public static void main(String[] args){
Text text=new Text();
text.cardAdd();
text.change();
text.playerAdd();
text.comparator();
}}
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();
}
}
package cardPlay;
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)));
}
}
}