使用easyx实现五子棋小游戏,能判断输赢
时间: 2023-12-13 21:05:18 浏览: 63
好的,实现五子棋小游戏需要以下几个步骤:
1. 引入 EasyX 头文件和命名空间
```c++
#include <graphics.h> // 引入 EasyX 头文件
using namespace std; // 使用 std 命名空间
```
2. 定义游戏面板和棋子
```c++
const int BOARD_SIZE = 15; // 游戏面板大小
const int BOARD_LEFT = 50; // 游戏面板左边距
const int BOARD_TOP = 50; // 游戏面板上边距
const int BOARD_BLOCK_SIZE = 40; // 每个格子大小
const int EMPTY = 0; // 空位
const int BLACK = 1; // 黑棋
const int WHITE = 2; // 白棋
int board[BOARD_SIZE][BOARD_SIZE] = {0}; // 游戏面板
```
3. 绘制游戏面板和棋子
```c++
void draw_board() {
for (int i = 0; i < BOARD_SIZE; i++) {
line(BOARD_LEFT, BOARD_TOP + i * BOARD_BLOCK_SIZE, BOARD_LEFT + (BOARD_SIZE - 1) * BOARD_BLOCK_SIZE, BOARD_TOP + i * BOARD_BLOCK_SIZE); // 绘制横线
line(BOARD_LEFT + i * BOARD_BLOCK_SIZE, BOARD_TOP, BOARD_LEFT + i * BOARD_BLOCK_SIZE, BOARD_TOP + (BOARD_SIZE - 1) * BOARD_BLOCK_SIZE); // 绘制竖线
}
for (int i = 0; i < BOARD_SIZE; i++) {
for (int j = 0; j < BOARD_SIZE; j++) {
if (board[i][j] == BLACK) {
setfillcolor(BLACK);
solidcircle(BOARD_LEFT + i * BOARD_BLOCK_SIZE, BOARD_TOP + j * BOARD_BLOCK_SIZE, BOARD_BLOCK_SIZE / 2 - 2); // 绘制黑子
} else if (board[i][j] == WHITE) {
setfillcolor(WHITE);
solidcircle(BOARD_LEFT + i * BOARD_BLOCK_SIZE, BOARD_TOP + j * BOARD_BLOCK_SIZE, BOARD_BLOCK_SIZE / 2 - 2); // 绘制白子
}
}
}
}
```
4. 判断输赢
```c++
bool check_win(int x, int y, int color) {
int count = 1; // 连子数
int i, j;
// 横向
for (i = x - 1; i >= 0 && board[i][y] == color; i--, count++);
for (i = x + 1; i < BOARD_SIZE && board[i][y] == color; i++, count++);
if (count >= 5) return true;
// 纵向
count = 1;
for (j = y - 1; j >= 0 && board[x][j] == color; j--, count++);
for (j = y + 1; j < BOARD_SIZE && board[x][j] == color; j++, count++);
if (count >= 5) return true;
// 左斜线
count = 1;
for (i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j] == color; i--, j--, count++);
for (i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j] == color; i++, j++, count++);
if (count >= 5) return true;
// 右斜线
count = 1;
for (i = x - 1, j = y + 1; i >= 0 && j < BOARD_SIZE && board[i][j] == color; i--, j++, count++);
for (i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0 && board[i][j] == color; i++, j--, count++);
if (count >= 5) return true;
return false;
}
```
5. 主函数
```c++
int main() {
initgraph(BOARD_LEFT * 2 + BOARD_SIZE * BOARD_BLOCK_SIZE, BOARD_TOP * 2 + BOARD_SIZE * BOARD_BLOCK_SIZE); // 初始化窗口
draw_board(); // 绘制游戏面板
int x, y; // 当前下棋的坐标
int color = BLACK; // 当前下棋的颜色
bool gameover = false; // 游戏是否结束
while (!gameover) {
if (ismouseclick(WM_LBUTTONDOWN)) { // 鼠标左键按下
x = (getmousex() - BOARD_LEFT + BOARD_BLOCK_SIZE / 2) / BOARD_BLOCK_SIZE;
y = (getmousey() - BOARD_TOP + BOARD_BLOCK_SIZE / 2) / BOARD_BLOCK_SIZE;
if (board[x][y] == EMPTY) { // 检查当前位置是否为空
board[x][y] = color; // 下棋
draw_board(); // 重新绘制游戏面板
if (check_win(x, y, color)) { // 判断是否获胜
gameover = true;
settextcolor(color == BLACK ? BLACK : WHITE);
outtextxy(BOARD_LEFT, BOARD_TOP + BOARD_SIZE * BOARD_BLOCK_SIZE + 10, color == BLACK ? "黑方胜利!" : "白方胜利!"); // 显示胜利者
}
color = color == BLACK ? WHITE : BLACK; // 切换下棋颜色
}
clearmouseclick(WM_LBUTTONDOWN); // 清除鼠标左键按下消息
}
}
getch(); // 等待按键
closegraph(); // 关闭窗口
return 0;
}
```
以上就是使用 EasyX 实现五子棋小游戏的全部内容,你可以将上面的代码复制到 Dev-C++、Visual Studio 等 IDE 中进行编译运行,如果需要修改代码,可以根据注释进行修改。