转载

使用Java实现简单的斗地主案例

使用Java实现简单的斗地主案例

案例说明:使用Java实现简单的斗地主洗牌发牌的操作;

具体规则:

共有54张牌,顺序打乱;

三个玩家参与游戏,三人交替摸牌,每人17张牌,最后留三张为底牌(地主牌)。

具体代码实现步骤:

扑克牌准备操作

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

public class PokerGame {

    public static void main(String[] args) {

        //定义扑克牌的花色以及数字的数组
        String[] flower = {":hearts:",":spades:",":clubs:",":diamonds:"};
        String[] numbers = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
        
        //创建牌组集合
        List poker = new ArrayList();

        //组合扑克牌样式
        //拿出每一个花色,然后跟每一个数字进行结合,储存在牌组中
        for (int i = 0; i < flower.length; i++) {
            for (int j = 0; j < numbers.length; j++) {
                poker.add(flower[i].concat(numbers[j]));
            }
        }

        //添加大小王到牌组集合中
        poker.add("大王");
        poker.add("小王");

        //打印输出所有扑克牌测试
        System.out.println(poker);

        //定义三个玩家和地主牌的集合
        List player1 = new ArrayList();
        List player2 = new ArrayList();
        List player3 = new ArrayList();
        List dizhupai = new ArrayList();

        //洗牌操作,使用shuffle方法
        Collections.shuffle(poker);
        System.out.println(poker);  //测试牌有没有被打乱

        //发牌操作
        for (int k = 0; k<poker.size() ; k++) {
            //留出三张牌存到底牌的集合中
            if (k > 50) {
                dizhupai.add(poker.get(k));
            } else if (k % 3 == 0) {
                player1.add(poker.get(k));
            } else if (k % 3 == 1) {
                player2.add(poker.get(k));
            } else if (k % 3 == 2) {
                player3.add(poker.get(k));
            }
        }

        //最终看牌操作
        System.out.println("玩家一的牌"+player1);
        System.out.println("玩家二的牌"+player2);
        System.out.println("玩家三的牌"+player3);
        System.out.println("地主牌"+dizhupai);

    }
}

结果展示

玩家一的牌:[:hearts:9, :spades:A, :spades:5, :hearts:A, :hearts:2, :hearts:10, :spades:7, :clubs:J, :hearts:3, :spades:2, :clubs:5, :diamonds:2, :spades:6, :hearts:7, :diamonds:K, :clubs:9, 大王]
玩家二的牌:[:diamonds:9, :spades:4, :clubs:4, :clubs:10, :hearts:J, :diamonds:4, :hearts:8, :hearts:5, :diamonds:A, :clubs:A, :diamonds:7, :diamonds:3, :diamonds:Q, :clubs:K, :hearts:6, :clubs:3, :spades:Q]
玩家三的牌:[:clubs:Q, :clubs:2, :clubs:8, :clubs:7, :hearts:Q, :diamonds:10, :spades:8, :clubs:6, :hearts:4, :spades:10, 小王, :hearts:K, :spades:3, :diamonds:8, :diamonds:6, :spades:K, :spades:J]
地主牌:[:spades:9, :diamonds:5, :diamonds:J]
原文  http://www.cnblogs.com/dingqf/p/12861261.html
正文到此结束
Loading...