1. 程式人生 > >c語言編程掃雷遊戲

c語言編程掃雷遊戲

endif clu string.h %d lec show rand() warning define

頭文件:(test.h)

#ifndef _MINE_H_
#define _MINE_H_

#include <stdio.h>
#include <windows.h>
#include <string.h>
#include <time.h>//調用標準庫或系統中的頭文件

#pragma warning(disable:4996)//VS中解決scanf函數問題

#define ROW 12
#define COL 12
#define MINES 20//宏定義

void Menu();
void Game();
void SetMine(char mine_board[][COL], int row, int col);
void PlayGame(char show_board[][COL], char mine_board[][COL], int row, int col);
int GetMineNum(char mine_board[][COL], int i, int j);
void ShowBoard(char show_board[][COL], int row, int col);//函數聲明

#endif

源文件:(test.c)

#include "mine.h"

void Menu()//菜單
{
    printf("###########################################\n");
    printf("##    1. Play                  2. Exit   ##\n");
    printf("###########################################\n");
    printf("Please Select:> ");
}
static int GetIndex(int start, int end)//[1, 10]
{
    return rand() % (end - start + 1) + start;//隨機埋雷
}

void SetMine(char mine_board[][COL], int row, int col)
{
    srand((unsigned long)time(NULL));
    int mine_num = MINES;
    while (mine_num){
        int i_index = GetIndex(1, col - 2);
        int j_index = GetIndex(1, col - 2);
        if (mine_board[i_index][j_index] == ‘0‘){
            mine_board[i_index][j_index] = ‘1‘;
            mine_num--;
        }
    }
}

int GetMineNum(char mine_board[][COL], int i, int j)//確定選擇坐標值旁邊雷的個數
{
    //‘0 ‘ + ‘1‘+‘1‘+‘0‘...+‘0‘
    return mine_board[i - 1][j - 1] + mine_board[i - 1][j] +         mine_board[i - 1][j + 1] + mine_board[i][j - 1] +         mine_board[i][j + 1] + mine_board[i + 1][j - 1] +         mine_board[i + 1][j] + mine_board[i + 1][j + 1] - 8 * ‘0‘;
}

void ShowBoard(char show_board[][COL], int row, int col)//顯示界面
{
    int i = 1;
    int j = 1;
    printf("    ");
    for (; i <= col - 2; i++){
        printf("%d   ", i);
    }
    printf("\n");
    for (i = 1; i <= col - 1; i++){
        printf("----");
    }
    printf("\n");
    for (i = 1; i <= row - 2; i++){
        printf("%2d|", i);
        for (j = 1; j <= col - 2; j++){
            printf(" %c |", show_board[i][j]);
        }
        printf("\n");
        int k = 1;
        for (; k <= col - 1; k++){
            printf("----");
        }
        printf("\n");
    }
}
void PlayGame(char show_board[][COL], char mine_board[][COL], int row, int col)
{
    int i = 0;
    int j = 0;
    int total = (ROW - 2)*(COL - 2);//total還剩多少位置沒掃
    while (1){
        ShowBoard(show_board, row, col);
        printf("Please Enter Pos<x, y>: ");
        scanf("%d%d", &i, &j);
        if (i >= 1 && i <= row - 2 && j >= 1 && j <= col - 2){
            if (mine_board[i][j] == ‘0‘){
                int num = GetMineNum(mine_board, i, j);
                show_board[i][j] = num + ‘0‘;
                total--;
            }
            else{
                ShowBoard(mine_board, row, col);
                printf("Game Over! You Lose :(!\n");
                break;
            }
        }
        else{
            printf("Enter Error, Try Again!\n");
            continue;
        }
        if (total == MINES){
            printf("You Win:)\n");
            break;
        }
    }
}
void Game()
{
    char show_board[ROW][COL];//顯示界面
    char mine_board[ROW][COL];//雷區界面
    memset(show_board, ‘*‘, sizeof(show_board));//未掃區域用“*”表示
    memset(mine_board, ‘0‘, sizeof(mine_board));//掃完區域用“0”表示
    SetMine(mine_board, ROW, COL);//掃完區域賦值
    PlayGame(show_board, mine_board, ROW, COL);//遊戲過程
}

執行文件(main.c)

#include "mine.h"

int main()
{
    int select = 0;
    int quit = 0;
    while (!quit){
        Menu();
        scanf("%d", &select);
        switch (select){
        case 1:
            Game();
            break;
        case 2:
            printf("I Am Quit!\n");
            quit = 1;
            break;
        default:
            printf("Select Error! Try Again!\n");
            break;
        }
    }
    return 0;
}

c語言編程掃雷遊戲