作者devilbilly (Billy)
看板HCHS60309
標題踩地雷程式碼
時間Mon Jan 7 20:02:36 2008
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <ctime>
using namespace std;
main()
{
int x[100000];
int mine,row,column,i,j,a,b,c,d,e=0,temp;
cout<<"\nplease enter how many rows:";
cin>>row;
cout<<"\nPlease enter how many columns:";
cin>>column;
cout<<"\nPlease enter how many mines";
cin>>mine;
b=row*column;
for (a=0;a<b;a++)
{
x[a]=0;
}
for (a=0;a<mine;a++)
{
x[a]=-1;
}
srand(time(0));
for(d=0;d<mine;d++)
{c=rand()%b;
temp=x[c];
x[c]=x[d];
x[d]=temp;
}
for(a=0;a<b;a++)
{if (a%row==0)
{cout<<"\n";}
if (x[a]==-1)
cout<<setw(2)<<"*";
else
cout<<setw(2)<<x[a];
}//This for loop is for debug use.
cout<<"\n\n\n\n\n\n\n";
for(j=0;j<column;j++)
{
for(i=0;i<row;i++)
{
if(x[j*row+i]==-1)
{
if((x[(j-1)*row+i-1]!=-1)&&((i-1)>= 0)&&((j-1)>=0))
{x[(j-1)*row+i-1]=x[(j-1)*row+i-1]+1;}
if((x[(j-1)*row+i]!=-1)&&(((i>=0)&&((j-1)>=0))))
{x[(j-1)*row+i]=x[(j-1)*row+i]+1;}
if(((((i+1)%row!=0 )&&((j-1))>=0))&&(x[(j-1)*row+i+1]!=-1))
{x[(j-1)*row+i+1]=x[(j-1)*row+i+1]+1;}
if((x[j*row+i-1]!=-1)&&((i-1)>=0)&&(j>=0))
{x[j*row+i-1]=x[j*row+i-1]+1;}
if((x[j*row+i+1]!=-1)&&((i+1)%row!=0)&&(j>=0))
{x[j*row+i+1]=x[j*row+i+1]+1;}
if((x[(j+1)*row+i-1]!=-1)&&((((i-1)>=0)&&((j+1)%column!=0))))
{x[(j+1)*row+i-1]=x[(j+1)*row+i-1]+1;}
if((x[(j+1)*row+i]!=-1)&&(i>=0)&&((j+1)%column!=0))
{x[(j+1)*row+i]=x[(j+1)*row+i]+1;}
if((x[(j+1)*row+i+1]!=-1)&&((((i+1)%row!=0)&&((j+1)%column!=0))))
{x[(j+1)*row+i+1]=x[(j+1)*row+i+1]+1; }
e++;
}
else
e++;
}
}
for(a=0;a<b;a++)
{if (a%row==0)
{cout<<"\n";}
if (x[a]==-1)
cout<<setw(2)<<"*";
else
cout<<setw(2)<<x[a];
}
cout<<"\nDevilBilly是超級大帥哥 禾斗 禾斗!\n";
system("pause");
return 0;
}
自己試試看八 !
--
我是帥哥B
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.126.5.95
※ 編輯: devilbilly 來自: 140.126.5.95 (01/07 20:03)
→ devilbilly:這個DEBUG挺累人的 演算法得先弄好 不然會幹到死.. 01/07 20:04
→ genma:有java版的踩地雷嗎~ 01/07 20:52
→ devilbilly:友JOVA版的 01/07 21:21
推 hank52078:幹 好酷喔 我都看不懂 哭哭 01/07 21:31
→ devilbilly:為啥 我現在沒看過ㄉ文章不會出現~ 01/07 21:53