斗牛/牛牛经典算法java版

斗牛众所周知的一款扑克牌游戏,其核心规则是需要对牌面进行计算出牛几或者牛牛的。我们看看程序上怎么去实现算法

首先,斗牛的规则是:

1.一副牌,去掉两个王,总共52张牌,2-6个人玩,每人发5张牌。

2.5张牌中的任意3张加起来不能成为10的倍数(如5,6,8,10,J)则是 无牛

3.张牌中的任意3张加起来为10的倍数,但另外2张不为10的倍数。至于到底是牛几,就要看另外2张牌相加之后的个位数。(如5,6,7,9,J 为牛7)

4.5张牌中的任意3张加起来为10的倍数,且另外2张也为10的倍数(如5,6,9,10,J)。

 弄清除基本规则后,直接上代码,代码中有注释。

public static String syncBull(Integer[] cards) {
        // 计算总点数
        int sums = 0;
        for (int i = 0; i < cards.length; i++) {
            if (cards[i] > 10) {
                cards[i] = 10;
            }
            sums += cards[i];
        }
        //牛牛
        if (sums % 10 == 0) {
            return "牛牛?";
        }
        // 牛丁 ~ 牛九
        int bull = 0;
        for (int i = 0; i < cards.length - 2; i++) {
            int cardI = cards[i];
            for (int j = i + 1; j < cards.length; j++) {
                int cardJ = cards[j];
                for (int k = j + 1; k < cards.length; k++) {
                    int cardK = cards[k];
                    int total = cardI + cardJ + cardK;
                    if (total % 10 == 0) {
                        int n = (sums - total) % 10;
                        bull = bull < n ? n : bull;
                    }
                }
            }
        }
        if (bull == 0) {
            return "无牛?";
        } else {
            return "牛" + bull + "?";
        }
    }
输入为一个数组,即是五张牌。

Integer[] cards = new Integer[]{5, 4, 11, 10, 12};

这样就实现了一个基本的牛牛核心算法。可能有些还有同花牛,顺子牛,五小牛之类的。下次再补充哈。

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
好的,斗牛游戏是一个非常经典的纸牌游戏,你可以使用 Python 语言来实现它。这里提供一个基本的框架,你可以在此基础上进行开发。 首先,你需要定义一组扑克牌。一副扑克牌共有 52 张牌,其中包括 4 种花色(方块、梅花、红心、黑桃)和 13 种点数(2、3、4、5、6、7、8、9、10、J、Q、K、A)。你可以使用列表来存储这些牌,例如: ``` suits = ['Diamonds', 'Clubs', 'Hearts', 'Spades'] ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'Jack', 'Queen', 'King', 'Ace'] deck = [(s, r) for s in suits for r in ranks] ``` 接着,你需要写一个函数来洗牌。洗牌可以使用 `random` 模块来实现,例如: ``` import random def shuffle_deck(deck): random.shuffle(deck) ``` 然后,你需要写一个函数来计算手牌的点数。斗牛游戏规定,点数大于 10 的牌都算作 10 点,A 可以算作 1 点或 11 点,具体取决于哪种取值更加优势。你可以使用以下代码来计算点数: ``` def calculate_points(hand): points = 0 num_aces = 0 for card in hand: if card[1].isdigit(): points += int(card[1]) elif card[1] in ['J', 'Q', 'K']: points += 10 elif card[1] == 'A': num_aces += 1 points += 11 while num_aces > 0 and points > 21: points -= 10 num_aces -= 1 return points ``` 最后,你需要编写游戏主程序。主程序中,你可以编写一个函数来模拟一次游戏,包括发牌、要牌、结算等操作。主程序还需要提示玩家输入命令,例如是否要牌、是否继续游戏等。这里给出一个简要示例: ``` def game(): deck = [(s, r) for s in suits for r in ranks] shuffle_deck(deck) player_hand = [deck.pop(), deck.pop()] dealer_hand = [deck.pop(), deck.pop()] player_points = calculate_points(player_hand) dealer_points = calculate_points(dealer_hand) while True: print(f'Your current hand is {player_hand}, with total points of {player_points}.') command = input('Do you want another card? (y/n)') if command == 'y': player_hand.append(deck.pop()) player_points = calculate_points(player_hand) if player_points > 21: print('You busted! Game over.') return -1 elif command == 'n': break while dealer_points < 16: dealer_hand.append(deck.pop()) dealer_points = calculate_points(dealer_hand) if dealer_points > 21: print('Dealer busted, you win!') return 1 elif dealer_points > player_points: print('Dealer wins, you lose!') return -1 elif player_points > dealer_points: print('You win!') return 1 else: print('Tie!') return 0 ``` 这就是一个简单的斗牛游戏的实现,你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值