我试着根据他们的军衔和军装来分类扑克牌,但是还没有完全分类,这是代码,请注意,套装是排序的,而不是排名。
我的手在那类人面前:
1:红心之王2: 4钻石3: 7红桃王5: 3红宝石6: 5钻石7:钻石王8:钻石王9: 4黑桃10: 3钻石3
之后:
1: 4黑桃2:红心之王3:红桃3:2红桃王5:球杆之王7:钻石王8: 5钻石9: 4钻石10: 3钻石
卡片对象
public int compareTo(Card that)
{
if(this.suit.ordinal() > that.suit.ordinal())
{
return 1;
}
if(this.suit.ordinal() < that.suit.ordinal())
{
return -1;
}
int rank1 = (this.rank.ordinal() + 11) % 13; //A>K
int rank2 = (that.rank.ordinal() + 11) % 13;
if(rank1 > rank2) return 1;
if(rank1 < rank2) return -1;
return 0;
}播放器对象
public void tryTest()
{
Card temp = new Card();
for(int i=0;i<countCard;i++)
{
for(int j=0;j<countCard;j++)
{
if(playerHand[i].compareTo(playerHand[j]) > 0)
{
temp=this.playerHand[j];
this.playerHand[j] = this.playerHand[i];
this.playerHand[i] = temp;
}
}
}
}Enum军衔
ACE,
TWO,
THREE,
FOUR,
FIVE,
SIX,
SEVEN,
EIGHT,
NINE,
TEN,
JACK,
QUEEN,
KING;Enum套装
DIAMONDS,
CLUBS,
HEARTS,
SPADES;发布于 2015-03-17 18:41:45
查看一些标准的排序算法。您现在拥有的代码将所有内容与其他所有内容进行比较,然后在其中一个大于另一个的情况下交换它们。然而,这可能会导致您向后交换:
1,2
因此,2>1交换
2,1
最近的算法是Bubble排序,它使用稍微不同的索引:
for(int i=0;i<countCard;i++)
{
for(int j=0;j<countCard -1;j++)
{
if(playerHand[j].compareTo(playerHand[j+1]) > 0)
{
temp=this.playerHand[j+1];
this.playerHand[j+1]= this.playerHand[j];
this.playerHand[j] = temp;
}
}
}https://stackoverflow.com/questions/29107069
复制相似问题