文章目錄
一、游戲簡介
二、代碼實作
1.呼叫test函式
2.列印選單(menu函式)
3.game函式
4.初始化(Initmine函式)
5.布置雷(SetMine函式)
6.排雷(FindMime函式)
三、總代碼
1.game.h
2.game.c
3.test.c
一、游戲簡介
一款小游戲,一張棋盤(可以自己設計,9*9 , 12*12 , 15*15 都可以 , 或是添加一些自己喜歡的元素)布滿許多未知是雷或是安全的格子,輸入坐標,若觸雷,游戲結束,反之,顯示周圍8個格子的雷的個數
二、代碼實作
1.首先呼叫test 函式
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("請選擇:>");
scanf("%d", &input);
switch (input)
{
case 1:
//掃雷
game();
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("選擇錯誤\n");
break;
}
} while (input);
}
2.列印選單,進行選擇
void menu()
{
printf("***********************\n");
printf("***** 1. play ****\n");
printf("***** 0. exit ****\n");
printf("***********************\n");
}
3.實作game函式
void game()
{
//創建陣列
char mine[ROWS][COLS] = { 0 };//存放布置好的雷的資訊
char show[ROWS][COLS] = { 0 };//存放排查出的雷的資訊
//初始化mine陣列為全'0'
InitBoard(mine, ROWS, COLS, '0');
//初始化show陣列為全'*'
InitBoard(show, ROWS, COLS, '*');
//列印棋盤
//DisplayBoard(mine, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//排雷
FindMine(mine, show, ROW, COL);
}
4.初始化棋盤及列印(可以按照自己的興趣自己設計)
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
//1~9
int i = 0;
int j = 0;
//列號的列印
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);//列印行號
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
5.布置雷
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
6.排雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("請輸入要排查的坐標:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遺憾你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
//計算x,y坐標周圍有幾個雷
int n = get_mine_count(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("輸入坐標非法,無法排雷,請重新輸入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
}
}
排雷內部的函式,用來在show棋盤上顯示雷的個數
static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] -
8 * '0';
}
三、總代碼
1.game.h
#pragma once
//頭檔案的包含
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
//符號的宣告
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 10
//函式的宣告
//初始化棋盤
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//列印棋盤
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷
void SetMine(char mine[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
2.game.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)
{
int i = 0;
int j = 0;
for (i = 0; i < rows; i++)
{
for (j = 0; j < cols; j++)
{
board[i][j] = set;
}
}
}
void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
//1~9
int i = 0;
int j = 0;
//列號的列印
for (i = 0; i <= col; i++)
{
printf("%d ", i);
}
printf("\n");
for (i = 1; i <= row; i++)
{
printf("%d ", i);//列印行號
for (j = 1; j <= col; j++)
{
printf("%c ", board[i][j]);
}
printf("\n");
}
}
void SetMine(char mine[ROWS][COLS], int row, int col)
{
int count = EASY_COUNT;
while (count)
{
int x = rand() % row + 1;
int y = rand() % col + 1;
if (mine[x][y] == '0')
{
mine[x][y] = '1';
count--;
}
}
}
static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
return mine[x - 1][y] +
mine[x - 1][y - 1] +
mine[x][y - 1] +
mine[x + 1][y - 1] +
mine[x + 1][y] +
mine[x + 1][y + 1] +
mine[x][y + 1] +
mine[x - 1][y + 1] -
8 * '0';
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
int x = 0;
int y = 0;
int win = 0;
while (win < row * col - EASY_COUNT)
{
printf("請輸入要排查的坐標:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col)
{
if (mine[x][y] == '1')
{
printf("很遺憾你被炸死了\n");
DisplayBoard(mine, row, col);
break;
}
else
{
//計算x,y坐標周圍有幾個雷
int n = get_mine_count(mine, x, y);
show[x][y] = n + '0';
DisplayBoard(show, row, col);
win++;
}
}
else
{
printf("輸入坐標非法,無法排雷,請重新輸入\n");
}
}
if (win == row * col - EASY_COUNT)
{
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
}
}
3.test.c
#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
printf("***********************\n");
printf("***** 1. play ****\n");
printf("***** 0. exit ****\n");
printf("***********************\n");
}
void game()
{
//創建陣列
char mine[ROWS][COLS] = { 0 };//存放布置好的雷的資訊
char show[ROWS][COLS] = { 0 };//存放排查出的雷的資訊
//初始化mine陣列為全'0'
InitBoard(mine, ROWS, COLS, '0');
//初始化show陣列為全'*'
InitBoard(show, ROWS, COLS, '*');
//列印棋盤
//DisplayBoard(mine, ROW, COL);
//布置雷
SetMine(mine, ROW, COL);
DisplayBoard(show, ROW, COL);
//DisplayBoard(mine, ROW, COL);
//排雷
FindMine(mine, show, ROW, COL);
}
void test()
{
int input = 0;
srand((unsigned int)time(NULL));
do
{
menu();
printf("請選擇:>");
scanf("%d", &input);
switch (input)
{
case 1:
//掃雷
game();
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("選擇錯誤\n");
break;
}
} while (input);
}
int main()
{
test();
return 0;
}
這只是掃雷的基礎版,還有很多功能沒有實作,我將會繼續改進,感謝觀看
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/357213.html
標籤:其他
上一篇:C語言小游戲之掃雷
