文章目錄
- 前言
- 掃雷優化版
- game.h
- game.c
- test.c
前言
有點累,所以說的話可能比較消極,希望大家諒解,
經過我的掙扎,因為本人能力較弱,掙扎了一下午才把優化的掃雷寫出來,寫完之后反而是一身疲憊,況且之前一直再寫博客,消磨的精力
實在是太多了,所以一時沒有想發的欲望,
不過為了給大家做一個小小參考(本人已經除錯程序式,未發現錯誤),在這里也就發給小伙伴瀏覽閱讀,有錯誤也請大家指正,
另外啊,其實提高代碼能力的方法是有的,就是要獨立思考與撰寫,換句話來說就是,離開你的參考答案,自己獨立撰寫出來,跟著代碼寫是有好處的,但是不夠,完完全全不夠,必須得自己寫出來,這才是屬于你自己的東西,也希望大家謹記,
掃雷優化版
跟之前一樣,只展示相關代碼,具體內容如下:
game.h
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define COUNT 10
enum Option
{
Exit,
Play
};
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);
game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
int i = 0;
int j = 0;
int ret_open = 0;
static int Get_Mine_Number(char board[ROWS][COLS], int x, int y)
{
int sum = 0;
for (i = x - 1; i <= x + 1; i++)
{
for (j = y - 1; j <= y + 1; j++)
{
sum += (board[i][j] - '0');
}
}
return (int)sum;
}
static void Mine_Open(char mine[ROWS][COLS],
char show[ROWS][COLS],
char mark[ROWS][COLS],
int x, int y,int* n)
{
int k = 0;
int l = 0;
if (Get_Mine_Number(mine, x, y) == 0 &&
mine[x][y] != '1' &&
mark[x][y] != '1' &&
x != 0 &&
x != 10 &&
y != 0 &&
y != 10)
{
mark[x][y] = '1';
for (k = x - 1; k <= x + 1; k++)
{
for (l = y - 1; l <= y + 1; l++)
{
if (k == x && l == y)
{
continue;
}
Mine_Open(mine, show, mark, k, l, n);
}
}
}
ret_open = Get_Mine_Number(mine, x, y);
show[x][y] = ret_open + '0';
(*n)++;
//會出現死遞回,需要把遍歷過的位置做標記,不太想寫了,這里,但經過掙扎還是寫了
}
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
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)
{
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 board[ROWS][COLS], int row, int col)
{
int count = COUNT;
while (count--)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (board[x][y] == '1')
{
count++;
}
else
{
board[x][y] = '1';
}
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
char mark[ROWS][COLS] = { 0 };//掃雷展開使用,給已經遍歷的過地方做標記,也就是給周圍都沒有雷的地方做了標記
int option = 0;
int x = 0;
int y = 0;
int count = 0;
int flag = 1;
int ret = 0;
while (flag && count < row*col - COUNT)
{
printf("請選擇: 1、標記 2、掃雷\n");
scanf("%d", &option);
switch (option)
{
case 1:
printf("輸入下標:");
scanf("%d %d", &x, &y);
show[x][y] = '#';
DisplayBoard(show, ROW, COL);
break;
case 2:
printf("輸入下標:");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= 9 && y >= 1 && y <= 9)
{
if (mine[x][y] == '1')
{
printf("很遺憾,你已經猜到地雷了\n");
DisplayBoard(mine, ROW, COL);
flag--;
}
else
{
//ret = Get_Mine_Number(mine, x, y);
//show[x][y] = ret + '0';
//DisplayBoard(show, ROW, COL);
//count++;
//優化如下
Mine_Open(mine, show, mark, x, y, &count);
DisplayBoard(show, ROW, COL);
}
}
else
{
printf("輸入錯誤,請重試\n");
}
if (count == row*col - COUNT)
{
printf("游戲通關\n");
}
break;
default:
printf("輸入錯誤,請重試\n");
break;
}
}
}
test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
extern int i, j;
void menu()
{
printf("**********************\n");
printf("***** 1.Play *****\n");
printf("***** 0.Exit *****\n");
printf("**********************\n");
printf("請選擇:");
}
void game()
{
char mine[ROWS][COLS] = { 0 };//布置雷
char show[ROWS][COLS] = { 0 };//顯示
//初始化棋盤和列印棋盤
InitBoard(mine, ROWS, COLS, '0');
SetMine(mine, ROW, COL);
DisplayBoard(mine, ROW, COL);//這里可以刪掉,就可以不用展示內層的實際埋雷情況
InitBoard(show, ROWS, COLS, '*');
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL);
//正式開始
}
int main()
{
srand((unsigned int)time(NULL));
int input = 0;
do
{
menu();
scanf("%d", &input);
switch (input)
{
case Play:
printf("掃雷\n");
game();
break;
case Exit:
printf("退出游戲\n");
break;
default:
printf("選擇錯誤,請重新選擇\n");
break;
}
} while (input);
return 0;
}
有問題,希望大家批評指正,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/281727.html
標籤:其他
