#include<bits/stdc++.h>
#include<windows.h>
#define kd(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
#include<conio.h>
using namespace std;
int qp[21][21], pd[21][21];
int xx, yy;
//1_5人//2_4机
void SetPos(int x, int y, int a) {
COORD pos;
pos.X = y * 2 - 2;
pos.Y = x - 1;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
if (a == 1)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 6); //开始页面
if (a == 2)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 3); //开始页面
if (a == 3)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 243); //开始——鼠标高亮
if (a == 4)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 96); //机
if (a == 5)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 100); //人
if (a == 6)SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 238); //对局——鼠标高亮
if (a == 7) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 78 ); //end
}
void getin() {
int xxx, yyy;
xx = 0;
yy = 0;
while (1) {
POINT p;
HWND h = GetForegroundWindow();
GetCursorPos(&p);
ScreenToClient(h, &p);
if (qp[p.y / 16 + 1][p.x / 16 + 1] == 0 && (xx != p.y / 16 + 1 || yy != p.x / 16 + 1) && p.y / 16 + 1 >= 1 && p.y / 16 + 1 <= 19 && p.x / 16 + 1 >= 1 && p.x / 16 + 1 <= 19) {
yyy = yy, xxx = xx, xx = p.y / 16 + 1, yy = p.x / 16 + 1;
SetPos(xx, yy, 6), cout << "▇";
if (xxx != 0) SetPos(xxx, yyy, 1), cout << "▇";
}
for (int i = 1; i <= 100; i++)
if (qp[p.y / 16 + 1][p.x / 16 + 1] == 0 && kd(VK_LBUTTON) && p.y / 16 + 1 >= 1 && p.y / 16 + 1 <= 19 && p.x / 16 + 1 >= 1 && p.x / 16 + 1 <= 19) return;
Sleep(70);
}
}
bool start() {
system("title 五子棋");
system("mode con cols=40 lines=20");
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode;
GetConsoleMode(hStdin, &mode);
mode &= ~ENABLE_QUICK_EDIT_MODE;
mode &= ~ENABLE_INSERT_MODE;
mode &= ~ENABLE_MOUSE_INPUT;
SetConsoleMode(hStdin, mode);
CONSOLE_CURSOR_INFO cur = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cur);
for (int i = 0; i <= 20; i++) { //边界锁定
qp[0][i] = 3;
qp[20][i] = 3;
qp[i][0] = 3;
qp[i][20] = 3;
}
SetPos(6, 9, 1);
cout << "五子棋";
SetPos(8,4,7);cout<<"警告!:字体用8*16,否则有Bug!";
SetPos(12, 9, 2);
cout << "我先";
SetPos(13, 9, 2);
cout << "我后";
int asd;
while (1) {
POINT p;
HWND h = GetForegroundWindow();
GetCursorPos(&p);
ScreenToClient(h, &p);
int px = p.y / 16 + 1;
int py = p.x / 16 + 1;
if (px == 12 && (py == 9 || py == 10)) {
SetPos(12, 9, 3);
cout << "我先";
SetPos(13, 9, 2);
cout << "我后";
asd = 1;
} else if (px == 13 && (py == 9 || py == 10)) {
SetPos(12, 9, 2);
cout << "我先";
SetPos(13, 9, 3);
cout << "我后";
asd = 0;
} else {
SetPos(12, 9, 2);
cout << "我先";
SetPos(13, 9, 2);
cout << "我后";
asd = 10;
}
for (int i = 1; i <= 1e7; i++)
if (kd(VK_LBUTTON) && asd != 10)
goto qqq;
}
qqq:
for (int i = 1; i <= 19; i++) {
for (int j = 1; j <= 19; j++)
SetPos(i, j, 1), printf("▇");
Sleep(rand() % 10 + 70);
}
return asd;
}
void printout(int i) {
if (i == 1)
SetPos(xx, yy, 5), qp[xx][yy] = 1, cout << "●";
else
{
qp[xx][yy] = 2;
for(int i=1;i<=4;i++)
{
SetPos(xx, yy, i), cout << "●";
Sleep(100);
}
}
pd[xx][yy] = 0;
for (int i = xx - 2; i <= xx + 2; i++)
for (int j = yy - 2; j <= yy + 2; j++) {
if (qp[i][j] == 0 && i >= 1 && i <= 19 && j >= 1 && j <= 19) pd[i][j] = 1;
}
}
bool cop(int i, int j) {
if (i >= 1 && i <= 19 && j >= 1 && j <= 19) return 1;
else return 0;
}
struct xl {
int x;
int y;
int su1, su2, susu, sumax;
int fx1, fx2;
};
xl fw[350];
int n;
void found() {
n = 0;
for (int i = 1; i <= 19; i++)
for (int j = 1; j <= 19; j++)
if (pd[i][j] == 1) {
fw[++n].x = i;
fw[n].y = j;
fw[n].fx1 = 0;
fw[n].fx2 = 0;
fw[n].su1 = 0;
fw[n].su2 = 0;
fw[n].susu = 0;
}
}
int a2[15] = {0, 0, 0, 0, 10, 500, 2000, 2300, 10000, 5000000, 5000000, 5000000, 5000000, 5000000, 5000000};
int a1[15] = {0, 0, 0, 5, 10, 500, 2003, 2350, 11000, 900000, 900000, 900000, 900000, 900000, 900000};
void search_x(int xxx, int yyy, int ii) {
int i = fw[ii].x, j = fw[ii].y;
int suk = 0, fs = 0, su1su = 0;
int lt1 = 0, lt2 = 0;
//人
do i += xxx, j += yyy, suk++, su1su++;
while (qp[i][j] == 1 && cop(i, j));
suk--;
su1su--;
if (qp[i][j] == 0) {
i += xxx;
j += yyy;
suk++;
if (qp[i][j] == 1) {
fs -= 20;
suk++;
su1su++;
do i += xxx, j += yyy, suk++, fs += 20;
while (qp[i][j] == 1 && cop(i, j) );
suk--;
fs -= 20;
if (qp[i][j] == 0)
while (qp[i][j] == 0 && cop(i, j)) i += xxx, j += yyy, suk++;
else
lt1 = 1;
} else if (qp[i][j] == 0)
while (qp[i][j] == 0 && cop(i, j)) i += xxx, j += yyy, suk++;
} else
lt1 = 1;
i = fw[ii].x, j = fw[ii].y;
xxx = -xxx;
yyy = -yyy;
do i += xxx, j += yyy, suk++, su1su++;
while (qp[i][j] == 1 && cop(i, j));
suk--;
su1su--;
if (qp[i][j] == 0) {
i += xxx;
j += yyy;
suk++;
if (qp[i][j] == 1) {
fs -= 20;
suk++;
su1su++;
do i += xxx, j += yyy, suk++, fs += 20;
while (qp[i][j] == 1 && cop(i, j) );
suk--;
fs -= 20;
if (qp[i][j] == 0)
while (qp[i][j] == 0 && cop(i, j)) i += xxx, j += yyy, suk++;
else
lt1 = 1;
} else if (qp[i][j] == 0)
while (qp[i][j] == 0 && cop(i, j)) i += xxx, j += yyy, suk++;
} else
lt1 = 1;
if (suk < 5) fw[ii].su1 += 0;
else
fw[ii].su1 += a1[su1su * 2 - lt1 - lt2] + fs;
i = fw[ii].x, j = fw[ii].y;
suk = 0, fs = 0, su1su = 0;
lt1 = 0, lt2 = 0;
do i += xxx, j += yyy, suk++, su1su++;
while (qp[i][j] == 2 && cop(i, j));
suk--;
su1su--;
if (qp[i][j] == 0) {
i += xxx;
j += yyy;
suk++;
if (qp[i][j] == 2) {
fs -= 20;
suk++;
su1su++;
do i += xxx, j += yyy, suk++, su1su++;
while (qp[i][j] == 2 && cop(i, j) );
suk--;
su1su--;
if (qp[i][j] == 0)
while (qp[i][j] == 0 && cop(i, j)) i += xxx, j += yyy, suk++;
else
lt1 = 1;
} else if (qp[i][j] == 0)
while (qp[i][j] == 0 && cop(i, j)) i += xxx, j += yyy, suk++;
} else
lt1 = 1;
i = fw[ii].x, j = fw[ii].y;
xxx = -xxx;
yyy = -yyy;
do i += xxx, j += yyy, suk++, su1su++;
while (qp[i][j] == 2 && cop(i, j));
suk--;
su1su--;
if (qp[i][j] == 0) {
i += xxx;
j += yyy;
suk++;
if (qp[i][j] == 2) {
fs -= 100;
suk++;
su1su++;
do i += xxx, j += yyy, suk++, su1su++;
while (qp[i][j] == 2 && cop(i, j) );
suk--;
su1su--;
if (qp[i][j] == 0)
while (qp[i][j] == 0 && cop(i, j)) i += xxx, j += yyy, suk++;
else
lt1 = 1;
} else if (qp[i][j] == 0)
while (qp[i][j] == 0 && cop(i, j)) i += xxx, j += yyy, suk++;
} else
lt1 = 1;
if (suk < 5) fw[ii].su2 += 0;
else
fw[ii].su2 += a2[su1su * 2 - (lt1 + lt2) / 2] + fs;
fw[ii].susu = fw[ii].su1 + fw[ii].su2;
fw[ii].sumax = max(fw[ii].su1, fw[ii].su2);
}
void search_1() {
for (int i = 1; i <= n; i++) {
search_x(1, 0, i);
search_x(1, 1, i);
search_x(0, 1, i);
search_x(1, -1, i);
}
return;
}
bool asd2(xl x, xl y) {
return x.sumax > y.sumax;
}
bool asd(xl x, xl y) {
return x.susu > y.susu;
}
int tsqq() {
sort(fw + 1, fw + n + 1, asd2);
xx = fw[1].x;
yy = fw[1].y;
return 1;
}
int search_2() {
return 0;
}
void rand_xy() {
return;
}
void search() {
found();
search_1();
if (tsqq() == 1) return;
if (search_2() == 1) return;
rand_xy();
// xx = fw[1].x;
// yy = fw[1].y;
}
int winor;
bool sl() {
if (n < 8) {
winor = 3;
return 0;
} else
//是否胜利
return 1;
}
void end() {
Sleep(100);
SetPos(6, 8, 7);
cout << "GAME OVER!";
SetPos(8, 8, 7);
if (winor == 1) cout << "你赢了 !";
else if (winor == 2) cout << "你输了 !";
else cout << "游戏平局 !";
SetPos(10, 7, 7);
Sleep(400);
cout << "按回车键继续--";
char a;
while (a != 13)
a = _getch();
SetPos(1, 1, 1);
system("cls");
}
int main() {
qq:
srand(time(0));
if (start() == 0) { //人后手
xx = rand() % 3 + 8;
yy = rand() % 3 + 8;
printout(2);
}
n = 10;
while (sl() != 0) {
getin();//
printout(1);//
search();
printout(2);//
}
end();//
goto qq;
return 0;
}
五子棋(初稿完成)
于 2024-07-30 16:53:33 首次发布