小編給大家分享一下C++實現(xiàn)掃雷游戲的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)于2013年成立,公司以成都網(wǎng)站設(shè)計、做網(wǎng)站、系統(tǒng)開發(fā)、網(wǎng)絡(luò)推廣、文化傳媒、企業(yè)宣傳、平面廣告設(shè)計等為主要業(yè)務(wù),適用行業(yè)近百種。服務(wù)企業(yè)客戶超過千家,涉及國內(nèi)多個省份客戶。擁有多年網(wǎng)站建設(shè)開發(fā)經(jīng)驗。為企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、創(chuàng)意設(shè)計、宣傳推廣等服務(wù)。 通過專業(yè)的設(shè)計、獨特的風(fēng)格,為不同客戶提供各種風(fēng)格的特色服務(wù)。
具體內(nèi)容如下
需要開一個map.txt 寫入地圖
地圖中 *表示空地 ; x表示地雷
**********
**********
**x*******
**********
**********
**********
**********
**********
**********
然后就是掃雷的控制臺代碼了,只簡單的檢測了一下
#include <stdio.h>
#include <string.h>
#define SIZE 10
char img_map[SIZE + 2][SIZE + 2]; // the image of a map
int num_map[SIZE + 2][SIZE + 2]; // calculate the total number of mine in one block.
int open_map[SIZE + 2][SIZE + 2]; // which img shoud user open.
int sumMine = 0;
int sumBlock = 0;
int dir[8][2] = {{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}};
int beyond_board (int x,int y){ // judge whether the step is out of board;
if( x < 0 || x >= SIZE || y < 0 || y >= SIZE ){
return 1; // beyond board return 1;
}
return 0;
}
void read_img_map(){ // get data from map.txt
FILE *p_file = fopen("map.txt","r");
int i = 0,j;
for (i = 0;i < SIZE;i++){
fread(img_map[i],sizeof(char),SIZE+1,p_file);
}
/* for (i = 0;i < SIZE;i++){
for (j = 0;j < SIZE;j++){
if(img_map[i][j] == '*'){
img_map[i][j] = ' ';
}
}
}
*/
}
void write_num_map(){ // transfer img_map to num_map
int i = 0,j = 0,k = 0;
for (i = 0;i < SIZE;i++){
for (j = 0;j < SIZE;j++){
if (img_map[i][j] == 'x'){
sumMine++; // the total number of mine in the map
num_map[i][j] = 9; // 9 represent a mine here
continue;
}
for (k = 0;k < 8;k++){
int stepx = i + dir[k][0],stepy = j + dir[k][1];
if ( !beyond_board (stepx,stepy) ){
if (img_map[stepx][stepy] == 'x'){
num_map[i][j] += 1;
}
}
}
}
}
/* for (i = 0;i < SIZE;i++){
for (j = 0;j < SIZE;j++){
printf("%d",num_map[i][j]);
}
printf("\n");
}
*/
}
void show_all_mine(){ // 在地圖中顯示所有的地雷的位置
int i,j;
for (i = 0;i < SIZE;i++){
for (j = 0;j < SIZE;j++){
if (num_map[i][j] == 9) {
open_map[i][j] = 1; // 找到地雷后在 openmap 中標(biāo)記
}
}
}
}
void show_all_map(){
int i,j;
for (i = 0;i < SIZE;i++){
for (j = 0;j < SIZE;j++){
if(open_map[i][j]){
if(num_map[i][j] == 9){
printf("X"); // x represetn mine
}
else{
printf("%d",num_map[i][j]); // show the number has been opened
}
}
else{
printf("*"); // the block is coverd;
}
}
printf("\n");
}
}
void find_empty(int x,int y){ //搜索算法
// printf("x = %d y = %d\n",x,y);
// show_all_map();
if (beyond_board(x,y)){
return ;
}
if (open_map[x][y]){
return ;
}
if (!num_map[x][y]){ // 遇到零時還要繼續(xù)翻上下左右
open_map[x][y] = 1;
}else if(num_map[x][y] != 9){ // 遇到數(shù)字了即搜索停止
open_map[x][y] = 1;
return ;
}
//else { //****遇到雷時搜索停止
// return ;
// }
int i;
for (i = 0 ;i < 8;i++){
find_empty(x + dir[i][0],y + dir[i][1]);
}
}
int sum_one_open_map(){
int i,j;
int s = 0;
for (i = 0;i < SIZE;i++){
for ( j = 0;j < SIZE;j++)
if (open_map[i][j]){
s++;
}
}
return s;
}
int main()
{
read_img_map();
write_num_map();
show_all_map();
memset(open_map,0,sizeof(open_map)); // reset the open_map.
int x,y; // empty = 0 , mine = 9, number = others
sumBlock = SIZE * SIZE - sumMine;
int sum = 0;
while(sumBlock != sum){
printf("please input the postion x,y: ");
scanf("%d %d",&x,&y);
scanf("%*[^\n]"); //clean the buffer
scanf("%*c");
x--;
y--;
if(beyond_board(x,y)){ // the position is beyond the board
printf("beyond the board and please input the position again:");
continue;
}
if(!num_map[x][y]){ //is empty
find_empty(x,y);
show_all_map(x,y);
}else if(num_map[x][y] == 9){ // is mine
show_all_mine();
show_all_map();
break;
}else{ // is number
open_map[x][y] = 1;
show_all_map();
}
sum = sum_one_open_map();
}
if (sum==sumBlock) printf("YOU WIN! \n");
else {
printf("YOU LOSE!\n");
}
return 0;
}運行截圖:


以上是“C++實現(xiàn)掃雷游戲的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁題目:C++實現(xiàn)掃雷游戲的方法
地址分享:http://www.chinadenli.net/article48/igpohp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、靜態(tài)網(wǎng)站、動態(tài)網(wǎng)站、定制開發(fā)、小程序開發(fā)、Google
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)