家人们谁懂啊!AI没法使用,哪位大佬帮下忙:
代码如下:
#include <bits/stdc++.h>
using namespace std;
string prop_list[7] = {" ", "小麦 果汁", "华 子", "小 刀", "手 铐", "放 大 镜", "上帝 契约"};
int x, y;
int a[11];
class player {
private:
int x() {
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= 4; j++)
if (!prop[i][j]) return i;
return 0;
}
int y() {
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= 4; j++)
if (!prop[i][j]) return j;
return 0;
}
int convert(int n) {
if (n == 1) return 0;
else if (n == 2) return 2;
else return 4;
}
public:
int blood, prop[3][5] = {};
bool handcuffs = 0;
player() { //初始化
blood = 0;
}
bool empty() {
for (int i = 1; i <= 2; i++)
for (int j = 1; j <= 4; j++)
if (prop[i][j]) return false;
return true;
}
int random() { //随机血条2~5
blood = rand() % 4 + 2;
return blood;
}
void prop_randon(int c) { //随机获取道具
int cnt = 0;
if (!x()) return ;
else {
if (x() == 1) {
for (int i = y(); i <= 4; i++) {
cnt++;
prop[1][i] = (rand() % 21 + 1 - 1) / 4 + 1;
if (cnt == convert(c)) return ;
}
for (int i = 1; i <= 4; i++) {
cnt++;
prop[2][i] = (rand() % 21 + 1 - 1) / 4 + 1;
if (cnt == convert(c)) return ;
};
} else {
for (int i = y(); i <= 4; i++) {
cnt++;
prop[2][i] = (rand() % 21 + 1 - 1) / 4 + 1;
if (cnt == convert(c)) return ;
};
}
}
}
void print() { //当前道具展示
printf("┌──────────┬──────────┬──────────┬──────────┐\n");
for (int j = 1; j <= 4; j++) {
cout << "│" << prop_list[prop[1][j]];
}
cout << "│\n├──────────┼──────────┼──────────┼──────────┤\n";
for (int j = 1; j <= 4; j++) {
cout << "│" << prop_list[prop[2][j]];
}
cout << "│\n└──────────┴──────────┴──────────┴──────────┘";
putchar('\n');
}
};
int stochastic(int &sum, int &sum1) { //空/实
int s = rand() % 7 + 2;
sum = sum1 = 0;
for (int i = 1; i <= s; i++) {
a[i] = rand() % 2;
if (a[i]) sum1++;
else sum++;
}
if(sum1==0) {a[sum--]=1;sum++;}
if(sum==0) {a[sum1--]=1;sum1++;}
return s;
}
void change(int x) {
if (x == 1) cout << "实弹";
else cout << "空弹";
}
int prop_use(int x, int &your_blood, int &cnt, player &dealer, int &sum, int &sum1) { //空/实
switch (x) {
case 1:
cout << "退出的是";
change(a[cnt]);
putchar('\n');
if (a[cnt++]) sum1--;
else sum--;
system("pause");
break;
case 2:
your_blood++;
break;
case 3:
a[cnt] *= 2;
break;
case 4:
dealer.handcuffs = 1;
break;
case 5:
cout << "当前子弹是";
change(a[cnt]);
putchar('\n');
system("pause");
break;
default:
return 0;
}
return 1;
}
int AI(int sb, player &my, player &dealer, int &sum, int &sum1, int &cnt) {
int second_flag = 0;
if (sb == 0) return rand() % 2 + 1;
int cos[6] = {}, x[6][9] = {}, y[6][9] = {};
for (int i = 1; i <= 2; i++) {
for (int j = 1; j <= 4; j++) {
switch (dealer.prop[i][j]) {
case 1:
cos[1]++;//汽水
x[1][++x[0][0]] = i;
y[1][++y[0][0]] = j;
break;
case 2://华子
cos[2]++;
x[2][++x[0][0]] = i;
y[2][++y[0][0]] = j;
break;
case 3://小刀
cos[3] = 1;
x[3][++x[0][0]] = i;
y[3][++y[0][0]] = j;
break;
case 4://手铐
cos[4] = 1;
x[4][++x[0][0]] = i;
y[4][++y[0][0]] = j;
break;
case 5://放大镜
cos[5] = 1;
x[5][++x[0][0]] = i;
y[5][++y[0][0]] = j;
break;
default:
break;
}
}
}
if (cos[4]) {
my.handcuffs = 1;
dealer.prop[x[4][1]][y[4][1]] = 0;
}
for (int i = 1; cos[2] && dealer.blood <= 2; i++) {
dealer.blood++;
dealer.prop[x[2][i]][y[2][i]] = 0;
}
if (cos[5]) {
dealer.prop[x[5][1]][y[5][1]] = 0;
if (a[cnt]) {
if (cos[3]) {
a[cnt] *= 2;
dealer.prop[x[3][1]][y[3][1]] = 0;
return 2;
}
} else if (cos[1]) {
a[cnt++] = 0;
second_flag = 1;
}
}
if (cos[1]) {
if (second_flag) {
a[cnt++] = 0;
dealer.prop[x[3][2]][y[3][2]] = 0;
} else {
a[cnt++] = 0;
dealer.prop[x[3][1]][y[3][1]] = 0;
}
}
return rand() % 2 + 1;
}
int main() {
srand(time(0));
player my, b; //自己,恶魔
string s;
int sum, sum1, cnt = 0, game = 2, killed, sin, playing = 1;
do {
system("cls");
cout << "恶魔轮盘\n请输入你的名字(小写):";
getline(cin, s);
} while (s == "god" || s == "dealer");
system("cls");
b.blood = my.random();
//游戏部分
for (int f = 1; f <= 3; f++) {
my.prop_randon(game);
b.prop_randon(game);
cnt = 1;
playing = 1;
sin = stochastic(sum, sum1);
do {
flag:
system("cls");
printf("%d : %d\n", sum, sum1);
b.print();
putchar('\n');
cout << s;
printf(" %d : dealer %d\n\n", my.blood, b.blood);
my.print();
if (playing == 1) {
if (!my.handcuffs) {
if (!my.empty()) {
cout << "请使用道具:";
do {
cout << endl << "你要使用什么道具?";
do {
cin >> x >> y;
} while (!((x == 0 and y == 0) || my.prop[x][y] ));
bool cos = prop_use(my.prop[x][y], my.blood, cnt, b, sum, sum1);
my.prop[x][y] = 0;
if (cos) goto flag;
} while (x != 0 && y != 0 && my.empty());
}
cout << "请选择发枪对象:\n1:dealer 2:" << s << "\n";
cin >> killed;
} else my.handcuffs = 0;
}/*
if ((playing == 2 || my.handcuffs) && b.handcuffs)
killed = AI(b.empty(), my, b, sum, sum1, cnt);
else
b.handcuffs = 0;
if (!(my.empty() and b.empty())) {
my.prop_randon(game);
b.prop_randon(game);
}*/
if (killed == 1 and a[cnt] == 1) b.blood--;
if (killed == 2 and a[cnt] == 1) my.blood--;
if (killed == 1 and a[cnt] == 2) b.blood -= 2;
if (killed == 2 and a[cnt] == 2) my.blood -= 2;
if (b.blood == 0 or my.blood == 0) break;
if (a[cnt]) sum1--;
else sum--;
cnt++;
if (cnt > sin) {
cnt = 1;
sin = stochastic(sum, sum1);
playing = 1;
}
} while (1);
if (my.blood == 0) {
cout << "You Die!";
return 0;
}
my.blood = b.random();
}
system("cls");
cout<<"你成功了!";
return 0;
}
如果有好建议发我信息学奥赛一本通-编程启蒙(C++版)在线评测系统 (ssoier.cn)邮箱
名称:dyh0