评分制,基于一步预测,未完成,待完善。
#include<iostream>
#include<Windows.h>
#define bound 15
using namespace std;
unsigned short position[bound + 1][bound + 1];
//unsigned short bound = 0;
bool model = 0;
bool isover = false;
short direction_x[8] = { 0,0,-1,1,-1,1,-1,1 };
short direction_y[8] = { -1,1,0,0,-1,1,1,-1 };
void gotoxy(short, short);
//void UI_welcome();
void UI(bool);
bool inrange(short, short);
bool isvictory(short, short, bool);
short calculate(short, short, bool);
void gotoxy(short x, short y)
{
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coordScreen = { y,x };
SetConsoleCursorPosition(hConsole, coordScreen);
return;
}
void UI(bool returntoblack)
{
gotoxy(6, 70);
cout << " ";
gotoxy(7, 70);
cout << " ";
gotoxy(5, 70);
if (returntoblack)
{
cout << "轮到黑色方";
gotoxy(6, 70);
cout << "请输入你的坐标:";
}
else {
cout << "轮到白色方";
gotoxy(6, 70);
cout << "白色方的坐标为:";
}
gotoxy(7, 70);
}
void initialization()
{
for (unsigned short i = 0; i < bound; i++) { cout << " --"; }
cout << endl;
for (unsigned short i = 0; i < bound; i++)
{
for (unsigned short j = 0; j <= bound; j++) { cout << "| "; }
cout << " " << i + 1 << endl;
for (unsigned short j = 0; j < bound; j++) { cout << " --"; }
cout << endl;
}
cout << " ";
for (int i = 1; i <= bound; i++)
{
cout << i % 10 << " ";
}
for (int i = 1; i <= bound; i++)
{
for (int j = 1; j <= bound; j++)
{
position[i][j] = 0;
}
}
isover = false;
}
bool inrange(short a, short b)
{
return(a >= 1 && a <= bound && b >= 1 && b <= bound);
}
bool isvictory(short a, short b, bool isblack)
{
short count = 0, c = 0;
c = (isblack == true ? 1 : -1);
for (int i = 0; i < 4; i++)
{
for (int j = 1; j <= 4; j++)
{
if (inrange(a + direction_x[2 * i]* j, b + direction_y[2 * i] * j) && position[a + direction_x[2 * i] * j][b + direction_y[2 * i] * j] == c)
count++;
else break;
}
for (int j = 1; j <= 4; j++)
{
if (inrange(a + direction_x[2 * i + 1] * j, b + direction_y[2 * i + 1] * j) && position[a + direction_x[2 * i + 1] * j][b + direction_y[2 * i + 1] * j] == c)
count++;
else break;
}
if (count >= 4)return true;
else count = 0;
}
return false;
}
short calculate(short a, short b, bool isblack)
{
short count1 = 0, count2 = 0, blank1 = 0, blank2 = 0, score = 0;
short c = (isblack == true ? 1 : -1);
bool bound1 = false, bound2 = false;
for (int i = 0; i < 4; i++)
{
if (inrange(a + direction_x[2 * i], b + direction_y[2 * i]) && position[a + direction_x[2 * i]][b + direction_y[2 * i]] == c)
blank1 = 1;
if (inrange(a + direction_x[2 * i + 1], b + direction_y[2 * i + 1]) && position[a + direction_x[2 * i + 1]][b + direction_y[2 * i + 1]] == c)
blank2 = 1;
if (blank1 + blank2 == 2)
{
for (int j = 1; j <= 4; j++)
{
if (inrange(a + direction_x[2 * i] * j, b + direction_y[2 * i] * j))
{
if (position[a + direction_x[2 * i] * j][b + direction_y[2 * i] * j] == c)
count1++;
else if (position[a + direction_x[2 * i] * j][b + direction_y[2 * i] * j] == -c)
{
bound1 = true;
break;
}
else break;
}
else
{
bound1 = true;
break;
}
}
for (int j = 1; j <= 4; j++)
{
if (inrange(a + direction_x[2 * i + 1] * j, b + direction_y[2 * i + 1] * j))
{
if (position[a + direction_x[2 * i + 1] * j][b + direction_y[2 * i + 1] * j] == c)
count1++;
else if (position[a + direction_x[2 * i + 1] * j][b + direction_y[2 * i + 1] * j] == -c)
{
bound2 = true;
break;
}
else break;
}
else
{
bound2 = true;
break;
}
}
if (bound1 && bound2)
{
if (count1 >= 4)return 1000;
}
else if (bound1 || bound2)
{
if (count1 >= 4)return 1000;
else if (count1 == 3)score += 250;
else score += 60;
}
else
{
if (count1 >= 3) return 1000;
else score += 260;
}
}
else if (blank1 == 1 && blank2 == 0)
{
for (int j = 1; j <= 4; j++)
{
if (inrange(a + direction_x[2 * i] * j, b + direction_y[2 * i] * j))
{
if (position[a + direction_x[2 * i] * j][b + direction_y[2 * i] * j] == c)
count1++;
else if (position[a + direction_x[2 * i] * j][b + direction_y[2 * i] * j] == -c)
{
bound1 = true;
break;
}
else break;
}
else
{
bound1 = true;
break;
}
}
for (int j = 2; j <= 4; j++)
{
if (inrange(a + direction_x[2 * i + 1] * j, b + direction_y[2 * i + 1] * j))
{
if (position[a + direction_x[2 * i + 1] * j][b + direction_y[2 * i + 1] * j] == c)
count2++;
else if (position[a + direction_x[2 * i + 1] * j][b + direction_y[2 * i + 1] * j] == -c)
{
bound2 = true;
break;
}
else break;
}
else
{
bound2 = true;
break;
}
}
if (bound1 && bound2)
{
if (count1 + count2 >= 3)
score += 80;
}
else if (bound1 || bound2)
{
if (count1 == 4)return 1000;
else if (count1 == 3)
{
if (bound1)score += 80;
else score += 260;
}
else if (count1 == 2)
{
if (bound1)score += 40;
else score += 220;
}
else
{
if (bound1)score += 20;
else score += 30;
}
}
else
{
if (count1 >= 3)score += 280;
else if (count1 == 2)
{
if (count2 >=1)score += 280;
else score += 260;
}
else
{
if (count2 >= 1)score += 40;
else score += 30;
}
}
}
else if (blank1 == 0 && blank2 == 1)
{
for (int j = 2; j <= 4; j++)
{
if (inrange(a + direction_x[2 * i] * j, b + direction_y[2 * i] * j))
{
if (position[a + direction_x[2 * i] * j][b + direction_y[2 * i] * j] == c)
count1++;
else if (position[a + direction_x[2 * i] * j][b + direction_y[2 * i] * j] == -c)
{
bound1 = true;
break;
}
else break;
}
else
{
bound1 = true;
break;
}
}
for (int j = 1; j <= 4; j++)
{
if (inrange(a + direction_x[2 * i + 1] * j, b + direction_y[2 * i + 1] * j))
{
if (position[a + direction_x[2 * i + 1] * j][b + direction_y[2 * i + 1] * j] == c)
count2++;
else if (position[a + direction_x[2 * i + 1] * j][b + direction_y[2 * i + 1] * j] == -c)
{
bound2 = true;
break;
}
else break;
}
else
{
bound2 = true;
break;
}
}
if (bound1 && bound2)
{
if (count1 + count2 >= 3)
score += 80;
}
else if (bound1 || bound2)
{
if (count2 == 4)return 1000;
else if (count2 == 3)
{
if (bound2)score += 80;
else score += 260;
}
else if (count2 == 2)
{
if (bound2)score += 40;
else score += 220;
}
else
{
if (bound2)score += 20;
else score += 30;
}
}
else
{
if (count2 >= 3)return 1000;
else if (count2 == 2)
{
if (count1 >= 1)score += 280;
else score += 260;
}
else
{
if (count1 >= 1)score += 40;
else score += 30;
}
}
}
count1 = 0, count2 = 0;
blank1 = 0, blank2 = 0;
bound1 = false, bound2 = false;
}
return score;
}
void single_mode(int hard)
{
bool scoremap[bound + 1][bound + 1];
for (int i = 1; i <= bound; i++)
{
for (int j = 1; j <= bound; j++)
{
scoremap[i][j] = false;
}
}
bool returntoblack = true;
short a = 0, b = 0, scoretemp = 0;
while (!isover)
{
if (returntoblack)
{
UI(true);
cin >> a >> b;
if (inrange(a, b) && position[a][b] == 0)
{
gotoxy(2 * a - 1, 3 * b - 2);
cout << "*";
position[a][b] = 1;
isover = isvictory(a, b, returntoblack);
returntoblack = !returntoblack;
}
else
{
gotoxy(8, 70);
cout << "Warning: 该位置已有棋子或落在棋盘外";
Sleep(1200);
gotoxy(8, 70);
cout << " ";
}
}
else
{
for (int i = 1; i <= bound; i++)
{
for (int j = 1; j <= bound; j++)
{
if (position[i][j] == 0)
{
for (int k = 0; k < 8; k++)
{
if (inrange(i + direction_x[k],j + direction_y[k]) && position[i + direction_x[k]][j + direction_y[k]] != 0)
{
scoremap[i][j] = true;
break;
}
}
}
else scoremap[i][j] = false;
}
}
short a = 0, b = 0, score = 0;
for (int i = 1; i <= bound; i++)
{
for (int j = 1; j <= bound; j++)
{
if (scoremap[i][j])
{
scoretemp = calculate(i, j, true) * 4+calculate(i, j, false) * 6;
if (scoretemp >= score)
{
a = i; b = j; score = scoretemp;
}
}
}
}
gotoxy(2 * a - 1, 3 * b - 2);
cout << "@";
gotoxy(9, 70);
cout << " ";
gotoxy(9, 70);
cout << "(" << a << "," << b << ") " << score;
position[a][b] = -1;
isover = isvictory(a, b, returntoblack);
returntoblack = !returntoblack;
}
}
gotoxy(8, 70);
if (returntoblack)cout << "White Wins!";
else cout << "Black Wins!";
isover = false;
returntoblack = true;
}
void double_mode()
{
}
int main()
{
initialization();
single_mode(1);
return 0;
}