仍然是暴力代码,不含估价函数等高难算法,运行环境:vs2015
#include
#include
#include
#include
#include
#include
char c[20][20];
int X1=0, Y1=0, X2=0, Y2=0;
MOUSEMSG mouse;
int num=0;//棋子个数 人走是奇数,电脑是偶数
using namespace std;
//绘制棋盘
void draw_chessboard() {
initgraph(1024, 1000);
setbkcolor(0x005498);//设置背景颜色
cleardevice();//清空画布
setlinecolor(BLACK);
for (int i = 50; i <= 500; i += 30)
line(50, i, 500, i);
for (int i = 50; i <= 500; i += 30)
line(i, 50, i, 500);
outtextxy(50, 560, "五子棋AI");
outtextxy(50, 580, "人先手,电脑后手");
}
//初始化系统
void init()//初始化系统
{
memset(c, 'o', sizeof(c));
//for (int i = 0; i <= 20; i++)
//c[0][i] = c[i][0] = 'z';
draw_chessboard();
}
//人走的位置,以及是否合法
bool people(int x, int y)//人走的位置,以及是否合法
{
if (c[x][y] == 'o') {
c[x][y] = 'w';
//print();
return true;
}
else {
//printf("该点处有子存在,请重新输入\n");
if (c[x][y] == 'b') { outtextxy(50, 620, "hei"); }
return false;
}
}
//人是否赢了
bool check(int x, int y) {//人是否赢了
int flag = -1;
for (int i = y - 4; i <= y; ++i) {//检查5个子连在一起的情况
if (c[x][i] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = y - 3; i <= y + 1; ++i) {//检查5个子连在一起的情况
if (c[x][i] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = y - 2; i <= y + 2; ++i) {//检查5个子连在一起的情况
if (c[x][i] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = y - 1; i <= y + 3; ++i) {//检查5个子连在一起的情况
if (c[x][i] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = y; i <= y + 4; ++i) {//检查5个子连在一起的情况
if (c[x][i] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 4; i <= x; ++i) {//检查5个子连在一起的情况
if (c[i][y] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 3; i <= x + 1; ++i) {//检查5个子连在一起的情况
if (c[i][y] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 2; i <= x + 2; ++i) {//检查5个子连在一起的情况
if (c[i][y] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 1; i <= x + 3; ++i) {//检查5个子连在一起的情况
if (c[i][y] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x; i <= x + 4; ++i) {//检查5个子连在一起的情况
if (c[i][y] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 4, j = y - 4; i <= x, j <= y; ++i, ++j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 3, j = y - 3; i <= x + 1, j <= y + 1; ++i, ++j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 2, j = y - 2; i <= x + 2, j <= y + 2; ++i, ++j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 1, j = y - 1; i <= x + 3, j <= y + 3; ++i, ++j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x, j = y; i <= x + 4, j <= y + 4; ++i, ++j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 2, j = y + 2; i <= x + 2, j >= y - 2; ++i, --j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 4, j = y + 4; i <= x, j >= y; ++i, --j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 3, j = y + 3; i <= x + 1, j >= y - 1; ++i, --j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 2, j = y + 2; i <= x + 2, j >= y - 2; ++i, --j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 1, j = y + 1; i <= x + 3, j >= y - 3; ++i, --j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x, j = y; i <= x + 4, j >= y - 4; ++i, --j) {
if (c[i][j] == 'w')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = y - 1; i <= y + 2; ++i) {//4个子的情况
if (c[x][i] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x; Y1 = y - 2; X2 = x; Y2 = y + 3;
return false;
}
for (int i = y; i <= y + 3; ++i) {
if (c[x][i] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x; Y1 = y - 1; X2 = x; Y2 = y + 4;
return false;
}
for (int i = y - 2; i <= y + 1; ++i) {
if (c[x][i] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x; Y1 = y - 3; X2 = x; Y2 = y + 2;
return false;
}
for (int i = y - 3; i <= y; ++i) {
if (c[x][i] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x; Y1 = y - 4; X2 = x; Y2 = y + 1;
return false;
}
for (int i = x - 3; i <= x; ++i) {
if (c[i][y] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 4; Y1 = y; X2 = x + 1; Y2 = y;
return false;
}
for (int i = x - 2; i <= x + 1; ++i) {
if (c[i][y] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 3; Y1 = y; X2 = x + 2; Y2 = y;
return false;
}
for (int i = x - 1; i <= x + 2; ++i) {
if (c[i][y] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 2; Y1 = y; X2 = x + 3; Y2 = y;
return false;
}
for (int i = x; i <= x + 3; ++i) {
if (c[i][y] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 1; Y1 = y; X2 = x + 4; Y2 = y;
return false;
}
for (int i = x - 3, j = y - 3; i <= x, j <= y; i++, j++) {
if (c[i][y] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 4; Y1 = y - 4; X2 = x + 1; Y2 = y + 1;
return false;
}
for (int i = x - 2, j = y - 2; i <= x + 1, j <= y + 1; i++, j++) {
if (c[i][y] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 3; Y1 = y - 3; X2 = x + 2; Y2 = y + 2;
return false;
}
for (int i = x - 1, j = y - 1; i <= x + 2, j <= y + 2; i++, j++) {
if (c[i][y] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 2; Y1 = y - 2; X2 = x + 3; Y2 = y + 3;
return false;
}
for (int i = x, j = y; i <= x + 3, j <= y + 3; i++, j++) {
if (c[i][y] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 1; Y1 = y - 1; X2 = x + 4; Y2 = y + 4;
return false;
}
for (int i = x - 3, j = y + 3; i <= x, j >= y; i++, j--) {
if (c[i][j] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 4; Y1 = y + 4; X2 = x + 1; Y2 = y - 1;
return false;
}
for (int i = x - 2, j = y + 2; i <= x + 1, j >= y - 1; i++, j--) {
if (c[i][j] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 3; Y1 = y + 3; X2 = x + 2; Y2 = y - 2;
return false;
}
for (int i = x - 1, j = y + 1; i <= x + 2, j >= y - 2; i++, j--) {
if (c[i][j] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 2; Y1 = y + 2; X2 = x + 3; Y2 = y - 3;
return false;
}
for (int i = x, j = y; i <= x + 3, j >= y - 3; i++, j--) {
if (c[i][j] == 'w')flag = 4;
else {
flag = -1; break;
}
}
if (flag == 4) {
X1 = x - 1; Y1 = y + 1; X2 = x + 4; Y2 = y - 4;
return false;
}
if (c[x][y] == 'w'&&c[x][y + 1] == 'w'&&c[x][y + 3] == 'w') {//** *的情况
flag = 3;
X1 = x; Y1 = y - 1; X2 = x; Y2 = y + 2;
return false;
}
if (c[x][y] == 'w'&&c[x][y - 1] == 'w'&&c[x][y + 2] == 'w') {
flag = 3;
X1 = x; Y1 = y - 2; X2 = x; Y2 = y + 1;
return false;
}
if (c[x][y] == 'w'&&c[x][y - 2] == 'w'&&c[x][y - 3] == 'w') {
flag = 3;
X1 = x; Y1 = y - 1; X2 = x; Y2 = y + 1;
return false;
}
if (c[x][y] == 'w'&&c[x][y + 2] == 'w'&&c[x][y + 3] == 'w') {//* **的情况
flag = 3;
X1 = x; Y1 = y + 1; X2 = x; Y2 = y - 1;
return false;
}
if (c[x][y] == 'w'&&c[x][y - 2] == 'w'&&c[x][y + 1] == 'w') {//* **的情况
flag = 3;
X1 = x; Y1 = y - 1; X2 = x; Y2 = y + 2;
return false;
}
if (c[x][y] == 'w'&&c[x][y - 1] == 'w'&&c[x][y - 3] == 'w') {//* **的情况
flag = 3;
X1 = x; Y1 = y - 2; X2 = x; Y2 = y + 1;
return false;
}
if (c[x][y] == 'w'&&c[x + 1][y + 1] == 'w'&&c[x + 3][y + 3] == 'w') {//*
flag = 3; //*
X1 = x + 2; Y1 = y + 2; X2 = x - 1; Y2 = y - 1;
return false; //*的情况
}
if (c[x][y] == 'w'&&c[x - 1][y - 1] == 'w'&&c[x + 2][y + 2] == 'w') {
flag = 3;
X1 = x + 1; Y1 = y + 1; X2 = x + 3; Y2 = y + 3;
return false;
}
if (c[x][y] == 'w'&&c[x - 2][y + 2] == 'w'&&c[x - 3][y + 3] == 'w') {
flag = 3;
X1 = x - 1; Y1 = y + 1; X2 = x + 1; Y2 = y - 1;
return false;
}
if (c[x][y] == 'w'&&c[x - 1][y + 1] == 'w'&&c[x + 2][y - 2] == 'w') {
flag = 3;
X1 = x + 1; Y1 = y - 1; X2 = x + 3; Y2 = y - 3;
return false;
}
if (c[x][y] == 'w'&&c[x - 2][y + 2] == 'w'&&c[x - 3][y + 3] == 'w') {//*
flag = 3; //*
X1 = x - 1; Y1 = y + 1; X2 = x + 1; Y2 = y - 1;
return false; //*
}
if (c[x][y] == 'w'&&c[x][y - 1] == 'w'&&c[x][y - 2] == 'w') {//3个子的情况
flag = 3;
X1 = x; Y1 = y - 3; X2 = x; Y2 = y + 1;
return false;
}
if (c[x][y + 1] == 'w'&&c[x][y] == 'w'&&c[x][y - 1] == 'w') {
flag = 3;
X1 = x; Y1 = y - 2; X2 = x; Y2 = y + 2;
return false;
}
if (c[x][y + 2] == 'w'&&c[x][y + 1] == 'w'&&c[x][y] == 'w') {
flag = 3;
X1 = x; Y1 = y - 1; X2 = x; Y2 = y + 3;
return false;
}
if (c[x - 2][y - 2] == 'w'&&c[x - 1][y - 1] == 'w'&&c[x][y] == 'w') {
flag = 3;
X1 = x - 3; Y1 = y - 3; X2 = x + 1; Y2 = y + 1;
return false;
}
if (c[x - 1][y - 1] == 'w'&&c[x][y] == 'w'&&c[x + 1][y + 1] == 'w') {
flag = 3;
X1 = x - 2; Y1 = y - 2; X2 = x + 2; Y2 = y + 2;
return false;
}
if (c[x][y] == 'w'&&c[x + 1][y + 1] == 'w'&&c[x + 2][y + 2] == 'w') {
flag = 3;
X1 = x - 1; Y1 = y - 1; X2 = x + 3; Y2 = y + 3;
return false;
}
if (c[x - 2][y + 2] == 'w'&&c[x - 1][y + 1] == 'w'&&c[x][y] == 'w') {
flag = 3;
X1 = x - 3; Y1 = y + 3; X2 = x + 1; Y2 = y - 1;
return false;
}
if (c[x - 1][y + 1] == 'w'&&c[x][y] == 'w'&&c[x + 1][y - 1] == 'w') {
flag = 3;
X1 = x - 2; Y1 = y + 2; X2 = x + 2; Y2 = y - 2;
return false;
}
if (c[x][y] == 'w'&&c[x + 1][y - 1] == 'w'&&c[x + 2][y - 2] == 'w') {
flag = 3;
X1 = x - 1; Y1 = y + 1; X2 = x + 3; Y2 = y - 3;
return false;
}
if (flag == -1) {
if (c[x][y] == 'w'&&c[x - 1][y] == 'w') {//两个子的情况
flag = 2;
X1 = x - 2; Y1 = y; X2 = x + 1; Y2 = y;
return false;
}
if (c[x][y] == 'w'&&c[x + 1][y] == 'w') {
flag = 2;
X1 = x - 1; Y1 = y; X2 = x + 2; Y2 = y;
return false;
}
if (c[x][y] == 'w'&&c[x + 1][y + 1] == 'w') {
flag = 2;
X1 = x - 1; Y1 = y - 1; X2 = x + 2; Y2 = y + 2;
return false;
}
if (c[x][y] == 'w'&&c[x - 1][y - 1] == 'w') {
flag = 2;
X1 = x - 2; Y1 = y - 2; X2 = x + 1; Y2 = y + 1;
return false;
}
if (c[x][y] == 'w'&&c[x][y - 1] == 'w') {
flag = 2;
X1 = x; Y1 = y; X2 = x; Y2 = y - 2;
return false;
}
if (c[x][y] == 'w'&&c[x][y + 1] == 'w') {
flag = 2;
X1 = x; Y1 = y; X2 = x; Y2 = y + 2;
return false;
}
for (int i = x - 1; i <= x + 1; i++) {//一个子的情况
for (int j = y - 1; j <= y + 1; j++) {
if (c[i][j] == 'o') {
X1 = i; Y1 = j; X2 = x; Y2 = y;
flag = 1;
return false;
}
}
}
}
}
//绘制白棋
void white() {
setfillcolor(WHITE);
fillcircle(mouse.x,mouse.y, 10);
}
//电脑是否赢了
bool check2(int x, int y) {//电脑是否赢了
int flag = -1;
for (int i = y - 4; i <= y; ++i) {//检查5个子连在一起的情况
if (c[x][i] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = y - 3; i <= y + 1; ++i) {//检查5个子连在一起的情况
if (c[x][i] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = y - 2; i <= y + 2; ++i) {//检查5个子连在一起的情况
if (c[x][i] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = y - 1; i <= y + 3; ++i) {//检查5个子连在一起的情况
if (c[x][i] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = y; i <= y + 4; ++i) {//检查5个子连在一起的情况
if (c[x][i] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 4; i <= x; ++i) {//检查5个子连在一起的情况
if (c[i][y] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 3; i <= x + 1; ++i) {//检查5个子连在一起的情况
if (c[i][y] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 2; i <= x + 2; ++i) {//检查5个子连在一起的情况
if (c[i][y] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 1; i <= x + 3; ++i) {//检查5个子连在一起的情况
if (c[i][y] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x; i <= x + 4; ++i) {//检查5个子连在一起的情况
if (c[i][y] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 4, j = y - 4; i <= x, j <= y; ++i, ++j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 3, j = y - 3; i <= x + 1, j <= y + 1; ++i, ++j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 2, j = y - 2; i <= x + 2, j <= y + 2; ++i, ++j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 1, j = y - 1; i <= x + 3, j <= y + 3; ++i, ++j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x, j = y; i <= x + 4, j <= y + 4; ++i, ++j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 2, j = y + 2; i <= x + 2, j >= y - 2; ++i, --j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 4, j = y + 4; i <= x, j >= y; ++i, --j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 3, j = y + 3; i <= x + 1, j >= y - 1; ++i, --j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 2, j = y + 2; i <= x + 2, j >= y - 2; ++i, --j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x - 1, j = y + 1; i <= x + 3, j >= y - 3; ++i, --j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
for (int i = x, j = y; i <= x + 4, j >= y - 4; ++i, --j) {
if (c[i][j] == 'b')flag = 5;
else {
flag = -1; break;
}
}
if (flag == 5)return true;
else return false;
}
//电脑走的位置,以及电脑是否赢了
int computer(int x1, int y1, int x2, int y2) {
int ret = 0;
/*char p1[5], p2[5];
sprintf(p1, "%d", x1);
outtextxy(100, 620, p1);
sprintf(p2, "%d", y1);
outtextxy(120, 620, p2);
//char p1[5], p2[5];
sprintf(p1, "%d", x2);
outtextxy(140, 620, p1);
sprintf(p2, "%d", y2);
outtextxy(160, 620, p2);*/
if (c[x1][y1] == 'o') {
c[x1][y1] = 'b';
ret = check2(x1, y1)+1;//check2赢了返回1输了返回0
return ret;
}
else if (c[x2][y2] == 'o') {
c[x2][y2] = 'b';
ret = check2(x2, y2)+3;
return ret;
}
}
//绘制黑棋
void black(int x,int y) {
x = (x - 1) * 30 + 80;
y = (y - 1) * 30 + 80;
setfillcolor(BLACK);
fillcircle(x, y, 10);
}
int main()
{
bool flag = 0;
init();
FlushMouseMsgBuffer();
while (1) {
mouse = GetMouseMsg();
int len = 10000;
struct Node {
int x;
int y;
}T;
if(mouse.uMsg == WM_LBUTTONDOWN) {//是否按鼠标左键
for (int i = 1; i <= 15; i++) {
int temx = (i - 1) * 30 + 80;
for (int j = 1; j <= 15; j++) {
int temy = (j - 1) * 30 + 80;
int ojld = sqrt((temx - mouse.x)*(temx - mouse.x) + (temy - mouse.y) * (temy - mouse.y));
if (len > ojld) {
len = ojld;
T.x = i;
T.y = j;
}
}
}
X1 = T.x;
Y1 = T.y;
if (people(X1, Y1) == false) {/* outtextxy(50, 620, "该点处有棋子");*/ continue; }
white();
if (check(X1, Y1) == true) { flag = 1; break; }
else {
int tem = computer(X1, Y1, X2, Y2);
if (tem == 2 || tem == 4) { flag = 0; break; }
else if (tem == 1)black(X1,Y1);
else if(tem==3)black(X2, Y2);
}
}
FlushMouseMsgBuffer();//清空鼠标信息缓存
X1 = 0; Y1 = 0; X2 = 0; Y2 = 0;
}
if (flag == 1)
outtextxy(50, 600, "人赢");
else
outtextxy(50, 600, "电脑赢");
system("pause");
closegraph();
return 0;
}