#include <time.h>
#include <conio.h>
/************************************************************************/
/* 地图状态约定*/
/* 0 : 周围9个格子都没有雷*/
/* 1~8: 周围9个格子有1~8个雷 */则茄拍
/* -1 : 有雷的格子 */
/* -2 : 被翻开的有雷的格子 */
/* -3 : 地图边界 */
/************************************************************************/
int Map[12][12]={0} /* 当前在玩的雷图 */
int MapShow[12][12]={0} /* 当前用户选择过的地方孙羡,即“已翻开”的格子 */
int nSpaceLeft = 100 /* 剩余的空白数,如果为0,则任务成功! */
int lastX,lastY /* 失败时,记录挖到雷的位置 */
int AllMaps[5][12][12]={0}/* 供用户选择的五张雷图 */
/* 显示雷区(每次用户 *** 作之后都要重新显示) */
void DrawMap()
/* 初始化雷区 */
void InitMap()
/* 游戏开始时,加载用户选择的一幅雷图 */
void LoadMap(int index)
/* 玩扫雷游戏 */
int Play()
/* 绘制主菜单 */
void DrawMainMenu()
/* 显示结果 */
void ShowResult(int result)
/* 主函数 */
int main(int argc, char* argv[])
{
char ch
int result
srand(time(NULL))/* 按当前时间初始化随机数,这样每次启动的时候,雷的位置的不一样 */
InitMap()/* 初始化5张雷图,供用户选择 */
while(1) /* 用while循环,保证只有在用户选择退出时,才退出游戏 */
{
DrawMainMenu() /* 绘制主菜单 */
flushall() /* 清空所有输入输出缓冲区,纳吵主要是清空输入缓冲区,防止前面的输入对getch()的干扰 */
ch = getch() /* 读取输入 */
switch(ch)
{
case '1': /* 用户选择1 */
printf("\n\t请输入雷图编号(1-5):")
while (1) /* 循环输入,如果输入错误就一直要求输入 */
{
flushall()
ch = getch()/* 读取用户输入的雷图编号 */
if (ch >= '1' &&ch <= '5') /* 只有在1-5之间有效 */
{
LoadMap(ch-'1')/* ch -'1',将用户输入转换为雷图下标(下标从0开始,所以是-'1')*/
break /* 如果输入正确,就退出循环输入 */
}
else
{
/* 输入错误,则提示重新输入 */
printf("\n\t输入无效!请重新输入雷图编号(1-5):")
flushall()
}
}
result = Play() /* 开始玩扫雷游戏 */
ShowResult(result)/* 显示游戏结果 */
break
case '2': /* 用户选择2 */
exit(0) /* 直接退出 */
break
default: /* 无效输入 */
/* 不做任何 *** 作,重新显示主菜单 */
break
}
}
return 0
}
void LoadMap(int index)
{
int i,j
nSpaceLeft = 90
for(i=0i<12i++)
for(j=0j<12j++)
{
Map[i][j] = AllMaps[index][i][j]/* 将5张雷图中的下标为index的那一张载入到Map数组 */
MapShow[i][j] = 0 /* 重新开始游戏,所以所有格子都是“未翻开”状态 */
}
}
void InitMap()
{
int i,j,k
int m,n
/* 要初始化5张地图 */
for(k=0k<5k++)
{
/* 初始化地图的边界 */
for(i=0i<12i++)
{
/* 下标为0和11的位置都是“边界”,这些位置不属于雷区,仅在程序内部使用 */
AllMaps[k][0][i] = -3
AllMaps[k][11][i] = -3
AllMaps[k][i][0] = -3
AllMaps[k][i][11] = -3
}
/* 先初始化10个雷的位置 */
for(i=0i<10i++)
{
m = rand()%10 + 1/* 随机选一个X坐标 */
n = rand()%10 + 1/* 随机选一个Y坐标 */
if(AllMaps[k][m][n] == 0) /* 如果随机产生的位置之前没有被安排放置雷 */
{
AllMaps[k][m][n] = -1/* 放置一个雷 */
}
else /* 随机产生的位置在之前已经放置了雷了 */
{
i--/* 这个位置无效,重新产生一个 */
}
}
/* 计算每个格子周围雷的个数 */
for(i=1i<11i++)
for(j=1j<11j++)
{
if(AllMaps[k][i][j] != -1)
{
AllMaps[k][i][j] = 0
/* 周围的8个位置,有一个雷就加一 */
/************************************************************************/
/* 坐标[i][j]周围的8个坐标位置: */
/* [i-1][j-1] [i-1][j] [i-1][j+1] */
/* [i][j-1] [i][j][i][j+1] */
/* [i+1][j-1] [i+1][j] [i+1][j+1] */
/************************************************************************/
if(AllMaps[k][i-1][j-1] == -1)
AllMaps[k][i][j]++
if(AllMaps[k][i-1][j] == -1)
AllMaps[k][i][j]++
if(AllMaps[k][i-1][j+1] == -1)
AllMaps[k][i][j]++
if(AllMaps[k][i][j-1] == -1)
AllMaps[k][i][j]++
if(AllMaps[k][i][j+1] == -1)
AllMaps[k][i][j]++
if(AllMaps[k][i+1][j-1] == -1)
AllMaps[k][i][j]++
if(AllMaps[k][i+1][j] == -1)
AllMaps[k][i][j]++
if(AllMaps[k][i+1][j+1] == -1)
AllMaps[k][i][j]++
}
}
}
}
void DrawMap()
{
int i,j
system("cls") /* 清屏 */
/* 绘制坐标和边框 */
printf("\n\n\n")
printf("\t Y ")
for(i=1i<11i++) printf("%-02d",i-1)
printf("\n\tX |###################|\n")
/* 每一行按规则绘制雷区 */
for(i=1i<11i++)
{
printf("\t%-02d|",i-1)/* 显示X坐标 */
for(j=1j<11j++)
{
if(MapShow[i][j]) /* 如果该位置被用户“挖开”了,就照实显示 */
{
if (Map[i][j] >= 0) /* 非雷,显示周围雷的个数 */
{
printf("%d|",Map[i][j])
}
/*else if(Map[i][j] == 0)
{
printf("0|")
}*/
else if (Map[i][j] == -1) /* 雷,显示* */
{
printf("*|")
}
else if (Map[i][j] == -2) /* 用户挖到的雷,显示@ */
{
printf("@|")
}
else /* 其他情况(目前不会出现,方便以后扩展) */
{
printf(" |")
}
}
else /* 如果该位置没有被用户“挖开”,则显示空格 */
{
printf(" |")
}
}
printf("\n")
}
printf("\t |###################|\n")
}
void DrawMainMenu()
{
system("cls")
printf("\n\n\n\n\n\n")
printf("\t|###################|\n")
printf("\t| 请选择! |\n")
printf("\t|1. 开始扫雷|\n")
printf("\t|2. 退出|\n")
printf("\t|###################|\n")
}
int Play()
{
char chX,chY/* 用户输入 */
int X,Y/* 用户输入转换为整数下标 */
int i,j
while (1)
{
DrawMap() /* 重新绘制雷区图 */
/* 输入X坐标 */
printf("\n\t请输入X:")
flushall()
while(1)
{
chX = getch()
if (chX >= '0' &&chX <= '9')
{
X = chX - '0' + 1
break
}
else
{
printf("\n\t输入无效!请重新输入X:")
flushall()
}
}
/* 输入Y坐标 */
printf("\n\t请输入Y:")
flushall()
while(1)
{
chY = getch()
if (chY >= '0' &&chY <= '9')
{
Y = chY - '0' + 1
break
}
else
{
printf("\n\t输入无效!请重新输入Y:")
flushall()
}
}
if(MapShow[X][Y] == 0) /* 输入的是未翻开的位置 */
{
MapShow[X][Y] = 1 /* 将该位置标记为“已翻开” */
if(Map[X][Y] == -1) /* 如果挖到的是雷 */
{
Map[X][Y] = -2 /* 标记为-2,表示这是被用户挖到的雷 */
for(i=1i<11i++)
for(j=1j<11j++)
MapShow[i][j]=1/* 游戏结束,自动将所有位置“翻开” */
/* 记录用户挖到雷的位置坐标 */
lastX = X-1
lastY = Y-1
return 0/* 游戏失败! */
}
else /* 如果挖到的不是雷 */
{
nSpaceLeft-- /* 剩余空白数减一 */
if(nSpaceLeft==0) /* 剩余空白数为0,则表示游戏成功 */
{
return 1/* 游戏胜利! */
}
}
}
else /* 输入的是已翻开的位置 */
{
printf("\n\t你输入的位置是[%d,%d]\n\t这个位置已经翻开!请重新输入!\n\t按任意键继续...\n",X-1,Y-1)
flushall()
getch()
}
}
}
void ShowResult( int result )
{
DrawMap()
if(result == 1) /* 游戏成功 */
{
printf("\n\t恭喜!您完成的扫雷任务!\n\t按任意键继续...")
flushall()
getch()
}
else /* 游戏失败 */
{
printf("\n\t哈哈!您在位置[%d,%d]挖到雷了,任务失败!\n\t按任意键继续...",lastX, lastY)
flushall()
getch()
}
}
Private Sub Form_Load()Init_FormEnd SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)Dim x1 As Integer, y1 As Integerx1 = Fix((x - x0) / a): y1 = Fix((y - y0) / a)If (x - x0) >((Col_Num + 1) * a) Or (y - y0) >((Col_Num + 1) * a) Then Exit SubIf x <x0 Or y <y0 Then Exit SubIf x <x0 + x1 * a + 2 Or _x >x0 + x1 * a + a - 4 Or _y <y0 + y1 * a + 2 Or _y >y0 + y1 * a + a - 4 _Then Exit SubIf Button = 1 ThenIf Map(x1, y1) = 0 ThenCall fan(x1, y1)ElseIf Map(x1, y1) = 1 ThenCall loseEnd IfElseIf Button = 2 ThenCall draw_flg(x1, y1)End IfDim n As Integern = 0Dim i As Integer, j As IntegerFor i = 0 To Col_NumFor j = 0 To Row_NumIf Map(i, j) = -2 Then n = n + 1Next jNext iIf n = (Col_Num + 1) * (Row_Num + 1) - Ant_Num ThenBeepCurrentX = (Col_Num / 2) * a + x0CurrentY = (Row_Num / 2) * a + y0Call sub1a = Form1.FontSizeForm1.FontSize = 80'Form1.ForeColor = vbBlackPrint "you win"Form1.FontSize = aEnd IfEnd Sub
Sub fan(x As Integer, y As Integer)Dim i As Integer, j As IntegerDim n As IntegerFor i = -1 To 1For j = -1 To 1If j * i = 0 And Map(x + i, y + j) = 0 ThenMap(x + i, y + j) = -2n = Counts(x + i, y + j)Form1.Line (x0 + (i + x) * a + 2, y0 + (j + y) * a + 2)-Step(a - 4, a - 4), Form1.BackColor, BF
If n <>0 ThenCurrentX = (x + i) * a + 2 + x0CurrentY = (y + j) * a + 2 + y0Select Case nCase Is = 1Form1.ForeColor = vbWhiteCase Is = 2Form1.ForeColor = vbYellowCase Is >2Form1.ForeColor = vbRedEnd SelectPrint nElseIf n = 0 Then
Call fan(x + i, y + j)End IfEnd IfNext jNext iEnd Sub
Function Counts(x As Integer, y As Integer) As IntegerDim i As Integer, j As IntegerFor i = -1 To 1For j = -1 To 1If Map(x + i, y + j) = 1 Then Counts = Counts + 1NextNextEnd FunctionSub lose()Dim i As Integer, j As IntegerFor i = 0 To Row_NumFor j = 0 To Col_NumIf Map(j, i) = 1 ThenForm1.FillColor = vbBlackForm1.FillStyle = 0Form1.Circle (x0 + j * a + a / 2, y0 + i * a + a / 2), a / 3, vbBlack, , , 0.8Form1.Line (x0 + j * a, y0 + i * a)-Step(a, a), vbWhiteForm1.Line (x0 + j * a + a, y0 + i * a)-Step(-a, a), vbWhite
ElseIf Map(j, i) = 0 ThenForm1.Line (x0 + j * a + 2, y0 + i * a + 2)-Step(a - 4, a - 4), Form1.BackColor, BFCurrentX = j * a + x0CurrentY = i * a + y0Print Counts(j, i)End IfNext j
Next iBeepa = Form1.FontSizeForm1.FontSize = 80'Form1.ForeColor = vbBlackPrint "you lose"Form1.FontSize = aEnd SubSub draw_flg(x As Integer, y As Integer)CurrentX = x * a + x0 + 2CurrentY = y * a + y0 + 2Print "?"End Sub
Public Sub Init_Form()
Form1.ClsForm1.ScaleMode = 3Form1.Width = 8000Form1.Height = 6000Form1.BackColor = vbGreenForm1.AutoRedraw = Trueform1.caption="一个简单扫雷游戏 "Col_Num = 10 '获取列数Row_Num = 10 '获取行数a = 20 '单元宽(高)度Ant_Num = 40 '雷的数量ReDim Map(-1 To Col_Num + 1, -1 To Row_Num + 1)Dim i As Integer, j As IntegerFor i = -1 To Row_Num + 1
For j = -1 To Col_Num + 1Form1.Line (x0 + j * a, y0 + i * a)-Step(a, a), 0, BForm1.Line (x0 + j * a + 2, y0 + i * a + 2)-Step(a - 4, a - 4), vbRed, BFMap(j, i) = 0 '初始化位置标记为空格0If i = -1 Or i = Row_Num + 1 Or j = -1 Or j = Row_Num + 1 ThenForm1.Line (x0 + j * a + 1, y0 + i * a + 1)-Step(a - 2, a - 2), RGB(100, 120, 100), BF '画四周墙体Map(j, i) = -1 '四周位置标记为墙体:-1End IfNext j
Next iDim x As Integer, y As IntegerFor i = 1 To Ant_Num1000Randomizex = Rnd * Col_Numy = Rnd * Row_NumDoEventsIf Map(x, y) <>0 Then GoTo 1000Map(x, y) = 1NextEnd Sub
Sub sub1()
Dim i As Integer, j As IntegerFor i = 0 To Row_NumFor j = 0 To Col_NumIf Map(j, i) = 1 ThenForm1.FillColor = vbBlackForm1.FillStyle = 0Form1.Circle (x0 + j * a + a / 2, y0 + i * a + a / 2), a / 3, vbBlack, , , 0.8ElseIf Map(j, i) = 0 ThenForm1.Line (x0 + j * a + 2, y0 + i * a + 2)-Step(a - 4, a - 4), Form1.BackColor, BFCurrentX = j * a + x0CurrentY = i * a + y0Print Counts(j, i)End IfNext j
Next iEnd Sub
俄罗斯方快http://topic.csdn.net/t/20051201/01/4429905.html扫雷
#include<stdio.h>
#include<graphics.h>
#include<stdlib.h>
struct list
{
int x
int y
int num
int bomb
int wa
}
struct list di[10][10]
int currentx=210
int currenty=130
void initxy(void)
{
int i,j
for(i=0i<=9i++)
for(j=0j<=9j++)
{
di[j].x=i*20+200
di[j].y=j*20+120
di[j].wa=0
di[j].bomb=0
}
}
void initmu(void)
{
int i,j
setcolor(2)
rectangle(200,120,400,320)
rectangle(190,110,410,330)
setfillstyle(8,14)
floodfill(191,111,2)
for(i=0i<=9i++)
for(j=0j<=9j++)
rectangle(di[j].x,di[j].y,di[j].x+19,di[j].y+19)
outtextxy(450,200,"press 'enter' to kick"芹做启嫌如)
outtextxy(450,250,"press '\' to mark")
}
void randbomb(void)
{
int k
int i,j
randomize()
for(i=0i<=9i++)
for(j=0j<=9j++)
{
k=random(5)
if(k==2)
di[j].bomb=1
}
}
void jisuan(void)
{
int k=0
int i,j
for(i=0i<=9i++)
for(j=0j<=9j++)
{
if(i&&j&&di[i-1][j-1].bomb)
k=k+1
if(i&&di[i-1][j].bomb)
k=k+1
if(j&&di[j-1].bomb)
k=k+1
if(i<=8&&di[i+1][j].bomb)
k=k+1
if(j<=8&&di[j+1].bomb)
k=k+1
if(i<胡亏=8&&j<=8&&di[i+1][j+1].bomb)
k=k+1
if(i&&j<=8&&di[i-1][j+1].bomb)
k=k+1
if(i<=8&&j&&di[i+1][j-1].bomb)
k=k+1
di[j].num=k
k=0
}
}
void xianbomb(void)
{
int i,j
char biaoji[2]
char znum[2]
biaoji[0]=1
biaoji[1]=NULL
for(i=0i<=9i++)
for(j=0j<=9j++)
{
if(di[j].bomb==1)
outtextxy(di[j].x+2,di[j].y+2,biaoji)
else
{
itoa(di[j].num,znum,10)
setfillstyle(1,0)
bar(i*20+202,j*20+122,i*20+218,j*20+138)
outtextxy(i*20+202,j*20+122,znum)
}
}
}
void move(void)
{
int key
key=bioskey(1)
if(key)
key=bioskey(0)
if(key==0x4800)
{
if(currenty>130)
{
setcolor(0)
circle(currentx,currenty,5)
currenty-=20
setcolor(4)
circle(currentx,currenty,5)
}
else
{
setcolor(0)
circle(currentx,currenty,5)
currenty=310
setcolor(4)
circle(currentx,currenty,5)
}
}
if(key==0x4b00)
{
if(currentx>210)
{
setcolor(0)
circle(currentx,currenty,5)
currentx-=20
setcolor(4)
circle(currentx,currenty,5)
}
else
{
setcolor(0)
circle(currentx,currenty,5)
currentx=390
setcolor(4)
circle(currentx,currenty,5)
}
}
if(key==0x4d00)
{
if(currentx<390)
{
setcolor(0)
circle(currentx,currenty,5)
currentx+=20
setcolor(4)
circle(currentx,currenty,5)
}
else
{
setcolor(0)
circle(currentx,currenty,5)
currentx=210
setcolor(4)
circle(currentx,currenty,5)
}
}
if(key==0x5000)
{
if(currenty<310)
{
setcolor(0)
circle(currentx,currenty,5)
currenty+=20
setcolor(4)
circle(currentx,currenty,5)
}
else
{
setcolor(0)
circle(currentx,currenty,5)
currenty=130
setcolor(4)
circle(currentx,currenty,5)
}
}
if(key==0x1c0d)
{
int i,j
char snum[2]
snum[0]=NULL
snum[1]=NULL
i=(currentx-210)/20
j=(currenty-130)/20
if(di[j].bomb==1)
{
outtextxy(100,100,"game over")
xianbomb()
sleep(2)
exit(0)
}
if(di[j].bomb==0)
{
di[j].wa=1
setfillstyle(1,0)
bar(currentx-8,currenty-8,currentx+8,currenty+8)
setcolor(15)
itoa(di[j].num,snum,10)
outtextxy(currentx-8,currenty-8,snum)
setcolor(4)
circle(currentx,currenty,5)
}
}
if(key==0x2b5c)
{
char biaoji[2]
biaoji[0]=1
biaoji[1]=NULL
setcolor(0)
bar(currentx-8,currenty-8,currentx+8,currenty+8)
setcolor(4)
outtextxy(currentx-8,currenty-8,biaoji)
circle(currentx,currenty,5)
}
}
void success(void)
{
int k=1
int i,j
for(i=0i<=9i++)
for(j=0j<=9j++)
if(di[j].bomb==0&&di[j].wa==0)
k=0
if(k==1)
{
outtextxy(100,100,"success good")
xianbomb()
sleep(2)
exit(0)
}
}
void main(void)
{
int gd=DETECT,gm
initgraph(&gd,&gm,"")
initxy()
initmu()
randbomb()
jisuan()
setcolor(4)
circle(210,130,5)
while(1)
{
move()
success()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)