本算法设计拍的表示为: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"); }
运行测试只取了牌值输出: