gpt4 book ai didi

c - 井字游戏 AI 错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:09 25 4
gpt4 key购买 nike

char checkwinner(char ttt[][3]) {
char winner = 0;

for (int p = 0; winner == 0 && p < 2; p++) {
char XO = p == 0 ? 'x' : 'o';
for (int i = 0; winner == 0 && i < 3; i++) {
if (ttt[i][0] == XO && ttt[i][1] == XO && ttt[i][2] == XO)
winner = XO;
else
if (ttt[0][i] == XO && ttt[1][i] == XO && ttt[2][i] == XO)
winner = XO;
}

if (winner == 0 && ttt[0][0] == XO && ttt[1][1] == XO && ttt[2][2] == XO)
winner = XO;
else
if (winner == 0 && ttt[0][2] == XO && ttt[1][1] == XO && ttt[2][0] == XO)
winner = XO;
}
return winner;
}


int main (void) {
char b[3][3] = {{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}};
int turn = 0, i, j, loc, type, counter = 0;
char XO, AL;
char theChar = 'A';
char junk ='A';

printf("Type in 1 to play with AL, or 2 to play with another human\n");
scanf("%d", &type);

while (turn <= 9) {
char winner = checkwinner(b);
if (winner == 'x' || winner == 'o') {
printf("%c has won! You get 100 million DOGECOINS! Just enter your debt card information to our website, as well as your checking account and social security number. Go to www.THIS_IS_NOT_A_SCAM.com to get your dogecoins today!\n", winner);
return (0);
} else
if (((b[0][0] == 'x') || (b[0][0] == 'o'))
&& ((b[0][1] == 'x') || (b[0][1] == 'o'))
&& ((b[0][2] == 'x') || (b[0][2] == 'o'))
&& ((b[1][0] == 'x') || (b[1][0] == 'o'))
&& ((b[1][1] == 'x') || (b[1][1] == 'o'))
&& ((b[1][2] == 'x') || (b[1][2] == 'o'))
&& ((b[2][0] == 'x') || (b[2][0] == 'o'))
&& ((b[2][1] == 'x') || (b[2][1] == 'o'))
&& ((b[2][2] == 'x') || (b[2][2] == 'o'))) {
printf("This is a tie.\n");
return (0);

} //<---- missing brace!

if (type == 1) {
counter = counter + 1;

if (counter == 1) { XO = 'x'; }
if (counter == 2) { XO = 'o'; }
if (counter == 3) { XO = 'x'; }
if (counter == 4) { XO = 'o'; }
if (counter == 5) { XO = 'x'; }
if (counter == 6) { XO = 'o'; }
if (counter == 7) { XO = 'x'; }
if (counter == 8) { XO = 'o'; }
if (counter == 9) { XO = 'x'; }
}

if (type == 2) {
XO = 'x';
AL = 'o';

for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
b[i][j] = AL;
printArray(b);
}
}
}

theChar = getchar();
putchar(theChar);
printf("Enter number: ");
scanf("%d", &loc);

j = (loc - 1) % 3;
i = (loc - 1) / 3;

if ((b[i][j] == 'x') || (b[i][j] == 'o')) {
printf("That spot has been taken!\n");
return (0);
}
putchar('\n');
junk = getchar();
b[i][j] = XO;
printArray(b);
}
}

void printArray(char thearray[3][3]) {
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf("%c ", thearray[i][j]);
}
printf("\n");
}
}

现在我正在尝试完成 AI 模式。我的游戏不只是去寻找第一个空白区域并打印一个“o”,而是像这样打印 9 个板:

o 2 3
4 5 6
7 8 9
o o 3
4 5 6
7 8 9
o o o
4 5 6
7 8 9
o o o
o 5 6
7 8 9
o o o
o o 6
7 8 9
o o o
o o o
7 8 9
o o o
o o o
o 8 9
o o o
o o o
o o 9
o o o
o o o
o o o

打印一个“o”然后让“x”选择一步后,如何让游戏停止?感谢所有帮助。

最佳答案

您查找 AL 播放位置的代码不正确:

    if (type == 2) {
XO = 'x';
AL = 'o';

for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
b[i][j] = AL;
printArray(b);
}
}
}

您只需在网格上播放每个点并打印出来!

试试这个:

    if (type == 2) {
XO = 'x';
AL = 'o';

for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
if (b[i][j] != 'x' && b[i][j] != 'o') {
b[i][j] = AL;
i = j = 2; /* ugly way to exit the nested loops */
}
}
}
printArray(b);
}

但这种游戏策略很容易被击败!

此外,您还忘记了增加 turn,但如果您这样做了,您可以用简单的 if (turn == 9) 代替乏味的平局测试。

编辑:退出这个嵌套循环需要更多逻辑……最好将 AL 游戏移动到一个单独的函数。此外,你的逻辑是有缺陷的:如果 type2 而不是 1 你让 AL 玩...

这是一个简化和更正的版本,可以播放双方:

#include <stdio.h>

char checkwinner(char b[3][3]) {
for (int i = 0; i < 3; i++) {
if (b[i][0] == b[i][1] && b[i][1] == b[i][2])
return b[i][0];
if (b[0][i] == b[1][i] && b[1][i] == b[2][i])
return b[0][i];
}
if ((b[0][0] == b[1][1] && b[1][1] == b[2][2])
|| (b[0][2] == b[1][1] && b[1][1] == b[2][0])) {
return b[1][1];
}
return 0;
}

void printArray(char b[3][3]) {
for (int i = 0; i < 3; i++) {
printf("%c %c %c\n", b[i][0], b[i][1], b[i][2]);
}
printf("\n");
}

void playAl(char b[3][3], char AL) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (b[i][j] != 'x' && b[i][j] != 'o') {
b[i][j] = AL;
return;
}
}
}
}

int main (void) {
char board[3][3] = {{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}};
int turn, i, j, loc, type;

printf("What does AL play? (0:none, 1:x, 2:o, 3:both) ");
scanf("%d", &type);

for (turn = 0;; turn++) {
printArray(board);
char winner = checkwinner(board);
if (winner == 'x' || winner == 'o') {
printf("%c has won! Game over\n", winner);
return 0;
}
if (turn == 9) {
printf("This is a tie.\n");
return 0;
}

char XO = "xo"[turn % 2];
if (type & (1 << (turn % 2))) {
/* AL's turn to play */
playAl(board, XO);
continue;
}

for (;;) {
printf("Enter number: ");
if (scanf("%d", &loc) != 1)
return 1;

j = (loc - 1) % 3;
i = (loc - 1) / 3 % 3;
if (board[i][j] != 'x' && board[i][j] != 'o')
break;

printf("This spot is already taken!, try again\n");
}
board[i][j] = XO;
}
}

关于c - 井字游戏 AI 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34801348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com