C语言扫雷代码(蹦蹦炸弹)(下)

简介: C语言扫雷代码(蹦蹦炸弹)(下)

综合上节我们对于扫雷代码的分析与代码的尝试

我们就写出了下面的扫雷代码

game.h

#define _CRT_SECURE_NO_WARNINGS

#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_court 10 //雷的个数

//初始化棋盘

void initboard1(char mine[ROWS][COLS], int rows, int cols, int set);

void initboard2(char show[ROWS][COLS], int rows, int cols, int set);

//布置雷

void set(char mine[ROW][COL],int row,int col);

//棋盘打印

void displayboard(char arr[ROWS][COLS],int row,int col);

//排查雷

void findmine(char mine[ROWS][COLS],char show[ROWS][COLS], int row, int col);

game.c

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include"game.h"

//棋盘初始化

void initboard1(char mine[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++) {

           mine[i][j] = set;

       }

   }

}

void initboard2(char show[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++) {

           show[i][j] = set;

       }

   }

}

//布置雷

void set(char mine[ROW][COL], int row, int col)

{

   int court = easy_court;

   while (court) {

       //产生1~9的数字

       int x = rand() % 9 + 1;

       int y = rand() % 9 + 1;

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

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

           court--;

       }

   }

}

//打印棋盘

   void displayboard(char arr[ROWS][COLS], int row, int col)

   {

   printf("--------扫雷游戏--------\n");

   int i = 0;

   for (i = 0; i < row+1; i++) {

       printf("%d ", i);

   }

   printf("\n");

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

       printf("%d ", i);

       int j = 0;

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

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

       }

       printf("\n");

   }

}

   int get_mine_court(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][y+1] + 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 a=0;

       int b=0;

       int win = 0;

       while (win < row * col - easy_court)

       {

           printf("请输入你要排查的坐标:>");

           scanf("%d%d", &a, &b);

           system("cls");

           if (a >= 1 && a <= row && b >= 1 && b <= col)

           {

               if (mine[a][b] == '1')

               {

                   printf("很遗憾,你被炸死了\n");

                   displayboard(mine, ROW, COL);

                   break;

               }

               else

               {

                   int court_ = get_mine_court(mine, ROW, COL);

                   show[a][b] = court_+'0';

                   displayboard(show, ROW, COL);

                   win++;

               }

           }

           else {

               printf("输入错误,请重新输入\n");

           }

           if (win == row * col - easy_court) {

               printf("恭喜你,排雷成功\n");

               displayboard(mine, ROW, COL);

           }

       }

}

test.c

#define _CRT_SECURE_NO_WARNINGS

#include<stdio.h>

#include"game.h"

void menu()

{

   printf("***********************\n");

   printf("********1.play*********\n");

   printf("********0.exit*********\n");

   printf("***********************\n");

   printf("请选择:>");

}

void play()

{

   //扫雷

   char mine[ROWS][COLS];//数组初始化全部为‘0’;

   char show[ROWS][COLS];//数组初始化全部为‘*’

   //初始化棋盘

   initboard1(mine,ROWS,COLS,'0');

   initboard2(show,ROWS,COLS,'*');

   //布置雷

   set(mine,ROW,COL);

   //打印棋盘

   //displayboard(mine,ROW,COL);

   displayboard(show,ROW,COL);

   //排查雷

   findmine(mine,show, ROW, COL);

}

void game()

{

   int input;

   srand((unsigned int)time(NULL));

   do

   {

       menu();

       scanf("%d", &input);

       switch (input)

       {

       case 1:

           printf("扫雷\n");

           play();

           break;

       case 0:

           printf("游戏结束,退出游戏\n");

           break;

       default:

           printf("输入错误,请重新输入\n");

           break;

       }

   } while (input);

}

int main()

{

   game();

   return 0;

}


相关文章
|
20天前
|
存储 数据可视化 C语言
【C语言】C语言 学生成绩管理系统(源码+报告)【千行代码】【独一无二】
【C语言】C语言 学生成绩管理系统(源码+报告)【千行代码】【独一无二】
|
11天前
|
NoSQL 编译器 程序员
【C语言】揭秘GCC:从平凡到卓越的编译艺术,一场代码与效率的激情碰撞,探索那些不为人知的秘密武器,让你的程序瞬间提速百倍!
【8月更文挑战第20天】GCC,GNU Compiler Collection,是GNU项目中的开源编译器集合,支持C、C++等多种语言。作为C语言程序员的重要工具,GCC具备跨平台性、高度可配置性及丰富的优化选项等特点。通过简单示例,如编译“Hello, GCC!”程序 (`gcc -o hello hello.c`),展示了GCC的基础用法及不同优化级别(`-O0`, `-O1`, `-O3`)对性能的影响。GCC还支持生成调试信息(`-g`),便于使用GDB等工具进行调试。尽管有如Microsoft Visual C++、Clang等竞品,GCC仍因其灵活性和强大的功能被广泛采用。
42 1
|
17天前
|
存储 C语言
【C语言】基础刷题训练4(含全面分析和代码改进示例)
【C语言】基础刷题训练4(含全面分析和代码改进示例)
|
24天前
|
人工智能 Rust 安全
DARPA计划“消灭”C语言代码
为了加速软件业向内存安全编程语言的过渡,美国国防高级研究计划局(DARPA)正在推动一个名为TRACTOR的人工智能代码转换工具,可自动将遗留C代码转换为Rust,以根治内存安全问题。TRACTOR是TRanslating All C TO Rust的缩写,即“将所有C语言代码转换为Rust语言”。
DARPA计划“消灭”C语言代码
|
23天前
|
C语言
扫雷(C语言)
扫雷(C语言)
25 4
|
2月前
|
存储 编译器 C语言
|
17天前
|
C语言
【C语言刷题训练】——第7节(含代码与分析思路)
【C语言刷题训练】——第7节(含代码与分析思路)
|
17天前
|
测试技术 C语言 C++
【C语言刷题训练——6】鹏哥C语言刷题训练营笔记,含代码讲解改进
【C语言刷题训练——6】鹏哥C语言刷题训练营笔记,含代码讲解改进
|
17天前
|
存储 C语言
【C语言】鹏哥C语言刷题训练营——第5节内容笔记(含代码全面分析和改进,讲解)
【C语言】鹏哥C语言刷题训练营——第5节内容笔记(含代码全面分析和改进,讲解)
|
3月前
|
C语言
C语言实现猜数字游戏:代码详解与函数解析
C语言实现猜数字游戏:代码详解与函数解析
29 0