Advertisement
Guest User

Transport Code

a guest
Jul 19th, 2018
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.12 KB | None | 0 0
  1. #pragma once
  2.  
  3. namespace MatchingGame
  4. {
  5.     using namespace System;
  6.     using namespace System::Collections::Generic;
  7.     using namespace System::ComponentModel;
  8.     using namespace System::Data;
  9.     using namespace System::Drawing;
  10.     // using namespace System::Linq;
  11.     using namespace System::Text;
  12.     using namespace System::Threading::Tasks;
  13.     using namespace System::Windows::Forms;
  14.  
  15.     public ref class Form1 : public System::Windows::Forms::Form
  16.     {
  17.     public:
  18.         Form1(void)
  19.         {
  20.             InitializeComponent();
  21.             icons = gcnew List<String^>();
  22.  
  23.             icons->Add("!");
  24.             icons->Add("!");
  25.             icons->Add("N");
  26.             icons->Add("N");
  27.             icons->Add(",");
  28.             icons->Add(",");
  29.             icons->Add("k");
  30.             icons->Add("k");
  31.  
  32.             icons->Add("b");
  33.             icons->Add("b");
  34.             icons->Add("v");
  35.             icons->Add("v");
  36.             icons->Add("w");
  37.             icons->Add("w");
  38.             icons->Add("z");
  39.             icons->Add("z");
  40.  
  41.             AssignIconsToSquares();
  42.         }
  43.  
  44.     protected:
  45.         /// <summary>
  46.         /// ќсвободить все используемые ресурсы.
  47.         /// </summary>
  48.         ~Form1()
  49.         {
  50.             if (components)
  51.             {
  52.                 delete components;
  53.             }
  54.         }
  55.         private: System::Windows::Forms::Timer^  timer1;
  56.         private: System::Windows::Forms::TableLayoutPanel^  tableLayoutPanel1;
  57.         protected:
  58.         private: System::ComponentModel::IContainer^  components;
  59.  
  60.     private:
  61.         /// <summary>
  62.         /// “ребуетс¤ переменна¤ конструктора.
  63.         /// </summary>
  64.  
  65.  
  66. #pragma region Windows Form Designer generated code
  67.         /// <summary>
  68.         /// ќб¤зательный метод дл¤ поддержки конструктора - не измен¤йте
  69.         /// содержимое данного метода при помощи редактора кода.
  70.         /// </summary>
  71.         void InitializeComponent(void)
  72.         {
  73.             this->components=(gcnew System::ComponentModel::Container());
  74.             this->timer1=(gcnew System::Windows::Forms::Timer(this->components));
  75.             this->tableLayoutPanel1=(gcnew System::Windows::Forms::TableLayoutPanel());
  76.             this->SuspendLayout();
  77.             //
  78.             // tableLayoutPanel1
  79.             //
  80.             this->tableLayoutPanel1->ColumnCount=2;
  81.             this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
  82.                 50)));
  83.             this->tableLayoutPanel1->ColumnStyles->Add((gcnew System::Windows::Forms::ColumnStyle(System::Windows::Forms::SizeType::Percent,
  84.                 50)));
  85.             this->tableLayoutPanel1->Location=System::Drawing::Point(115,89);
  86.             this->tableLayoutPanel1->Name=L"tableLayoutPanel1";
  87.             this->tableLayoutPanel1->RowCount=2;
  88.             this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent,50)));
  89.             this->tableLayoutPanel1->RowStyles->Add((gcnew System::Windows::Forms::RowStyle(System::Windows::Forms::SizeType::Percent,50)));
  90.             this->tableLayoutPanel1->Size=System::Drawing::Size(200,100);
  91.             this->tableLayoutPanel1->TabIndex=0;
  92.             //
  93.             // Form1
  94.             //
  95.             this->AutoScaleDimensions=System::Drawing::SizeF(6,13);
  96.             this->AutoScaleMode=System::Windows::Forms::AutoScaleMode::Font;
  97.             this->ClientSize=System::Drawing::Size(545,399);
  98.             this->Controls->Add(this->tableLayoutPanel1);
  99.             this->Name=L"Form1";
  100.             this->Text=L"Form1";
  101.             this->ResumeLayout(false);
  102.  
  103.         }
  104. #pragma endregion
  105.  
  106.         Label ^firstClicked = nullptr;
  107.         Label ^secondClicked = nullptr;
  108.  
  109.         Random ^random = gcnew Random();
  110.  
  111.         List<String^> ^icons;
  112.  
  113.         private:void AssignIconsToSquares()
  114.         {
  115.             for each (Control ^control in tableLayoutPanel1->Controls)
  116.             {
  117.                 Label ^iconLabel = (Label^)control;
  118.                 if (iconLabel != nullptr)
  119.                 {
  120.                     int randomNumber = random->Next(icons->Count);
  121.                     iconLabel->Text = icons[randomNumber];
  122.                     iconLabel->ForeColor = iconLabel->BackColor;
  123.                     icons->RemoveAt(randomNumber);
  124.                 }
  125.             }
  126.         }
  127.  
  128.         private:void label_Click(Object ^sender, EventArgs ^e)
  129.         {
  130.             if (timer1->Enabled == true)
  131.                 return;
  132.  
  133.             Label ^clickedLabel = (Label^)sender;
  134.  
  135.             if (clickedLabel != nullptr)
  136.             {
  137.                 if (clickedLabel->ForeColor == Color::Black)
  138.                 // All done - leave the if statements.
  139.                 return;
  140.  
  141.  
  142.                 if (firstClicked == nullptr)
  143.                 {
  144.                     firstClicked = clickedLabel;
  145.                     firstClicked->ForeColor = Color::Black;
  146.                     return;
  147.                 }
  148.  
  149.                 secondClicked = clickedLabel;
  150.                 secondClicked->ForeColor = Color::Black;
  151.  
  152.                 CheckForWinner();
  153.  
  154.                 if (firstClicked->Text == secondClicked->Text)
  155.                 {
  156.                     firstClicked = nullptr;
  157.                     secondClicked = nullptr;
  158.                     return;
  159.                 }
  160.                 timer1->Start();
  161.             }
  162.         }
  163.  
  164.         private:void timer1_Tick(Object ^sender, EventArgs ^e)
  165.         {
  166.             timer1->Stop();
  167.  
  168.             firstClicked->ForeColor = firstClicked->BackColor;
  169.             secondClicked->ForeColor = secondClicked->BackColor;
  170.  
  171.             firstClicked = nullptr;
  172.             secondClicked = nullptr;
  173.         }
  174.  
  175.         private:void CheckForWinner()
  176.         {
  177.             for each (Control ^control in tableLayoutPanel1->Controls)
  178.             {
  179.                 Label ^iconLabel = (Label^)control;
  180.                
  181.                 if (iconLabel != nullptr)
  182.                 {
  183.                     if (iconLabel->ForeColor == iconLabel->BackColor)
  184.                         return;
  185.                 }
  186.             }
  187.             MessageBox::Show("You matched all the icons!", "Congratulations!");
  188.             Close();
  189.         }
  190.  
  191.     };
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement