掃雷遊戲

//Main.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "game.h"

void Menu();
int main()
{

    Menu();
    system("pause");
    return 0;
}

void Menu()
{
    int count=10;
    int n=0;
    int choose=0;
    char ch='Y';
start:  printf("\n**********歡迎進入掃雷遊戲!**********\n");
    printf("\n1->進入遊戲  2->遊戲說明  3->遊戲設置  0->退出遊戲\n");
    printf("\n輸入選擇:");
    scanf("%d",&choose);
        switch(choose)
        {
            while(ch=='Y'||ch=='Y')
         {
            case 0:
                system("cls");
                printf("\n************退出遊戲!************\n");
                ch='N';
                break;
            case 1:
                system("cls");
                while(ch=='Y'||ch=='y')
                {
                printf("\n************遊戲開始!************\n");
                Game(count);//遊戲實現函數
                getchar();
                printf("是否繼續遊戲! Y or N \n");
                printf("輸入選擇:");
                scanf("%c",&ch);
                }
                system("cls");
                printf("\n************退出遊戲!************\n");
                break;
            case 2:
                system("cls");
                printf("\n************遊戲說明!************\n");
                printf("\n*搜索非雷的位置,直至找出所有的非雷區!\n");
                printf("\n*找到雷區,則失敗!\n");
                printf("\n*可以設置雷的數目!\n");
                getchar();
                printf("\n是否繼續遊戲! Y or N \n");
                printf("輸入選擇:");
                scanf("%c",&ch);
                system("cls");
                if(ch=='Y'||ch=='y')
                goto start;
                printf("\n************退出遊戲!************\n");
                break;
            case 3:
                printf("\n輸入雷的數目(1~99):");
                scanf("%d",&n);
                getchar();
                printf("是確認! Y or N \n");
                printf("輸入選擇:");
                scanf("%c",&ch);
                system("cls");
                if(ch=='Y'||ch=='y')
                count=n;
                goto start;

            default:
                system("cls");
                getchar();
                printf("輸入錯誤!\n");
                printf("是否繼續遊戲! Y or N \n");
                printf("輸入選擇:");
                scanf("%c",&ch);
                system("cls");
                if(ch=='Y'||ch=='y')
                {
                goto start;
                }
                printf("\n************退出遊戲!************\n");
                break;
        }
    }

}

//game.h

#ifndef __GAME_H__
#define __GAME___

#endif __GAME_H__
#define Column 10
#define Line 10

void Game(const int count);//遊戲
void Board(const int column,const int line,char arr2[][Line+2]);//打印棋盤
int Position(const int count,const int column,const int line,char arr1[][Line+2]);//設雷
int Choose(const int count,const int column,const int line,char arr1[][Line+2],char arr2[][Line+2]);//位置選擇
static void print(const int column,const int line,char arr1[][Line+2],char arr2[][Line+2]);//輸出管理
void Board1(const int column,const int line,char arr1[][Line+2]);//打印雷區
static int num(const int count,const int column,const int line,char arr2[][Line+2]);//判斷是否贏了

//game.c

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"game.h"



void Game(const int count)
{
    char arr1[Column+2][Line+2]={' '};//存放雷(#)
    char arr2[Column+2][Line+2];//存放雷數,和打印所選的位置(空格),0,column+2行和列用於計算不打印
    //memset(arr,0,sizeof(arr));//數組所有值替換函數memset(數組名,替換數,sizeof(數組名))
    memset(arr2,'*',sizeof(arr2));//給數組所有元素初始化
    Position(count,Column,Line,arr1);//佈雷

    while(1)
    {
        system("cls");
        printf("%c  %c\n",arr1[10][5],arr1[9][5]);
        Board(Column,Line,arr2);
        if(0==Choose(count,Column,Line,arr1,arr2))//0表示失敗,
        {
         printf("\t失敗!\n\n");
         Board1(Column,Line,arr1);//打印雷區
         break;
        }
        else if(num(count,Column,Line,arr2))//非0表示成功
        {
            printf("\t成功!\n\n");
            Board(Column,Line,arr2);//打印結果
            break;
        }

    }

}

void Board(const int column,const int line,char arr2[][Line+2])
{
    int i=0;
    printf("\t  -1 -2- 3 -4 -5 -6 -7 -8 -9 -10- \n");
    for(i=1;i<=column;i++)
    {
        printf("\t%2d|%c |%c |%c |%c |%c |%c |%c |%c |%c |%c |\n",i,arr2[i][1],arr2[i][2],
        arr2[i][3],arr2[i][4],arr2[i][5],arr2[i][6],arr2[i][7],arr2[i][8],arr2[i][9],arr2[i][10]);
        printf("\t  |--|--|--|--|--|--|--|--|--|--|\n");
    }
}

int Position(const int count,const int column,const int line,char arr1[][Line+2])
{
    int i=0;
    int x=0;
    int y=0;

    while(i<count)
    {
        x=rand()%column+1;
        y=rand()%line+1;
        if(arr1[x][y]!='#')
        {
            arr1[x][y]='#';
            i++;         
        }
    }       
}

int Choose(const int count,const int column,const int line,char arr1[][Line+2],char arr2[][Line+2])
{
    int x=0;
    int y=0;
    int i=0;
    int j=0;
    static int n=1;//標記第一次

    while(1)//用於檢驗輸入是否正確
    {
        printf("\n\t輸入位置(x,y):");
        scanf("%d %d",&x,&y);
        printf("\n");
        if(x<1||x>column||y<1||y>line)
        {
          printf("重新輸入!\n");
        }
        else
            break;
    }
    i=x;
    j=y;
    while(arr1[i][j]=='#'&&n==1)//保證第一次不死
    {
        arr1[x][y]=' ';
        i=rand()%column+1;
        j=rand()%line+1;
        if(arr1[i][j]=='#')//重新位置爲雷,重新尋找位置
         continue;
        else
        {
        arr1[i][j]='#';
        break;
        }
    }
    n++;

    while(1)
    {
        if(arr1[x][y]=='#')
        {
            return 0;//失敗,雷
        }
        else if(arr1[x][y]!='#')
        {
            print(x,y,arr1,arr2);
            if(x-1!=1&&y-1!=1)
            print(x-1,y-1,arr1,arr2);
            if(x+1!=column&&y+1!=line)
            print(x+1,y+1,arr1,arr2);
            return 1;//未失敗,非雷
        }
    }
}

static void print(const int x,const int y,char arr1[][Line+2],char arr2[][Line+2])
{
    int n=0;
    int i=0;
    int j=0;

    if(arr1[x][y]!='#')
    {
        for(i=x-1;i<=x+1;i++)
        {
            for(j=y-1;j<=y+1;j++)
            {
                if(arr1[i][j]=='#')
                {
                    n++;
                }
                /*else if ((arr2[i][y-1]!='#'&&arr2[i][y]!='#'&&arr2[i][y+1]!='#')||
                         (arr2[y-1][i]!='#'&&arr2[y][i]!='#'&&arr2[y+1][i]!='#'))
                         arr2[i][j]=' ';*/
                else if ((arr2[i][y-1]=='*'&&arr2[i][y]=='*'&&arr2[i][y+1]=='*')||
                         (arr2[y-1][i]=='*'&&arr2[y][i]=='*'&&arr2[y+1][i]=='*'))
                         arr2[i][j]=' ';
            }
        }
        arr2[x][y]=n+'0';
    }
}

void Board1(const int column,const int line,char arr1[][Line+2])
{
    int i=0;
    printf("\t  -1 -2- 3 -4 -5 -6 -7 -8 -9 -10- \n");
    for(i=1;i<=column;i++)
    {
        printf("\t%2d|%c |%c |%c |%c |%c |%c |%c |%c |%c |%c |\n",i,arr1[i][1],arr1[i][2],
        arr1[i][3],arr1[i][4],arr1[i][5],arr1[i][6],arr1[i][7],arr1[i][8],arr1[i][9],arr1[i][10]);
        printf("\t  |--|--|--|--|--|--|--|--|--|--|\n");
    }
}

static int num(const int count,const int column,const int line,char arr2[][Line+2])
{
    int i=0;
    int j=0;
    int n=0;

    for(i=1;i<=column;i++)
    {
        for(j=1;j<=line;j++)
        {
            if(arr2[i][j]=='*')
                n ++; 
        }
    }
        if(n==count)
        {
            return 1;
        }
        else
            return 0;
}
發佈了43 篇原創文章 · 獲贊 18 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章