用C语言基于EASYX制作一个井字棋小游戏
时间: 2023-12-28 09:05:04 浏览: 52
好的,下面是基于EASYX库的井字棋小游戏的C语言代码:
```c
#include <graphics.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ROW 3
#define COL 3
#define SIZE 100
int board[ROW][COL]; // 棋盘数组,0表示空位,1表示玩家1,2表示玩家2
// 初始化棋盘
void init_board() {
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
board[i][j] = 0;
}
}
}
// 绘制棋盘
void draw_board() {
int i, j;
setcolor(WHITE);
for (i = 0; i <= ROW; i++) {
line(SIZE, i * SIZE + SIZE, COL * SIZE + SIZE, i * SIZE + SIZE);
}
for (j = 0; j <= COL; j++) {
line(j * SIZE + SIZE, SIZE, j * SIZE + SIZE, ROW * SIZE + SIZE);
}
}
// 绘制棋子
void draw_piece(int row, int col, int player) {
int x = col * SIZE + SIZE;
int y = row * SIZE + SIZE;
if (player == 1) {
setcolor(YELLOW);
circle(x, y, SIZE / 2 - 5);
} else if (player == 2) {
setcolor(RED);
line(x - SIZE / 2 + 10, y - SIZE / 2 + 10, x + SIZE / 2 - 10, y + SIZE / 2 - 10);
line(x - SIZE / 2 + 10, y + SIZE / 2 - 10, x + SIZE / 2 - 10, y - SIZE / 2 + 10);
}
}
// 判断胜负
int check_win(int player) {
int i, j;
// 判断每一行
for (i = 0; i < ROW; i++) {
if (board[i][0] == player && board[i][1] == player && board[i][2] == player) {
return 1;
}
}
// 判断每一列
for (j = 0; j < COL; j++) {
if (board[0][j] == player && board[1][j] == player && board[2][j] == player) {
return 1;
}
}
// 判断对角线
if (board[0][0] == player && board[1][1] == player && board[2][2] == player) {
return 1;
}
if (board[0][2] == player && board[1][1] == player && board[2][0] == player) {
return 1;
}
return 0;
}
// 判断平局
int check_tie() {
int i, j;
for (i = 0; i < ROW; i++) {
for (j = 0; j < COL; j++) {
if (board[i][j] == 0) {
return 0;
}
}
}
return 1;
}
int main() {
int player = 1; // 当前玩家,初始为玩家1
int row, col; // 选择的行列
int win = 0; // 是否胜利
int tie = 0; // 是否平局
init_board();
initgraph(COL * SIZE + 2 * SIZE, ROW * SIZE + 2 * SIZE);
draw_board();
while (!win && !tie) {
if (player == 1) {
outtextxy(10, ROW * SIZE + SIZE + 10, "玩家1,请选择位置:");
} else {
outtextxy(10, ROW * SIZE + SIZE + 10, "玩家2,请选择位置:");
}
while (1) {
if (kbhit()) {
char c = getch();
if (c == 'q') {
closegraph();
exit(0);
}
row = (int) (c - '1') / COL;
col = (int) (c - '1') % COL;
if (row >= 0 && row < ROW && col >= 0 && col < COL && board[row][col] == 0) {
break;
}
}
delay(10);
}
board[row][col] = player;
draw_piece(row, col, player);
win = check_win(player);
tie = check_tie();
player = (player == 1) ? 2 : 1; // 切换玩家
}
if (win) {
if (player == 1) {
outtextxy(10, ROW * SIZE + SIZE + 10, "玩家2胜利!");
} else {
outtextxy(10, ROW * SIZE + SIZE + 10, "玩家1胜利!");
}
} else {
outtextxy(10, ROW * SIZE + SIZE + 10, "平局!");
}
while (1) {
if (kbhit()) {
char c = getch();
if (c == 'q') {
break;
}
}
delay(10);
}
closegraph();
return 0;
}
```
这个程序使用了EASYX库来实现图形界面,通过键盘输入来选择棋子的位置。你可以在开发环境中编译运行此程序,体验一下这个简单的井字棋小游戏。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)