上一篇博客发表了关于用C语言来实现小游戏三字棋的代码,但是这个代码中的电脑难度可谓是极低,我通过改变电脑落子的算法,增强了电脑的难度。
具体思路为:
一、让电脑优先判断自己是否有可能赢。
二、判断自己可能赢的前提是,让自己能够知道自己在行、列、两条对角线中是否有两个相同的棋子,并且第三个地方为空,也就是‘ ’。
三、如果电脑没有任何赢的可能,那么判断玩家在有行、列、两条对角线中是否有两个相同的棋子,并且第三个地方为空,进行对玩家的堵截。
四、若电脑跟玩家都没有可能赢的情况那么让电脑自己随机产生一个坐标进行落子。
以下为增强后的电脑下棋代码:
void RobotMove(char board[ROW][COL], int row, int col)
{
int i = 0;
int j = 0;
int a = 0;
int b = 0;
int c = 0;
if (a == 0)
{
for (i = 0; i < ROW; i++)
{
if ((board[i][0] == board[i][1] && board[i][0] == 'O'&&board[i][2] != 'X')
|| (board[i][1] == board[i][2] && board[i][1] == 'O'&&board[i][0] != 'X')
|| (board[i][0] == board[i][2] && board[i][0] == 'O'&&board[i][1] != 'X'))
{
a = 1;
b = 1;
break;
}
}
}
if (a == 0)
{
for (j = 0; j < ROW; j++)
{
if ((board[0][j] == board[1][j] && board[0][j] == 'O'&&board[2][j] != 'X')
|| (board[1][j] == board[2][j] && board[1][j] == 'O'&&board[0][j] != 'X')
|| (board[0][j] == board[2][j] && board[0][j] == 'O'&&board[1][j] != 'X'))
{
b = 2;
a = 1;
break;
}
}
}
if ((board[0][0] == board[1][1] && board[0][0] == 'O'&&board[2][2] != 'X')
|| (board[1][1] == board[2][2] && board[1][1] == 'O'&&board[0][0] != 'X')
|| (board[0][0] == board[2][2] && board[0][0] == 'O'&&board[1][1] != 'X')
&& (a == 0))
{
b = 3;
a = 1;
}
if ((board[0][2] == board[1][1] && board[0][2] == 'O'&&board[2][0] != 'X')
|| (board[1][1] == board[2][0] && board[1][1] == 'O'&&board[0][2] != 'X')
|| (board[0][2] == board[2][0] && board[0][2] == 'O'&&board[1][1] != 'X')
&& (a == 0))
{
b = 4;
a = 1;
}
switch (b)
{
case 1:
do
{
j = rand() % 3;
if (board[i][j] == ' ')
{
board[i][j] = 'O';
break;
}
} while (1);
break;
case 2:
do
{
i = rand() % 3;
if (board[i][j] == ' ')
{
board[i][j] = 'O';
break;
}
} while (1);
break;
case 3:
do
{
i = rand() % 3;
j = rand() % 3;
if ((i == j) && (board[i][j] == ' '))
{
board[i][j] = 'O';
break;
}
} while (1);
break;
case 4:
do
{
i = rand() % 3;
j = rand() % 3;
if ((i == j) && (board[i][j] == ' ') && (i == 1) ||
(i == j + 2) && (board[i][j] == ' ') ||
(j == i + 2) && (board[i][j] == ' '))
{
board[i][j] = 'O';
break;
}
} while (1);
break;
}
if (a == 0)
{
for (i = 0; i < ROW; i++)
{
if ((board[i][0] == board[i][1] && board[i][0] == 'X'&&board[i][2] != 'O')
|| (board[i][1] == board[i][2] && board[i][1] == 'X'&&board[i][0] != 'O')
|| (board[i][0] == board[i][2] && board[i][0] == 'X'&&board[i][1] != 'O'))
{
c = 1;
a = 1;
break;
}
}
}
if (a == 0)
{
for (j = 0; j < ROW; j++)
{
if ((board[0][j] == board[1][j] && board[0][j] == 'X'&&board[2][j] != 'O')
|| (board[1][j] == board[2][j] && board[1][j] == 'X'&&board[0][j] != 'O')
|| (board[0][j] == board[2][j] && board[0][j] == 'X'&&board[1][j] != 'O'))
{
c = 2;
a = 1;
break;
}
}
}
if ((board[0][0] == board[1][1] && board[0][0] == 'X'&&board[2][2] != 'O')
|| (board[1][1] == board[2][2] && board[1][1] == 'X'&&board[0][0] != 'O')
|| (board[0][0] == board[2][2] && board[0][0] == 'X'&&board[1][1] != 'O')
&& (a == 0))
{
c = 3;
a = 1;
}
if ((board[0][2] == board[1][1] && board[0][2] == 'X'&&board[2][0] != 'O')
|| (board[1][1] == board[2][0] && board[1][1] == 'X'&&board[0][2] != 'O')
|| (board[0][2] == board[2][0] && board[0][2] == 'X'&&board[1][1] != 'O')
&& (a == 0))
{
c = 4;
a = 1;
}
switch (c)
{
case 1:
do
{
j = rand() % 3;
if (board[i][j] == ' ')
{
board[i][j] = 'O';
break;
}
} while (1);
break;
case 2:
do
{
i = rand() % 3;
if (board[i][j] == ' ')
{
board[i][j] = 'O';
break;
}
} while (1);
break;
case 3:
do
{
i = rand() % 3;
j = rand() % 3;
if ((i == j) && (board[i][j] == ' '))
{
board[i][j] = 'O';
break;
}
} while (1);
break;
case 4:
do
{
i = rand() % 3;
j = rand() % 3;
if ((i == j) && (board[i][j] == ' ') && (i == 1) ||
(i == j + 2) && (board[i][j] == ' ') ||
(j == i + 2) && (board[i][j] == ' '))
{
board[i][j] = 'O';
break;
}
} while (1);
break;
}
if (a == 0)
{
do
{
i = rand() % 3;
j = rand() % 3;
if (board[i][j] == ' ')
{
board[i][j] = 'O';
break;
}
} while (1);
}
}
如有问题,请及时联系博主,便于及时更改错误,谢谢大家!