Java实现简易斗地主

声明:

1.此简易程序只包含,定义牌,洗牌,发牌三步,至于游戏环节会在后续的博客中给出。

2.此程序采用的存储结构为集合,如果想用数组存储也可以,自己转化一下就可以了。

 

简易斗地主

1.组装牌

首先,我们需要一个集合pokerBox用于存放54张扑克牌。54张扑克牌可分为两张特殊牌(大王,小王)和52张普通牌,其中普通牌均由花色和牌号两部分组成。因此,对于52张普通牌的存储可以通过定义两个集合——颜色pokeColor与数字pokeNums,并循环嵌套遍历两个集合来存储。

代码如下:

//组装54张牌
        ArrayList<String> pokerBox = new ArrayList();

        ArrayList<String> pokerColor = new ArrayList();//花色
        pokerColor.add("♠");
        pokerColor.add("♥");
        pokerColor.add("♣");
        pokerColor.add("♦");
        ArrayList<String> pokeNums = new ArrayList();//点数
        pokeNums.add("A");
        pokeNums.add("J");
        pokeNums.add("K");
        pokeNums.add("Q");
        for (int i=2 ; i<=10; i++)
        {
            pokeNums.add(""+i);
        }

此时我们已经准备好了组装54张牌的前提,下面进行组装,其中大王与小王单独存储

//组装特殊数据
        pokerBox.add("大王");
        pokerBox.add("小王");

        //组装其他52张牌
        for(String color : pokerColor)
        {
            for(String num:pokeNums)
            {
                String temp = color + num;
                pokerBox.add(temp);
            }
        }

这样我们就完成了54张牌的组装.

 

2.洗牌

洗牌部分较为简单,用到了Collections下的shuffle函数,该函数用法简单,在此不过多说明,如果不太明白可以直接查询该函数来得到详细信息。

洗牌部分代码如下:

//洗牌
        Collections.shuffle(pokerBox);

如果大家在此想查看当前洗牌的结果,可以通过如下语句查看:

System.out.println(pokerBox);

 

3.发牌

斗地主中,发牌结束后牌将被分为四堆:底牌堆(dipai),第一个人(theFirst),第二个人(theSecond),第三个人(theThird),我们将这四组分别定义为一个集合,然后分别存储。

集合是一种可以通过角标索引的存储结构,因此我们可以通过控制角标(index)来获取元素。

因为共有三个人,所以可以通过index%3求余的结果来对应三个人的手牌。

代码如下:

//发牌
        ArrayList<String> diPai = new ArrayList();
        ArrayList<String> theFirst = new ArrayList();
        ArrayList<String> theSecond = new ArrayList();
        ArrayList<String> theThird = new ArrayList();

        for(int index = 0;index < pokerBox.size();index++)
        {
            if(index >= 51)
            {diPai.add(pokerBox.get(index));}
            else
            {
                if(index % 3 == 0)
                {theFirst.add(pokerBox.get(index));}
                else
                {
                    if(index % 3 == 1)
                    {theSecond.add(pokerBox.get(index));}
                    else
                    {
                        if(index % 3 == 2)
                        {theThird.add(pokerBox.get(index));}
                    }
                }
            }
        }

 

4.输出最后结果

我们可以通过如下代码来查询现在四堆牌的结果:

System.out.println(diPai);
System.out.println(theFirst);
System.out.println(theSecond);
System.out.println(theThird);

 

 

总代码如下:

package cn.huida.poker;

import java.util.ArrayList;
import java.util.Collections;

public class PokerTest {
    public static void main(String[] args) {

        //组装54张牌
        ArrayList<String> pokerBox = new ArrayList();

        ArrayList<String> pokerColor = new ArrayList();//花色
        pokerColor.add("♠");
        pokerColor.add("♥");
        pokerColor.add("♣");
        pokerColor.add("♦");
        ArrayList<String> pokeNums = new ArrayList();//点数
        pokeNums.add("A");
        pokeNums.add("J");
        pokeNums.add("K");
        pokeNums.add("Q");
        for (int i=2 ; i<=10; i++)
        {
            pokeNums.add(""+i);
        }

        //组装特殊数据
        pokerBox.add("大王");
        pokerBox.add("小王");

        //组装其他52张牌
        for(String color : pokerColor)
        {
            for(String num:pokeNums)
            {
                String temp = color + num;
                pokerBox.add(temp);
            }
        }

        //洗牌
        Collections.shuffle(pokerBox);

        //发牌
        ArrayList<String> diPai = new ArrayList();
        ArrayList<String> theFirst = new ArrayList();
        ArrayList<String> theSecond = new ArrayList();
        ArrayList<String> theThird = new ArrayList();

        for(int index = 0;index < pokerBox.size();index++)
        {
            if(index >= 51)
            {diPai.add(pokerBox.get(index));}
            else
            {
                if(index % 3 == 0)
                {theFirst.add(pokerBox.get(index));}
                else
                {
                    if(index % 3 == 1)
                    {theSecond.add(pokerBox.get(index));}
                    else
                    {
                        if(index % 3 == 2)
                        {theThird.add(pokerBox.get(index));}
                    }
                }
            }
        }

        System.out.println(diPai);
        System.out.println(theFirst);
        System.out.println(theSecond);
        System.out.println(theThird);


    }
}

 

 

==========================分割线==========================

感觉这一篇解释的不是很详细,如果有不理解的可以直接问我....

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值