C語言實(shí)現(xiàn)掃雷游戲及其優(yōu)化
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)掃雷游戲及其優(yōu)化的具體代碼,供大家參考,具體內(nèi)容如下
關(guān)于掃雷優(yōu)化
1.核心思想:使用兩個二維數(shù)組進(jìn)行設(shè)計(jì),一個用于顯示,一個用于后臺雷的布置。
2.使用宏常量,后期可以任意修改游戲難度。
3.關(guān)于掃雷拓展模塊,目前使用的方法比較low,若周圍均沒有,則全部顯示。
4.剩余位置數(shù)使用全局變量count,必須考慮拓展之后count變化。
有待改進(jìn)之處
1.需設(shè)計(jì)標(biāo)記雷的步驟,增加用戶體驗(yàn)。
2.拓展方式有待改進(jìn)。
3.界面布局仍需要進(jìn)行優(yōu)化。
掃雷游戲代碼
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<string.h>
#include<time.h>
#define ROW 12
#define COL 12
#define MINE_NUM 15
#define TOTAL 100
#pragma warning(disable:4996)
int count = TOTAL;
void inter(){
printf("=======================\n");
printf("=======游戲菜單========\n");
printf("======1.開始游戲=======\n");
printf("========2.退出=========\n");
printf("=======================\n");
printf("請輸入您的選擇: \n");
}
int GetRandIndex(int start, int end){
return rand() % (end - start + 1) + start;
}
void layout(char mine[][COL], int row, int col){
srand((unsigned long)time(NULL));
int count = 0;
while (count<MINE_NUM){
int x = GetRandIndex(1, 10);
int y = GetRandIndex(1, 10);
if (mine[x][y] == '0'){
mine[x][y] = '1';
count++;
}
}
}
void Board(char board[][COL], int row, int col){
printf(" ");
int i = 1;
for (; i <= 10; i++)
{
printf(" %d ", i);
}
printf("\n----");
for (i = 1; i <= 29; i++)
{
printf("-");
}
printf("\n");
for (i = 1; i <= 10; i++)
{
printf("%2d|",i);
int j = 1;
for (; j <= 10; j++){
printf(" %c|", board[i][j]);
}
printf("\n");
int k = 1;
for (k = 1; k <= 11; k++)
{
printf("---");
}
printf("\n");
}
}
char GetMines(char mine[][COL],int row,int col){
return mine[row - 1][col - 1] + mine[row - 1][col] + mine[row - 1][col + 1]\
+ mine[row][col - 1] + mine[row][col + 1]\
+ mine[row + 1][col - 1] + mine[row + 1][col] + mine[row +1][col + 1]-7*'0';
}
void expand(char mine[ROW][COL], char board[ROW][COL], int x, int y){
if ((x >= 1) && (y >= 1) && (x <= ROW) && (y <= COL))
{
if (GetMines(mine, x, y) == '0')
{
if (x > 1 && x < 10 && y>1 && y < 10)
{
count = count - 8;
}
else if((x==1&&y==1)||(x==10&&y==10) || (x == 1 && y == 10) || (x == 10 && y == 1)) {
count -= 3;
}
else {
count -= 5;
}
board[x - 1][y - 1] = GetMines(mine, x-1, y-1);
board[x - 1][y] = GetMines(mine, x - 1, y);
board[x - 1][y + 1] = GetMines(mine, x - 1, y + 1);
board[x][y - 1] = GetMines(mine, x , y - 1);
board[x][y + 1] = GetMines(mine, x , y + 1);
board[x + 1][y - 1] = GetMines(mine, x + 1, y - 1);
board[x + 1][y] = GetMines(mine, x + 1, y);
board[x + 1][y + 1] = GetMines(mine, x + 1, y + 1);
}
}
}
void Game(){
char mine[ROW][COL];
char board[ROW][COL];
memset(mine,'0',sizeof(mine));
memset(board, '*', sizeof(board));
layout(mine, ROW, COL);
Board(mine, ROW, COL);
int x = 0;
int y = 0;
while (1){
int i = 0;
Board(board, ROW, COL);
printf("請選擇您要排除的位置: ");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= ROW - 2 && y >= 1 && y <= COL - 2){
if (mine[x][y] == '0'){
char num = GetMines(mine,x,y);
board[x][y] = num;
expand(mine, board, x, y);
Board(board, ROW, COL);
count--;
if (count == MINE_NUM)
{
Board(board, ROW, COL);
printf("你贏了!\n");
break;
}
}
else{
printf("您輸了!\n");
Board(mine, ROW, COL);
break;
}
printf("還有%d個位置 \n", count);
}
else{
printf("你輸入的坐標(biāo)有誤,請重新輸入!\n");
}
}
}
int main(){
int quit = 0;
int select = 0;
while (!quit){
inter();
scanf("%d", &select);
switch (select)
{
case 1:
Game();
Sleep(5000);
system("cls");
break;
case 2:
printf("再見!\n");
quit = 1;
break;
default:
printf("您的輸入不正確,請重新輸入!\n");
break;
}
}
system("pause");
return 0;
}
相關(guān)運(yùn)行樣例
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
欄 目:C語言
下一篇:opencv攝像頭捕獲識別顏色
本文標(biāo)題:C語言實(shí)現(xiàn)掃雷游戲及其優(yōu)化
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/244.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求


