五子棋(AI做)
2026-07-31 12:57:28
发布于:浙江
#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);
COORD bufferSize = {90, 45};
SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), bufferSize);
SMALL_RECT windowRect = {0, 0, 88, 42};
SetConsoleWindowInfo(GetStdHandle(STD_OUTPUT_HANDLE), TRUE, &windowRect);
}
//切换窗口置顶
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";
//顶部横坐标
cout << " ";
for (int i = 0; i < BOARD_SIZE; i++)
{
if (i < 10)
cout << i << " ";
else
cout << i << " ";
}
cout << endl;
for (int y = 0; y < BOARD_SIZE; y++)
{
if (y < 10)
cout << " " << y << " ";
else
cout << y << " ";
for (int x = 0; x < BOARD_SIZE; x++)
{
if (x == curX && y == curY && board[y][x] == 0)
{
cout << "□ "; //全角预选框
}
else
{
if (board[y][x] == 0)
cout << "· "; //全角点
else if (board[y][x] == 1)
cout << "● "; //全角黑棋
else
cout << "○ "; //全角白棋
}
}
cout << endl;
}
cout << "\n";
if (player == 1)
cout << ">>> 当前玩家:【黑棋 ●】";
else
cout << ">>> 当前玩家:【白棋 ○】";
cout << " 置顶状态:" << (isTopMost ? "开启" : "关闭");
}
// 完整补全的胜负判断函数
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();
while (true)
{
drawBoard();
if (gameOver)
{
gotoXY(0, 30);
cout << "\n游戏结束!按R重新开局,ESC退出程序";
}
if (_kbhit())
{
int key = _getch();
// ESC退出
if (key == 27)
break;
// R重置游戏
if (key == 'r' || key == 'R')
{
resetGame();
continue;
}
// F2 最小化
if (key == 113) //F2
{
toggleMinimize();
continue;
}
// F8 置顶
if (key == 119) //F8
{
toggleTopMost();
continue;
}
// 方向键
if (key == 224)
{
key = _getch();
if (key == 72 && curY > 0) curY--; //上
if (key == 80 && curY < BOARD_SIZE-1) curY++; //下
if (key == 75 && curX > 0) curX--; //左
if (key == 77 && curX < BOARD_SIZE-1) curX++; //右
}
// 空格落子
if (key == ' ' && !gameOver)
{
if (board[curY][curX] == 0)
{
board[curY][curX] = player;
// 判断胜利
if (checkWin(curX, curY, player))
{
gameOver = true;
gotoXY(0, 28);
if (player == 1)
cout << "黑棋 ● 获胜!";
else
cout << "白棋 ○ 获胜!";
}
// 和棋判断
else if (getEmptyCount() == 0)
{
gameOver = true;
gotoXY(0, 28);
cout << "棋盘下满,本局和棋!";
}
// 切换玩家
player = (player == 1) ? 2 : 1;
}
}
}
Sleep(30);
}
return 0;
}
全部评论 1
做的不好,建议看看工具大全
2026-07-31 来自 浙江
0这是Devc++,已经尽可能优化了,中间还差点走上歪路(代码)
2026-07-31 来自 浙江
0还是建议看看工具大全
2026-07-31 来自 浙江
0看过了,确实更好。
2026-07-31 来自 浙江
0



















有帮助,赞一个