游戏代码
#include <graphics.h>
#include <conio.h>const int BOARD_SIZE = 15; // 棋盘大小
const int GRID_SIZE = 40; // 格子大小
const int WINDOW_WIDTH = (BOARD_SIZE + 2) * GRID_SIZE; // 窗口宽度
const int WINDOW_HEIGHT = (BOARD_SIZE + 2) * GRID_SIZE; // 窗口高度const int PLAYER_ONE = 1; // 玩家一
const int PLAYER_TWO = 2; // 玩家二enum class GameState {PLAYING,PLAYER_ONE_WIN,PLAYER_TWO_WIN,DRAW
};int board[BOARD_SIZE][BOARD_SIZE]; // 棋盘
int currentPlayer; // 当前玩家
GameState gameState; // 游戏状态void drawBoard() {setbkcolor(LIGHTGRAY);cleardevice();for (int i = 0; i < BOARD_SIZE + 1; i++) {line(GRID_SIZE, (i + 1) * GRID_SIZE, (BOARD_SIZE + 1) * GRID_SIZE, (i + 1) * GRID_SIZE); // 画横线line((i + 1) * GRID_SIZE, GRID_SIZE, (i + 1) * GRID_SIZE, (BOARD_SIZE + 1) * GRID_SIZE); // 画竖线}for (int i = 0; i < BOARD_SIZE; i++) {for (int j = 0; j < BOARD_SIZE; j++) {if (board[i][j] == PLAYER_ONE) {setfillcolor(BLACK);solidcircle((j + 1) * GRID_SIZE, (i + 1) * GRID_SIZE, GRID_SIZE / 2 - 2); // 画黑棋}else if (board[i][j] == PLAYER_TWO) {setfillcolor(WHITE);solidcircle((j + 1) * GRID_SIZE, (i + 1) * GRID_SIZE, GRID_SIZE / 2 - 2); // 画白棋}}}flushbatch();
}bool isValidMove(int row, int col) {if (row < 0 || row >= BOARD_SIZE || col < 0 || col >= BOARD_SIZE) {return false;}if (board[row][col] != 0) {return false;}return true;
}bool isWinningMove(int player, int row, int col) {// 水平方向int count = 1;for (int i = 1; i < 5; i++) {if (col - i >= 0 && board[row][col - i] == player) {count++;}else {break;}}for (int i = 1; i < 5; i++) {if (col + i < BOARD_SIZE && board[row][col + i] == player) {count++;}else {break;}}if (count >= 5) {return true;}// 垂直方向count = 1;for (int i = 1; i < 5; i++) {if (row - i >= 0 && board[row - i][col] == player) {count++;}else {break;}}for (int i = 1; i < 5; i++) {if (row + i < BOARD_SIZE && board[row + i][col] == player) {count++;}else {break;}}if (count >= 5) {return true;}// 左上到右下方向count = 1;for (int i = 1; i < 5; i++) {if (row - i >= 0 && col - i >= 0 && board[row - i][col - i] == player) {count++;}else {break;}}for (int i = 1; i < 5; i++) {if (row + i < BOARD_SIZE && col + i < BOARD_SIZE && board[row + i][col + i] == player) {count++;}else {break;}}if (count >= 5) {return true;}// 左下到右上方向count = 1;for (int i = 1; i < 5; i++) {if (row + i < BOARD_SIZE && col - i >= 0 && board[row + i][col - i] == player) {count++;}else {break;}}for (int i = 1; i < 5; i++) {if (row - i >= 0 && col + i < BOARD_SIZE && board[row - i][col + i] == player) {count++;}else {break;}}if (count >= 5) {return true;}return false;
}void play() {int mouseX, mouseY;while (gameState == GameState::PLAYING) {MOUSEMSG msg = GetMouseMsg();if (msg.uMsg == WM_LBUTTONDOWN) {mouseX = msg.x;mouseY = msg.y;int col = mouseX / GRID_SIZE - 1;int row = mouseY / GRID_SIZE - 1;if (isValidMove(row, col)) {board[row][col] = currentPlayer;if (isWinningMove(currentPlayer, row, col)) {if (currentPlayer == PLAYER_ONE) {gameState = GameState::PLAYER_ONE_WIN;}else {gameState = GameState::PLAYER_TWO_WIN;}}else {bool isFull = true;for (int i = 0; i < BOARD_SIZE; i++) {for (int j = 0; j < BOARD_SIZE; j++) {if (board[i][j] == 0) {isFull = false;break;}}if (!isFull) {break;}}if (isFull) {gameState = GameState::DRAW;}}currentPlayer = (currentPlayer == PLAYER_ONE) ? PLAYER_TWO : PLAYER_ONE;drawBoard();}}}
}int main() {initgraph(WINDOW_WIDTH, WINDOW_HEIGHT);currentPlayer = PLAYER_ONE;gameState = GameState::PLAYING;drawBoard();play();settextstyle(50, 0, _T("微软雅黑"));if (gameState == GameState::PLAYER_ONE_WIN) {outtextxy(200, 200, _T("玩家一胜利"));}else if (gameState == GameState::PLAYER_TWO_WIN) {outtextxy(200, 200, _T("玩家二胜利"));}else if (gameState == GameState::DRAW) {outtextxy(250, 200, _T("平局"));}_getch();closegraph();return 0;
}
ege库安装教程
安装C++ ege库的步骤如下:
1. 下载ege库:您可以从ege官方网站(新手入门『一』EGE的介绍、安装和使用 - Easy Graphics Engine)或者其他可信的软件下载站点上下载ege库的安装包。
2. 解压安装包:将下载的安装包解压到您想要安装ege库的目录下。
3. 配置环境变量:将ege库的bin目录添加到系统的环境变量中。打开计算机的属性设置,点击“高级系统设置”,在弹出的窗口中点击“环境变量”,找到系统变量中的“Path”,点击“编辑”,将ege库的bin目录路径添加到Path变量的末尾。
4. 配置开发环境:如果您使用的是Code::Blocks等IDE,您需要将ege库的include目录添加到IDE的编译选项中。打开Code::Blocks,点击“设置”->“编译器和调试器”->“搜索目录”,在“编译器”的“附加编译器标志”中添加`-I<ege库的include目录>`。
5. 配置链接选项:在Code::Blocks的“设置”->“编译器和调试器”->“链接器”的“附加库和链接器标志”中添加`-lbgi -lgdi32 -lcomdlg32 -luuid -loleaut32 -lole32`。
6. 测试ege库:编写一个简单的ege程序,并编译运行,查看是否能够正常工作。例如,您可以尝试运行以下示例程序:
#include <graphics.h>int main() {initgraph(640, 480);circle(320, 240, 100);_getch();closegraph();return 0;
}
如果能够正常绘制一个圆,并且没有报错信息,则表明ege库已经安装成功。
请注意,ege库目前只支持Windows操作系统。如果您使用的是其他操作系统,可能需要寻找其他图形库进行图形编程。
详细教程可以看EGE 安装与配置_ege图形库-CSDN博客