C++#include <iostream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <cstdio> // 引入sprintf所需的头文件
using namespace std;
// 轮盘格子总数
const int WHEEL_SIZE = 38;
// 轮盘格子颜色:0-绿色,1-红色,2-黑色
const int COLORS[WHEEL_SIZE] = {0, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 2, 1, 2, 1, 2, 1, 2, 1, 0, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1};
// 轮盘格子数字
const int NUMBERS[WHEEL_SIZE] = {0, 32, 15, 19, 4, 21, 2, 25, 17, 34, 6, 27, 13, 36, 11, 30, 8, 23, 10, 00, 5, 24, 16, 33, 1, 20, 14, 31, 9, 22, 18, 29, 7, 28, 12, 35, 3, 26};
// 显示轮盘信息
void displayWheel() {
cout << "恶魔轮盘:" << endl;
cout << "-------------------------" << endl;
for (int i = 0; i < WHEEL_SIZE; i++) {
cout << setw(2) << NUMBERS[i] << " ";
if (COLORS[i] == 0) cout << "绿 | ";
else if (COLORS[i] == 1) cout << "红 | ";
else cout << "黑 | ";
if ((i + 1) % 5 == 0) cout << endl;
}
cout << endl << "-------------------------" << endl;
}
// 旋转轮盘并返回结果索引
int spinWheel() {
return rand() % WHEEL_SIZE;
}
// 显示下注选项
void displayBetOptions() {
cout << "\n下注选项:" << endl;
cout << "1. 猜数字 (0-36, 00)" << endl;
cout << "2. 猜颜色 (红/黑)" << endl;
cout << "3. 猜奇偶 (除0和00外)" << endl;
cout << "4. 猜大小 (1-18为小, 19-36为大)" << endl;
cout << "5. 退出游戏" << endl;
cout << "请选择下注类型: ";
}
// 将数字转换为显示格式(00特殊处理)
string numberToString(int num) {
if (num == 0 && NUMBERS[19] == 00) { // 处理00的情况
for (int i = 0; i < WHEEL_SIZE; i++) {
if (i == 19) return "00";
}
}
// 替换to_string,使用sprintf实现数字转字符串
char buffer[10]; // 缓冲区存储转换后的字符串
sprintf(buffer, "%d", num); // 将数字格式化到缓冲区
return string(buffer); // 转换为string并返回
}
int main() {
srand(time(0)); // 初始化随机数生成器
int money = 1000; // 初始资金
int betType, betAmount, betChoice;
bool running = true;
cout << "欢迎来到恶魔轮盘!" << endl;
cout << "初始资金: " << money << " 筹码" << endl;
while (running && money > 0) {
displayWheel();
cout << "当前资金: " << money << " 筹码" << endl;
displayBetOptions();
cin >> betType;
if (betType == 5) {
running = false;
break;
}
// 输入下注金额
cout << "请输入下注金额: ";
cin >> betAmount;
if (betAmount <= 0 || betAmount > money) {
cout << "无效的下注金额!" << endl;
continue;
}
// 根据下注类型处理
switch (betType) {
case 1: {
cout << "请输入要猜的数字(0-36或00): ";
string numStr;
cin >> numStr;
// 转换用户输入为内部表示
if (numStr == "00") {
betChoice = 19; // 00在数组中的索引
} else {
// 使用atoi替代stoi,将字符串转为整数
betChoice = atoi(numStr.c_str()); // 关键修改处
// 找到数字对应的索引
bool found = false;
for (int i = 0; i < WHEEL_SIZE; i++) {
if (NUMBERS[i] == betChoice) {
betChoice = i;
found = true;
break;
}
}
if (!found) {
cout << "无效的数字!" << endl;
continue;
}
}
break;
}
case 2: {
cout << "请选择颜色(1=红, 2=黑): ";
cin >> betChoice;
if (betChoice != 1 && betChoice != 2) {
cout << "无效的颜色选择!" << endl;
continue;
}
break;
}
case 3: {
cout << "请选择奇偶(1=奇, 2=偶): ";
cin >> betChoice;
if (betChoice != 1 && betChoice != 2) {
cout << "无效的奇偶选择!" << endl;
continue;
}
break;
}
case 4: {
cout << "请选择大小(1=小(1-18), 2=大(19-36)): ";
cin >> betChoice;
if (betChoice != 1 && betChoice != 2) {
cout << "无效的大小选择!" << endl;
continue;
}
break;
}
default: {
cout << "无效的选项!" << endl;
continue;
}
}
// 旋转轮盘
cout << "\n轮盘开始旋转..." << endl;
int resultIndex = spinWheel();
int resultNum = NUMBERS[resultIndex];
string resultNumStr = numberToString(resultNum);
string resultColor;
if (COLORS[resultIndex] == 0) resultColor = "绿色";
else if (COLORS[resultIndex] == 1) resultColor = "红色";
else resultColor = "黑色";
cout << "轮盘停止! 结果是: " << resultNumStr << " (" << resultColor << ")" << endl;
// 判断输赢
bool win = false;
int odds = 0;
switch (betType) {
case 1:
win = (resultIndex == betChoice);
odds = 35; // 猜中数字赔率35:1
break;
case 2:
win = (COLORS[resultIndex] == betChoice);
odds = 1; // 猜中颜色赔率1:1
break;
case 3:
// 0和00不算奇偶
if (resultNum == 0 || resultIndex == 19) {
win = false;
} else {
win = ((resultNum % 2 == 1 && betChoice == 1) ||
(resultNum % 2 == 0 && betChoice == 2));
}
odds = 1; // 猜中奇偶赔率1:1
break;
case 4:
// 0和00不算大小
if (resultNum == 0 || resultIndex == 19) {
win = false;
} else {
win = ((resultNum >= 1 && resultNum <= 18 && betChoice == 1) ||
(resultNum >= 19 && resultNum <= 36 && betChoice == 2));
}
odds = 1; // 猜中大小赔率1:1
break;
}
// 处理结果
if (win) {
int winAmount = betAmount * odds;
money += winAmount;
cout << "恭喜你赢了! 赢得 " << winAmount << " 筹码" << endl;
} else {
money -= betAmount;
cout << "很遗憾, 你输了 " << betAmount << " 筹码" << endl;
}
if (money <= 0) {
cout << "你已经没有筹码了, 游戏结束!" << endl;
break;
}
cout << "按回车键继续...";
cin.ignore();
cin.get();
system("cls"); // 清屏,Windows系统适用
// 如果是Linux或Mac系统,请使用 system("clear");
}
cout << "游戏结束! 最终资金: " << money << " 筹码" << endl;
return 0;
}