import java.util.Arrays;
import java.util.Collections;
import java.util.Random;
public class CardTest
{
public static void main(String[] args)
{
Cards c = new Cards();
c.shuffle();//洗牌
Player p1 = new Player("张三");
Player p2 = new Player("李四");
for(int i=0;i<10;i++)
{
p1.getOneCard(c);
p2.getOneCard(c);
p1.compare(p2);
c.getCount(); //显示还几张牌
}
}
}
//扑克类
class Cards
{
String [] type = new String[]{"黑桃","红桃","梅花","方片"};
String[] card = new String[52];
int[] overCard = new int[52]; //保存抽出的牌索引
int count = 52; //还剩多少张牌没抽
public Cards()
{
init();
}
void init()
{
int n=0;
for(int i=0;i<type.length;i++)
{
for(int j=1;j<=13;j++)
{
card[n] = type[i] + j;
n++;
}
}
}
void display()
{
for(int k=0;k<card.length;k++)
{
System.out.println(card[k]);
}
}
void shuffle()
{
Collections.shuffle(Arrays.asList(card));
}
public String getCard()
{
if(count <=0 )
{
System.out.println("已经没有牌了");
}
int random = 0;
while(true)
{
random = new Random().nextInt(52);
if(!Arrays.asList(overCard).contains(random))
{
overCard[52-count] = random;
break;
}
}
count--;
return card[random];
}
public void getCount()
{
System.out.println("还剩:" + count + "张牌!");
}
}
//玩家类
class Player
{
String name;
String card; Player(String name)
{
this.name = name;
}
public void getOneCard(Cards c)
{
this.card = c.getCard();
}
public void compare(Player p)
{
String pCard = p.getCard().replace("11", "J").replace("12", "Q")
.replace("13", "K").replace("1", "A");
String myCard = this.getCard().replace("11", "J").replace("12", "Q")
.replace("13", "K").replace("1", "A");
System.out.println(p.getName()+"抓的是:" + pCard);
System.out.println(this.getName()+"抓的是:" + myCard);
int play1 = Integer.parseInt(p.getCard().replaceAll("\\W", ""));
int my = Integer.parseInt(this.getCard().replaceAll("\\W", ""));
if(play1>my)System.out.println(p.getName()+"赢了!");
if(my>play1)System.out.println(this.getName()+"赢了!");
if(play1==my)System.out.println("平手!");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCard() {
return card;
}
public void setCard(String card) {
this.card = card;
}
}