package com.sun;
/**
* 扑克牌类
* @author T430u
*
*/
public class Card {
private String name;//大小
private String color;//花色
//无参构造器
public Card(){
}
//含参构造器
public Card(String name,String color){
this.name =name;
this.color =color;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((color == null) ? 0 : color.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Card))
return false;
Card other = (Card) obj;
if (color == null) {
if (other.color != null)
return false;
} else if (!color.equals(other.color))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public String toString() {
return color + name;
}
}
package com.sun;
import java.util.Comparator;
/**
* 比较类
* @author T430u
*
*/
public class CardComparator implements Comparator<Card> {
public int compare(Card arg0, Card arg1) {
String n = ((Card) arg0).getName();
String n1 = ((Card) arg1).getName();
int t = 0;
if((t = n.compareTo(n1)) == 0){
String c = ((Card) arg0).getColor();
String c1 = ((Card) arg1).getColor();
t = c.compareTo(c1);
}
return t;
}
}
package com.sun;
import java.util.*;
/**
* 玩家类
* @author T430u
*
*/
public class Player {
private int id;//id
private String name;//姓名
List<Card> holdCard;//手牌
//无参构造器
Player(){
}
//含参构造器
Player(int id,String name){
this.id=id;
this.name =name;
this.holdCard=new ArrayList<Card>();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.sun;
import java.util.*;
/**
* 游戏类
* @author T430u
*
*/
public class Game {
public ArrayList<Card> l,l2;
Scanner input;
public Game(){
this.l2=new ArrayList<Card>();
this.l=new ArrayList<Card>();
this.input=new Scanner(System.in);
}
/**
* 创建一副扑克牌
* @param args
*/
public void careatCard(){
System.out.println("***************创建一副扑克牌******************");
String b="黑桃";
String m="梅花";
String r="红桃";
String f="方片";
Card[] c ={
new Card("2",f),new Card("3",f),new Card("4",f),new Card("5",f),
new Card("6",f),new Card("7",f),new Card("8",f),new Card("9",f),
new Card("10",f),new Card("J",f),new Card("Q",f),new Card("K",f),
new Card("A",f),new Card("2",r),new Card("3",r),new Card("4",r),
new Card("5",r),new Card("6",r),new Card("7",r),new Card("8",r),
new Card("9",r),new Card("10",r),new Card("J",r),new Card("Q",r),
new Card("K",r),new Card("A",r),new Card("2",m),new Card("3",m),
new Card("4",m),new Card("5",m),new Card("6",m),new Card("7",m),
new Card("8",m),new Card("9",m),new Card("10",m),new Card("J",m),
new Card("Q",m),new Card("K",m),new Card("A",m),new Card("2",b),
new Card("3",b),new Card("4",b),new Card("5",b),new Card("6",b),
new Card("7",b),new Card("8",b),new Card("9",b),new Card("10",b),
new Card("J",b),new Card("Q",b),new Card("K",b),new Card("A",b)};
System.out.println("*****************扑克牌创建成功********************");
System.out.println("为:");
System.out.println(Arrays.toString(c));
l.addAll(Arrays.asList(c));
}
/**
* 洗牌
* @param args
*/
public void washCard(){
System.out.println("********************开始洗牌***********************");
ArrayList<Card> result=new ArrayList<Card>();
List<Integer> o=new ArrayList<Integer>();
for(int i=0;i<52;i++){
Integer index;
do{
Random r=new Random();
index=r.nextInt(52);
}while(o.contains(index));
o.add(index);
//index就是捡出来的牌,加到新的数组中
Card card=l.get(index);
result.add(card);
}
System.out.println("******************洗牌结束**********************");
}