
能实现计时、记步功能
#include <graphics.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <windows.h>
int chessColor = BLACK;
int board[25][25] = { 0 };
DWORD startTime;
bool gameRunning;
int playerSteps = 0;
int aiSteps = 0;
#define CELL 25
#define MARGIN 0
// 判赢逻辑
int checkWin(int row, int col, int color) {
int dir[4][2] = { {1,0}, {0,1}, {1,1}, {1,-1} };
for (int d = 0; d < 4; d++) {
int dx = dir[d][0];
int dy = dir[d][1];
int count = 1;
for (int i = 1; i < 5; i++) {
int r = row + dy * i;
int c = col + dx * i;
if (r < 0 || r >= 25 || c < 0 || c >= 25 || board[r][c] != color) break;
count++;
}
for (int i = 1; i < 5; i++) {
int r = row - dy * i;
int c = col - dx * i;
if (r < 0 || r >= 25 || c < 0 || c >= 25 || board[r][c] != color) break;
count++;
}
if (count >= 5) return 1;
}
return 0;
}
// AI评分
int getScore(int row, int col, int color) {
int dir[4][2] = { {1,0},{0,1},{1,1},{1,-1} };
int score = 0;
for (int d = 0; d < 4; d++) {
int dx = dir[d][0], dy = dir[d][1];
int cnt = 0, empty = 0;
for (int i = 1; i < 5; i++) {
int r = row + dy * i, c = col + dx * i;
if (r < 0 || r >= 25 || c < 0 || c >= 25) break;
if (board[r][c] == color) cnt++;
else if (board[r][c] == 0) { empty++; break; }
else break;
}
for (int i = 1; i < 5; i++) {
int r = row - dy * i, c = col - dx * i;
if (r < 0 || r >= 25 || c < 0 || c >= 25) break;
if (board[r][c] == color) cnt++;
else if (board[r][c] == 0) { empty++; break; }
else break;
}
if (cnt >= 4) score += 100000;
else if (cnt == 3 && empty >= 1) score += 10000;
else if (cnt == 2 && empty >= 1) score += 1000;
else if (cnt == 1) score += 100;
}
return score;
}
// 坐标校准
bool getGrid(int mx, int my, int* col, int* row) {
*col = (mx - MARGIN + CELL / 2) / CELL;
*row = (my - MARGIN + CELL / 2) / CELL;
return (*col >= 0 && *col < 25 && *row >= 0 && *row < 25);
}
// 画圆润棋子(渐变+更饱满)
void drawChess(int x, int y, int color) {
if (color == 1) { // 黑棋
setfillcolor(BLACK);
solidcircle(x, y, 11); // 半径变大,更圆润
}
else { // 白棋
setfillcolor(WHITE);
solidcircle(x, y, 11);
setcolor(RGB(100, 100, 100)); // 加一圈浅灰边,更立体
circle(x, y, 11);
}
}
void resetGame() {
memset(board, 0, sizeof(board));
chessColor = BLACK;
playerSteps = 0;
aiSteps = 0;
initgraph(800, 600);
BeginBatchDraw();
// 温暖浅木色背景
setbkcolor(RGB(250, 224, 188));
cleardevice();
// 窗口居中
HWND hWnd = GetHWnd();
RECT deskRect;
GetWindowRect(GetDesktopWindow(), &deskRect);
int x = (deskRect.right - 800) / 2;
int y = (deskRect.bottom - 600) / 2;
SetWindowPos(hWnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
// 棋盘线
setlinecolor(RGB(101, 67, 33));
setlinestyle(PS_SOLID, 2);
for (int i = 0; i < 25; i++) {
line(MARGIN, MARGIN + i * CELL, MARGIN + 24 * CELL, MARGIN + i * CELL);
line(MARGIN + i * CELL, MARGIN, MARGIN + i * CELL, MARGIN + 24 * CELL);
}
setbkmode(TRANSPARENT);
settextcolor(BLACK);
outtextxy(650, 100, _T("玩家:黑棋"));
outtextxy(650, 200, _T("电脑:白棋"));
FlushBatchDraw();
startTime = GetTickCount();
gameRunning = true;
}
void aiPlay() {
if (!gameRunning) return;
Sleep(rand() % 300 + 500);
// 电脑赢
for (int row = 0; row < 25; row++) {
for (int col = 0; col < 25; col++) {
if (board[row][col] != 0) continue;
board[row][col] = 2;
if (checkWin(row, col, 2)) {
drawChess(MARGIN + col * CELL, MARGIN + row * CELL, 2);
FlushBatchDraw();
aiSteps++;
gameRunning = false;
if (MessageBox(GetHWnd(), _T("电脑胜利!再来一局?"), _T("游戏结束"), MB_YESNO) == IDYES) {
closegraph(); resetGame();
}
else exit(0);
return;
}
board[row][col] = 0;
}
}
// 堵人
for (int row = 0; row < 25; row++) {
for (int col = 0; col < 25; col++) {
if (board[row][col] != 0) continue;
board[row][col] = 1;
if (checkWin(row, col, 1)) {
board[row][col] = 2;
drawChess(MARGIN + col * CELL, MARGIN + row * CELL, 2);
FlushBatchDraw();
aiSteps++;
chessColor = BLACK;
return;
}
board[row][col] = 0;
}
}
// 最佳位置
int bestRow = -1, bestCol = -1, maxScore = 0;
for (int row = 0; row < 25; row++) {
for (int col = 0; col < 25; col++) {
if (board[row][col] != 0) continue;
board[row][col] = 2; int s2 = getScore(row, col, 2); board[row][col] = 0;
board[row][col] = 1; int s1 = getScore(row, col, 1); board[row][col] = 0;
int total = s2 + s1;
if (total > maxScore) {
maxScore = total; bestRow = row; bestCol = col;
}
}
}
if (bestRow == -1)
do { bestRow = rand() % 25; bestCol = rand() % 25; } while (board[bestRow][bestCol] != 0);
board[bestRow][bestCol] = 2;
drawChess(MARGIN + bestCol * CELL, MARGIN + bestRow * CELL, 2);
FlushBatchDraw();
aiSteps++;
chessColor = BLACK;
}
void drawInfo() {
setcolor(WHITE); setfillcolor(WHITE);
fillrectangle(620, 280, 780, 450);
if (!gameRunning) return;
DWORD now = GetTickCount();
DWORD sec = (now - startTime) / 1000;
int min = sec / 60, s = sec % 60;
wchar_t timeStr[50], pStep[50], aStep[50];
swprintf_s(timeStr, _T("时间:%02d:%02d"), min, s);
swprintf_s(pStep, _T("玩家:%d 步"), playerSteps);
swprintf_s(aStep, _T("电脑:%d 步"), aiSteps);
settextcolor(RED);
settextstyle(20, 0, _T("微软雅黑"));
outtextxy(640, 300, timeStr);
outtextxy(640, 340, pStep);
outtextxy(640, 380, aStep);
FlushBatchDraw();
}
void playGame() {
MOUSEMSG msg;
srand((unsigned int)time(NULL));
while (true) {
drawInfo();
while (MouseHit()) {
msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN && chessColor == BLACK && gameRunning) {
int col, row;
if (getGrid(msg.x, msg.y, &col, &row) && board[row][col] == 0) {
board[row][col] = 1;
drawChess(MARGIN + col * CELL, MARGIN + row * CELL, 1);
FlushBatchDraw();
playerSteps++;
chessColor = WHITE;
if (checkWin(row, col, 1)) {
gameRunning = false;
if (MessageBox(GetHWnd(), _T("你胜利了!再来一局?"), _T("游戏结束"), MB_YESNO) == IDYES) {
closegraph(); resetGame();
}
else exit(0);
continue;
}
aiPlay();
}
}
}
Sleep(10);
}
}
int main() {
resetGame();
playGame();
closegraph();
return 0;
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。