写一个五子棋,c代码,easyx库
时间: 2023-05-11 19:02:00 浏览: 110
好的,以下是一个简单的五子棋游戏的C代码,使用了EasyX库:
```c
#include <graphics.h>
#include <conio.h>
#define BOARD_SIZE 15
#define BLOCK_SIZE 40
#define BOARD_LEFT 100
#define BOARD_TOP 100
#define BOARD_RIGHT BOARD_LEFT + BLOCK_SIZE * (BOARD_SIZE - 1)
#define BOARD_BOTTOM BOARD_TOP + BLOCK_SIZE * (BOARD_SIZE - 1)
enum ChessType { NONE, BLACK, WHITE };
struct Chess {
ChessType type;
int x, y;
};
Chess board[BOARD_SIZE][BOARD_SIZE];
ChessType current_player = BLACK;
void draw_board() {
setbkcolor(RGB(255, 206, 158));
cleardevice();
setlinecolor(BLACK);
for (int i = 0; i < BOARD_SIZE; i++) {
line(BOARD_LEFT, BOARD_TOP + BLOCK_SIZE * i, BOARD_RIGHT, BOARD_TOP + BLOCK_SIZE * i);
line(BOARD_LEFT + BLOCK_SIZE * i, BOARD_TOP, BOARD_LEFT + BLOCK_SIZE * i, BOARD_BOTTOM);
}
}
void draw_chess(Chess chess) {
int x = BOARD_LEFT + BLOCK_SIZE * chess.x;
int y = BOARD_TOP + BLOCK_SIZE * chess.y;
if (chess.type == BLACK) {
setfillcolor(BLACK);
solidcircle(x, y, BLOCK_SIZE / 2 - 2);
} else if (chess.type == WHITE) {
setfillcolor(WHITE);
solidcircle(x, y, BLOCK_SIZE / 2 - 2);
setfillcolor(BLACK);
solidcircle(x, y, BLOCK_SIZE / 4);
}
}
bool is_valid_pos(int x, int y) {
return x >= 0 && x < BOARD_SIZE && y >= 0 && y < BOARD_SIZE && board[x][y].type == NONE;
}
bool is_win(int x, int y) {
ChessType type = board[x][y].type;
int count = 1;
for (int i = x - 1; i >= 0 && board[i][y].type == type; i--) count++;
for (int i = x + 1; i < BOARD_SIZE && board[i][y].type == type; i++) count++;
if (count >= 5) return true;
count = 1;
for (int i = y - 1; i >= 0 && board[x][i].type == type; i--) count++;
for (int i = y + 1; i < BOARD_SIZE && board[x][i].type == type; i++) count++;
if (count >= 5) return true;
count = 1;
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j].type == type; i--, j--) count++;
for (int i = x + 1, j = y + 1; i < BOARD_SIZE && j < BOARD_SIZE && board[i][j].type == type; i++, j++) count++;
if (count >= 5) return true;
count = 1;
for (int i = x - 1, j = y + 1; i >= 0 && j < BOARD_SIZE && board[i][j].type == type; i--, j++) count++;
for (int i = x + 1, j = y - 1; i < BOARD_SIZE && j >= 0 && board[i][j].type == type; i++, j--) count++;
if (count >= 5) return true;
return false;
}
void switch_player() {
current_player = current_player == BLACK ? WHITE : BLACK;
}
void play() {
initgraph(BOARD_RIGHT + 100, BOARD_BOTTOM + 100);
draw_board();
while (true) {
int x = (getmousex() - BOARD_LEFT + BLOCK_SIZE / 2) / BLOCK_SIZE;
int y = (getmousey() - BOARD_TOP + BLOCK_SIZE / 2) / BLOCK_SIZE;
if (is_valid_pos(x, y)) {
setcursor(HAND_CURSOR);
if (MouseHit()) {
MouseMsg msg = GetMouseMsg();
if (msg.uMsg == WM_LBUTTONDOWN) {
Chess chess = { current_player, x, y };
board[x][y] = chess;
draw_chess(chess);
if (is_win(x, y)) {
settextstyle(50, 0, _T("Arial"));
settextcolor(current_player == BLACK ? BLACK : WHITE);
outtextxy(BOARD_LEFT, BOARD_BOTTOM + 50, current_player == BLACK ? _T("黑方胜利") : _T("白方胜利"));
getch();
break;
}
switch_player();
}
}
} else {
setcursor(ARROW_CURSOR);
}
}
closegraph();
}
int main() {
play();
return 0;
}
```
这个程序使用了EasyX库来绘制图形界面,实现了一个简单的五子棋游戏。玩家可以使用鼠标来下棋,程序会自动判断胜负并显示结果。
阅读全文