四子棋程序两个问题求帮助!

namanama 2016-11-23 10:51:33
简单的四子棋程序,人人或者人机。玩家1在竖着连出四个棋子的时候不被判定成赢,但是玩家2竖着四连的时候会被判定成赢。没发现在player2和player1的两个判断里面有什么不同。
以及在人机模式下。在1~7里面随机生成一个数做横坐标。有时候正常,有时候人输入放置棋后轮到电脑,电脑没有放置棋,就直接再跳到人输入放棋。找不到原因在哪,求改程序

#include <stdio.h>
#include<stdlib.h>
int main()
{
char a = '+';
char board[6][7] = { { a, a, a, a, a, a, a },
{ a, a, a, a, a, a, a },
{ a, a, a, a, a, a, a },
{ a, a, a, a, a, a, a },
{ a, a, a, a, a, a, a },
{ a, a, a, a, a, a, a } };


int M1;
int M2;
int win = 0;
int i,j;
int depth;
int r;
int m = 1;
int n = 7;
int S1 = 0;
int S2 = 0;
char X = 0;
char O = 0;
char again = 'y';
int mode;

while (again == 'Y' || again == 'y')
{
printf("1: Player1 VS Player2\n");
printf("0: Player VS Computer(non intelligent)\n");
printf("Enter the mode number you choose:\n");
scanf("%d", &mode);
for (i = 0; i < 6; i++)
{
if (i==0)
{
printf(" 1 2 3 4 5 6 7 \n ---------------\n");
printf(" | %c %c %c %c %c %c %c |\n", board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6]);
}
if (i == 1)
printf(" | %c %c %c %c %c %c %c |\n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6]);
if (i == 2)
printf(" | %c %c %c %c %c %c %c |\n", board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6]);
if (i == 3)
printf(" | %c %c %c %c %c %c %c |\n", board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6]);
if (i == 4)
printf(" | %c %c %c %c %c %c %c |\n", board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6]);
if (i == 5)
{
printf(" | %c %c %c %c %c %c %c |\n", board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6]);
printf(" ---------------\n");
}
}
while (win == 0)
{
if (win == 0)
{
printf("Player1 enter the column number you want to place:\n");
scanf("%d", &M1);
if (M1 == 1)
{
depth = 5;
while (board[depth][0] == 'X' || board[depth][0] == 'O')
{
depth--;
}
board[depth][0] = 'X';
}
else if (M1 == 2)
{
depth = 5;
while (board[depth][1] == 'X' || board[depth][1] == 'O')
{
depth--;
}
board[depth][1] = 'X';
}
else if (M1 == 3)
{
depth = 5;
while (board[depth][2] == 'X' || board[depth][2] == 'O')
{
depth--;
}
board[depth][2] = 'X';
}
else if (M1 == 4)
{
depth = 5;
while (board[depth][3] == 'X' || board[depth][3] == 'O')
{
depth--;
}
board[depth][3] = 'X';
}
else if (M1 == 5)
{
depth = 5;
while (board[depth][4] == 'X' || board[depth][4] == 'O')
{
depth--;
}
board[depth][4] = 'X';
}
else if (M1 == 6)
{
depth = 5;
while (board[depth][5] == 'X' || board[depth][5] == 'O')
{
depth--;
}
board[depth][5] = 'X';
}
else if (M1 == 7)
{
depth = 5;
while (board[depth][6] == 'X' || board[depth][6] == 'O')
{
depth--;
}
board[depth][6] = 'X';
}
}

for (i = 0; i < 6; i++)
{
if (i == 0)
{
printf(" 1 2 3 4 5 6 7 \n ---------------\n");
printf(" | %c %c %c %c %c %c %c |\n", board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6]);
}
if (i == 1)
printf(" | %c %c %c %c %c %c %c |\n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6]);
if (i == 2)
printf(" | %c %c %c %c %c %c %c |\n", board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6]);
if (i == 3)
printf(" | %c %c %c %c %c %c %c |\n", board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6]);
if (i == 4)
printf(" | %c %c %c %c %c %c %c |\n", board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6]);
if (i == 5)
{
printf(" | %c %c %c %c %c %c %c |\n", board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6]);
printf(" ---------------\n");
}
}


for (i = 5; i > 0; --i)
{
if ((board[i][0] == 'X'&&board[i][1] == 'X'&&board[i][2] == 'X'&&board[i][3] == 'X') || (board[i][4] == 'X'&&board[i][1] == 'X'&&board[i][2] == 'X'&&board[i][3] == 'X') || (board[i][4] == 'X'&&board[i][5] == 'X'&&board[i][2] == 'X'&&board[i][3] == 'X') || (board[i][4] == 'X'&&board[i][5] == 'X'&&board[i][6] == 'X'&&board[i][3] == 'X'))
win = 1;
}
for (j = 6; j > 0; --j) //这里判断条件错在哪里?为什么运行时玩家1竖着四连不被判定成赢?
{
if ((board[0][j] == 'X'&&board[1][j] == 'X'&&board[2][j] == 'X'&&board[3][j] == 'X') || (board[1][j] == 'X'&&board[2][j] == 'X'&&board[3][j] == 'X'&&board[4][j] == 'X') || (board[2][j] == 'X'&&board[3][j] == 'X'&&board[4][j] == 'X'&&board[5][j] == 'X'))
win = 1; //for vertical
}
for (i = 5; i > 2; --i)
{

if ((board[i][0] == 'X'&&board[i - 1][1] == 'X'&&board[i - 2][2] == 'X'&&board[i - 3][3] == 'X') || (board[i][1] == 'X'&&board[i - 1][2] == 'X'&&board[i - 2][3] == 'X'&&board[i - 3][4] == 'X') || (board[i][2] == 'X'&&board[i - 1][3] == 'X'&&board[i - 2][4] == 'X'&&board[i - 3][5] == 'X') || (board[i][3] == 'X'&&board[i - 1][4] == 'X'&&board[i - 2][5] == 'X'&&board[i - 3][6] == 'X'))
win = 1; //for diagonally up right
}
for (j = 5; i > 2; --i)
{
if ((board[0][j] == 'X'&&board[1][j + 1] == 'X'&&board[2][j + 2] == 'X'&&board[3][j + 3] == 'X') || (board[1][j + 1] == 'X'&&board[2][j + 2] == 'X'&&board[3][j + 3] == 'X'&&board[4][j + 4] == 'X') || (board[2][j + 2] == 'X'&&board[3][j + 3] == 'X'&&board[4][j + 4] == 'X'&&board[5][j + 5] == 'X'))
win = 1; //for diagonally up left
}


if (win == 0)
{
if (mode == 1)
{
printf("Player2 enter the column number you want to place:\n");
scanf("%d", &M2);
}
if (mode == 0)
{
printf("Computer's turn.\n");
r = rand() % (n - m + 1) + m;
M2 = r;
}

if (M2 == 1)
{
depth = 5;
while (board[depth][0] == 'X' || board[depth][0] == 'O')
{
depth--;
}
board[depth][0] = 'O';
}
else if (M2 == 2)
{
depth = 5;
while (board[depth][1] == 'X' || board[depth][1] == 'O')
{
depth--;
}
board[depth][1] = 'O';
}
else if (M2 == 3)
{
depth = 5;
while (board[depth][2] == 'X' || board[depth][2] == 'O')
{
depth--;
}
board[depth][2] = 'O';
}
else if (M2 == 4)
{
depth = 5;
while (board[depth][3] == 'X' || board[depth][3] == 'O')
{
depth--;
}
board[depth][3] = 'O';
}
else if (M2 == 5)
{
depth = 5;
while (board[depth][4] == 'X' || board[depth][4] == 'O')
{
depth--;
}
board[depth][4] = 'O';
}
else if (M2 == 6)
{
depth = 5;
while (board[depth][5] == 'X' || board[depth][5] == 'O')
{
depth--;
}
board[depth][5] = 'O';
}
else if (M2 == 7)
{
if (board[0][6] == 'X' || board[0][6] == 'O')
printf("nope");
else
{
depth = 5;
while (board[depth][6] == 'X' || board[depth][6] == 'O')
{
depth--;
}
board[depth][6] = 'O';
}
}
}

for (i = 0; i < 6; i++)
{
if (i == 0)
{
printf(" 1 2 3 4 5 6 7 \n ---------------\n");
printf(" | %c %c %c %c %c %c %c |\n", board[0][0], board[0][1], board[0][2], board[0][3], board[0][4], board[0][5], board[0][6]);
}
if (i == 1)
printf(" | %c %c %c %c %c %c %c |\n", board[1][0], board[1][1], board[1][2], board[1][3], board[1][4], board[1][5], board[1][6]);
if (i == 2)
printf(" | %c %c %c %c %c %c %c |\n", board[2][0], board[2][1], board[2][2], board[2][3], board[2][4], board[2][5], board[2][6]);
if (i == 3)
printf(" | %c %c %c %c %c %c %c |\n", board[3][0], board[3][1], board[3][2], board[3][3], board[3][4], board[3][5], board[3][6]);
if (i == 4)
printf(" | %c %c %c %c %c %c %c |\n", board[4][0], board[4][1], board[4][2], board[4][3], board[4][4], board[4][5], board[4][6]);
if (i == 5)
{
printf(" | %c %c %c %c %c %c %c |\n", board[5][0], board[5][1], board[5][2], board[5][3], board[5][4], board[5][5], board[5][6]);
printf(" ---------------\n");
}
}

for (i = 5; i > 0; --i)
{
if ((board[i][0] == 'O'&&board[i][1] == 'O'&&board[i][2] == 'O'&&board[i][3] == 'O') || (board[i][4] == 'O'&&board[i][1] == 'O'&&board[i][2] == 'O'&&board[i][3] == 'O') || (board[i][4] == 'O'&&board[i][5] == 'O'&&board[i][2] == 'O'&&board[i][3] == 'O') || (board[i][4] == 'O'&&board[i][5] == 'O'&&board[i][6] == 'O'&&board[i][3] == 'O'))
win = 2;
}
for (j = 6; j > 0; --j)
{
if ((board[0][j] == 'O'&&board[1][j] == 'O'&&board[2][j] == 'O'&&board[3][j] == 'O') || (board[1][j] == 'O'&&board[2][j] == 'O'&&board[3][j] == 'O'&&board[4][j] == 'O') || (board[2][j] == 'O'&&board[3][j] == 'O'&&board[3][j] == 'O'&&board[5][j] == 'O'))
win = 2; //for vertical
}
for (i = 5; i > 2; --i)
{
省略了
}
for (j = 5; i > 2; --i)
{
省略了
}
}
if (win == 1)
{
++S1;
printf("Player 1 Wins!!!\n");
}
else if (win == 2)
{
++S2;
printf("Player 2 Wins!!!\n");
}

scanf("%c", &again);
while (again != 'Y'&&again != 'y'&&again != 'N'&&again != 'n')
{
printf("Would you like to play again? (Y/N): ");
scanf("%c", &again);
}

printf("\n\n\n\n\n\n");
win = 0;
for (i = 0; i<6; i++)
{
board[i][0] = a;
board[i][1] = a;
board[i][2] = a;
board[i][3] = a;
board[i][4] = a;
board[i][5] = a;
board[i][6] = a;
}
}
return 0;
}
...全文
167 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 2016-11-24
  • 打赏
  • 举报
回复
建议在必要地方设置断点,然后单步跟踪运行,观察每一步是否与你期望的结果一致
小弟有礼了 2016-11-24
  • 打赏
  • 举报
回复
你会调试吗?就问你这句话,自己debug一下,就可以了。不会debug的话,上网搜这方面的内容就可以了

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧