当前位置: 代码迷 >> Java相关 >> 编写小程序半天也弄不出来
  详细解决方案

编写小程序半天也弄不出来

热度:246   发布时间:2009-11-04 20:45:28.0
编写小程序半天也弄不出来
请高手帮忙谢谢!
程序是这样的:开始――初始化54张牌――少于100次洗牌(随机交换俩张牌的位置)――分发54张牌――发牌(发3个人的牌)
――打印出自己的牌――结束
搜索更多相关的解决方案: 编写  

----------------解决方案--------------------------------------------------------
import java.util.*;

class Card {
    private String face;
    private String suit;
   
    Card(String face, String suit) {
        this.face = face;
        this.suit = suit;
    }
   
    public String toString() {
        return suit+face;
    }
   
}

class Person {
    ArrayList<Card> cards;
   
    Person() {
        cards = new ArrayList<Card>();
    }
   
    public void getCard(Card card) {
        cards.add(card);
    }
   
    public void showCards() {
        for (int i=0; i<cards.size(); i++) {
            System.out.print(cards.get(i)+" ");
        }
        System.out.println();
    }
}

class CardGame {
    LinkedList<Card> deck;
   
    CardGame() {
        init();
    }
   
    public void init() {
        String faces[] = {"1","2","3","4","5","6","7","8","9","10","J","Q","K"};
        String suits[] = {"黑桃","红桃","梅花","方块"};
        
        deck = new LinkedList<Card>();
        
        for (int i=0; i<52; i++) {
            deck.add(new Card(faces[i%13], suits[i/13]));
        }
        
        deck.add(new Card("鬼","红"));
        deck.add(new Card("鬼","黑"));
    }
   
    public void shuffle() {
        for( int i = 0; i < deck.size(); i++ ){
        int j = ( int )( Math.random() * 54 );
        Card temp = deck.get(i);
        deck.set(i,deck.get(j));
        deck.set(j,temp);
      }
    }
}
public class TestCardGame {
        public static void main(String[] args) {
        CardGame cg = new CardGame();
        for (int i=0; i<100; i++)
            cg.shuffle();//洗牌100次
        Person a = new Person();
        Person b = new Person();
        Person c = new Person();
            
        while(cg.deck.size()!=0) {//分牌给三个人
            a.getCard(cg.deck.pop());
            b.getCard(cg.deck.pop());
            c.getCard(cg.deck.pop());
        }
        
        a.showCards();//打印手中的牌
        b.showCards();
        c.showCards();
        
    }
}

[ 本帖最后由 lampeter123 于 2009-11-5 11:20 编辑 ]
----------------解决方案--------------------------------------------------------
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ZhiPai {
    public static void main(String[] args) {
        int x,y;
        Card card1 = null;
        Card card2 = null;

        //先定义两个随机数,待会洗牌的时候会用到
        Random x1 = new Random();
        Random x2 = new Random();
        
        //使用泛型,定义了四个容器,分别用来存放整幅牌玩家1、玩家2和自己的牌
        List<Card> cards = new ArrayList<Card>();
        List<Card> wanjia1 = new ArrayList<Card>();
        List<Card> wanjia2 = new ArrayList<Card>();
        List<Card> my = new ArrayList<Card>();
        
        //添加一对王牌
        cards.add(new Card(0,"红鬼"));
        cards.add(new Card(0,"黑鬼"));
        
        //初始化一副牌
        for(int i=1; i<=13; i++) {
            cards.add(new Card(i,"黑桃"));
            cards.add(new Card(i,"红桃"));
            cards.add(new Card(i,"梅花"));
            cards.add(new Card(i,"方块"));
        }
        
        //洗牌
        for(int i=0; i<100; i++) {
            x = x1.nextInt(cards.size());
            y = x2.nextInt(cards.size());
            
            card1 = cards.get(x);
            card2 = cards.get(y);
            cards.set(x, card2);
            cards.set(y, card1);
        }
        
        //发牌
        for(int i=0; i<cards.size();) {
            my.add(cards.get(i++));
            wanjia1.add(cards.get(i++));
            wanjia2.add(cards.get(i++));
        }
        //打印
        for(int i=0,j=0; i<my.size(); i++) {
            x = my.get(i).id;
            switch(x){
            case 11:
                System.out.print(my.get(i).s + " " + "J" + "   ");
                break;
            case 12:
                System.out.print(my.get(i).s + " " + "J" + "   ");
                break;
            case 13:
                System.out.print(my.get(i).s + " " + "J" + "   ");
                break;
            }
            System.out.print(my.get(i).s + " " + my.get(i).id + "   ");
            j++;
            if(j%6==0) System.out.println();
        }
    }
   
}

//将牌定义为一个单独的内。原本发牌洗牌这些都应该在这个内里面的,这样更符合面向对象的思想。不过为了个人写起来更简便,所以直接把它们全部写到main()里面去了
class Card {
    int id;
    String s;
   
    Card(int id, String s) {
        this.id = id;
        this.s = s;
    }
}

[ 本帖最后由 vecomwa 于 2009-11-5 10:57 编辑 ]
----------------解决方案--------------------------------------------------------
以下是引用lampeter123在2009-11-5 10:18:46的发言:

import java.util.*;

class Card {
    private String face;
    private String suit;
   
    Card(String face, String suit) {
        this.face = face;
        this.suit = suit;
    }
   
    public  ...
谢谢你啊,对于你来说这个应该很简单,请谅解初学者打扰你的意思,愿你一切顺利
----------------解决方案--------------------------------------------------------
  相关解决方案