基于C控制台扫雷游戏Word文档格式.docx
《基于C控制台扫雷游戏Word文档格式.docx》由会员分享,可在线阅读,更多相关《基于C控制台扫雷游戏Word文档格式.docx(12页珍藏版)》请在冰豆网上搜索。
![基于C控制台扫雷游戏Word文档格式.docx](https://file1.bdocx.com/fileroot1/2022-11/18/93a56124-ff76-4fea-b0ba-a1a449bbe08c/93a56124-ff76-4fea-b0ba-a1a449bbe08c1.gif)
voidtoLeft();
voidtoRight();
voidtoTop();
voidtoBottom();
};
#endif
2)实现
#include"
Button.h"
voidButton:
:
initButton(intirow,inticolumn){
row=irow;
column=icolumn;
posX=0;
posY=0;
}
toLeft()
if(posY-1>
=0)
posY--;
toRight(){
if(posY+1<
column)
posY++;
toTop(){
if(posX-1>
posX--;
toBottom(){
if(posX+1<
row)
posX++;
intButton:
getPosX(){
returnposX;
getPosY()
returnposY;
二、Map类(存在空地和雷区)
#ifndefMAP_H_
#defineMAP_H_
classMap
int**map;
//地图指针
//行;
//列;
intnum;
//布雷数;
public:
Map();
voidnewMap();
//申请存储地图空间
voiddeleteMap();
//释放地图内存
voidinitMap();
//初始化地图
voidprintMap(Button&
);
//打印地图
voidprintMine();
//打印雷
intgetRow();
//得到行数;
voidsetRow(int);
//设置行数;
intgetColumn();
//得到列数;
voidsetColumn(int);
//设置列数;
intgetNum();
//得到布雷数;
voidsetNum(int);
//设置布雷数;
intgetVal(intx,inty);
voidsetVal(intx,inty,intval);
boolis_win();
voidOpen(int,int,Map&
//打开选取位置voidSwap(int&
int&
Map.h"
#include<
iostream>
time.h>
stdlib.h>
usingnamespacestd;
Map:
Map()
column=row=num=10;
voidMap:
newMap()
map=newint*[row];
for(inti=0;
i<
row;
i++)
*(map+i)=newint[column];
}
deleteMap()
i++)delete[]*(map+i);
delete[]map;
initMap()
intk=0;
for(intj=0;
j<
column;
j++){
if(k<
num)
*(*(map+i)+j)=-1;
k++;
else
*(*(map+i)+j)=0;
srand(time(NULL));
intrandX1,randY1;
intrandX2,randY2;
1000;
randX1=rand()%row;
randY1=rand()%column;
randX2=rand()%row;
randY2=rand()%column;
Swap(*(*(map+randX1)+randY1),*(*(map+randX2)+randY2));
printMap(Button&
button)
j++)
if(i==button.getPosX()&
&
j==button.getPosY())
cout<
<
"
☆"
;
▉"
elseif(*(*(map+i)+j)==0)
█"
elseif(*(*(map+i)+j)==1)
□"
<
*(*(map+i)+j)-1;
endl;
printMine()
if(*(*(map+i)+j)==-1)
●"
*(*(map+i)+j)-1;
boolMap:
is_win()
if(*(*(map+i)+j)==0)
returnfalse;
returntrue;
intMap:
getRow()
returnrow;
setRow(intn){
row=n;
getColumn()
returncolumn;
setColumn(intn){
column=n;
getNum()
setNum(intn)
num=n;
getVal(intx,inty)
return*(*(map+x)+y);
setVal(intx,inty,intval)
*(*(map+x)+y)=val;
voidOpen(intx,inty,Map&
myMap)
if(x<
0||y<
0||x>
=myMap.getRow()||y>
=myMap.getColumn())//访问越界
intcount=0;
if(myMap.getVal(x,y)==0)
if((x-1>
=0)&
(y-1>
myMap.getVal(x-1,y-1)==-1)//左上
count++;
myMap.getVal(x-1,y)==-1)//上
(y+1<
myMap.getColumn())&
myMap.getVal(x-1,y+1)==-1)//右上
if((y-1>
myMap.getVal(x,y-1)==-1)//左
if((y+1<
myMap.getVal(x,y+1)==
-1)//右
if((x+1<
myMap.getRow())&
myMap.getVal(x+1,y-1)==-1)//左下
myMap.getVal(x+1,y)==-1)//下
myMap.getVal(x+1,y+1)==-1)//右下
myMap.setVal(x,y,count+1);
if(myMap.getVal(x,y)==1)//若未访问且四周无雷
myMap.getVal(x-1,y-1)==0)//左上
Open(x-1,y-1,myMap);
myMap.getVal(x-1,y)==0)//上
Open(x-1,y,myMap);
myMap.getVal(x-1,y+1)==0)//右上
Open(x-1,y+1,myMap);
myMap.getVal(x,y-1)==0)//左
Open(x,y-1,myMap);
myMap.getVal(x,y+1)==0)//右
Open(x,y+1,myMap);
myMap.getVal(x+1,y-1)==0)//左下
Open(x+1,y-1,myMap);
myMap.getVal(x+1,y)==0)//下
Open(x+1,y,myMap);
(y+1