扫雷之C++.NET实现
时间:2011-04-02 来源:sanwave
历经整整一周的开发,断断续续,终于完成了本阶段最后一件作品:扫雷。效果还可以,我想,可以打八十分了。整套方法都是完全独立自主开发,没有参考任何范例。细枝末节我也顾不上整理了,马上投入下一阶段的战斗......
#pragma once
namespace Mine { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing;
/// <summary> /// Form1 摘要 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: 在此处添加构造函数代码 //
////////////////////////对手工添加的雷块Label控件数组进行初始化 for(int i=0;i<256;i++){ btnMine[i]=gcnew Label(); this->Controls->Add(btnMine[i]); btnMine[i]->Left=30+18*(i%16); btnMine[i]->Top=50+18*(i/16); btnMine[i]->Width=22; btnMine[i]->Height=22; btnMine[i]->Name=i.ToString(); btnMine[i]->BorderStyle= System::Windows::Forms::BorderStyle::Fixed3D; btnMine[i]->TextAlign=System::Drawing::ContentAlignment::MiddleCenter; btnMine[i]->Font=(gcnew System::Drawing::Font(L"微软雅黑", 10.5F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134))); btnMine[i]->MouseDown+=gcnew System::Windows::Forms::MouseEventHandler(this,&Form1::Mine_MouseDown); btnMine[i]->MouseUp+=gcnew System::Windows::Forms::MouseEventHandler(this,&Form1::Mine_MouseUp); } } /////////////////////////////// protected: /// <summary> /// 清理所有正在使用的资源。 /// </summary> ~Form1() { if (components) { delete components; } }
/////////////////////////////////自定义变量 private: static array<System::Windows::Forms::Label^>^ btnMine=gcnew array<System::Windows::Forms::Label^>(256); //Label控件数组 int lCounter,rCounter,bCounter,RestOfMines; //用于记录鼠标按键状态 int TimeCounter; //用于计时 static array<int>^ MineCounter=gcnew array<int>(256); //记录无雷方块对应的数字 static array<bool>^ Covered=gcnew array<bool>(256); //记录雷块状态 static array<bool>^ IsMine = gcnew array<bool>(256); //是否为雷 static array<bool>^ IsJudge=gcnew array<bool>(256); //是否被猜 private: System::Windows::Forms::ToolStripMenuItem^ tsmGame; private: System::Windows::Forms::ToolStripMenuItem^ tsmOpening; private: System::Windows::Forms::ToolStripMenuItem^ tsmExit; private: System::Windows::Forms::ToolStripMenuItem^ tsmHelp; private: System::Windows::Forms::Timer^ tmr;
private: System::Windows::Forms::StatusStrip^ statusStrip1; private: System::Windows::Forms::ToolStripStatusLabel^ lblRest; private: System::Windows::Forms::ToolStripStatusLabel^ lblTime; private: System::Windows::Forms::Timer^ timTime; private: System::Windows::Forms::ToolStripMenuItem^ tsmAbout;
//////开局 public: void Startup(){ lCounter=rCounter=bCounter=0; TimeCounter=0;RestOfMines=40; for(int index=0;index<256;index++){ IsMine[index]=false; Covered[index]=true; IsJudge[index]=false; MineCounter[index]=0; btnMine[index]->Text=""; btnMine[index]->BackColor=System::Drawing::SystemColors::MenuHighlight; } CreateMine(); CountMine(); } ///////产生雷 public: void CreateMine(){ Random^ rand=gcnew Random; ArrayList Mines=gcnew ArrayList(40); int MineNumber; while(Mines.Count<40){ MineNumber=rand->Next(0,255); if(!Mines.Contains(MineNumber)){ Mines.Add(MineNumber); IsMine[MineNumber]=true; } } } ///////计算无雷方块对应的数字 void CountMine(){ for(int index=0;index<256;index++){ for(int i=index/16;i<index/16+3;i++){ for(int j=index%16;j<index%16+3;j++){ if(i<=0||i>16||j<=0||j>16){ continue; } if(16*(i-1)+j-1==index){ continue; } if(IsMine[16*(i-1)+j-1]==true){ MineCounter[index]++; } } } } } ///////对零方块进行一次拓展,同时响应右击左击推开一次雷 void PutZero(int r){ int index; for(int i=r/16;i<r/16+3;i++){ for(int j=r%16;j<r%16+3;j++){ index=16*(i-1)+j-1; if(i<=0||i>16||j<=0||j>16){continue;} if(IsMine[index]==true){ continue;} btnMine[index]->Text=MineCounter[index].ToString(); btnMine[index]->BackColor=System::Drawing::SystemColors::ActiveCaption; Covered[index]=false; } } }
///////游戏结束 void GameOver(bool win){ if(win==true){ ///如果赢了 MessageBox::Show("恭喜,您赢了!","Success"); } else{ ///输了 for(int index=0;index<256;index++){ if(IsMine[index]){ btnMine[index]->Text="雷";//加延迟 btnMine[index]->BackColor=System::Drawing::Color::Red; } } MessageBox::Show("哈哈,您白了!","Fail!"); } Startup(); }
protected: private: System::Windows::Forms::MenuStrip^ menuStrip; private: System::ComponentModel::IContainer^ components;
private: /// <summary> /// 必需的设计器变量。 /// </summary>
#pragma region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->menuStrip = (gcnew System::Windows::Forms::MenuStrip()); this->tsmGame = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->tsmOpening = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->tsmExit = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->tsmHelp = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->tsmAbout = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->tmr = (gcnew System::Windows::Forms::Timer(this->components)); this->statusStrip1 = (gcnew System::Windows::Forms::StatusStrip()); this->lblRest = (gcnew System::Windows::Forms::ToolStripStatusLabel()); this->lblTime = (gcnew System::Windows::Forms::ToolStripStatusLabel()); this->timTime = (gcnew System::Windows::Forms::Timer(this->components)); this->menuStrip->SuspendLayout(); this->statusStrip1->SuspendLayout(); this->SuspendLayout(); // // menuStrip // this->menuStrip->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {this->tsmGame, this->tsmHelp}); this->menuStrip->Location = System::Drawing::Point(0, 0); this->menuStrip->Name = L"menuStrip"; this->menuStrip->Size = System::Drawing::Size(344, 25); this->menuStrip->TabIndex = 1; this->menuStrip->Text = L"menuStrip1"; // // tsmGame // this->tsmGame->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {this->tsmOpening, this->tsmExit}); this->tsmGame->Name = L"tsmGame"; this->tsmGame->Size = System::Drawing::Size(61, 21); this->tsmGame->Text = L"游戏(G)"; // // tsmOpening // this->tsmOpening->Name = L"tsmOpening"; this->tsmOpening->Size = System::Drawing::Size(118, 22); this->tsmOpening->Text = L"开局(O)"; this->tsmOpening->Click += gcnew System::EventHandler(this, &Form1::tsmOpening_Click); // // tsmExit // this->tsmExit->Name = L"tsmExit"; this->tsmExit->Size = System::Drawing::Size(118, 22); this->tsmExit->Text = L"退出(E)"; this->tsmExit->Click += gcnew System::EventHandler(this, &Form1::tsmExit_Click); // // tsmHelp // this->tsmHelp->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->tsmAbout}); this->tsmHelp->Name = L"tsmHelp"; this->tsmHelp->Size = System::Drawing::Size(61, 21); this->tsmHelp->Text = L"帮助(H)"; // // tsmAbout // this->tsmAbout->Name = L"tsmAbout"; this->tsmAbout->Size = System::Drawing::Size(125, 22); this->tsmAbout->Text = L"关于(A)..."; this->tsmAbout->Click += gcnew System::EventHandler(this, &Form1::tsmAbout_Click); // // tmr // this->tmr->Enabled = true; this->tmr->Tick += gcnew System::EventHandler(this, &Form1::tmr_Tick); // // statusStrip1 // this->statusStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {this->lblRest, this->lblTime}); this->statusStrip1->Location = System::Drawing::Point(0, 348); this->statusStrip1->Name = L"statusStrip1"; this->statusStrip1->Size = System::Drawing::Size(344, 22); this->statusStrip1->TabIndex = 2; this->statusStrip1->Text = L"statusStrip1"; // // lblRest // this->lblRest->Margin = System::Windows::Forms::Padding(75, 3, 0, 2); this->lblRest->Name = L"lblRest"; this->lblRest->Size = System::Drawing::Size(0, 17); // // lblTime // this->lblTime->Margin = System::Windows::Forms::Padding(100, 3, 0, 2); this->lblTime->Name = L"lblTime"; this->lblTime->Size = System::Drawing::Size(0, 17); // // timTime // this->timTime->Enabled = true; this->timTime->Interval = 1000; this->timTime->Tick += gcnew System::EventHandler(this, &Form1::timTime_Tick); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(344, 370); this->Controls->Add(this->statusStrip1); this->Controls->Add(this->menuStrip); this->MainMenuStrip = this->menuStrip; this->Name = L"Form1"; this->Text = L"Mine"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->menuStrip->ResumeLayout(false); this->menuStrip->PerformLayout(); this->statusStrip1->ResumeLayout(false); this->statusStrip1->PerformLayout(); this->ResumeLayout(false); this->PerformLayout();
} #pragma endregion private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { Startup(); } private: System::Void Mine_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) { switch ( e->Button ){ case System::Windows::Forms::MouseButtons::Right: rCounter++; bCounter++; break; case System::Windows::Forms::MouseButtons::Left: lCounter++; bCounter++; break; } } private: System::Void Mine_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) { Label^ bClick=(Label^) sender; int s=Convert::ToInt32(bClick->Name); switch (e->Button ){ case System::Windows::Forms::MouseButtons::Left: lCounter++;bCounter++;break; case System::Windows::Forms::MouseButtons::Right: rCounter++;bCounter++;break; }
if(bCounter>=3){ ////如果右击左击 int counter=0; if(Covered[s]==true); else{ for(int i=s/16;i<s/16+3;i++){ for(int j=s%16;j<s%16+3;j++){ if(i<=0||i>16||j<=0||j>16){continue;} if(IsMine[16*(i-1)+j-1]&&IsJudge[16*(i-1)+j-1]){counter++;} if(!IsMine[16*(i-1)+j-1]&&IsJudge[16*(i-1)+j-1]){counter=-9;} } } if(counter==MineCounter[s]){PutZero(s);} if(counter<0){GameOver(false);} } } else if(lCounter==2) { ///如果左击 if(IsJudge[s]); else if(IsMine[s]){btnMine[s]->Text="雷";GameOver(false);} else if(Covered[s]==false||IsJudge[s]); else { btnMine[s]->Text=MineCounter[s].ToString(); bClick->BackColor=System::Drawing::SystemColors::ActiveCaption; } } else if(rCounter==2){ ///如果右击 if(Covered[s]==false); else if(IsJudge[s]){ bClick->Text=""; bClick->BackColor=System::Drawing::SystemColors::MenuHighlight; RestOfMines+=1; } else{ bClick->Text="!"; bClick->BackColor=System::Drawing::Color::Orange; IsJudge[s]=true; RestOfMines-=1; } } lCounter=rCounter=bCounter=0; ///初始化三个变量 } /////////开局 private: System::Void tsmOpening_Click(System::Object^ sender, System::EventArgs^ e) { Startup(); } /////////退出 private: System::Void tsmExit_Click(System::Object^ sender, System::EventArgs^ e) { Application::Exit(); } /////////关于 private: System::Void tsmAbout_Click(System::Object^ sender, System::EventArgs^ e) { Form^ About=gcnew Form(); } /////////时钟 private: System::Void tmr_Tick(System::Object^ sender, System::EventArgs^ e) { int counter=0; for(int index=0;index<256;index++){ if(IsJudge[index]==true&&IsMine[index]==true||Covered[index]==false) counter++; if(btnMine[index]->Text=="0") PutZero(index); } if(counter==256){ for(int index=0;index<256;index++){ IsMine[index]=false; Covered[index]=true; } GameOver(true); Startup(); } lblRest->Text="还有"+RestOfMines.ToString()+"个雷"; } /////////专用计时时钟 private: System::Void timTime_Tick(System::Object^ sender, System::EventArgs^ e) { TimeCounter+=1; lblTime->Text="时间:"+Convert::ToInt32(TimeCounter).ToString()+" s"; } }; }
在扫雷时,经常会遇到无雷零方块,它会自动拓展到非零方块。实现这个过程,我颇费了一番脑筋,要知道直接函数递归是不可行的。最后想出两种方案:都有一个对零方块进行一次拓展的函数PutZero(),一种方案就是上述方法,用时钟卡住,发现零方块露出自动调用PutZero()函数进行拓展;还有一种方法,就是在PutZero()外再开一个函数,互相嵌套,理论上可以实现,但我没试成。既已实现过程,也就不再无谓的去尝试了...
源码下载:http://cid-c96c6ec8ad8dcfea.office.live.com/browse.aspx/Program/Mines
#pragma once
namespace Mine { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing;
/// <summary> /// Form1 摘要 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: 在此处添加构造函数代码 //
////////////////////////对手工添加的雷块Label控件数组进行初始化 for(int i=0;i<256;i++){ btnMine[i]=gcnew Label(); this->Controls->Add(btnMine[i]); btnMine[i]->Left=30+18*(i%16); btnMine[i]->Top=50+18*(i/16); btnMine[i]->Width=22; btnMine[i]->Height=22; btnMine[i]->Name=i.ToString(); btnMine[i]->BorderStyle= System::Windows::Forms::BorderStyle::Fixed3D; btnMine[i]->TextAlign=System::Drawing::ContentAlignment::MiddleCenter; btnMine[i]->Font=(gcnew System::Drawing::Font(L"微软雅黑", 10.5F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_cast<System::Byte>(134))); btnMine[i]->MouseDown+=gcnew System::Windows::Forms::MouseEventHandler(this,&Form1::Mine_MouseDown); btnMine[i]->MouseUp+=gcnew System::Windows::Forms::MouseEventHandler(this,&Form1::Mine_MouseUp); } } /////////////////////////////// protected: /// <summary> /// 清理所有正在使用的资源。 /// </summary> ~Form1() { if (components) { delete components; } }
/////////////////////////////////自定义变量 private: static array<System::Windows::Forms::Label^>^ btnMine=gcnew array<System::Windows::Forms::Label^>(256); //Label控件数组 int lCounter,rCounter,bCounter,RestOfMines; //用于记录鼠标按键状态 int TimeCounter; //用于计时 static array<int>^ MineCounter=gcnew array<int>(256); //记录无雷方块对应的数字 static array<bool>^ Covered=gcnew array<bool>(256); //记录雷块状态 static array<bool>^ IsMine = gcnew array<bool>(256); //是否为雷 static array<bool>^ IsJudge=gcnew array<bool>(256); //是否被猜 private: System::Windows::Forms::ToolStripMenuItem^ tsmGame; private: System::Windows::Forms::ToolStripMenuItem^ tsmOpening; private: System::Windows::Forms::ToolStripMenuItem^ tsmExit; private: System::Windows::Forms::ToolStripMenuItem^ tsmHelp; private: System::Windows::Forms::Timer^ tmr;
private: System::Windows::Forms::StatusStrip^ statusStrip1; private: System::Windows::Forms::ToolStripStatusLabel^ lblRest; private: System::Windows::Forms::ToolStripStatusLabel^ lblTime; private: System::Windows::Forms::Timer^ timTime; private: System::Windows::Forms::ToolStripMenuItem^ tsmAbout;
//////开局 public: void Startup(){ lCounter=rCounter=bCounter=0; TimeCounter=0;RestOfMines=40; for(int index=0;index<256;index++){ IsMine[index]=false; Covered[index]=true; IsJudge[index]=false; MineCounter[index]=0; btnMine[index]->Text=""; btnMine[index]->BackColor=System::Drawing::SystemColors::MenuHighlight; } CreateMine(); CountMine(); } ///////产生雷 public: void CreateMine(){ Random^ rand=gcnew Random; ArrayList Mines=gcnew ArrayList(40); int MineNumber; while(Mines.Count<40){ MineNumber=rand->Next(0,255); if(!Mines.Contains(MineNumber)){ Mines.Add(MineNumber); IsMine[MineNumber]=true; } } } ///////计算无雷方块对应的数字 void CountMine(){ for(int index=0;index<256;index++){ for(int i=index/16;i<index/16+3;i++){ for(int j=index%16;j<index%16+3;j++){ if(i<=0||i>16||j<=0||j>16){ continue; } if(16*(i-1)+j-1==index){ continue; } if(IsMine[16*(i-1)+j-1]==true){ MineCounter[index]++; } } } } } ///////对零方块进行一次拓展,同时响应右击左击推开一次雷 void PutZero(int r){ int index; for(int i=r/16;i<r/16+3;i++){ for(int j=r%16;j<r%16+3;j++){ index=16*(i-1)+j-1; if(i<=0||i>16||j<=0||j>16){continue;} if(IsMine[index]==true){ continue;} btnMine[index]->Text=MineCounter[index].ToString(); btnMine[index]->BackColor=System::Drawing::SystemColors::ActiveCaption; Covered[index]=false; } } }
///////游戏结束 void GameOver(bool win){ if(win==true){ ///如果赢了 MessageBox::Show("恭喜,您赢了!","Success"); } else{ ///输了 for(int index=0;index<256;index++){ if(IsMine[index]){ btnMine[index]->Text="雷";//加延迟 btnMine[index]->BackColor=System::Drawing::Color::Red; } } MessageBox::Show("哈哈,您白了!","Fail!"); } Startup(); }
protected: private: System::Windows::Forms::MenuStrip^ menuStrip; private: System::ComponentModel::IContainer^ components;
private: /// <summary> /// 必需的设计器变量。 /// </summary>
#pragma region Windows Form Designer generated code /// <summary> /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// </summary> void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->menuStrip = (gcnew System::Windows::Forms::MenuStrip()); this->tsmGame = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->tsmOpening = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->tsmExit = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->tsmHelp = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->tsmAbout = (gcnew System::Windows::Forms::ToolStripMenuItem()); this->tmr = (gcnew System::Windows::Forms::Timer(this->components)); this->statusStrip1 = (gcnew System::Windows::Forms::StatusStrip()); this->lblRest = (gcnew System::Windows::Forms::ToolStripStatusLabel()); this->lblTime = (gcnew System::Windows::Forms::ToolStripStatusLabel()); this->timTime = (gcnew System::Windows::Forms::Timer(this->components)); this->menuStrip->SuspendLayout(); this->statusStrip1->SuspendLayout(); this->SuspendLayout(); // // menuStrip // this->menuStrip->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {this->tsmGame, this->tsmHelp}); this->menuStrip->Location = System::Drawing::Point(0, 0); this->menuStrip->Name = L"menuStrip"; this->menuStrip->Size = System::Drawing::Size(344, 25); this->menuStrip->TabIndex = 1; this->menuStrip->Text = L"menuStrip1"; // // tsmGame // this->tsmGame->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {this->tsmOpening, this->tsmExit}); this->tsmGame->Name = L"tsmGame"; this->tsmGame->Size = System::Drawing::Size(61, 21); this->tsmGame->Text = L"游戏(G)"; // // tsmOpening // this->tsmOpening->Name = L"tsmOpening"; this->tsmOpening->Size = System::Drawing::Size(118, 22); this->tsmOpening->Text = L"开局(O)"; this->tsmOpening->Click += gcnew System::EventHandler(this, &Form1::tsmOpening_Click); // // tsmExit // this->tsmExit->Name = L"tsmExit"; this->tsmExit->Size = System::Drawing::Size(118, 22); this->tsmExit->Text = L"退出(E)"; this->tsmExit->Click += gcnew System::EventHandler(this, &Form1::tsmExit_Click); // // tsmHelp // this->tsmHelp->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->tsmAbout}); this->tsmHelp->Name = L"tsmHelp"; this->tsmHelp->Size = System::Drawing::Size(61, 21); this->tsmHelp->Text = L"帮助(H)"; // // tsmAbout // this->tsmAbout->Name = L"tsmAbout"; this->tsmAbout->Size = System::Drawing::Size(125, 22); this->tsmAbout->Text = L"关于(A)..."; this->tsmAbout->Click += gcnew System::EventHandler(this, &Form1::tsmAbout_Click); // // tmr // this->tmr->Enabled = true; this->tmr->Tick += gcnew System::EventHandler(this, &Form1::tmr_Tick); // // statusStrip1 // this->statusStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {this->lblRest, this->lblTime}); this->statusStrip1->Location = System::Drawing::Point(0, 348); this->statusStrip1->Name = L"statusStrip1"; this->statusStrip1->Size = System::Drawing::Size(344, 22); this->statusStrip1->TabIndex = 2; this->statusStrip1->Text = L"statusStrip1"; // // lblRest // this->lblRest->Margin = System::Windows::Forms::Padding(75, 3, 0, 2); this->lblRest->Name = L"lblRest"; this->lblRest->Size = System::Drawing::Size(0, 17); // // lblTime // this->lblTime->Margin = System::Windows::Forms::Padding(100, 3, 0, 2); this->lblTime->Name = L"lblTime"; this->lblTime->Size = System::Drawing::Size(0, 17); // // timTime // this->timTime->Enabled = true; this->timTime->Interval = 1000; this->timTime->Tick += gcnew System::EventHandler(this, &Form1::timTime_Tick); // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 12); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(344, 370); this->Controls->Add(this->statusStrip1); this->Controls->Add(this->menuStrip); this->MainMenuStrip = this->menuStrip; this->Name = L"Form1"; this->Text = L"Mine"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->menuStrip->ResumeLayout(false); this->menuStrip->PerformLayout(); this->statusStrip1->ResumeLayout(false); this->statusStrip1->PerformLayout(); this->ResumeLayout(false); this->PerformLayout();
} #pragma endregion private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { Startup(); } private: System::Void Mine_MouseDown(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) { switch ( e->Button ){ case System::Windows::Forms::MouseButtons::Right: rCounter++; bCounter++; break; case System::Windows::Forms::MouseButtons::Left: lCounter++; bCounter++; break; } } private: System::Void Mine_MouseUp(System::Object^ sender, System::Windows::Forms::MouseEventArgs^ e) { Label^ bClick=(Label^) sender; int s=Convert::ToInt32(bClick->Name); switch (e->Button ){ case System::Windows::Forms::MouseButtons::Left: lCounter++;bCounter++;break; case System::Windows::Forms::MouseButtons::Right: rCounter++;bCounter++;break; }
if(bCounter>=3){ ////如果右击左击 int counter=0; if(Covered[s]==true); else{ for(int i=s/16;i<s/16+3;i++){ for(int j=s%16;j<s%16+3;j++){ if(i<=0||i>16||j<=0||j>16){continue;} if(IsMine[16*(i-1)+j-1]&&IsJudge[16*(i-1)+j-1]){counter++;} if(!IsMine[16*(i-1)+j-1]&&IsJudge[16*(i-1)+j-1]){counter=-9;} } } if(counter==MineCounter[s]){PutZero(s);} if(counter<0){GameOver(false);} } } else if(lCounter==2) { ///如果左击 if(IsJudge[s]); else if(IsMine[s]){btnMine[s]->Text="雷";GameOver(false);} else if(Covered[s]==false||IsJudge[s]); else { btnMine[s]->Text=MineCounter[s].ToString(); bClick->BackColor=System::Drawing::SystemColors::ActiveCaption; } } else if(rCounter==2){ ///如果右击 if(Covered[s]==false); else if(IsJudge[s]){ bClick->Text=""; bClick->BackColor=System::Drawing::SystemColors::MenuHighlight; RestOfMines+=1; } else{ bClick->Text="!"; bClick->BackColor=System::Drawing::Color::Orange; IsJudge[s]=true; RestOfMines-=1; } } lCounter=rCounter=bCounter=0; ///初始化三个变量 } /////////开局 private: System::Void tsmOpening_Click(System::Object^ sender, System::EventArgs^ e) { Startup(); } /////////退出 private: System::Void tsmExit_Click(System::Object^ sender, System::EventArgs^ e) { Application::Exit(); } /////////关于 private: System::Void tsmAbout_Click(System::Object^ sender, System::EventArgs^ e) { Form^ About=gcnew Form(); } /////////时钟 private: System::Void tmr_Tick(System::Object^ sender, System::EventArgs^ e) { int counter=0; for(int index=0;index<256;index++){ if(IsJudge[index]==true&&IsMine[index]==true||Covered[index]==false) counter++; if(btnMine[index]->Text=="0") PutZero(index); } if(counter==256){ for(int index=0;index<256;index++){ IsMine[index]=false; Covered[index]=true; } GameOver(true); Startup(); } lblRest->Text="还有"+RestOfMines.ToString()+"个雷"; } /////////专用计时时钟 private: System::Void timTime_Tick(System::Object^ sender, System::EventArgs^ e) { TimeCounter+=1; lblTime->Text="时间:"+Convert::ToInt32(TimeCounter).ToString()+" s"; } }; }
在扫雷时,经常会遇到无雷零方块,它会自动拓展到非零方块。实现这个过程,我颇费了一番脑筋,要知道直接函数递归是不可行的。最后想出两种方案:都有一个对零方块进行一次拓展的函数PutZero(),一种方案就是上述方法,用时钟卡住,发现零方块露出自动调用PutZero()函数进行拓展;还有一种方法,就是在PutZero()外再开一个函数,互相嵌套,理论上可以实现,但我没试成。既已实现过程,也就不再无谓的去尝试了...
源码下载:http://cid-c96c6ec8ad8dcfea.office.live.com/browse.aspx/Program/Mines
相关阅读 更多 +