定义一副纸牌,有发牌功能,3个人在玩,每个人10张,然后按花色排序看牌再按点数排序看牌应该怎样去做,3个人分别为张三,李四,王五
1 回答
已采纳
未来的开拓者
TA贡献1条经验 获得超1个赞
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
public class Main {
public static Card getBiggest(List<Card> card){//得到最大的那张牌。
if(card==null)return null;
Iterator<Card>iterator=card.iterator();//用Iterator遍历集合
Card temp=new Card(Card.Points._3, Card.Colour.黑桃 );//temp初始值为最小的牌
while (iterator.hasNext()){
Card c=iterator.next();
if(c.compareTo(temp)>0){
temp=c;
}
}
return temp;
}
public static void main(String[] args) {
/*
*
* 创建有序牌堆
*/
List<Card> card=new ArrayList<Card>();//创建空牌堆
for(Card.Points p:Card.Points.values()){
for (Card.Colour c: Card.Colour.values()){
card.add(new Card(p,c));
}
}
// for(Card c:card){
// System.out.println(c.toString());
// }
/*
* 洗牌
* shuffle()方法
*
* */
Collections.shuffle(card);
/*
* 发牌
*
* */
List<Card> player1=new ArrayList<Card>();
List<Card> player2=new ArrayList<Card>();
List<Card> player3=new ArrayList<Card>();
int newPosition=1;
for (Card c:card
) {
if(newPosition%3==0)player3.add(c);
if(newPosition%3==1)player1.add(c);
if(newPosition%3==2)player2.add(c);
if(newPosition>=9)break;//修改参数可以改变剩余牌数
newPosition++;
}
// for(Card c:player1){
// System.out.println(c.toString());
// }
Card p1=getBiggest(player1);
Card p2=getBiggest(player2);
Card p3=getBiggest(player3);
if(p1.compareTo(p2)>0&&p1.compareTo(p3)>0){
System.out.println("player1 has the biggest card:"+p1.toString());
}else if(p2.compareTo(p1)>0&&p2.compareTo(p3)>0){
System.out.println("player2 has the biggest card:"+p2.toString());
}else {
System.out.println("player3 has the biggest card:"+p3.toString());
}
}
}基本功能是实现了,看看对你有没有用。
添加回答
举报
0/150
提交
取消
