结构布局怎样修改优化,还有俩人比较的地方感觉写的好繁琐,自己都快绕进去了 哎
*********************************************************************************************
Card 类
package gamecard;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Card implements Comparable<Card>{
/**
* 无参和有参构造器
*/
public Card() {
}
public Card(String color, String value) {
this.color = color;
this.value = value;
}
//static vocabulary
public final int cardSum = 13;
public final int colorSum = 4;
public final String[] colors = { "红桃", "黑桃", "方块", "梅花", "大" };
public final String[] values = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "王" };
public String VVV = "2345678910JQKA王 ";
public String CCC = "梅花方块黑桃红桃小大 ";
List<Card> cardList = new ArrayList<Card> ();
/**
* 创建一副扑克牌
* 1、建立一个list容器来存放扑克牌
* 2、循环遍历产生扑克牌
* 3、把扑克牌加入list容器
* 4、添加大小王
* 5、返回list容易值
* @return
*/
public void createCard() {
for (int i = 0; i < cardSum; ++i) {
for (int j = 0; j < colorSum; ++j) {
color = colors[j];
value = values[i];
cardList.add(new Card(color,value));
}
}
cardList.addAll(Arrays.asList(new Card("大", "王"), new Card("小", "王")));
printCards();
}
//打印牌
public void printCards() {
int m = 0;
for (Card card : cardList) {
m++;
System.out.print(card.toString());
if (0 == m%4) {
System.out.print("\n");
}
}
System.out.println();
}
/**
* 洗牌
*/
public void shuffleCards() {
Collections.shuffle(cardList);
// printCards();
}
/**
* 牌的花色和大小
* @return
*/
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
private String color;
private String value;
/**
* toString()方法重写
*/
public String toString() {
return " " + this.getColor() + this.getValue() + " \t";
}
@Override
public int compareTo(Card o) {
// TODO Auto-generated method stub
if (VVV.indexOf(this.value) != VVV.indexOf(o.value)) {
return Integer.valueOf(VVV.indexOf(this.value)).compareTo(Integer.valueOf(VVV.indexOf(o.value)));
} else {
return Integer.valueOf(CCC.indexOf(this.color)).compareTo(Integer.valueOf(CCC.indexOf(o.color)));
}
}
}
********************************************************************
玩家类
package gamecard;
import java.util.ArrayList;
import java.util.List;
public class Player {
public List<Card> pCards = new ArrayList<Card>();
//构造器
public Player() {
}
public Player(int id, String name) {
this.id = id;
this.name = name;
}
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;
}
private int id;
private String name;
//手牌增加
public void addpCards(Card card) {
pCards.add(card);
}
@Override
public String toString() {
StringBuilder str = new StringBuilder("");
for (Card card : pCards) {
str.append(card.getColor() + card.getValue() + " ");
}
return "玩家" + this.getName() + "\t手牌 : [ "
+ str + " ]";
}
}