void check_game_over() { int i, j; for (i = 0; i < ROW; i++) { for (j = 0; j < COL; j++) { if (board[i][j] != 0 && check_win(i, j)) { game_over = 1; printf("玩家 %d 胜利!\n", player); return; } } } if (game_over == 0) { printf("游戏结束,没有人胜利\n"); } }
时间: 2024-02-14 17:24:26 浏览: 17
这段代码是一个函数,用于检查游戏是否结束。函数中使用了两个循环,遍历了游戏棋盘上的每一个格子。如果某个格子上有棋子并且该棋子所在位置满足胜利条件,那么游戏就结束了,此时game_over变量被设置为1,同时输出胜利信息。如果所有格子都被遍历完了,但是没有发现胜利的情况,那么就说明游戏结束了,但是没有人胜利,此时输出相应的信息。
相关问题
可以帮我配合着easyx+c++语言写一个五子棋游戏么
当然可以!这里给您提供一个简单的五子棋游戏的代码示例,使用了 EasyX 图形库和 C++ 语言。
```c++
#include <graphics.h>
#include <conio.h>
#include <iostream>
using namespace std;
const int WIN_WIDTH = 640; // 窗口宽度
const int WIN_HEIGHT = 640; // 窗口高度
const int ROWS = 15; // 棋盘行数
const int COLS = 15; // 棋盘列数
const int BLOCK_SIZE = 40; // 棋盘格子大小
int board[ROWS][COLS] = {0}; // 棋盘状态,0表示空,1表示黑子,2表示白子
bool is_black_turn = true; // 是否轮到黑子
bool game_over = false; // 游戏是否结束
// 绘制棋盘
void draw_board() {
setbkcolor(WHITE);
cleardevice();
// 绘制横线
for (int i = 0; i < ROWS; i++) {
line(BLOCK_SIZE, BLOCK_SIZE * (i + 1), BLOCK_SIZE * COLS, BLOCK_SIZE * (i + 1));
}
// 绘制竖线
for (int i = 0; i < COLS; i++) {
line(BLOCK_SIZE * (i + 1), BLOCK_SIZE, BLOCK_SIZE * (i + 1), BLOCK_SIZE * ROWS);
}
// 绘制棋子
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
if (board[i][j] == 1) {
setfillcolor(BLACK);
solidcircle(BLOCK_SIZE * (j + 1), BLOCK_SIZE * (i + 1), BLOCK_SIZE / 2);
} else if (board[i][j] == 2) {
setfillcolor(WHITE);
solidcircle(BLOCK_SIZE * (j + 1), BLOCK_SIZE * (i + 1), BLOCK_SIZE / 2);
}
}
}
// 刷新屏幕
flushbatch();
}
// 判断是否胜利
bool check_win(int x, int y) {
// 水平方向
int count = 1;
for (int i = x - 1; i >= 0 && board[i][y] == board[x][y]; i--) count++;
for (int i = x + 1; i < ROWS && board[i][y] == board[x][y]; i++) count++;
if (count >= 5) return true;
// 垂直方向
count = 1;
for (int j = y - 1; j >= 0 && board[x][j] == board[x][y]; j--) count++;
for (int j = y + 1; j < COLS && board[x][j] == board[x][y]; j++) count++;
if (count >= 5) return true;
// 左上到右下方向
count = 1;
for (int i = x - 1, j = y - 1; i >= 0 && j >= 0 && board[i][j] == board[x][y]; i--, j--) count++;
for (int i = x + 1, j = y + 1; i < ROWS && j < COLS && board[i][j] == board[x][y]; i++, j++) count++;
if (count >= 5) return true;
// 右上到左下方向
count = 1;
for (int i = x - 1, j = y + 1; i >= 0 && j < COLS && board[i][j] == board[x][y]; i--, j++) count++;
for (int i = x + 1, j = y - 1; i < ROWS && j >= 0 && board[i][j] == board[x][y]; i++, j--) count++;
if (count >= 5) return true;
return false;
}
// 处理鼠标点击事件
void handle_mouse_click(int x, int y) {
if (game_over) return;
// 计算行列号
int row = y / BLOCK_SIZE - 1;
int col = x / BLOCK_SIZE - 1;
// 判断是否越界
if (row < 0 || row >= ROWS || col < 0 || col >= COLS) return;
// 判断是否已经有棋子
if (board[row][col] != 0) return;
// 下子
if (is_black_turn) {
board[row][col] = 1;
} else {
board[row][col] = 2;
}
// 判断是否胜利
if (check_win(row, col)) {
game_over = true;
if (is_black_turn) {
cout << "黑方胜利!" << endl;
} else {
cout << "白方胜利!" << endl;
}
}
// 切换下子方
is_black_turn = !is_black_turn;
// 重新绘制棋盘
draw_board();
}
int main() {
// 初始化窗口
initgraph(WIN_WIDTH, WIN_HEIGHT, SHOWCONSOLE);
// 绘制棋盘
draw_board();
// 处理鼠标点击事件
setbkcolor(WHITE);
setmousequeuestatus(MOUSE_NOWMOVING);
while (!game_over) {
MOUSEMSG m = GetMouseMsg();
if (m.uMsg == WM_LBUTTONDOWN) {
handle_mouse_click(m.x, m.y);
}
}
// 关闭窗口
closegraph();
return 0;
}
```
您可以将上述代码拷贝到 C++ 编辑器中编译运行,就可以玩五子棋游戏了。
请用C语言代码实现贪吃蛇游戏
以下是一个简单的贪吃蛇游戏的 C 语言代码示例:
```c
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_ROW 20
#define MAX_COL 30
int row, col;
int snake_row, snake_col;
int food_row, food_col;
int snake_body_row[100], snake_body_col[100];
int snake_length;
char direction;
char map[MAX_ROW][MAX_COL];
void init_map();
void init_snake();
void init_food();
void draw_map();
void update_snake();
void update_map();
int check_game_over();
void game_over();
int main()
{
srand(time(NULL));
init_map();
init_snake();
init_food();
draw_map();
while (1) {
if (kbhit()) {
direction = getch();
}
update_snake();
update_map();
if (check_game_over()) {
game_over();
break;
}
draw_map();
Sleep(100);
}
return 0;
}
void init_map()
{
for (row = 0; row < MAX_ROW; row++) {
for (col = 0; col < MAX_COL; col++) {
if (row == 0 || row == MAX_ROW - 1 || col == 0 || col == MAX_COL - 1) {
map[row][col] = '#';
} else {
map[row][col] = ' ';
}
}
}
}
void init_snake()
{
snake_row = MAX_ROW / 2;
snake_col = MAX_COL / 2;
snake_body_row[0] = snake_row;
snake_body_col[0] = snake_col;
snake_length = 1;
direction = ' ';
}
void init_food()
{
food_row = rand() % (MAX_ROW - 2) + 1;
food_col = rand() % (MAX_COL - 2) + 1;
}
void draw_map()
{
system("cls");
for (row = 0; row < MAX_ROW; row++) {
for (col = 0; col < MAX_COL; col++) {
printf("%c", map[row][col]);
}
printf("\n");
}
}
void update_snake()
{
int i;
for (i = snake_length - 1; i > 0; i--) {
snake_body_row[i] = snake_body_row[i - 1];
snake_body_col[i] = snake_body_col[i - 1];
}
switch (direction) {
case 'w':
case 'W':
snake_row--;
break;
case 's':
case 'S':
snake_row++;
break;
case 'a':
case 'A':
snake_col--;
break;
case 'd':
case 'D':
snake_col++;
break;
}
snake_body_row[0] = snake_row;
snake_body_col[0] = snake_col;
if (snake_row == food_row && snake_col == food_col) {
snake_length++;
init_food();
}
}
void update_map()
{
int i;
for (row = 0; row < MAX_ROW; row++) {
for (col = 0; col < MAX_COL; col++) {
map[row][col] = ' ';
}
}
for (i = 0; i < snake_length; i++) {
row = snake_body_row[i];
col = snake_body_col[i];
if (i == 0) {
map[row][col] = '@';
} else {
map[row][col] = '*';
}
}
map[food_row][food_col] = '$';
}
int check_game_over()
{
int i;
if (snake_row == 0 || snake_row == MAX_ROW - 1 || snake_col == 0 || snake_col == MAX_COL - 1) {
return 1;
}
for (i = 1; i < snake_length; i++) {
if (snake_row == snake_body_row[i] && snake_col == snake_body_col[i]) {
return 1;
}
}
return 0;
}
void game_over()
{
printf("Game Over!\n");
}
```
该示例中使用了 Windows 系统下的 conio.h 头文件提供的 kbhit() 和 getch() 函数来实现键盘输入的检测和获取。如果你使用的是其他系统或环境,可能需要替换这部分代码。