基于C控制台扫雷游戏Word文档格式.docx
《基于C控制台扫雷游戏Word文档格式.docx》由会员分享,可在线阅读,更多相关《基于C控制台扫雷游戏Word文档格式.docx(15页珍藏版)》请在冰点文库上搜索。
:
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类(存在空地和雷区)
1)头文件
#ifndefMAP_H_
#defineMAP_H_
classMap
int**map;
//地图指针
//行;
//列;
intnum;
//布雷数;
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()
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)
for(intj=0;
if(i==button.getPosX()&
&
j==button.getPosY())
{
cout<
<
"
☆"
;
}
else
if(*(*(map+i)+j)==-1)
cout<
▉"
elseif(*(*(map+i)+j)==0)
█"
elseif(*(*(map+i)+j)==1)
□"
else
<
*(*(map+i)+j)-1;
cout<
endl;
printMine()
if(*(*(map+i)+j)==-1)
●"
elseif(*(*(map+i)+j)==0)
elseif(*(*(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()
returnnum;
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())//访问越界
return;
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)//若未访问且四周无雷
if((x-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);
myMap.getVal(x+1,y+1)==0)//右下
Open(x+1,y+1,myMap);
a,int&
b)
inttemp=a;
a=b;
b=temp;
三、界面及游戏运行(MineClearance)
conio.h>
intmain()
{
intiRow,iColumn,iNum;
charstart,next,direction;
MapmyMap;
Buttonbutton;
while(true)
cout<
\t**********自定义扫雷************\n\n"
<
\t\t1.游戏开始\n"
\t\t2.退出游戏\n"
\n\n===========================================\n"
start=getch();
system("
cls"
if(start=='
2'
)
break;
elseif(start=='
1'
请您输入(<
=20)行,(<
=30)列和布雷的数目:
"
cin>
>
iRow>
iColumn>
iNum;
while(iNum>
=iRow*iColumn)
您输入的布雷数超了,请重新输入:
cin>
system("
button.initButton(iRow,iColumn);
myMap.setRow(iRow);
myMap.setColumn(iColumn);
myMap.setNum(iNum);
myMap.newMap();
myMap.initMap();
myMap.printMap(button);
while(true)
\n请输入方位(a左d右w上s下,q打开):
direction=getch();
system("
if(direction!
='
q'
{
switch(direction)
{
case'
a'
case'
A'
button.toLeft();
d'
D'
button.toRight();
s'
S'
button.toBottom();
w'
W'
button.toTop();
}
myMap.printMap(button);
}
break;
if(myMap.getVal(button.getPosX(),button.getPosY())==-1)
cout<
很遗憾,您差一点就赢了!
\n"
break;
Open(button.getPosX(),button.getPosY(),myMap);
myMap.printMap(button);
if(myMap.is_win())
恭喜你赢了!
myMap.printMine();
myMap.deleteMap();
\n按任意键返回主菜单\n"
next=getch();
return0;