From 8a7e4498b7ca1e022a93e325c4ec5d49be44533f Mon Sep 17 00:00:00 2001 From: p7fu53y4l <2834282750@qq.com> Date: Tue, 27 May 2025 11:27:51 +0800 Subject: [PATCH] ADD file via upload --- main.cpp | 258 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 258 insertions(+) create mode 100644 main.cpp diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..63704b2 --- /dev/null +++ b/main.cpp @@ -0,0 +1,258 @@ +#include +#include +#include +#include // 添加Windows API头文件 +#include // 添加多媒体库头文件 +#pragma comment(lib, "winmm.lib") // 链接多媒体库 +//播放音乐 +void playMusic(const char* filename) +{ + char command[256]; + sprintf_s(command, "open \"%s\" type mpegvideo alias music", filename); + mciSendString(command, NULL, 0, NULL); + mciSendString("play music", NULL, 0, NULL); +} + +void qipan() +{ + //创建界面 宽度 高度 单位:px 像素 + initgraph(700, 677); + + //插入背景图片,默认插入于界面原点 + loadimage(NULL, "bj.jpg"); + + //绘制棋盘 28*28 + setlinecolor(BLACK); + for (int i = 0; i <= 27; i++) + { + //绘制横线 + line(28, 12 + i * 23, 672, 12 + i * 23); + } + for (int i = 0; i <= 27; i++) + { + //绘制竖线 + line(28 + i * 23, 12, 28 + i * 23, 656); + } + + //插入文字 + //1.设置文字颜色 + settextcolor(BLACK); + //2.设置背景颜色透明 + setbkmode(0); + //3.根据坐标插入文字,x,y就是文字显示的坐标 + outtextxy(30, 660, "玩家1:黑棋"); + outtextxy(350, 660, "玩家2:白棋"); + + //播放音乐 + playMusic("知我.mp3"); // 替换为你的音乐文件路径 +} + +int check(int Map[29][29], int x, int y, int time) +{ + int player = time % 2 + 1; // 1:黑棋胜利 2:白棋胜利 + int count; + + // 水平方向检查 + count = 1; + for (int i = 1; i < 5; i++) + { + if (x + i <= 28 && Map[x + i][y] == player) + { + count++; + } + else + { + break; + } + } + for (int i = 1; i < 5; i++) + { + if (x - i >= 0 && Map[x - i][y] == player) + { + count++; + } + else + { + break; + } + } + if (count >= 5) + { + return player; + } + + // 垂直方向检查 + count = 1; + for (int i = 1; i < 5; i++) + { + if (y + i <= 28 && Map[x][y + i] == player) + { + count++; + } + else + { + break; + } + } + for (int i = 1; i < 5; i++) + { + if (y - i >= 0 && Map[x][y - i] == player) + { + count++; + } + else + { + break; + } + } + if (count >= 5) + { + return player; + } + + // 对角线方向检查 (右下到左上) + count = 1; + for (int i = 1; i < 5; i++) + { + if (x + i <= 28 && y + i <= 28 && Map[x + i][y + i] == player) + { + count++; + } + else + { + break; + } + } + for (int i = 1; i < 5; i++) + { + if (x - i >= 0 && y - i >= 0 && Map[x - i][y - i] == player) + { + count++; + } + else + { + break; + } + } + if (count >= 5) + { + return player; + } + + // 对角线方向检查 (左下到右上) + count = 1; + for (int i = 1; i < 5; i++) + { + if (x + i <= 28 && y - i >= 0 && Map[x + i][y - i] == player) + { + count++; + } + else + { + break; + } + } + for (int i = 1; i < 5; i++) + { + if (x - i >= 0 && y + i <= 28 && Map[x - i][y + i] == player) + { + count++; + } + else + { + break; + } + } + if (count >= 5) + { + return player; + } + + return 0; +} + +void qizi() +{ + // 获取鼠标的消息 + MOUSEMSG Msg;//定义变量 + + HWND hwnd = GetHWnd();//获取窗口句柄 + + int time = 1;//第几次落子 + int ChessX, ChessY; + + //坐标是0~28 + int Map[29][29] = { 0 }; + while (1) + { + Msg = GetMouseMsg();//获取鼠标信息 + + // 优化鼠标位置,在线条交汇的点位上 + for (int i = 0; i <= 28; i++) + { + for (int j = 0; j<=28; j++) + { + if (abs(Msg.x - i * 23 - 28) <= 11.5 && abs(Msg.y - j * 23 - 12) <= 11.5) + { + ChessX = 28 + i * 23;//优化后的x坐标 + ChessY = 12 + j * 23;//优化后的y坐标 + } + } + } + int x = (ChessX - 28) / 23;//横坐标 + int y = (ChessY - 12) / 23;//纵坐标 + // 添加标志位,标记是否成功落子 + int placed = 0; + if (Map[x][y] == 0) + { + // 判断是否点击左键 + if (Msg.uMsg == WM_LBUTTONDOWN) + { + // 白子 + if (time % 2 == 0) + { + setfillcolor(WHITE); + solidcircle(ChessX, ChessY, 9); + Map[x][y] = 2;//白棋落子为2 + placed = 1; + } + // 黑子 + else + { + setfillcolor(BLACK); + solidcircle(ChessX, ChessY, 9); + Map[x][y] = 1;//黑棋落子为1 + placed = 1; + } + time++; + } + } + // 只有在成功落子后才进行胜利条件的检查 + if (placed) + { + int winner = check(Map, x, y, time); + if (winner != 0) + { + if (winner == 1) + { + MessageBox(hwnd, "玩家1胜利", "Game Over", MB_OK); + } + else + { + MessageBox(hwnd, "玩家2胜利", "Game Over", MB_OK); + } + return; + } + } + } +} + +int main() +{ + qipan(); + qizi(); + + //卡屏 + closegraph(); + return 0; +} \ No newline at end of file