【经典项目】Java入门,实现斗地主小游戏

一、需求分析

实现斗地主游戏可以涉及以下几个关键步骤和思路:

  1. 游戏规则定义:首先,你需要明确斗地主游戏的规则,包括牌的花色和大小、玩家数量、发牌顺序、出牌规则、胜利条件等。

  2. 牌的表示和初始化:定义一套扑克牌的数据结构,可以使用数字或字符表示每张牌,例如使用数字1-54或字符2-3A等。然后,初始化一副完整的牌,并进行洗牌操作。

  3. 发牌和牌局管理:根据游戏规则,按照特定的发牌顺序将牌分发给玩家,可以使用列表或数组来保存每个玩家手中的牌。同时,需要维护当前牌局的状态,例如当前轮到哪个玩家出牌、当前出牌的牌型和大小等。

  4. 出牌逻辑和判断:实现出牌阶段的逻辑,根据规则判断玩家出的牌是否合法,并进行相应的处理。需要考虑牌型的判断(如单牌、对子、顺子、炸弹等)和大小比较,以及特殊牌型的处理(如王炸)。

  5. AI策略和智能出牌:如果要实现AI玩家,你可以设计一套策略来指导AI玩家在出牌时的决策。这可能涉及牌型的分析、记忆其他玩家出的牌、考虑当前胜利条件、权衡出牌的风险等。

  6. 回合轮换和胜利条件判断:在游戏中,玩家的出牌轮换,直到其中一位玩家出完所有手牌或达到游戏规定的胜利条件。需要在每轮出牌后更新当前出牌玩家,并判断是否满足胜利条件。

  7. 用户界面和交互:可以通过命令行界面、图形界面或网络界面等方式,提供用户与游戏进行交互的界面。用户可以进行出牌、pass、查看牌等操作。

  8. 异常处理和错误检测:要考虑用户输入的合法性检查和异常处理,以避免程序崩溃或发生错误。

以上是一个基本的斗地主游戏实现的思路,具体的实现细节会有所不同,可以根据自己的编程语言和技术选择进行具体的开发。

二、主要实现思路

【文字思路】:

  1. 先创建HashMap,键是编号,值是牌
  2. 创建ArrayList,存储编号
  3. 创建花色数组和点数数组
  4. 从0开始往HashMap里面存储编号,并存储对应的牌,同时往ArrayList里面存储编号
  5. 洗牌(洗的是编号),用Collections的shuffle()方法实现
  6. 发牌(洗的是编号),用Collections的shuffle()方法实现
  7. 发牌(发的也是编号,为了保证编号是排序的,创建TreeSet集合接收)
  8. 定义方法看牌(遍历TreeSet集合,获取编号,到HashMap集合找对应的牌)
  9. 最后调用看牌方法。

【图片思路】

【经典项目】Java入门,实现斗地主小游戏_第1张图片

【发牌结果】

【经典项目】Java入门,实现斗地主小游戏_第2张图片

【主要代码】

public class PlayerOperation extends Thread {

    //游戏主界面
    GameJFrame gameJFrame;

    //是否能够出牌
    boolean isRun = true;

    //倒计时
    int i;

    public PlayerOperation(GameJFrame m, int i) {
        this.gameJFrame = m;
        this.i = i;
    }

    @Override
    public void run() {
        while (i > -1 && isRun) {
            gameJFrame.time[1].setText("倒计时:" + i--);
            sleep(1);
        }
        if (i == -1) {
            gameJFrame.time[1].setText("不抢");
        }
        gameJFrame.landlord[0].setVisible(false);
        gameJFrame.landlord[1].setVisible(false);
        for (Poker poker2 : gameJFrame.playerList.get(1)) {
            poker2.setCanClick(true);// 可被点击
        }

        if (gameJFrame.time[1].getText().equals("抢地主")) {
            gameJFrame.playerList.get(1).addAll(gameJFrame.lordList);
            openlord(true);
            sleep(2);
            Common.order(gameJFrame.playerList.get(1));
            Common.rePosition(gameJFrame, gameJFrame.playerList.get(1), 1);
            gameJFrame.publishCard[1].setEnabled(false);
            setlord(1);
        } else {
            if (Common.getScore(gameJFrame.playerList.get(0)) < Common.getScore(gameJFrame.playerList.get(2))) {
                gameJFrame.time[2].setText("抢地主");
                gameJFrame.time[2].setVisible(true);
                setlord(2);
                openlord(true);
                sleep(3);
                gameJFrame.playerList.get(2).addAll(gameJFrame.lordList);
                Common.order(gameJFrame.playerList.get(2));
                Common.rePosition(gameJFrame, gameJFrame.playerList.get(2), 2);
                openlord(false);
            } else {
                gameJFrame.time[0].setText("抢地主");
                gameJFrame.time[0].setVisible(true);
                setlord(0);
                openlord(true);
                sleep(3);
                gameJFrame.playerList.get(0).addAll(gameJFrame.lordList);
                Common.order(gameJFrame.playerList.get(0));
                Common.rePosition(gameJFrame, gameJFrame.playerList.get(0), 0);
                openlord(false);
            }
        }
        gameJFrame.landlord[0].setVisible(false);
        gameJFrame.landlord[1].setVisible(false);
        turnOn(false);
        for (int i = 0; i < 3; i++) {
            gameJFrame.time[i].setText("不要");
            gameJFrame.time[i].setVisible(false);
        }
        gameJFrame.turn = gameJFrame.dizhuFlag;
        while (true) {

            if (gameJFrame.turn == 1) {

                if (gameJFrame.time[0].getText().equals("不要") && gameJFrame.time[2].getText().equals("不要"))
                    gameJFrame.publishCard[1].setEnabled(false);
                else {
                    gameJFrame.publishCard[1].setEnabled(true);
                }
                turnOn(true);
                timeWait(30, 1);
                turnOn(false);
                gameJFrame.turn = (gameJFrame.turn + 1) % 3;
                if (win())
                    break;
            }
            if (gameJFrame.turn == 0) {
                computer0();
                gameJFrame.turn = (gameJFrame.turn + 1) % 3;
                if (win())
                    break;
            }
            if (gameJFrame.turn == 2) {
                computer2();
                gameJFrame.turn = (gameJFrame.turn + 1) % 3;
                if (win())
                    break;
            }
        }
    }

    //定义一个方法用来暂停N秒
    //参数为等待的时间
    //因为线程中的sleep方法有异常,直接调用影响阅读
    public void sleep(int i) {
        try {
            Thread.sleep(i * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void openlord(boolean is) {
        for (int i = 0; i < 3; i++) {
            if (is)
                gameJFrame.lordList.get(i).turnFront();
            else {
                gameJFrame.lordList.get(i).turnRear();
            }
            gameJFrame.lordList.get(i).setCanClick(true);
        }
    }

    public void setlord(int i) {
        Point point = new Point();
        if (i == 1) {
            point.x = 80;
            point.y = 430;
            gameJFrame.dizhuFlag = 1;
        }
        if (i == 0) {
            point.x = 80;
            point.y = 20;
            gameJFrame.dizhuFlag = 0;
        }
        if (i == 2) {
            point.x = 700;
            point.y = 20;
            gameJFrame.dizhuFlag = 2;
        }
        gameJFrame.dizhu.setLocation(point);
        gameJFrame.dizhu.setVisible(true);
    }

    public void turnOn(boolean flag) {
        gameJFrame.publishCard[0].setVisible(flag);
        gameJFrame.publishCard[1].setVisible(flag);
    }

    public void computer0() {
        timeWait(1, 0);
        ShowCard(0);

    }

    public void computer2() {
        timeWait(1, 2);
        ShowCard(2);

    }

    public void ShowCard(int role) {
        int orders[] = new int[]{4, 3, 2, 1, 5};
        Model model = Common.getModel(gameJFrame.playerList.get(role), orders);
        ArrayList list = new ArrayList<>();
        if (gameJFrame.time[(role + 1) % 3].getText().equals("不要") && gameJFrame.time[(role + 2) % 3].getText().equals("不要")) {
            if (model.a123.size() > 0) {
                list.add(model.a123.get(model.a123.size() - 1));
            } else if (model.a3.size() > 0) {
                if (model.a1.size() > 0) {
                    list.add(model.a1.get(model.a1.size() - 1));
                } else if (model.a2.size() > 0) {
                    list.add(model.a2.get(model.a2.size() - 1));
                }
                list.add(model.a3.get(model.a3.size() - 1));
            } else if (model.a112233.size() > 0) {
                list.add(model.a112233.get(model.a112233.size() - 1));
            } else if (model.a111222.size() > 0) {
                String name[] = model.a111222.get(0).split(",");

                if (name.length / 3 <= model.a1.size()) {
                    list.add(model.a111222.get(model.a111222.size() - 1));
                    for (int i = 0; i < name.length / 3; i++)
                        list.add(model.a1.get(i));
                } else if (name.length / 3 <= model.a2.size()) {
                    list.add(model.a111222.get(model.a111222.size() - 1));
                    for (int i = 0; i < name.length / 3; i++)
                        list.add(model.a2.get(i));
                }

            } else if (model.a2.size() > (model.a111222.size() * 2 + model.a3.size())) {
                list.add(model.a2.get(model.a2.size() - 1));
            } else if (model.a1.size() > (model.a111222.size() * 2 + model.a3.size())) {
                list.add(model.a1.get(model.a1.size() - 1));
            } else if (model.a4.size() > 0) {
                int sizea1 = model.a1.size();
                int sizea2 = model.a2.size();
                if (sizea1 >= 2) {
                    list.add(model.a1.get(sizea1 - 1));
                    list.add(model.a1.get(sizea1 - 2));
                    list.add(model.a4.get(0));

                } else if (sizea2 >= 2) {
                    list.add(model.a2.get(sizea1 - 1));
                    list.add(model.a2.get(sizea1 - 2));
                    list.add(model.a4.get(0));

                } else {
                    list.add(model.a4.get(0));

                }

            }
        } else {

            if (role != gameJFrame.dizhuFlag) {
                int f = 0;
                if (gameJFrame.time[gameJFrame.dizhuFlag].getText().equals("不要")) {
                    f = 1;
                }
                if ((role + 1) % 3 == gameJFrame.dizhuFlag) {
                    if ((Common.jugdeType(gameJFrame.currentList.get((role + 2) % 3)) != PokerType.c1
                            || Common.jugdeType(gameJFrame.currentList.get((role + 2) % 3)) != PokerType.c2)
                            && gameJFrame.currentList.get(gameJFrame.dizhuFlag).size() < 1)
                        f = 1;
                    if (gameJFrame.currentList.get((role + 2) % 3).size() > 0
                            && Common.getValue(gameJFrame.currentList.get((role + 2) % 3).get(0)) > 13)
                        f = 1;
                }
                if (f == 1) {
                    gameJFrame.time[role].setVisible(true);
                    gameJFrame.time[role].setText("不要");
                    return;
                }
            }

            int can = 0;
            if (role == gameJFrame.dizhuFlag) {
                if (gameJFrame.playerList.get((role + 1) % 3).size() <= 5 || gameJFrame.playerList.get((role + 2) % 3).size() <= 5)
                    can = 1;
            } else {
                if (gameJFrame.playerList.get(gameJFrame.dizhuFlag).size() <= 5)
                    can = 1;
            }

            ArrayList player;
            if (gameJFrame.time[(role + 2) % 3].getText().equals("不要"))
                player = gameJFrame.currentList.get((role + 1) % 3);
            else
                player = gameJFrame.currentList.get((role + 2) % 3);

            PokerType cType = Common.jugdeType(player);

            if (cType == PokerType.c1) {
                if (can == 1)
                    model = Common.getModel(gameJFrame.playerList.get(role), new int[]{1, 4, 3, 2, 5});
                AI_1(model.a1, player, list, role);
            } else if (cType == PokerType.c2) {
                if (can == 1)
                    model = Common.getModel(gameJFrame.playerList.get(role), new int[]{2, 4, 3, 5, 1});
                AI_1(model.a2, player, list, role);
            } else if (cType == PokerType.c3) {
                AI_1(model.a3, player, list, role);
            } else if (cType == PokerType.c4) {
                AI_1(model.a4, player, list, role);
            } else if (cType == PokerType.c31) {
                if (can == 1)
                    model = Common.getModel(gameJFrame.playerList.get(role), new int[]{3, 1, 4, 2, 5});
                AI_2(model.a3, model.a1, player, list, role);
            } else if (cType == PokerType.c32) {
                if (can == 1)
                    model = Common.getModel(gameJFrame.playerList.get(role), new int[]{3, 2, 4, 5, 1});
                AI_2(model.a3, model.a2, player, list, role);
            } else if (cType == PokerType.c411) {
                AI_5(model.a4, model.a1, player, list, role);
            } else if (cType == PokerType.c422) {
                AI_5(model.a4, model.a2, player, list, role);
            } else if (cType == PokerType.c123) {
                if (can == 1)
                    model = Common.getModel(gameJFrame.playerList.get(role), new int[]{5, 3, 2, 4, 1});
                AI_3(model.a123, player, list, role);
            } else if (cType == PokerType.c112233) {
                if (can == 1)
                    model = Common.getModel(gameJFrame.playerList.get(role), new int[]{2, 4, 3, 5, 1});
                AI_3(model.a112233, player, list, role);
            } else if (cType == PokerType.c11122234) {
                AI_4(model.a111222, model.a1, player, list, role);
            } else if (cType == PokerType.c1112223344) {
                AI_4(model.a111222, model.a2, player, list, role);
            }
            if (list.size() == 0 && can == 1) {
                int len4 = model.a4.size();
                if (len4 > 0)
                    list.add(model.a4.get(len4 - 1));
            }

        }

        gameJFrame.currentList.get(role).clear();
        if (list.size() > 0) {
            Point point = new Point();
            if (role == 0)
                point.x = 200;
            if (role == 2)
                point.x = 550;
            if (role == 1) {
                point.x = (770 / 2) - (gameJFrame.currentList.get(1).size() + 1) * 15 / 2;
                point.y = 300;
            }
            point.y = (400 / 2) - (list.size() + 1) * 15 / 2;
            ArrayList temp = new ArrayList<>();
            for (int i = 0, len = list.size(); i < len; i++) {
                List pokers = getCardByName(gameJFrame.playerList.get(role), list.get(i));
                for (Poker poker : pokers) {
                    temp.add(poker);
                }
            }
            temp = Common.getOrder2(temp);
            for (Poker poker : temp) {
                Common.move(poker, poker.getLocation(), point);
                point.y += 15;
                gameJFrame.container.setComponentZOrder(poker, 0);
                gameJFrame.currentList.get(role).add(poker);
                gameJFrame.playerList.get(role).remove(poker);
            }
            Common.rePosition(gameJFrame, gameJFrame.playerList.get(role), role);
        } else {
            gameJFrame.time[role].setVisible(true);
            gameJFrame.time[role].setText("不要");
        }
        for (Poker poker : gameJFrame.currentList.get(role))
            poker.turnFront();
    }

    public List getCardByName(List list, String n) {
        String[] name = n.split(",");
        ArrayList cardsList = new ArrayList();
        int j = 0;
        for (int i = 0, len = list.size(); i < len; i++) {
            if (j < name.length && list.get(i).getName().equals(name[j])) {
                cardsList.add(list.get(i));
                i = 0;
                j++;
            }
        }
        return cardsList;
    }

    public void AI_3(List model, List player, List list, int role) {

        for (int i = 0, len = model.size(); i < len; i++) {
            String[] s = model.get(i).split(",");
            if (s.length == player.size() && getValueInt(model.get(i)) > Common.getValue(player.get(0))) {
                list.add(model.get(i));
                return;
            }
        }
    }

    public void AI_4(List model1, List model2, List player, List list, int role) {
        player = Common.getOrder2(player);
        int len1 = model1.size();
        int len2 = model2.size();

        if (len1 < 1 || len2 < 1)
            return;
        for (int i = 0; i < len1; i++) {
            String[] s = model1.get(i).split(",");
            String[] s2 = model2.get(0).split(",");
            if ((s.length / 3 <= len2) && (s.length * (3 + s2.length) == player.size())
                    && getValueInt(model1.get(i)) > Common.getValue(player.get(0))) {
                list.add(model1.get(i));
                for (int j = 1; j <= s.length / 3; j++)
                    list.add(model2.get(len2 - j));
            }
        }
    }

    public void AI_5(List model1, List model2, List player, List list, int role) {
        player = Common.getOrder2(player);
        int len1 = model1.size();
        int len2 = model2.size();

        if (len1 < 1 || len2 < 2)
            return;
        for (int i = 0; i < len1; i++) {
            if (getValueInt(model1.get(i)) > Common.getValue(player.get(0))) {
                list.add(model1.get(i));
                for (int j = 1; j <= 2; j++)
                    list.add(model2.get(len2 - j));
            }
        }
    }

    public void AI_1(List model, List player, List list, int role) {

        for (int len = model.size(), i = len - 1; i >= 0; i--) {
            if (getValueInt(model.get(i)) > Common.getValue(player.get(0))) {
                list.add(model.get(i));
                break;
            }
        }

    }

    public void AI_2(List model1, List model2, List player, List list, int role) {
        player = Common.getOrder2(player);
        int len1 = model1.size();
        int len2 = model2.size();
        if (len1 > 0 && model1.get(0).length() < 10) {
            list.add(model1.get(0));
            System.out.println("王炸");
            return;
        }
        if (len1 < 1 || len2 < 1)
            return;
        for (int len = len1, i = len - 1; i >= 0; i--) {
            if (getValueInt(model1.get(i)) > Common.getValue(player.get(0))) {
                list.add(model1.get(i));
                break;
            }
        }
        list.add(model2.get(len2 - 1));
        if (list.size() < 2)
            list.clear();
    }

    public void timeWait(int n, int player) {

        if (gameJFrame.currentList.get(player).size() > 0)
            Common.hideCards(gameJFrame.currentList.get(player));
        if (player == 1) {
            int i = n;

            while (gameJFrame.nextPlayer == false && i >= 0) {
                gameJFrame.time[player].setText("倒计时:" + i);
                gameJFrame.time[player].setVisible(true);
                sleep(1);
                i--;
            }
            if (i == -1 && player == 1) {

                ShowCard(1);
            }
            gameJFrame.nextPlayer = false;
        } else {
            for (int i = n; i >= 0; i--) {
                sleep(1);
                gameJFrame.time[player].setText("倒计时:" + i);
                gameJFrame.time[player].setVisible(true);
            }
        }
        gameJFrame.time[player].setVisible(false);
    }

    public int getValueInt(String n) {
        String name[] = n.split(",");
        String s = name[0];
        int i = Integer.parseInt(s.substring(2, s.length()));
        if (s.substring(0, 1).equals("5"))
            i += 3;
        if (s.substring(2, s.length()).equals("1") || s.substring(2, s.length()).equals("2"))
            i += 13;
        return i;
    }

    public boolean win() {
        for (int i = 0; i < 3; i++) {
            if (gameJFrame.playerList.get(i).size() == 0) {
                String s;
                if (i == 1) {
                    s = "恭喜你,胜利了!";
                } else {
                    s = "恭喜电脑" + i + ",赢了! 你的智商有待提高哦";
                }
                for (int j = 0; j < gameJFrame.playerList.get((i + 1) % 3).size(); j++)
                    gameJFrame.playerList.get((i + 1) % 3).get(j).turnFront();
                for (int j = 0; j < gameJFrame.playerList.get((i + 2) % 3).size(); j++)
                    gameJFrame.playerList.get((i + 2) % 3).get(j).turnFront();
                JOptionPane.showMessageDialog(gameJFrame, s);
                return true;
            }
        }
        return false;
    }
}

三、效果展示

1、游戏界面

【经典项目】Java入门,实现斗地主小游戏_第3张图片

2、地主牌

【经典项目】Java入门,实现斗地主小游戏_第4张图片

3、出牌

【经典项目】Java入门,实现斗地主小游戏_第5张图片 四、完整代码获取

经典项目Java入门,实现斗地主小游戏资源-CSDN文库

你可能感兴趣的:(JAVA基础,java,开发语言)