日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C語言編寫掃雷小程序_C 語言

作者:Rae8023 ? 更新時間: 2022-11-03 編程語言

本文實例為大家分享了C語言實現掃雷小程序的具體代碼,供大家參考,具體內容如下

首先創建一個項目,建立一個頭文件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//行數
#define COLS 10//列數
#define MINE 20//雷數

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)//用于清空沒有雷區的地方
{
? ? 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() ?//玩游戲函數
{
? ? 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);*/
? ? //打印雷區棋盤 方便調試
? ? 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)//設置雷區
{
? ? 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)//將所有不是雷區的位置清空
{
? ? 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] = ' ';
? ? ? ? }
? ? }


}

原文鏈接:https://blog.csdn.net/Rae8023/article/details/54317838

欄目分類
最近更新