如何用Java写一个斗地主(二)

如何用Java写一个斗地主(二)

  • 序言
  • 正文
    • 卡牌类创建
    • 牌组创建
    • 手牌类创建
    • 玩家类创建
    • 使用JFrame对卡牌进行展示
    • main方法
  • 后续

序言

源码地址:https://github.com/disasterZ/landLord
书接上文,这一次我们先说创建对象,纵观斗地主这个游戏,我能想到的对象只有如下几种:

卡牌 卡组 手牌 选手 底牌 组成 分发 属于 剩余卡牌 卡牌 卡组 手牌 选手 底牌

下面就按照这几个类别创建对象类

正文

卡牌类创建

首先创建一个Card类:

public class Card{
     
}

每一张扑克牌都具有自己的花色与点数,因此我们给Card类花色与点数两个属性,同时,为了方便在发到玩家手上时按大小排序,我们再给每一张卡牌一个序号,与每张卡片对应的图片地址。(在这里为了方便序号,我将花色数字化,并将数字从小到大按1-13排序,1对应3)

    private int decor;
    private int number;
    private int code;
    private BufferedImage draw;

创建构造函数,使序号与花色、数字间存在关系。

public Card(int decor, int number) {
     
        this.decor = decor;
        this.number = number;
        this.code = decor+number*10;
        {
     
            try {
     
                draw = ImageIO.read(new File(System.getProperty("user.dir")+"/image/"+code +".jpg"));
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }
    }

最后再将Getter方法写入,完成Card类

public class Card {
     
    private int decor;
    private int number;
    private int code;
    private BufferedImage draw;



    public Card(int decor, int number) {
     
        this.decor = decor;
        this.number = number;
        this.code = decor+number*10;
        {
     
            try {
     
                draw = ImageIO.read(new File(System.getProperty("user.dir")+"/image/"+code +".jpg"));
            } catch (IOException e) {
     
                e.printStackTrace();
            }
        }
    }

    public int getDecor() {
     
        return decor;
    }


    public int getNumber() {
     
        return number;
    }


    public int getCode() {
     
        return code;
    }

    public BufferedImage getDraw() {
     
        return draw;
    }

    @Override
    public String toString() {
     
        return "Card{" +
                "code=" + code +
                '}';
    }
}

牌组创建

首先,在牌组类——Pokes中创建Card集合

public class Pokes{
     
	 private List<Card> pokes=new ArrayList<>();
}

之后再创建一个方法fullCard,将54张纸牌有序写入牌组中(别忘了洗入大小王)。

public void fullCard(){
     
        for(int i=1;i<14;i++){
     
            for(int k=1;k<5;k++){
     
                Card card=new Card(k,i);
                pokes.add(card);
            }
        }
        Card joker=new Card(1,14);
        Card theJoker=new Card(2,14);
        pokes.add(joker);
        pokes.add(theJoker);
    }

之后创建将牌组随机打乱的函数,也可以直接在创建实例之后使用Collections.shuffle();

 public void shuffle(){
     
        Collections.shuffle(pokes);
    }

之后放上所有代码
在创建牌组时,因为一局斗地主游戏中只有一副扑克牌,所以在写牌组时可以使用单例模式,确保牌组唯一,我这里使用了Holder模式

public class Pokes {
     

    /*
    * holder模式
    * */
    private static class PokesHolder{
     
        private static Pokes instance=new Pokes();
    }
    private Pokes(){
     

    }
    public static Pokes getInstance(){
     
        return PokesHolder.instance;
    }

    private List<Card> pokes=new ArrayList<>();

    public void fullCard(){
     
        for(int i=1;i<14;i++){
     
            for(int k=1;k<5;k++){
     
                Card card=new Card(k,i);
                pokes.add(card);
            }
        }
        Card joker=new Card(1,14);
        Card theJoker=new Card(2,14);
        pokes.add(joker);
        pokes.add(theJoker);
    }

    public void shuffle(){
     
        Collections.shuffle(pokes);
    }

    public List<Card> getPokes() {
     
        return pokes;
    }

}

手牌类创建

与牌组一样,我们首先先创建Card集合
之后创建添加卡牌与删除卡牌的方法

	List<Card> cards =new ArrayList<>();

    public void addCard(Card card){
     
        cards.add(card);
    }

    public void addCards(List<Card> cardList){
     
        cards.addAll(cardList);
        this.sortCards();
    }

    public void removeCards(List<Card> cardList){
     
        for(Card card:cardList){
     
            cards.remove(card);
        }
    }

之后声明使用Comparator<>接口,对两张卡的code进行比较,完成对卡牌的排序
手牌类就完成了

public class HandCard implements Comparator<Card>{
     
    List<Card> cards =new ArrayList<>();

    public void addCard(Card card){
     
        cards.add(card);
    }

    public void addCards(List<Card> cardList){
     
        cards.addAll(cardList);
        this.sortCards();
    }

    public void removeCards(List<Card> cardList){
     
        for(Card card:cardList){
     
            cards.remove(card);
        }
    }

    public void sortCards(){
     
        cards.sort(this::compare);
    }

    public List<Card> getCards() {
     
        return cards;
    }

    @Override
    public int compare(Card o1, Card o2) {
     
        if(o1.getCode()>o2.getCode()){
     
            return -1;
        }else {
     
            return 1;
        }
    }

    @Override
    public String toString() {
     
        List<Integer> list=new ArrayList<>();
        for(Card card:cards){
     
            list.add(card.getCode());
        }
        return "Cards{" +
                "handCards=" + list +
                '}';
    }
}

玩家类创建

首先明确,玩家在除了手牌外,还有名称、分数以及状态(判断是否为地主)。因此使玩家类继承手牌类后,创建以上三个属性。

public class Player extends HandCard {
     
    private String userName;
    private int status=0;
    private int sorce=0;

    public Player(String userName) {
     
        this.userName = userName;
    }

    public String getUserName() {
     
        return userName;
    }

    public int getStatus() {
     
        return status;
    }

    public void setStatus(int status) {
     
        this.status = status;
    }

    public int getSorce() {
     
        return sorce;
    }

    public void setSorce(int sorce) {
     
        this.sorce = sorce;
    }

    @Override
    public String toString() {
     
        List<Integer> list=new ArrayList<>();
        for(Card card:cards){
     
            list.add(card.getCode());
        }
        return "Player{" +"Player="+userName+
                "/handCards=" + list +
                '}';
    }
}

使用JFrame对卡牌进行展示

这个章节的存在的意义就是能更好的展示,所以就直接上代码

public class GameFarme extends JFrame {
     
    private static final int Win_Width=1080;
    private static final int Win_Height=800;

    public GameFarme(){
     
        this.setSize(Win_Width,Win_Height);
        this.setTitle("斗地主");
        this.setResizable(false);
        this.setLayout(null);
        this.getContentPane().setBackground(Color.WHITE);
        this.getContentPane().setVisible(true);//如果改为true那么就变成了黑色。
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width -Win_Width) / 2,
                (Toolkit.getDefaultToolkit().getScreenSize().height - Win_Height) / 2);
    }

}
public class DrawFarme extends JPanel {
     
    private List<List<Card>> cards=new ArrayList<>();
    private List<Integer> x=new ArrayList<>();
    private List<Integer> y=new ArrayList<>();

    public DrawFarme(List<Card> player1, int x1, int y1,List<Card> player2, int x2, int y2,List<Card> player3, int x3, int y3,List<Card> playerhand, int x4, int y4) {
     
        cards.add(player1);
        cards.add(player2);
        cards.add(player3);
        cards.add(playerhand);
        x.add(x1);
        x.add(x2);
        x.add(x3);
        x.add(x4);
        y.add(y1);
        y.add(y2);
        y.add(y3);
        y.add(y4);
    }

    @Override
    public void paintComponent(Graphics g){
     
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        for(int i=0;i<x.size();i++){
     
            List<Card> cardList=cards.get(i);
            int xs=x.get(i);
            int ys=y.get(i);
            for(Card card:cardList){
     
                g2.drawImage(card.getDraw(),xs,ys,null);
                xs+=20;
            }
        }
        g2.drawString("玩家1",30,30);
        g2.drawString("玩家2",30,190);
        g2.drawString("玩家3",30,380);
        g2.drawString("底牌",30,540);
    }
}

main方法

public class Test {
     
    public static void main(String[] args) {
     
        GameFarme gameFarme=new GameFarme();
        Pokes pokes=Pokes.getInstance();
        pokes.fullCard();
        pokes.shuffle();
        Player one=new Player("one");
        Player two=new Player("two");
        Player three=new Player("three");
        HandCard hand=new HandCard();
        for(int i=0;i<pokes.getPokes().size();i++){
     
            Card card=pokes.getPokes().get(i);
            if(i>=51){
     
                hand.addCard(card);
            }else {
     
                if(i%3==0){
     
                    one.addCard(card);
                }else if(i%3==1){
     
                    two.addCard(card);
                }else {
     
                    three.addCard(card);
                }
            }
        }
        one.sortCards();
        two.sortCards();
        three.sortCards();
        System.out.println(hand.toString());
        three.setStatus(1);
        if(three.getStatus()==1){
     
            three.addCards(hand.getCards());
        }
        System.out.println(three.toString());
        three.removeCards(hand.getCards());
        System.out.println(three.toString());
        DrawFarme drawFarme=new DrawFarme(one.getCards(),70,30,two.getCards(),70,190,
                three.getCards(),70,380,hand.getCards(),70,540);
        gameFarme.setContentPane(drawFarme);
        gameFarme.setVisible(true);
    }

}

如何用Java写一个斗地主(二)_第1张图片
运行结果

后续

后续写不写就看心情了 23333

你可能感兴趣的:(斗地主,java,游戏)