//创建卡牌对象类
public class Card implements Comparable<Card> {
private String shape;
private String id;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
public Card(){
}
public Card(String shape, String id) {
//super();
this.shape = shape;
this.id = id;
}
public String getShape() {
return shape;
}
public void setShape(String shape) {
this.shape = shape;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override//重写了卡牌中花色与序号的排序方法
public int compareTo(Card o) {
// TODO Auto-generated method stub
//return o.getId().compareTo(this.getId());
String[] num = {"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
String[] color = {"黑桃","红桃","梅花","方片"};
int n1 = 0,n2=0,o1=0,o2=0;
for(int i=0;i<num.length;i++){
if(this.getId().equals(num[i]))
n1=i;
if(o.getId().equals(num[i]))
n2=i;}
for(int i=0;i<color.length;i++){
if(this.getShape().equals(color[i]))
o1=i;
if(o.getShape().equals(color[i]))
o2=i;}
if(n1>n2){
return 1;}
else if(n1<n2){
return -1;
}else{
if(o1>o2){
return -1;
}else if(o1<o2){
return 1;
}else
return 0;
}
}
public Card getCards() {
// TODO Auto-generated method stub
return null;
}
}
import java.util.ArrayList;
import java.util.List;
//创建玩家类
public class Player {
private String name ;
private List<Card> cards;
private String ID;
public void Player(){
}
public List<Card> getCards() {
return cards;
}
public Player(String name, String iD) {
//super();
this.name = name;
this.cards = new ArrayList<Card>();
ID = iD;
}
public void setCards(List<Card> cards) {
this.cards = cards;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getID() {
return ID;
}
public void setID(String iD) {
ID = iD;
}
}
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class PaiTest {
public List<Card> cardList;
public PaiTest(){
this.cardList = new ArrayList<Card>();
}
//创建扑克牌的方法
public void purcard(){
String[] sh= {"方片","黑桃","红桃","梅花"};
Card cars = new Card();
//第一个循环是创建4个花色,第二个循环创建每种花色的大小
for(int i=0;i<4;i++){
cars.setShape(sh[i]);
for( int j=2;j<15;j++){
if(j==11){
cars.setId("J");
cardList.add(new Card(sh[i],"J"));
System.out.println("添加了卡牌:"+cars.getShape()+cars.getId());
continue;
}
if(j==12){
cars.setId("Q");
cardList.add(new Card(sh[i],"Q"));
System.out.println("添加了卡牌:"+cars.getShape()+cars.getId());
continue;
}
if(j==13){
cars.setId("K");
cardList.add(new Card(sh[i],"K"));
System.out.println("添加了卡牌:"+cars.getShape()+cars.getId());
continue;
}
if(j==14){
cars.setId("A");
cardList.add(new Card(sh[i],"A"));
System.out.println("添加了卡牌:"+cars.getShape()+cars.getId());
continue;
}
cars.setId(j+"");
System.out.println("添加了卡牌:"+cars.getShape()+cars.getId());
cardList.add(new Card(sh[i],j+""));
}
}
System.out.println("卡牌创建成功--------------------");
for(Card card:cardList)
System.out.println("输出卡牌:"+cardList.size()+card.getShape()+" "+card.getId());
}
public void fapai(){
Scanner sc = new Scanner(System.in);
System.out.println("请输入玩家一的姓名:");
String p1 = sc.next();
Player play1 = new Player(p1, "1");
System.out.println("玩家:"+p1+"成功进入游戏");
System.out.println("请输入玩家二的姓名:");
String p2 = sc.next();
Player play2 = new Player(p2, "2");
System.out.println("玩家:"+p2+"成功进入游戏");
Collections.shuffle(cardList);//打乱集合中的元素
System.out.println(cardList.get(0).getShape()+cardList.get(0).getId());//打乱集合输出的第一个元素
play1.getCards().add(cardList.get(0));//依次给玩家发牌
play2.getCards().add(cardList.get(1));
play1.getCards().add(cardList.get(2));
play2.getCards().add(cardList.get(3));
Collections.sort(play1.getCards());//对玩家1的手牌排序
Collections.sort(play2.getCards());//对玩家2的手牌排序
for(Card card:play1.getCards())
System.out.println("玩家1的卡牌为:"+card.getShape()+card.getId());
System.out.println("最大点数为:"+play1.getCards().get(1).getShape()+play1.getCards().get(1).getId());
for(Card card:play2.getCards())
System.out.println("玩家2的卡牌为:"+card.getShape()+card.getId());
System.out.println("最大点数为:"+play2.getCards().get(1).getShape()+play2.getCards().get(1).getId());
//利用comparable排序
List<Card> newlist = new ArrayList<Card>();//创建新的集合,把玩家1和2的手牌放入
newlist.addAll( play1.getCards());
newlist.addAll( play2.getCards());
System.out.println("-----------------排序前:");
for(Card st:newlist)
System.out.println("排序前:"+st.getShape()+st.getId());
//Collections.sort(newlist, new PaiComparator());
Collections.sort(newlist);
System.out.println("-----------------排序后:");
for(Card st:newlist)
System.out.println(st.getShape()+st.getId());
//这里用equals方法记得要对卡片对象Card重写
if(newlist.get(3).equals(play1.getCards())){
System.out.println(" 玩家: "+play1.getName()+"获胜!!!手牌为:"+newlist.get(3).getShape()+newlist.get(3).getId());
}else
System.out.println(" 玩家: "+play2.getName()+"获胜!!!手牌为:"+newlist.get(3).getShape()+newlist.get(3).getId());
}
public static void main(String[] args) {
PaiTest pt = new PaiTest();
pt.purcard();//创建扑克牌
pt.fapai();//游戏过程
}
}