#include <iostream>
#include <algorithm>
#include <time.h>
#include<conio.h>
#include <windows.h>
#define kd(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0)
using namespace std;
int qp[21][21], pd[21][21];
int xx, yy;
//1_5人//2_4机
struct jl {
int x;
int y;
};
jl qp1[360];
int winor;
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
}
int kz = 0;
void stop() {
while (1) {
SetPos(8, 22, 3), cout << "游戏已暂停—— ";
SetPos(9, 22, 2), cout << "按空格键继续游戏";
for (int i = 1; i <= 1000000; i++) {
if (kd(VK_SPACE))
goto qpi;
}
SetPos(8, 22, 2), cout << "游戏已暂停—— ";
SetPos(9, 22, 3), cout << "按空格键继续游戏";
for (int i = 1; i <= 1000000; i++) {
if (kd(VK_SPACE))
goto qpi;
}
}
qpi:
SetPos(8, 22, 2), cout << "游戏进行中—— ";
SetPos(9, 22, 2), cout << " ";
}
void getin() {
int xxx, yyy;
xx = 0;
yy = 0;
while (1) {
POINT p;
HWND h = GetForegroundWindow();
GetCursorPos(&p);
ScreenToClient(h, &p);
int py = p.y / 16 + 1;
int px = p.x / 16 + 1;
SetPos(6, 23, 2), cout << " ";
SetPos(6, 26, 2), cout << " ";
SetPos(6, 23, 2), cout << py;
SetPos(6, 26, 2), cout << px;
//菜单
swap(px, py);
int zt;
if (px == 15 && py >= 23 && py <= 26)
SetPos(15, 23, 3), cout << " 悔棋 ", SetPos(16, 23, 2), cout << " 暂停 ", zt = 1;
else if (px == 16 && py >= 23 && py <= 26)
SetPos(15, 23, 2), cout << " 悔棋 ", SetPos(16, 23, 3), cout << " 暂停 ", zt = 2;
else
SetPos(15, 23, 2), cout << " 悔棋 ", SetPos(16, 23, 2), cout << " 暂停 ", zt = 0;
//
swap(px, py);
if (qp[py][px] == 0 && (xx != py || yy != px) && py >= 1 && py <= 19 && px >= 1 && px <= 19) {
yyy = yy, xxx = xx, xx = py, yy = px;
SetPos(xx, yy, 6), cout << "■";
if (xxx != 0) SetPos(xxx, yyy, 1), cout << "■";
}
for (int i = 1; i <= 1000000; i++) {
if (qp[py][px] == 0 && kd(VK_LBUTTON) && py >= 1 && py <= 19 && px >= 1 && px <= 19) return;
if (kd(VK_LBUTTON) && zt == 1 && winor >= 2) {
SetPos(qp1[winor].x, qp1[winor].y, 1), cout << "■";
qp[qp1[winor].x][qp1[winor].y] = 0;
winor--;
SetPos(qp1[winor].x, qp1[winor].y, 1), cout << "■";
qp[qp1[winor].x][qp1[winor].y] = 0;
winor--;
Sleep(200);
}
if (kd(VK_LBUTTON) && zt == 2)
stop();
}
Sleep(10);
}
return;
}
bool start() {
system("title 五子棋");
system("mode con cols=60 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 = 1; i <= 19; i++)
for (int j = 1; j <= 19; j++)
qp[i][j] = 0, pd[i][j] = 0;
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, 11, 1);
cout << "五子棋";
SetPos(8, 6, 7);
cout << "警告!: 字体用8*16,否则有Bug";
SetPos(12, 11, 2);
cout << "我先";
SetPos(13, 11, 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 == 11 || py == 12)) {
SetPos(12, 11, 3);
cout << "我先";
SetPos(13, 11, 2);
cout << "我后";
asd = 1;
} else if (px == 13 && (py == 11 || py == 12)) {
SetPos(12, 11, 2);
cout << "我先";
SetPos(13, 11, 3);
cout << "我后";
asd = 0;
} else {
SetPos(12, 11, 2);
cout << "我先";
SetPos(13, 11, 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);
}
SetPos(3, 24, 3), cout << "五子棋";
SetPos(5, 22, 2), cout << "当前鼠标位子:" ;
SetPos(6, 22, 2), cout << "( , )";
SetPos(8, 22, 2), cout << "游戏进行中——";
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;
}
}
// 0 s1 l1 s2 l2 s3 l3 s4 l4
int a2[15] = {0, 0, 1, 5, 10, 500, 1001, 1105, 13000, 5000000, 5000000, 5000000, 5000000, 5000000, 5000000};
int a1[15] = {0, 0, 1, 2, 10, 500, 1000, 1100, 10000, 4000000, 4000000, 4000000, 4000000, 4000000, 4000000};
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 -= 200;
suk++;
su1su++;
do i += xxx, j += yyy, suk++;
while (qp[i][j] == 1 && cop(i, j) );
suk--;
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 -= 200;
suk++;
su1su++;
do i += xxx, j += yyy, suk++;
while (qp[i][j] == 1 && cop(i, j) );
suk--;
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;
su1su++;
suk++;
if (suk < 5) fw[ii].su1 += 0;
else {
if (su1su < 5)
fw[ii].su1 += a1[su1su * 2 - lt1 - lt2] + fs;
else
fw[ii].su1 += a1[10] + fs * 2000;
if (su1su * 2 - (lt1 + lt2) / 2 == 6 || su1su * 2 - (lt1 + lt2) / 2 == 7)
fw[ii].fx1 = 3 * xxx + yyy;
}
//机
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 -= 200;
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 -= 200;
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;
su1su++;
suk++;
if (suk < 5) fw[ii].su2 += 0;
else {
if (su1su < 5)
fw[ii].su2 += a2[su1su * 2 - lt1 - lt2] + fs;
else
fw[ii].su2 += a2[10] + fs * 2000;
if (su1su * 2 - (lt1 + lt2) / 2 == 6 || su1su * 2 - (lt1 + lt2) / 2 == 7)
fw[ii].fx2 = 3 * xxx + yyy;
}
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;
}
void search() {
found();
search_1();
sort(fw + 1, fw + n + 1, asd2);
if (fw[1].sumax >= 2000) {
xx = fw[1].x, yy = fw[1].y;
return;
}
sort(fw + 1, fw + n + 1, asd);
if (fw[1].susu > 990) {
xx = fw[1].x;
yy = fw[1].y;
return;
}
sort(fw + 1, fw + n + 1, asd2);
if (fw[1].sumax >= 400) {
xx = fw[1].x, yy = fw[1].y;
return;
}
int arf = rand() % 3 + 1;
xx = fw[arf].x;
yy = fw[arf].y;
return;
}
bool pdpd(int ii, int jj, int wh) {
int i = xx;
int j = yy;
int nnnn = 1;
while (qp[i][j] == wh) i += ii, j += jj, nnnn++;
nnnn--;
i = xx;
j = yy;
while (qp[i][j] == wh) i -= ii, j -= jj, nnnn++;
nnnn--;
if (nnnn >= 5) return 1;
return 0;
}
bool winornot(int i) {
if (pdpd(1, 1, i) || pdpd(1, 0, i) || pdpd(0, 1, i) || pdpd(1, -1, i)) {
winor = -i;
return 1;
}
return 0;
}
void end() {
Sleep(100);
SetPos(3, 8, 7);
cout << "GAME OVER!";
SetPos(5, 8, 7);
if (winor == -1) cout << "你赢了 !";
else if (winor == -2) cout << "你输了 !";
else cout << "游戏平局 !";
SetPos(7, 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 (winor <= 361) {
getin();
qp1[++winor].x = xx, qp1[winor].y = yy;
printout(1);
if (winornot(1) == 1) goto o;
search();
printout(2);
qp1[++winor].x = xx, qp1[winor].y = yy;
if (winornot(2) == 1) goto o;
}
o:
end();//
goto qq;
return 0;
}
五子棋(完成)
于 2024-08-07 09:05:03 首次发布