利用C语言实现扫雷游戏

通过一段时间的C语言学习,想必小伙伴们也想跃跃欲试的编写一些小程序,这个扫雷简易游戏,非常适合C语言初学者去实践。

实现扫雷,首先要有两个棋盘,一个棋盘放置着雷的信息,另个用于展示到屏幕上;然后就是玩家输入坐标排雷,如果输入的坐标有雷,就游戏结束,要是没有雷就在展示棋盘对应的位置,显示出周边雷的个数;最后排查完所有坐标并且不被雷炸死,就是胜利。

以上是扫雷的基本逻辑,现在开始,一步一步的实现。

1.初始化棋盘

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set){ int i = 0; for (i = 0; i < rows; i++) {  int j = 0;  for (j = 0; j < cols; j++)  {   board[i][j] = set;  } }}

mine就是放雷的棋盘,show就是展示出来棋盘。我们把mine都初始化成‘0’,雷为‘1’,show

都初始化为‘*’。

2.布置雷

void SetMine(char board[ROWS][COLS], int row, int col){ int count = EASY_COUNT;//10个雷 while (count) {  //1. 生成随机下标  int x = rand() % row + 1;  int y = rand() % col + 1;  if (board[x][y] != '1')  {   board[x][y] = '1';   count--;  } }}

3.排查雷

排查雷是本游戏关键,也稍有难度,首先,需要将俩个棋盘都传过去,每次输入坐标都会去雷盘里找,如果是雷就游戏结束,不是雷就会使对应的展示盘的位置 展现出周围的雷数。

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col){ int x = 0; int y = 0; int win = 0; while (win<row*col - EASY_COUNT) {  printf("请输入要排查的坐标:>");  scanf("%d %d", &x, &y);  if (x >= 1 && x <= row && y >= 1 && y <= col)  {   if (mine[x][y] == '1')   {    printf("很遗憾,你被炸死了\n");    DisplayBoard(mine, ROW, COL);    break;   }   else   {    int count = GetMineCount(mine, x, y);    show[x][y] = count + '0';    DisplayBoard(show, ROW, COL);    win++;   }  }  else  {   printf("坐标非法,重新输入\n");  } } if (win == row * col - EASY_COUNT) {  printf("恭喜你,排雷成功\n");  DisplayBoard(mine, ROW, COL); }}

看(x,y)周围雷数的函数:

int GetMineCount(char mine[ROWS][COLS], int x, int y){ return (mine[x - 1][y] +  mine[x - 1][y - 1] +  mine[x][y - 1] +  mine[x + 1][y - 1] +  mine[x + 1][y] +  mine[x + 1][y + 1] +  mine[x][y + 1] +  mine[x - 1][y + 1] - 8 * '0');}

上面使扫雷游戏一步一步完成的步骤,接下来是游戏的运行代码:

1.头文件

#include <stdio.h>#include <stdlib.h>#include <time.h>#define ROW 9#define COL 9#define ROWS ROW+2#define COLS COL+2#define EASY_COUNT 10//初始化棋盘void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);//显示棋盘void DisplayBoard(char board[ROWS][COLS], int row, int col);//布置雷void SetMine(char board[ROWS][COLS], int row, int col);//排查雷的void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

2.游戏代码

#include "game.h"void InitBoard(char board[ROWS][COLS], int rows, int cols, char set){ int i = 0; for (i = 0; i < rows; i++) {  int j = 0;  for (j = 0; j < cols; j++)  {   board[i][j] = set;  } }}void DisplayBoard(char board[ROWS][COLS], int row, int col){ int i = 0; printf("------------------------\n"); for (i = 0; i <= 9; i++) {  printf("%d ", i); } printf("\n"); for (i = 1; i <= row; i++) {  int j = 0;  printf("%d ", i);  for (j = 1; j <= col; j++)  {   printf("%c ", board[i][j]);  }  printf("\n"); } printf("------------------------\n");}void SetMine(char board[ROWS][COLS], int row, int col){ int count = EASY_COUNT; while (count) {  //1. 生成随机下标  int x = rand() % row + 1;  int y = rand() % col + 1;  if (board[x][y] != '1')  {   board[x][y] = '1';   count--;  } }}int GetMineCount(char mine[ROWS][COLS], int x, int y){ return (mine[x - 1][y] +  mine[x - 1][y - 1] +  mine[x][y - 1] +  mine[x + 1][y - 1] +  mine[x + 1][y] +  mine[x + 1][y + 1] +  mine[x][y + 1] +  mine[x - 1][y + 1] - 8 * '0');}void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col){ int x = 0; int y = 0; int win = 0; while (win<row*col - EASY_COUNT) {  printf("请输入要排查的坐标:>");  scanf("%d %d", &x, &y);  if (x >= 1 && x <= row && y >= 1 && y <= col)  {   if (mine[x][y] == '1')   {    printf("很遗憾,你被炸死了\n");    DisplayBoard(mine, ROW, COL);    break;   }   else   {    int count = GetMineCount(mine, x, y);    show[x][y] = count + '0';    DisplayBoard(show, ROW, COL);    win++;   }  }  else  {   printf("坐标非法,重新输入\n");  } } if (win == row * col - EASY_COUNT) {  printf("恭喜你,排雷成功\n");  DisplayBoard(mine, ROW, COL); }}

3.总程序

#include "game.h"void menu(){ printf("********************************\n"); printf("*********   1. play     ********\n"); printf("*********   0. exit     ********\n"); printf("********************************\n");}void game(){ char mine[ROWS][COLS] = { 0 };//存放雷的信息 char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息 //初始化一下棋盘 InitBoard(mine, ROWS, COLS, '0');//'0' InitBoard(show, ROWS, COLS, '*');//'*' //布置雷 SetMine(mine, ROW, COL); DisplayBoard(show, ROW, COL); //排查雷 FindMine(mine, show, ROW, COL);}int main(){ int input = 0; srand((unsigned int)time(NULL)); do {  menu();  printf("请选择:>");  scanf("%d", &input);  switch (input)  {  case 1:   game();   break;  case 0:   printf("退出游戏\n");   break;  default:   printf("选择错误,重新选择!\n");   break;  } } while (input); return 0;}

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

没有口水与汗水,就没有成功的泪水。

利用C语言实现扫雷游戏

相关文章:

你感兴趣的文章:

标签云: