Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- namespace MatchingGame
- {
- using namespace System;
- using namespace System::Collections::Generic;
- using namespace System::ComponentModel;
- using namespace System::Data;
- using namespace System::Drawing;
- // using namespace System::Linq;
- using namespace System::Text;
- using namespace System::Threading::Tasks;
- using namespace System::Windows::Forms;
- public ref class Form1 : public System::Windows::Forms::Form
- {
- public:
- Form1(void)
- {
- InitializeComponent();
- icons = gcnew List<String^>();
- icons->Add("!");
- icons->Add("!");
- icons->Add("N");
- icons->Add("N");
- icons->Add(",");
- icons->Add(",");
- icons->Add("k");
- icons->Add("k");
- icons->Add("b");
- icons->Add("b");
- icons->Add("v");
- icons->Add("v");
- icons->Add("w");
- icons->Add("w");
- icons->Add("z");
- icons->Add("z");
- AssignIconsToSquares();
- }
- protected:
- /// <summary>
- /// ќсвободить все используемые ресурсы.
- /// </summary>
- ~Form1()
- {
- if (components)
- {
- delete components;
- }
- }
- private: System::Windows::Forms::Timer^ timer1;
- private: System::Windows::Forms::TableLayoutPanel^ tableLayoutPanel1;
- protected:
- 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->timer1=(gcnew System::Windows::Forms::Timer(this->components));
- this->tableLayoutPanel1=(gcnew System::Windows::Forms::TableLayoutPanel());
- this->SuspendLayout();
- //
- // tableLayoutPanel1
- //
- this->tableLayoutPanel1->ColumnCount=2;
- this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
- 50)));
- this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
- 50)));
- this->tableLayoutPanel1->Location=System::Drawing::Point(115,89);
- this->tableLayoutPanel1->Name=L"tableLayoutPanel1";
- this->tableLayoutPanel1->RowCount=2;
- this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent,50)));
- this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent,50)));
- this->tableLayoutPanel1->Size=System::Drawing::Size(200,100);
- this->tableLayoutPanel1->TabIndex=0;
- //
- // Form1
- //
- this->AutoScaleDimensions=System::Drawing::SizeF(6,13);
- this->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
- this->ClientSize=System::Drawing::Size(545,399);
- this->Controls->Add(this->tableLayoutPanel1);
- this->Name=L"Form1";
- this->Text=L"Form1";
- this->ResumeLayout(false);
- }
- #pragma endregion
- Label ^firstClicked = nullptr;
- Label ^secondClicked = nullptr;
- Random ^random = gcnew Random();
- List<String^> ^icons;
- private:void AssignIconsToSquares()
- {
- for each (Control ^control in tableLayoutPanel1->Controls)
- {
- Label ^iconLabel = (Label^)control;
- if (iconLabel != nullptr)
- {
- int randomNumber = random->Next(icons->Count);
- iconLabel->Text = icons[randomNumber];
- iconLabel->ForeColor = iconLabel->BackColor;
- icons->RemoveAt(randomNumber);
- }
- }
- }
- private:void label_Click(Object ^sender, EventArgs ^e)
- {
- if (timer1->Enabled == true)
- return;
- Label ^clickedLabel = (Label^)sender;
- if (clickedLabel != nullptr)
- {
- if (clickedLabel->ForeColor == Color::Black)
- // All done - leave the if statements.
- return;
- if (firstClicked == nullptr)
- {
- firstClicked = clickedLabel;
- firstClicked->ForeColor = Color::Black;
- return;
- }
- secondClicked = clickedLabel;
- secondClicked->ForeColor = Color::Black;
- CheckForWinner();
- if (firstClicked->Text == secondClicked->Text)
- {
- firstClicked = nullptr;
- secondClicked = nullptr;
- return;
- }
- timer1->Start();
- }
- }
- private:void timer1_Tick(Object ^sender, EventArgs ^e)
- {
- timer1->Stop();
- firstClicked->ForeColor = firstClicked->BackColor;
- secondClicked->ForeColor = secondClicked->BackColor;
- firstClicked = nullptr;
- secondClicked = nullptr;
- }
- private:void CheckForWinner()
- {
- for each (Control ^control in tableLayoutPanel1->Controls)
- {
- Label ^iconLabel = (Label^)control;
- if (iconLabel != nullptr)
- {
- if (iconLabel->ForeColor == iconLabel->BackColor)
- return;
- }
- }
- MessageBox::Show("You matched all the icons!", "Congratulations!");
- Close();
- }
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement