黑马程序员技术交流社区
标题:
斗地主发牌洗牌,终于写完了
[打印本页]
作者:
蓝色风信子
时间:
2016-5-30 23:19
标题:
斗地主发牌洗牌,终于写完了
package 模拟斗地主洗牌发牌;
import java.util.ArrayList;
import java.util.Collections;
import java.util.TreeSet;
class Poker implements Comparable
{
String name;
int age;
public Poker(String name,int age)
{
this.name=name;
this.age=age;
}
@Override
public int compareTo(Object o)
{
Poker p=(Poker)o;
if(p.age==this.age)
return p.name.compareTo(this.name);
return p.age-this.age;
}
}
public class Test
{
public static int pos=1;
public static void main(String[] args)
{
ArrayList<Poker> al=new ArrayList<>();
String[] arr={"♥","♠","♣","♦"};
String[] arr1={"3","4","5","6","7","8","9","10","J","Q","K","A","2"};
for(String color : arr)
{
int count=1;
for(String num : arr1)
{
Poker p=new Poker((color+num),count++);
al.add(p);
}
}
al.add(new Poker("大王",15));
al.add(new Poker("小王",14));
Collections.shuffle(al);
TreeSet<Poker> ts=new TreeSet<>();
TreeSet<Poker> ts1=new TreeSet<>();
TreeSet<Poker> ts2=new TreeSet<>();
ArrayList<Poker> al4=new ArrayList<>();
for(int i=0;i<al.size();i++)
{
if(i>=al.size()-3)
{
al4.add(al.get(i));
}
else
{
if(i%3==0)
ts.add(al.get(i));
if(i%3==1)
ts1.add(al.get(i));
if(i%3==2)
ts2.add(al.get(i));
}
}
getPoker(ts);
getPoker(ts1);
getPoker(ts2);
System.out.print("底牌: ");
for(Poker p : al4)
{
System.out.print(p.name+" ");
}
}
public static void getPoker(TreeSet<Poker> ts)
{
System.out.print("玩家"+pos+++": ");
for(Poker s : ts)
{
System.out.print(s.name+" ");
}
System.out.println();
System.out.println("---------------------------------------------------------");
}
}
作者:
hero_king
时间:
2016-5-30 23:20
嗯这个例子还是很不错的,主要是理解所用到的集合的特点,应用场景。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2