#include <bits/stdc++.h>
#include <windows.h>
#include <conio.h>
using namespace std;
class DiceGame {
private:
int playerDice[5];
int aiDice[5];
int currentBidQuantity;
int currentBidValue;
bool isPlayerTurn;
int playerScore;
int aiScore;
// 摇骰子
void rollDice(int dice[], int size) {
for (int i = 0; i < size; ++i) {
dice[i] = rand() % 6 + 1;
}
}
// 显示骰子(仅显示自己的骰子)
void displayDice(int dice[], int size, const string& owner, bool showAll = false) {
if (owner == "你" || showAll) {
cout << owner << "的骰子: ";
for (int i = 0; i < size; ++i) {
cout << dice[i] << " ";
}
cout << endl;
} else {
cout << owner << "的骰子: [隐藏]" << endl;
}
}
// 计算特定点数的骰子数量(包括1点,因为1点可以当任意点数)
int countDice(int dice[], int size, int value) {
int count = 0;
for (int i = 0; i < size; ++i) {
if (dice[i] == value || dice[i] == 1) {
count++;
}
}
return count;
}
// 玩家回合
void playerTurn() {
cout << "\n--- 你的回合 ---" << endl;
displayDice(playerDice, 5, "你");
while (true) {
cout << "当前叫骰: ";
if (currentBidQuantity > 0) {
cout << currentBidQuantity << " 个 " << currentBidValue;
} else {
cout << "[无]";
}
cout << endl;
cout << "请选择操作:\n";
cout << "1. 叫骰\n";
cout << "2. 开骰\n";
cout << "3. Spot On(精准叫骰)\n";
cout << "输入选择(1-3): ";
int choice;
cin >> choice;
// 清除输入缓冲区
cin.ignore(numeric_limits<streamsize>::max(), '\n');
if (choice == 1) { // 叫骰
int quantity, value;
cout << "请输入你的叫骰(数量 点数,例如 3 4 表示三个4,1可以叫): ";
cin >> quantity >> value;
// 验证输入
while (quantity <= 0 || value < 1 || value > 6 ||
(currentBidQuantity > 0 && (quantity < currentBidQuantity ||
(quantity == currentBidQuantity && value <= currentBidValue)))) {
if (currentBidQuantity == 0) {
cout << "无效输入,请重新输入(数量>0, 点数1-6): ";
} else {
cout << "叫骰必须大于前一个叫骰(数量或点数),请重新输入: ";
}
cin >> quantity >> value;
}
currentBidQuantity = quantity;
currentBidValue = value;
isPlayerTurn = false;
break;
}
else if (choice == 2) { // 开骰
if (currentBidQuantity == 0) {
cout << "还没有叫骰,不能开骰!" << endl;
continue;
}
resolveChallenge(true);
break;
}
else if (choice == 3) { // Spot On
if (currentBidQuantity == 0) {
cout << "还没有叫骰,不能使用Spot On!" << endl;
continue;
}
cout << "你选择了Spot On(精准叫骰)!" << endl;
resolveSpotOn(true);
break;
}
else {
cout << "无效选择,请重新输入!" << endl;
}
}
}
// AI回合
void aiTurn() {
cout << "\n--- AI回合 ---" << endl;
// 计算当前叫骰的成功概率
int totalPossible = countDice(aiDice, 5, currentBidValue) + 3; // AI估计玩家可能有3个
// 决定AI行为: 1=叫骰, 2=开骰, 3=spot on
int action;
if (currentBidQuantity == 0) {
// 如果还没有叫骰,AI必须叫骰
action = 1;
} else {
// 根据概率决定行为
double confidence = static_cast<double>(totalPossible) / currentBidQuantity;
if (confidence > 1.3) {
// 很自信,选择叫骰或spot on
if (rand() % 100 < 20 && abs(totalPossible - currentBidQuantity) <= 1) {
action = 3; // 20%几率spot on
} else {
action = 1;
}
} else if (confidence > 0.8) {
// 中等自信,通常叫骰
action = 1;
} else {
// 不自信,开骰
action = 2;
}
}
// 执行AI行为
if (action == 1) { // 叫骰
int newQuantity = currentBidQuantity;
int newValue = currentBidValue;
if (currentBidQuantity == 0) {
// 第一次叫骰,AI随机选择
newQuantity = countDice(aiDice, 5, rand() % 6 + 1) + 1;
newValue = rand() % 6 + 1;
} else {
// AI策略: 随机增加数量或点数
int strategy = rand() % 3;
if (strategy == 0 && newValue < 6) {
newValue++;
} else if (strategy == 1 && newValue > 1) {
newValue--;
} else {
newQuantity++;
}
// 确保叫骰比之前大
if (newQuantity < currentBidQuantity ||
(newQuantity == currentBidQuantity && newValue <= currentBidValue)) {
newQuantity = currentBidQuantity + 1;
}
}
cout << "AI叫骰: " << newQuantity << " 个 " << newValue << endl;
currentBidQuantity = newQuantity;
currentBidValue = newValue;
isPlayerTurn = true;
}
else if (action == 2) { // 开骰
cout << "AI选择开骰!" << endl;
resolveChallenge(false);
}
else { // Spot On
cout << "AI选择Spot On(精准叫骰)!" << endl;
resolveSpotOn(false);
}
}
// 解决挑战(开骰)
void resolveChallenge(bool playerChallenging) {
int totalPlayer = countDice(playerDice, 5, currentBidValue);
int totalAI = countDice(aiDice, 5, currentBidValue);
int total = totalPlayer + totalAI;
cout << "\n--- 开骰结果 ---" << endl;
displayDice(playerDice, 5, "你", true);
displayDice(aiDice, 5, "AI", true);
cout << "总共有 " << total << " 个 " << currentBidValue << "(包括1点)" << endl;
cout << "叫骰是 " << currentBidQuantity << " 个 " << currentBidValue << endl;
if (total >= currentBidQuantity) {
cout << "叫骰成功!" << endl;
if (playerChallenging) {
cout << "你输了这一轮! AI得1分" << endl;
aiScore++;
} else {
cout << "AI输了这一轮! 你得1分" << endl;
playerScore++;
}
} else {
cout << "叫骰失败!" << endl;
if (playerChallenging) {
cout << "你赢了这一轮! 你得1分" << endl;
playerScore++;
} else {
cout << "AI赢了这一轮! AI得1分" << endl;
aiScore++;
}
}
displayScores();
cout << "按任意键继续......" << endl;
char ch = getch();
startNewRound();
}
// 解决Spot On挑战
void resolveSpotOn(bool playerInitiated) {
int totalPlayer = countDice(playerDice, 5, currentBidValue);
int totalAI = countDice(aiDice, 5, currentBidValue);
int total = totalPlayer + totalAI;
cout << "\n--- Spot On 结果 ---" << endl;
displayDice(playerDice, 5, "你", true);
displayDice(aiDice, 5, "AI", true);
cout << "总共有 " << total << " 个 " << currentBidValue << "(包括1点)" << endl;
cout << "叫骰是 " << currentBidQuantity << " 个 " << currentBidValue << endl;
if (total == currentBidQuantity) {
cout << "Spot On 成功! 精准命中!" << endl;
if (playerInitiated) {
cout << "你赢了这一轮! 你得2分" << endl;
playerScore += 2;
} else {
cout << "AI赢了这一轮! AI得2分" << endl;
aiScore += 2;
}
} else {
cout << "Spot On 失败!" << endl;
if (playerInitiated) {
cout << "你输了这一轮! AI得1分" << endl;
aiScore++;
} else {
cout << "AI输了这一轮! 你得1分" << endl;
playerScore++;
}
}
displayScores();
cout << "按任意键继续......" << endl;
char ch = getch();
startNewRound();
}
// 显示当前分数
void displayScores() {
cout << "\n当前比分 - 你: " << playerScore << " | AI: " << aiScore << endl;
}
// 开始新一轮
void startNewRound() {
system("cls");
displayScores();
cout << '\n';
cout << "\n=== 开始新一轮 ===" << endl;
rollDice(playerDice, 5);
rollDice(aiDice, 5);
currentBidQuantity = 0;
currentBidValue = 0;
isPlayerTurn = (rand() % 2 == 0); // 随机决定谁先开始
if (isPlayerTurn) {
cout << "你先开始!" << endl;
} else {
cout << "AI先开始!" << endl;
}
}
public:
DiceGame() : playerScore(0), aiScore(0) {
srand(time(0));
startNewRound();
}
void play() {
cout << "欢迎来到大话骰游戏(增强版)!" << endl;
cout << "新功能:\n";
cout << "1. 可以叫1点(1点仍然是万能牌)\n";
cout << "2. 加入Spot On(精准叫骰)选项\n";
cout << "3. 计分系统\n";
while (true) {
if (isPlayerTurn) {
playerTurn();
} else {
aiTurn();
}
}
}
// 修正displayDice拼写错误
// void displayDice(int dice[], int size, const string& owner, bool showAll = false) {
// if (owner == "你" || showAll) {
// cout << owner << "的骰子: ";
// for (int i = 0; i < size; ++i) {
// cout << dice[i] << " ";
// }
// cout << endl;
// } else {
// cout << owner << "的骰子: [隐藏]" << endl;
// }
// }
};
int main() {
cout << "欢迎来到大话骰游戏(增强版)!" << endl;
cout << "规则说明:" << endl;
cout << "1. 你和AI各有5个骰子" << endl;
cout << "2. 轮流叫骰(数量和点数,1点可以叫)" << endl;
cout << "3. 1点可以当作任意点数" << endl;
cout << "4. 你可以选择:" << endl;
cout << " - 叫骰(数量必须比前一个大)" << endl;
cout << " - 开骰(如果认为对方叫骰不真实)" << endl;
cout << " - Spot On(精准叫骰,如果数量完全正确获得双倍分数)" << endl;
cout << "5. 开骰后统计双方所有该点数的骰子(包括1点)" << endl;
cout << "6. 普通胜利得1分,Spot On胜利得2分" << endl;
cout << "按任意键继续......" << endl;
char ch = getch();
DiceGame game;
game.play();
return 0;
}