为什么我的井字棋ai部分不起作用
#include <graphics.h>
#include <conio.h>
void play();
void draw();
int main()
{
draw();
play();
return 0;
}
void draw()
{
initgraph(300, 300);
setbkcolor(WHITE);
line(0, 100, 300, 100);
line(0, 200, 300, 200);
line(100, 0, 100, 300);
line(200, 0, 200, 300);
}
void play()
{
int set_x = 0, set_y = 0, time = 0, win = 0, map[3][3] = { 0 }, rand_x = rand() % 3, rand_y = rand() % 3;
const wchar_t* s[4] = { _T("平局!"), _T("●队获胜!"), _T("○队获胜!"), _T("请按Q退出") };
bool who = true;
MOUSEMSG msg;
char u;
int t1, t2, k = 0, j = 0, maps[3][3] = { 0 }, pre = 0; //maps影子 pre优先度
while (true)
{
msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN && who == true) //左键按下
{
if (msg.x < 100) {
set_x = 50; t1 = 0;
}
else if (msg.x > 100 && msg.x < 200) {
set_x = 150; t1 = 1;
}
else {
set_x = 250; t1 = 2;
}
if (msg.y < 100) {
set_y = 50; t2 = 0;
}
else if (msg.y > 100 && msg.y < 200) {
set_y = 150; t2 = 1;
}
else {
set_y = 250; t2 = 2;
}
//下棋位置
if (who) {
if (map[t1][t2] != 0) continue;
fillcircle(set_x, set_y, 25);
map[t1][t2] = 1; //容易出现1+2+0=3 1+1+1=3 的bug 换成发4和1
}
who = !who;
++time;
}
//ai
if (who == false)
{
if (pre == 1)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (maps[i][j] == 1)
{
set_x = (i) * 50 + (i + 1) * 50;
set_y = (j) * 50 + (j + 1) * 50;
circle(set_x, set_y, 35);
map[i][j] = 4; who = !who;
++time; break;
}
}
else if (pre == 2)
{
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
if (maps[i][j] == 2)
{
set_x = (i) * 50 + (i + 1) * 50;
set_y = (j) * 50 + (j + 1) * 50;
circle(set_x, set_y, 35);
map[i][j] = 4; who = !who;
++time; break;
}
}
else {
if (time <= 8)
{
while (map[rand_x][rand_y] != 0) {
rand_x = rand() % 3;
rand_y = rand() % 3;
}
set_x = (rand_x) * 50 + (rand_x + 1) * 50;
set_y = (rand_y) * 50 + (rand_y + 1) * 50;
circle(set_x, set_y, 35);
map[rand_x][rand_y] = 4;
who = !who;
++time;
}
}
}
//
for (int i = 0; i < 4; i++)
{
if (i == 0)
{
j = map[0][0] + map[1][1] + map[2][2];
k = map[2][0] + map[1][1] + map[0][2];
}
else
{
j = map[i - 1][0] + map[i - 1][1] + map[i - 1][2];
k = map[0][i - 1] + map[1][i - 1] + map[2][i - 1]; //hang
}
//
if (pre == 0)
{
if (k == 8 || j == 8)
{
pre = 1;
if (k == 8)
{
//1zuigao
if (i == 0)
{
for (int p = 0; p < 3; p++)
if (map[2 - p][p] == 0)
{
maps[2 - p][p] = 1; break; //1zuigao
}
}
else
{
for (int q = 0; q < 3; q++)
if (map[q][i - 1] == 0)
{
maps[q][i - 1] = 1; break;
}
}
}
//
if (j == 8)
{
//1zuigao
if (i == 0)
{
for (int p = 0; p < 3; p++)
if (map[p][p] == 0)
{
maps[p][p] = 1; break; //1zuigao
}
}
else
{
for (int q = 0; q < 3; q++)
if (map[i - 1][q] == 0)
maps[i - 1][q] = 1; break; //
;
}
}
break;
}
}
}
//di er ji
if (pre == 0)
{
for (int i = 0; i < 4; i++)
{
if (i == 0)
{
j = map[0][0] + map[1][1] + map[2][2];
k = map[2][0] + map[1][1] + map[0][2];
}
else
{
j = map[i - 1][0] + map[i - 1][1] + map[i - 1][2];
k = map[0][i - 1] + map[1][i - 1] + map[2][i - 1]; //hang
}
if (k == 2 || j == 2)
{
pre = 2;
if (k == 2)
{
if (i == 0)
{
for (int p = 0; p < 3; p++)
if (map[2 - p][p] == 0)
{
maps[2 - p][p] = 2; //1zuigao
}
}
else
{
for (int q = 0; q < 3; q++)
if (map[q][i - 1] == 0)
maps[q][i - 1] = 2;
}
}
//
if (j == 2)
{
if (i == 0)
{
for (int p = 0; p < 3; p++)
if (map[p][p] == 0)
{
maps[p][p] = 2; //1zuigao
}
}
else
{
for (int q = 0; q < 3; q++)
if (map[i - 1][q] == 0)
maps[i - 1][q] = 2; //
;
}
}
}
}
}
//3ji
if (pre == 0)
{
for (int i = 0; i < 4; i++)
{
if (i == 0)
{
j = map[0][0] + map[1][1] + map[2][2];
k = map[2][0] + map[1][1] + map[0][2];
}
else
{
j = map[i - 1][0] + map[i - 1][1] + map[i - 1][2];
k = map[0][i - 1] + map[1][i - 1] + map[2][i - 1]; //hang
}
if (k == 4 || j == 4)
{
pre = 3;
if (k == 4)
{
if (i == 0)
{
for (int p = 0; p < 3; p++)
if (map[2 - p][p] == 0)
{
maps[2 - p][p] = 3; //1zuigao
}
}
else
{
for (int q = 0; q < 3; q++)
if (map[q][i - 1] == 0)
maps[q][i - 1] = 3;
}
}
//
if (j == 3)
{
if (i == 0)
{
for (int p = 0; p < 3; p++)
if (map[p][p] == 0)
{
maps[p][p] = 3; //1zuigao
}
}
else
{
for (int q = 0; q < 3; q++)
if (map[i - 1][q] == 0)
maps[i - 1][q] = 3; //
;
}
}
}
}
}
//
for (int i = 0; i < 4; i++)
{
if (i == 0)
{
j = map[0][0] + map[1][1] + map[2][2];
k = map[2][0] + map[1][1] + map[0][2];
}
else
{
j = map[i - 1][0] + map[i - 1][1] + map[i - 1][2];
k = map[0][i - 1] + map[1][i - 1] + map[2][i - 1]; //hang
}
//
if (k == 12 || j == 12)
{
win = 2; break;
}
if (k == 3 || j == 3)
{
win = 1; break;
}
}
//最高级判断
if (k == 12 || j == 12)
{
win = 2; break;
}
if (k == 3 || j == 3)
{
win = 1; break;
}
if (time == 9 && win == 0) {
settextcolor(YELLOW);// 设置文字颜色为黄色
setbkmode(TRANSPARENT);// 设置文字背景为透明色
outtextxy(100, 125, s[0]); break;
}
}
//结束
settextcolor(YELLOW);// 设置文字颜色为黄色
setbkmode(TRANSPARENT);// 设置文字背景为透明色
outtextxy(100, 125, s[win]);
// 退出
outtextxy(100, 170, s[3]);
u = _getch();
if (u == 'q' || u == 'Q') closegraph();
}