C語言編寫掃雷小程序
本文實(shí)例為大家分享了C語言實(shí)現(xiàn)掃雷小程序的具體代碼,供大家參考,具體內(nèi)容如下
首先創(chuàng)建一個項(xiàng)目,建立一個頭文件game.h,兩個源文件game.c和test.c
game.h代碼片:
#ifndef ?__GAME_H__ #define ?__GAME_H__ #include<stdio.h> #include<stdlib.h> #include<string.h> #define ROWS 10//行數(shù) #define COLS 10//列數(shù) #define MINE 20//雷數(shù) void Init_board(char arr[ROWS+2][COLS+2],int rows,int cols, char a); void Is_show(char arr[ROWS+2][COLS+2], int rows, int cols); void Set_mine(char arr[ROWS + 2][COLS + 2], int rows, int cols); void Over_board(char arr[ROWS + 2][COLS + 2], int rows, int cols); #endif
test.c代碼片:
#include"game.h"
#include<time.h>
void menu() ? //打印菜單欄
{
? ? int i = 0;
? ? int j = 0;
? ? for (i = 0; i < 5; i++)
? ? {
? ? ? ? if (i == 2)
? ? ? ? {
? ? ? ? ? ? printf(" ? 1.Play ? ? ? 0.Exit ? ?");
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? for (j = 0; j < COLS * 3; j++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? printf("%c", 3);
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? printf("\n");
? ? }
}
int ?get_mine(char arr[ROWS + 2][COLS + 2], int x, int y)//用于清空沒有雷區(qū)的地方
{
? ? return (arr[x - 1][y] - '0')
? ? ? ? + (arr[x - 1][y - 1] - '0')
? ? ? ? + (arr[x - 1][y + 1] - '0')
? ? ? ? + (arr[x][y - 1] - '0')
? ? ? ? + (arr[x][y + 1] - '0')
? ? ? ? + (arr[x + 1][y - 1] - '0')
? ? ? ? + (arr[x + 1][y + 1] - '0')
? ? ? ? + (arr[x + 1][y] - '0');
}
void game() ?//玩游戲函數(shù)
{
? ? char mine[ROWS+2][COLS+2] = { 0 };
? ? char show[ROWS+2][COLS+2] = { 0 };
? ? int win = 1;
? ? srand((unsigned int)time(NULL));
? ? Init_board(mine, ROWS + 2, COLS + 2, '0');
? ? Init_board(show, ROWS + 2, COLS + 2, '*');
? ? /*Is_show(mine, ROWS + 2, COLS + 2);
? ? Is_show(show, ROWS + 2, COLS + 2);*/
? ? //打印雷區(qū)棋盤 方便調(diào)試
? ? Set_mine(mine, ROWS + 2, COLS + 2);
? ? /*Is_show(mine, ROWS + 2, COLS + 2);*/
? ? Is_show(show, ROWS + 2, COLS + 2);
? ? while (win < ROWS*COLS - MINE)
? ? {
? ? ? ? int x = 0;
? ? ? ? int y = 0;?
? ? ? ? printf("請選擇>:");
? ? ? ? scanf("%d%d", &x, &y);
? ? ? ? if ((x >= 1) && (x <= 10) && (y >= 1) && (y <= 10))
? ? ? ? {
? ? ? ? ? ? if (mine[x][y] == '1')
? ? ? ? ? ? {
? ? ? ? ? ? ? ? printf("你輸了!\n");
? ? ? ? ? ? ? ? Over_board(mine, ROWS + 2, COLS + 2);
? ? ? ? ? ? ? ? Is_show(mine, ROWS + 2, COLS + 2);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? int count = get_mine(mine, x, y);
? ? ? ? ? ? ? ? if (count == 0)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? win += 9;
? ? ? ? ? ? ? ? ? ? show[x][y] = ' ';
? ? ? ? ? ? ? ? ? ? show[x - 1][y - 1] = ' ';
? ? ? ? ? ? ? ? ? ? show[x - 1][y] = ' ';
? ? ? ? ? ? ? ? ? ? show[x - 1][y + 1] = ' ';
? ? ? ? ? ? ? ? ? ? show[x][y - 1] = ' ';
? ? ? ? ? ? ? ? ? ? show[x][y + 1] = ' ';
? ? ? ? ? ? ? ? ? ? show[x + 1][y - 1] = ' ';
? ? ? ? ? ? ? ? ? ? show[x + 1][y] = ' ';
? ? ? ? ? ? ? ? ? ? show[x + 1][y + 1] = ' ';
? ? ? ? ? ? ? ? ? ? Is_show(show, ROWS + 2, COLS + 2);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? win++;
? ? ? ? ? ? ? ? ? ? show[x][y] = count + '0';
? ? ? ? ? ? ? ? ? ? Is_show(show, ROWS + 2, COLS + 2);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? else
? ? ? ? {
? ? ? ? ? ? printf("輸入有誤,請重新輸入%c", 1);
? ? ? ? }
? ? }
? ? if (win >=ROWS*COLS - MINE)
? ? {
? ? ? ? Over_board(show, ROWS + 2, COLS + 2);
? ? ? ? printf("你贏了!\n");
? ? ? ? Is_show(show, ROWS + 2, COLS + 2);
? ? }
}
int main()
{
? ? int input = 0;
? ? do
? ? {
? ? ? ? menu();
? ? ? ? printf("請選擇>;");
? ? ? ? scanf("%d", &input);
? ? ? ? switch (input)
? ? ? ? {
? ? ? ? case 1:
? ? ? ? ? ? game();
? ? ? ? ? ? break;
? ? ? ? case 0:
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? printf("選擇錯誤,請重新選擇\n");
? ? ? ? ? ? break;
? ? ? ? }
? ? } while (input);
? ? return 0;
}game.c代碼片:
#include"game.h"
void Init_board(char arr[ROWS+2][COLS+2], int rows, int cols, char a)//初始化棋盤
{
? ? memset(arr, a, rows*cols);
}
void Is_show(char arr[ROWS+2][COLS+2], int rows, int cols)//打印棋盤
{
? ? int i = 0;
? ? int j = 0;
? ? printf(" ? ? ?");
? ? for (i = 0; i < rows - 2; i++)
? ? {
? ? ? ? printf("_%d__", i + 1);
? ? }
? ? printf("\n");
? ? for (i = 0; i < rows - 2; i++)
? ? {
? ? ? ? printf("%2d ? ", i + 1);
? ? ? ? for (j = 0; j < cols - 2; j++)
? ? ? ? {
? ? ? ? ? ? printf("|_%c_", arr[i + 1][j + 1]);
? ? ? ? }
? ? ? ? printf("|\n");
? ? }
}
void Set_mine(char arr[ROWS + 2][COLS + 2], int rows, int cols)//設(shè)置雷區(qū)
{
? ? int x = 0;
? ? int y = 0;
? ? int count = MINE;
? ? while (count)
? ? {
? ? ? ? x = rand() % 10 + 1;
? ? ? ? y = rand() % 10 + 1;
? ? ? ? if (arr[x][y] != '1')
? ? ? ? {
? ? ? ? ? ? arr[x][y] = '1';
? ? ? ? ? ? count--;
? ? ? ? }
? ? }
}
void Over_board(char arr[ROWS + 2][COLS + 2], int rows, int cols)//將所有不是雷區(qū)的位置清空
{
? ? int i = 0;
? ? int j = 0;
? ? for (i = 0; i < rows - 2; i++)
? ? {
? ? ? ? for (j = 0; j < cols - 2; j++)
? ? ? ? {
? ? ? ? ? ? if (arr[i + 1][j + 1] == '0')
? ? ? ? ? ? ? ? arr[i + 1][j + 1] = ' ';
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言編程中統(tǒng)計輸入的行數(shù)以及單詞個數(shù)的方法
這篇文章主要介紹了C語言編程中統(tǒng)計輸入的行數(shù)以及單詞個數(shù)的方法,利用最基礎(chǔ)的循環(huán)和判斷語句寫成,需要的朋友可以參考下2015-11-11
C/C++實(shí)現(xiàn)獲取系統(tǒng)時間的示例代碼
C 標(biāo)準(zhǔn)庫提供了 time() 函數(shù)與 localtime() 函數(shù)可以獲取到當(dāng)前系統(tǒng)的日歷時間。本文將通過一些簡單的示例為大家講講C++獲取系統(tǒng)時間的具體方法,需要的可以參考一下2022-12-12
C++ Qt開發(fā)之PushButton按鈕組件的使用詳解
Qt 是一個跨平臺C++圖形界面開發(fā)庫,利用Qt可以快速開發(fā)跨平臺窗體應(yīng)用程序,本文將重點(diǎn)介紹QPushButton按鈕組件的常用方法及靈活運(yùn)用,感興趣的小伙伴可以學(xué)習(xí)一下2023-12-12
C++中實(shí)現(xiàn)多態(tài)有幾種方式小結(jié)
在C++中,多態(tài)是一種面向?qū)ο缶幊痰奶匦?允許以統(tǒng)一的方式處理不同類型的對象,并根據(jù)實(shí)際對象的類型來執(zhí)行相應(yīng)的操作,本文給大家介紹了C++中實(shí)現(xiàn)多態(tài)有幾種方式小結(jié),需要的朋友可以參考下2024-12-12
C++中引用(&)的用法與應(yīng)用實(shí)例分析
引用是C++引入的新語言特性,是C++常用的一個重要內(nèi)容之一,正確、靈活地使用引用,可以使程序簡潔、高效。故在本篇中我將對引用進(jìn)行詳細(xì)討論,希望對大家更好地理解和使用引用起到拋磚引玉的作用2013-09-09

