欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C语言实现扫雷游戏简易版

程序员文章站 2022-06-24 12:00:46
本文实例为大家分享了c语言实现扫雷游戏的简易版,供大家参考,具体内容如下game.h#pragma once#include #include

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

game.h

#pragma once
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>

#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++经典小游戏汇总

python经典小游戏汇总

python俄罗斯方块游戏集合

javascript经典游戏 玩不停

java经典小游戏汇总

javascript经典小游戏汇总

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