初级扫雷小程序代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了初级扫雷小程序代码相关的知识,希望对你有一定的参考价值。
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#define ROW 9
#define COL 9
#define Easy_count 80
#define ROWS ROW+2
#define COLS COL+2
void InitBoard(char Board[ROWS][COLS], int rows, int cols,char set);
void Display(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);
#include "game.h"
void InitBoard(char Board[ROWS][COLS], int rows, int cols,char set)
{
int i, j;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols;j++)
{
Board[i][j] = set;
}
}
}
void Display(char Board[ROWS][COLS], int row, int col)
{
int i, j;
//打印列号
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}printf("
");
for (i = 1; i <= row; i++)
{
printf("%d ",i);
for (j = 1; j <= col; j++)
{
printf("%c ", Board[i][j]);
}
printf("
");
}
}
void Setmine(char Board[ROWS][COLS], int row, int col)
{
int count = Easy_count;
while (count)
{
int x = rand()% row + 1;
int y = rand() % col + 1;
if (Board[x][y] == ‘0‘)
{
Board[x][y] = ‘1‘;
count--;
}
}
}
int getmine(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, y;
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("很遗憾,死了
");
Display(mine, row, col);
break;
}
else
{
//非雷 计算周围几个雷
int count=getmine(mine, x, y);
show[x][y] = count + ‘0‘;
Display(show, row, col);
win++;
}
}
else
{
printf("输入非法重新输
");
}
}
if (win == row * col - Easy_count)
{
printf("恭喜你成功");
Display(mine, row, col);
}
}
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include "game.h"
void menu()
{
printf("********************************
");
printf("***1.玩游戏****2.退出***********
");
printf("********************************
");
}
void game()
{
//1 布置好的雷的信息
char mine[ROWS][COLS] = {0};
//2 排查出雷的信息
char show [ROWS][COLS] = { 0 };
//初始化
InitBoard(mine, ROWS, COLS, ‘0‘);
InitBoard(show, ROWS, COLS, ‘*‘);
//打印棋盘
//Display(mine, ROW, COL);
Display(show, ROW, COL);
//布置雷
Setmine(mine, ROW, COL);
Display(mine, ROW, COL);
//扫雷
findmine(mine,show,ROW,COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("请选择:");
scanf("%d", &input);
switch (input)
{
case 1:
game();
break;
case 2:
printf("退出游戏,结束
");
break;
default:
printf("选择错误,重新选择
");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
以上是关于初级扫雷小程序代码的主要内容,如果未能解决你的问题,请参考以下文章
[AndroidStudio]_[初级]_[配置自动完成的代码片段]