目錄
一、選單模塊
?
二、game函式
1、函式宣告及行數列數雷數
2、初始化雷陣
3、列印雷陣
4、布置雷陣
5、計算雷數
6、排雷
7、game函式
三、全部代碼
1、main.c
2、game.h
3、game.c
一、選單模塊
選單與井字棋類似都是先列印選單然后用一個do……while回圈嵌套switch選擇玩游戲或著退出游戲,如果選擇錯誤則提示輸入錯誤,讓用戶重新選擇
#include"game.h"
void menu()
{
printf("**************************************\n");
printf("************** 1. play ************\n");
printf("**************************************\n");
printf("************** 0. exit ************\n");
printf("**************************************\n");
}
void test()
{
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1 :
game();
break;
case 0 :
exit(0);
break;
default:
printf("選擇錯誤,重新選擇\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
二、game函式
1、函式宣告及行數列數雷數
#include<stdio.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 rows, int cols);//列印棋盤
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、初始化雷陣
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
3、列印雷陣
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0, j = 0;
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");
}
}

為方便輸入要排雷的坐標在第一行列印列數,第一列列印行數
4、布置雷陣
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--;
}
}
}
用count記錄雷數當布置夠則跳出回圈,結束函式
x,y在行列數以內隨機取值,如果沒有雷就布置雷
為正確生成亂數在test函式中加入
srand((unsigned int)time(NULL));
5、計算雷數
int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
+ mine[x][y - 1] + mine[x][y + 1] - 8 * '0'
+ mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1];
}
如果輸入的坐標正確,計算周圍八個坐標中有幾個雷
因為初始化時在有雷的地方,寫為字符一,沒雷的地方為字符零,所以加起來減去八個字符零,就是雷的個數
6、排雷
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 n = get_mine_count(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("輸入錯誤,請重新輸入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("你贏了,排雷成功!\n");
}
}
若輸入的坐標x,y超出行與列的范圍則輸入錯誤,讓玩家重新輸入
若輸入的坐標x,y位置處為字符一,則踩雷輸掉游戲,再列印出雷陣讓玩家“瞑目”
若輸入的坐標x,y位置處為字符零,預先準備一個計數器,計算排雷的數量,當數量等于所有的格子減雷的數量則排雷完成
7、game函式
void game()
{
char mine[ROWS][COLS] = { 0 };//存放布雷位置
char show[ROWS][COLS] = { 0 };//存放排雷資訊
InitBoard(mine, ROWS, COLS, '0');//初始化mine為0
InitBoard(show, ROWS, COLS, '*');//初始化show為0
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
首先準備兩個陣列,一個存放雷的位置,一個存放排雷的資訊
將mine陣列全部初始化為零,show全部初始化為*
先將棋盤列印一下,然后開始排雷
三、全部代碼
1、main.c
#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
printf("**************************************\n");
printf("************** 1. play ************\n");
printf("**************************************\n");
printf("************** 0. exit ************\n");
printf("**************************************\n");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//存放布雷位置
char show[ROWS][COLS] = { 0 };//存放排雷資訊
InitBoard(mine, ROWS, COLS, '0');//初始化mine為0
InitBoard(show, ROWS, COLS, '*');//初始化show為0
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
}
void test()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case 1 :
game();
break;
case 0 :
exit(0);
break;
default:
printf("選擇錯誤,重新選擇\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
2、game.h
#pragma once
#include<stdio.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 rows, int cols);//列印棋盤
void SetMine(char board[ROWS][COLS], int row, int col);//布置雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);//排查雷
3、game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
int i = 0, j = 0;
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");
}
}
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 get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
+ mine[x][y - 1] + mine[x][y + 1] - 8 * '0'
+ mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1];
}
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 n = get_mine_count(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("輸入錯誤,請重新輸入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("你贏了,排雷成功!\n");
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/357212.html
標籤:其他
上一篇:c語言實作掃雷(簡易版)
下一篇:C語言實作初級掃雷

