import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
 
public class PokerServer { /**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
       
ServerSocket ser=new ServerSocket(8888);
System.out.println("服务器启动");
    Socket sc=ser.accept();
    System.out.println("有客户端请求连接,程序开始运行");
     
    //定义一个数组,存放1-54之间的整数,代表54张牌
    //1,2代表大小王
    //3--15代表红桃A-K
    //16--28代表黑桃A-K
    //29-41 代表方片A-K
    //42-54 代表朝花A-K
    int[] a=new int[54];
    for(int i=0;i<54;i++){
     a[i]=i+1;
    }
    //洗牌
    for(int i=0;i<20;i++){
     int random_1=(int) (Math.random()*54+1);
     int random_2=(int) (Math.random()*54+1);
     int b=0;
     //通过取两个随机数,交换数组元素内容,达到洗牌的目的
     b=a[random_1];
     a[random_1]=a[random_2];
     a[random_2]=b;
    }
    //定义四个数组,代表四个玩家 
    int[] ct_1=new int[14];
    int[] ct_2=new int[14];
    int[] ct_3=new int[14];
    int[] ct_4=new int[14];
    PokerServer ps=new PokerServer();
    for(int i=0, j=1;i<54;i+=4,j++){
     //循环最后一次时,进行判断,避免数组越界
     if(i!=52){
       ct_1[j]=i+1;
       ct_1[j]=i+2;
       ct_1[j]=i+3;
       ct_1[j]=i+4;
     } else{
       ct_1[12]=i+1;
       ct_1[12]=i+2;
     }
 
    }
    // 发牌
      ps.sendPoker(sc,ct_1,ct_2,ct_3,ct_4); 
       
}

// 发牌的方法,根据传递进来的四个数组,服务器返回客户端四个数组
public void sendPoker(Socket sc,int[] a,int[] b,int[] c,int[] d) throws IOException{
PrintWriter out;
try {
out = new PrintWriter(
      new BufferedWriter(
      new OutputStreamWriter(sc.getOutputStream())),true);
System.out.println("发牌");
         out.println(a);
         out.println(b);
         out.println(c);
         out.println(d);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
         finally{
          sc.close();
         }
}}

解决方案 »

  1.   

    懒的摘,自己CTRL+C有用的吧import java.util.LinkedList;
    import java.util.Random;
    import net.xz.card.Card;//牌的类,你可以直接换成int型/**
     * 所有卡组类的根类
     * 
     * @see Card
     * @see LinkedList
     * @author XZ
     */
    public class CardsGroup extends LinkedList { private static final long serialVersionUID = 6337740579545408429L; private LinkedList<Card> cardsGroup; /**
     * 设置卡组
     * 
     * @param cards
     *            要设置的卡组
     */
    public void setCardsGroup(Card[] cards) {
    for (int i = 0; i < cards.length; i++)
    this.cardsGroup.addLast(cards[i]);
    } /**
     * 取出卡组
     * 
     * @return Card[] 卡组
     */
    public Card[] getCatdsGroup() {
    Card card[] = new Card[cardsGroup.size()];
    return cardsGroup.toArray(card);
    } /**
     * 洗牌
     */
    public void shuffle() { Random rand = new Random(System.currentTimeMillis());
    Card[] cards = new Card[cardsGroup.size()];
    cards = cardsGroup.toArray(cards); for (int i = 0; i < cards.length; i++) {
    swap(cards, i, rand.nextInt(cards.length));
    }
    cardsGroup.clear(); setCardsGroup(cards); } private void swap(Card[] intCards, int index, int end) {
    Card temp = intCards[index];
    intCards[index] = intCards[end];
    intCards[end] = temp;
    } /**
     * 删除卡片
     * <p>如果卡片存在则删除
     * 
     * @param card
     *            要删除的卡片
     * @return boolean
     *         <p>
     *         存在true;不存在false
     */
    public boolean deleteCard(Card card) {
    if (cardsGroup.indexOf(card) == -1)
    return false; cardsGroup.remove(cardsGroup.indexOf(card));
    return true; } /**
     * 取卡组前n张卡并删除这n张卡
     * 
     * @param n
     *            要取卡的数量
     * @return Card[]
     *         <p>
     *         前n张卡
     */
    public Card[] getTopCard(int n) {
    Card[] cards = new Card[n];
    for (int i = 0; i < n; i++)
    cards[i] = cardsGroup.remove();
    return cards;
    } /**
     * 取出特定的卡并删除卡组中该卡片
     * 
     * @param card
     *            要取的卡
     * @return 要取的卡
     */
    public Card getCard(Card card) {
    return cardsGroup.remove(cardsGroup.indexOf(card));
    } /**
     * 在卡组顶端插入一张卡
     * 
     * @param card
     *            要插入的卡
     */
    public void insertCardFirst(Card card) {
    cardsGroup.addFirst(card);
    } /**
     * 在卡组底端插入一张卡
     * 
     * @param card
     *            要插入的卡
     */
    public void insertCardLast(Card card) {
    cardsGroup.addLast(card);
    }
    }
      

  2.   


     for(int   i=0;i <52;i+=4){ 
                ct_1[i/4]=a[i]; 
                ct_2[i/4]=a[i+1]; 
                ct_3[i/4]=a[i+2]; 
                ct_4[i/4]=a[i+3];
     }