c语言设计扫雷游戏报告,C语言实现扫雷游戏简易版

编程入门 行业动态 更新时间:2024-10-25 10:27:15

c<a href=https://www.elefans.com/category/jswz/34/1770116.html style=语言设计扫雷游戏报告,C语言实现扫雷游戏简易版"/>

c语言设计扫雷游戏报告,C语言实现扫雷游戏简易版

本文实例为大家分享了C语言实现扫雷游戏的简易版,供大家参考,具体内容如下

game.h

#pragma once

#include

#include

#include

#include

#define ROW 12

#define COL 12

#define NUMS 20

#pragma warning(disable:4996)

void Menu();

void Game();

game.c

#include "game.h"

void Menu()

{

printf("###########################\n");

printf("## 1.Play 2. Exit ##\n");

printf("###########################\n");

printf("请输入# ");

}

void SetMines(char board[][COL], int row, int col)

{

int num = NUMS;

while (num) {

int x = rand() % 10 + 1;

int y = rand() % 10 + 1;

if (board[x][y] == '0') {

board[x][y] = '1';

num--;

}

}

}

int GetNums(char board[][COL], int row, int col, int x, int y)

{

return board[x - 1][y - 1] + board[x - 1][y] + \

board[x - 1][y + 1] + board[x][y + 1] + \

board[x + 1][y + 1] + board[x + 1][y] + \

board[x + 1][y - 1] + board[x][y - 1] - 8 * '0';

}

void ShowBoard(char board[][COL], int row, int col)

{

printf(" ");

for (int i = 1; i < col - 1; i++) {

printf(" %2d ", i);

}

printf("\n");

printf("-------------------------------------------\n");

for (int i = 1; i < row - 1; i++) {

printf("%2d|", i);

for (int j = 1; j < col - 1; j++) {

printf(" %c |", board[i][j]);

}

printf("\n");

printf("-------------------------------------------\n");

}

}

void Game()

{

system("cls");

srand((unsigned long)time(NULL));

char show_board[ROW][COL];

char mine_board[ROW][COL];

memset(show_board, '*', sizeof(show_board));

memset(mine_board, '0', sizeof(mine_board));

SetMines(mine_board, ROW, COL);

int count = (ROW - 2) * (COL - 2) - NUMS;

int x = 0;

int y = 0;

do {

ShowBoard(show_board, ROW, COL);

printf("请输入坐标# ");

scanf("%d %d", &x, &y);

if (x < 1 || x > ROW - 2 || y < 1 || y > COL - 2) {

printf("输入位置越界,请重新输入!\n");

continue;

}

if (show_board[x][y] != '*') {

printf("该位置已经被排除!\n");

continue;

}

if (mine_board[x][y] == '1') {

break;

}

int num = GetNums(mine_board, ROW, COL, x, y);

show_board[x][y] = num + '0';

count--;

system("cls");

} while (count > 0);

if (count > 0) {

printf("你被炸死了!\n");

ShowBoard(mine_board, ROW, COL);

}

else {

printf("恭喜,你通过游戏!\n");

}

}

main.c

#include "game.h"

void Menu()

{

printf("###########################\n");

printf("## 1.Play 2. Exit ##\n");

printf("###########################\n");

printf("请输入# ");

}

void SetMines(char board[][COL], int row, int col)

{

int num = NUMS;

while (num) {

int x = rand() % 10 + 1;

int y = rand() % 10 + 1;

if (board[x][y] == '0') {

board[x][y] = '1';

num--;

}

}

}

int GetNums(char board[][COL], int row, int col, int x, int y)

{

return board[x - 1][y - 1] + board[x - 1][y] + \

board[x - 1][y + 1] + board[x][y + 1] + \

board[x + 1][y + 1] + board[x + 1][y] + \

board[x + 1][y - 1] + board[x][y - 1] - 8 * '0';

}

void ShowBoard(char board[][COL], int row, int col)

{

printf(" ");

for (int i = 1; i < col - 1; i++) {

printf(" %2d ", i);

}

printf("\n");

printf("-------------------------------------------\n");

for (int i = 1; i < row - 1; i++) {

printf("%2d|", i);

for (int j = 1; j < col - 1; j++) {

printf(" %c |", board[i][j]);

}

printf("\n");

printf("-------------------------------------------\n");

}

}

void Game()

{

system("cls");

srand((unsigned long)time(NULL));

char show_board[ROW][COL];

char mine_board[ROW][COL];

memset(show_board, '*', sizeof(show_board));

memset(mine_board, '0', sizeof(mine_board));

SetMines(mine_board, ROW, COL);

int count = (ROW - 2) * (COL - 2) - NUMS;

int x = 0;

int y = 0;

do {

ShowBoard(show_board, ROW, COL);

printf("请输入坐标# ");

scanf("%d %d", &x, &y);

if (x < 1 || x > ROW - 2 || y < 1 || y > COL - 2) {

printf("输入位置越界,请重新输入!\n");

continue;

}

if (show_board[x][y] != '*') {

printf("该位置已经被排除!\n");

continue;

}

if (mine_board[x][y] == '1') {

break;

}

int num = GetNums(mine_board, ROW, COL, x, y);

show_board[x][y] = num + '0';

count--;

system("cls");

} while (count > 0);

if (count > 0) {

printf("你被炸死了!\n");

ShowBoard(mine_board, ROW, COL);

}

else {

printf("恭喜,你通过游戏!\n");

}

}

更多有趣的经典小游戏实现专题,分享给大家:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多推荐

c语言设计扫雷游戏报告,C语言实现扫雷游戏简易版

本文发布于:2024-03-08 21:34:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1722462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语言   游戏   报告   简易版

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!