陣列小練習(2)——掃雷
一、游戲介紹
掃雷,一款經典的小游戲,我們用c語言來實作一下,
二、編程思路
用兩個二維陣列來分別存盤布置好的地雷資料和排查出來的地雷資料;
在存盤布置地雷資料的二維陣列中,用“0”表示沒有地雷,用“1”表示布置了地雷;
在存盤排查地雷資料的二維陣列中,用“*”表示還沒有排查的位置,用“0”-“9”表示排查位置周圍地雷的數量;
布置地雷用rand函式模擬;
三、代碼
Minesweeper.h(函式宣告)
#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#define ROW 9
#define ROWS ROW+2
#define COL 9
#define COLS COL+2
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
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 board[ROWS][COLS], int row, int col,int count);//布置地雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int num);//探查地雷
Minesweeper.cpp(游戲的函式具體實作)
#include"Minesweeper.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) {
int i = 0;
int j = 0;
printf("-----------------------------------------\n");
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");
}
printf("-----------------------------------------\n");
}
void SetMine(char board[ROWS][COLS], int row, int col,int count) {
while (count) {
int x = rand() % row+1;
int y = rand() % col+1;
if (board[x][y] == '0') {
board[x][y] = '1';
count--;
}
}
}
int GetMineCount(char mine[ROWS][COLS], int x, int y) {
int sum = 0;
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
sum += mine[i][j];
}
}
return sum - 9 * '0';
}
void ShowMine(char mine[ROWS][COLS],char show[ROWS][COLS],int x, int y) {
int count = GetMineCount(mine, x, y);
if (count == 0) {
show[x][y] = count + '0';
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (i >= 1 && i <= ROW && j >= 1 && j <= COL && show[i][j] == '*') {
ShowMine(mine, show, i, j);
}
}
}
}
else {
show[x][y] = count + '0';
}
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col,int num) {
int x = 0;
int y = 0;
int win = 0;
while (win<row*col-num) {
printf("請輸入要排查的坐標:>");
scanf("%d %d", &x, &y);
if (x >= 1 && x <= row && y >= 1 && y <= col) {
if (mine[x][y] == '1') {
printf("\n很遺憾,你被炸死了!\n");
DisplayBoard(mine, row, col);
break;
}
else {
ShowMine(mine, show, x, y);
DisplayBoard(show, row, col);
win++;
}
}
else {
printf("坐標非法!\n");
}
}
if (win == row * col - num) {
printf("恭喜你,排雷成功\n");
DisplayBoard(mine, row, col);
}
}
test.cpp(測驗)
#include"Minesweeper.h"
void menu() {
printf("****************************************\n");
printf("********** 1. play **********\n");
printf("********** 0. exit **********\n");
printf("****************************************\n");
}
void Minesweeper() {
char mine[ROWS][COLS] = {0};//存放布置好的雷
char show[ROWS][COLS] = {0};//存放排查出來的雷的資訊
InitBoard(mine, ROWS, COLS,'0');
InitBoard(show, ROWS, COLS,'*');
SetMine(mine, ROW, COL, 10);
DisplayBoard(show, ROW, COL);
FindMine(mine, show, ROW, COL,10);
}
int main() {
int input = 0;
srand((unsigned int)time(NULL));//用時間戳設定亂數的生成起點
do {
menu();
printf("請選擇:>");
scanf("%d", &input);
switch (input) {
case 1:
Minesweeper();
break;
case 0:
printf("退出游戲\n");
break;
default:
printf("選擇錯誤\n");
}
} while (input);
return 0;
}
四、改進思路
1.增加傳統掃雷游戲里的插紅旗功能,
2.增加可視化,
3.增加游戲難度選擇,增加游戲趣味性,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/353526.html
標籤:其他
上一篇:三字棋的實作以及模塊化的思想
下一篇:C語言實作三子棋(13步)
