掃雷
掃雷游戲:游戲目標是在最短的時間內根據點擊格子出現的數字找出所有非雷格子,同時避免踩雷,踩到一個雷即全盤皆輸,

掃雷實作
- test.c - 掃雷游戲的測驗
- game.c -游戲函式的實作
- game.h -游戲函式的宣告
掃雷代碼
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()//列印游戲選單
{
printf("********************\n");
printf("***** 1.paly *****\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, '*');//'*'
//列印一下棋盤
DisplayBoard(show, ROW, COL);
//布置雷
SetMine(mine, 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 2:
printf("退出游戲");
break;
default:
printf("選擇錯誤,重新選擇\n");
break;
}
} while (input);
return 0;
}
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)//初始化陣列
{
int i= 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)//列印表格
{
int i = 0;
int j = 0;
printf("—··掃雷游戲··—\n");
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
printf("—··掃雷游戲··—\n");
}
void SetMine(char mine[ROWS][COLS], int row, int col)//布置雷
{
//布置10個雷
int count = EASY_COUNT;
while (count)
{
//生產隨機的下標
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
static int get_mine_count(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 = get_mine_count(mine, x, y);//不是雷情況下,統計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);//顯示被隱藏的坐標
}
}
game.h
#pragma once
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define EASY_COUNT 10//雷的個數
#define ROW 9//顯示的棋盤大小
#define COL 9
#define ROWS ROW+2//棋盤的總大小
#define COLS COL+2
//初始化棋盤
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 mine[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char shwo[ROWS][COLS], int row, int col);
關于掃雷,我想說,
真上頭!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/281722.html
標籤:其他
上一篇:Unity中的群組行為
