#include <iostream>
#include <cstring>
#include <conio.h>
#include <windows.h>
using namespace std;
const int BOARD_SIZE = 15;
int board[BOARD_SIZE][BOARD_SIZE];
int curX = 7;
int curY = 7;
int player = 1; //1黑● 2白○
bool gameOver = false;
HWND hConsoleWnd;
bool isTopMost = false; //置顶开关状态
bool isMinimized = false; //窗口最小化状态
//控制台定位
void gotoXY(int x, int y)
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
//隐藏光标
void hideCursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.dwSize = 1;
cursor.bVisible = FALSE;
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor);
}
//锁定控制台窗口大小,禁止拖拽拉伸
void lockConsoleWindow()
{
hConsoleWnd = GetConsoleWindow();
//禁用缩放、最大化
SetWindowLong(hConsoleWnd, GWL_STYLE,
GetWindowLong(hConsoleWnd, GWL_STYLE) & ~WS_THICKFRAME & ~WS_MAXIMIZEBOX);
}
//切换窗口置顶
void toggleTopMost()
{
isTopMost = !isTopMost;
if (isTopMost)
{
SetWindowPos(hConsoleWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
else
{
SetWindowPos(hConsoleWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
}
//最小化/还原窗口
void toggleMinimize()
{
isMinimized = !isMinimized;
if (isMinimized)
{
ShowWindow(hConsoleWnd, SW_MINIMIZE);
}
else
{
ShowWindow(hConsoleWnd, SW_RESTORE);
SetForegroundWindow(hConsoleWnd);
}
}
//重置对局
void resetGame()
{
memset(board, 0, sizeof(board));
curX = 7;
curY = 7;
player = 1;
gameOver = false;
}
//统计空位 和棋判断
int getEmptyCount()
{
int cnt = 0;
for (int y = 0; y < BOARD_SIZE; y++)
for (int x = 0; x < BOARD_SIZE; x++)
if (board[y][x] == 0) cnt++;
return cnt;
}
void drawBoard()
{
gotoXY(0, 0);
cout << "==================== 五 子 棋 ====================\n";
cout << "【方向键移动|空格落子|ESC退出|F2最小化/恢复|F8置顶开关】\n\n";
}
// 完整补全的胜负判断函数
bool checkWin(int x, int y, int p)
{
int dir[4][2] = {{1,0}, {0,1}, {1,1}, {1,-1}};
for (int d = 0; d < 4; d++)
{
int count = 1;
int dx = dir[d][0];
int dy = dir[d][1];
// 正向统计
int nx = x + dx, ny = y + dy;
while (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[ny][nx] == p)
{
count++;
nx += dx; ny += dy;
}
// 反向统计
nx = x - dx;
ny = y - dy;
while (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && board[ny][nx] == p)
{
count++;
nx -= dx; ny -= dy;
}
// 连成5子即获胜
if (count >= 5)
return true;
}
return false;
}
// 主游戏逻辑
int main()
{
lockConsoleWindow();
hideCursor();
resetGame();
}