斗牛牛牛换牌算法

本算法设计拍的表示为:char类型字符 把这个字符的前四位表示为花型(1:黑、2:红:3:梅、4:方),后四位表示对应牌值(1-13)。

 

#include<iostream>
#include<vector>
#include <stdlib.h>
#include <time.h>
#include <algorithm> 

using namespace std;

/*
type:
    1、五小牛  1-4 和 小于 10
    2、五花牛
    3、四炸
    4、牛牛
    5、牛九
    6、牛八
    7、牛七
    8、牛六
    9、牛五
    10、牛四
    11、牛三
    12、牛二

*/
bool changeCards(int seat, int type, char table_Cards[5][5]) {
    if (seat < 0 || seat > 4 || type < 1 || type > 12)
        return false;
    char cardsPond[52][2] = { 0 };
    for (int i = 0; i < 52; ++i) {
        cardsPond[i][0] = i;
    }

    //将要除要换掉的牌拿出牌池
    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            if (i != seat) {
                int _card = (((table_Cards[i][j] & 0xf0) >> 4) - 1) + ((table_Cards[i][j] & 0x0f) - 1) * 4;
                cardsPond[_card][1] = 1;
            }
        }
    }
    //for (int i = 0; i < 52; ++i) {
    //    cout << int(cardsPond[i][0])<<":" << int(cardsPond[i][1]) << endl;
    //}
    char _cardsArr[6] = { 0 };
    int _temporary1 = 0, _temporary2 = 0, _temporary4 = 0;;
    vector<int> _Cards;
    switch (type) {
    case 1:    //五小牛
        for (int j = 0; j < 52; ++j) {
            if (cardsPond[j][1] == 0) {
                _cardsArr[_temporary1] = j;
                _cardsArr[5] += j / 4 + 1;
                if (j > 15)
                    return false;
                if (_temporary1 == 4)
                    break;
                _temporary1++;
            }
        }
        //for (int i = 0; i < 5; ++i) {
        //    cout << int(_cardsArr[i])<< endl;
        //}
        if (_cardsArr[5] <= 10) {
            for (int i = 0; i < 5; ++i) {
                table_Cards[seat][i] = (((_cardsArr[i] % 4) + 1) << 4) | (_cardsArr[i] / 4 + 1);
            }
            return true;
        }
        else
            return false;
        break;
    case 2://五花牛
        for (int i = 40; i < 52; ++i) {
            if (cardsPond[i][1] == 0) {
                _Cards.push_back(i);
                _temporary2++; //花牌剩余数量
            }
        }
        //cout << _temporary2 << endl;
        //for (int i = 0; i < 7; ++i) {
        //    cout << _Cards[i] << endl;
        //}
        if (_temporary2 < 5)
            return false;
        else {
            srand((unsigned)time(NULL));
            for (int i = 0; i < 5; ++i) {
                int Rand = rand() % _temporary2;
                //cout << Rand << "__" << "111" << endl;
                _cardsArr[i] = _Cards.at(Rand);
                //cout << int(_cardsArr[i]) << "__" << "222" << endl;
                _Cards.erase(_Cards.begin() + Rand);
                _temporary2--;
            }
            for (int i = 0; i < 5; ++i) {
                table_Cards[seat][i] = (((_cardsArr[i] % 4) + 1) << 4) | (_cardsArr[i] / 4 + 1);
            }
            return true;
        }
        break;
    case 3:
        _cardsArr[5] = -1;
        for (int i = 0; i < 13; ++i) {
            if (cardsPond[i * 4][1] == 0 && 
                cardsPond[i * 4 + 1][1] == 0 && 
                cardsPond[i * 4 + 2][1] == 0 && 
                cardsPond[i * 4 + 3][1] == 0) {
                cardsPond[i * 4][0] = 1;
                cardsPond[i * 4 + 1][0] = 1;
                cardsPond[i * 4 + 2][0] = 1;
                cardsPond[i * 4 + 3][0] = 1;
                _cardsArr[5] = i;
                break;
            }
        }
        if (_cardsArr[5] == -1) {
            return false;
        }
        printf("+++++++++++++++++++++++++++++++++++++++\n");
        for (int i = 0; i < 5; ++i) {
            for (int j = 0; j < 5; ++j) {
                cout << int(table_Cards[i][j]) << "___";
            }
            cout << endl;
        }
        printf("+++++++++++++++++++++++++++++++++++++++\n");
        for (int i = 0; i < 4; ++i) {
            table_Cards[seat][i] = ((((_cardsArr[5] * 4 + i) % 4) + 1) << 4) | ((_cardsArr[5] * 4 + i) / 4 + 1);
        }
        if ((table_Cards[seat][0] & 0x0f) == (table_Cards[seat][4] & 0x0f)) {
            printf("+++++++++++++++++++++++++++++++++++++++\n");
            for (int i = 0; i < 52; ++i) {
                if (cardsPond[i][1] == 0) {
                    table_Cards[seat][4] = (((i % 4) + 1) << 4) | (i / 4 + 1);
                }
            }
        }
        return true;
        break;
    case 4: _temporary1 = 0; break;
    case 5:    _temporary1 = 9; break;
    case 6: _temporary1 = 8; break;
    case 7: _temporary1 = 7; break;
    case 8: _temporary1 = 6; break;
    case 9: _temporary1 = 5; break;
    case 10:_temporary1 = 4; break;
    case 11:_temporary1 = 3; break;
    case 12:_temporary1 = 2; break;
    }
    //获取三张相加有牛牌
    for (int i = 0; i < 52; ++i) {
        if (cardsPond[i][1] == 0) {
            _Cards.push_back(i);
            _temporary2++;
        }
    }
    random_shuffle(_Cards.begin(), _Cards.end());
    vector<int> _arr;
    _arr.resize(_temporary2);
    for (int i = 0; i < 3; ++i) {
        _arr[i] = 1;
        _cardsArr[5] += ((_Cards[i] / 4) + 1 > 10 ? 10 : (_Cards[i] / 4) + 1);
    }
    if (_cardsArr[5] % 10 != 0) {
        while (true) {
            _temporary4 = 0;
            for (int i = 0; i < _temporary2 - 1; ++i) {
                if (_arr[i] == 1 && _arr[i + 1] == 0) {
                    _arr[i] = 0;
                    _arr[i+1] = 1;
                    for (int j = 0; j < i; ++j) {
                        if (j < _temporary4)
                            _arr[j] = 1;
                        else
                            _arr[j] = 0;
                    }
                    break;
                }
                if (_arr[i] == 1)
                    _temporary4++;
            }
            _cardsArr[5] = 0;
            for (int i = 0; i < _temporary2; ++i) {
                if (_arr[i] == 1) {
                    _cardsArr[5] += ((_Cards[i] / 4) + 1 > 10 ? 10 : (_Cards[i] / 4) + 1);
                }
            }
            if (_cardsArr[5] % 10 == 0) 
                break;
            bool _flag = true;
            for (int i = 0; i < 3; ++i) {
                if (_arr[_temporary2 - i - 1] == 0) {
                    _flag = false;
                }
            }
            if (_flag == true)
                return false;
        }
    }
    int _temporary3 = 0;
    for (int i = 0; i < _temporary2; ++i) {
        if (_arr[i] == 1) {
            _cardsArr[_temporary3] = _Cards[i];
            cardsPond[_Cards[i]][1] = 1;
            _temporary3++;
        }
    }
    //获取对应点数
    _temporary2 = 0;
    _temporary4 = 0;
    _Cards.clear();
    _cardsArr[5] = 0;
    for (int i = 0; i < 52; ++i) {
        if (cardsPond[i][1] == 0) {
            _Cards.push_back(i);
            _temporary2++;
        }
    }

    random_shuffle(_Cards.begin(), _Cards.end());
    _arr.resize(_temporary2);
    for (int i = 0; i < _temporary2; ++i) {
        _arr[i] = 0;
    }
    for (int i = 0; i < 2; ++i) {
        _arr[i] = 1;
        _cardsArr[5] += (_Cards[i] / 4) + 1 > 10 ? 10 : (_Cards[i] / 4) + 1;
    }
    if (_cardsArr[5] % 10 != _temporary1) {
        while (true) {
            _temporary4 = 0;
            for (int i = 0; i < _temporary2 - 1; ++i) {
                if (_arr[i] == 1 && _arr[i + 1] == 0) {
                    _arr[i] = 0;
                    _arr[i + 1] = 1;
                    for (int j = 0; j < i; ++j) {
                        if (j < _temporary4)
                            _arr[j] = 1;
                        else
                            _arr[j] = 0;
                    }
                    break;
                }
                if (_arr[i] == 1)
                    _temporary4++;
            }
            _cardsArr[5] = 0;
            for (int i = 0; i < _temporary2; ++i) {
                if (_arr[i] == 1) {
                    _cardsArr[5] += (_Cards[i] / 4) + 1 > 10 ? 10 : (_Cards[i] / 4) + 1;
                }
            }
            if (_cardsArr[5] % 10 == _temporary1)
                break;
            bool _flag = true;
            for (int i = 0; i < 2; ++i) {
                if (_arr[_temporary2 - i - 1] == 0) {
                    _flag = false;
                }
            }
            if (_flag == true)
                return false;
        }
    }
    for (int i = 0; i < _temporary2; ++i) {
        if (_arr[i] == 1) {
            _cardsArr[_temporary3] = _Cards[i];
            cardsPond[_Cards[i]][1] = 1;
            _temporary3++;
        }
    }
    for (int i = 0; i < 5; ++i) {
        table_Cards[seat][i] = (((_cardsArr[i] % 4) + 1) << 4) | (_cardsArr[i] / 4 + 1);
    }
    return true;
}

void main() {
    char table_Cards[5][5] = { 0 };
    table_Cards[0][0] = 37;
    table_Cards[0][1] = 28;
    table_Cards[0][2] = 25;
    table_Cards[0][3] = 72;
    table_Cards[0][4] = 53;
    table_Cards[1][0] = 17;
    table_Cards[1][1] = 74;
    table_Cards[1][2] = 58;
    table_Cards[1][3] = 35;
    table_Cards[1][4] = 42;
    table_Cards[2][0] = 34;
    table_Cards[2][1] = 59;
    table_Cards[2][2] = 40;
    table_Cards[2][3] = 19;
    table_Cards[2][4] = 22;
    table_Cards[3][0] = 18;
    table_Cards[3][1] = 21;
    table_Cards[3][2] = 68;
    table_Cards[3][3] = 77;
    table_Cards[3][4] = 52;
    table_Cards[4][0] = 75;
    table_Cards[4][1] = 50;
    table_Cards[4][2] = 73;
    table_Cards[4][3] = 56;
    table_Cards[4][4] = 69;

    for (int i = 0; i < 5; ++i) {
        for (int j = 0; j < 5; ++j) {
            cout << int(table_Cards[i][j] & 0x0f) << "___";
        }
        cout << endl;
    }
    cout << "++++++++++++++++++" <<endl;
    for (int k = 1; k < 13; ++k) {
        changeCards(0, k, table_Cards);

        for (int i = 0; i < 5; ++i) {
            for (int j = 0; j < 5; ++j) {
                cout << "__" << int(table_Cards[i][j] & 0x0f) << "__";
            }
            cout << endl;
        }
        cout << "-------------------" << endl;
    }
    system("pause");
}

 

 

 

 

 

 

运行测试只取了牌值输出:

 

转载于:https://www.cnblogs.com/L-N-C-D/p/7590986.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,斗牛游戏是一个非常经典的纸游戏,你可以使用 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 ``` 这就是一个简单的斗牛游戏的实现,你可以根据自己的需求进行修改和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值