#pragma once #include "pqueue.h" #include "robot.h" #include namespace PriorityQueue { 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 for Form1 /// /// WARNING: If you change the name of this class, you will need to change the /// 'Resource File Name' property for the managed resource compiler tool /// associated with all .resx files this class depends on. Otherwise, /// the designers will not be able to interact properly with localized /// resources associated with this form. /// public __gc class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); } protected: void Dispose(Boolean disposing) { if (disposing && components) { components->Dispose(); } __super::Dispose(disposing); } private: System::Windows::Forms::TextBox * LogBox; private: System::Windows::Forms::Button * StartBtn; private: System::Windows::Forms::Button * CloseBtn; private: System::Windows::Forms::Button * NextBtn; private: /// /// Required designer variable. /// RobotQueue *rq; System::ComponentModel::Container * components; /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// void InitializeComponent(void) { this->LogBox = new System::Windows::Forms::TextBox(); this->StartBtn = new System::Windows::Forms::Button(); this->CloseBtn = new System::Windows::Forms::Button(); this->NextBtn = new System::Windows::Forms::Button(); this->SuspendLayout(); // // LogBox // this->LogBox->Location = System::Drawing::Point(8, 8); this->LogBox->Multiline = true; this->LogBox->Name = S"LogBox"; this->LogBox->ReadOnly = true; this->LogBox->ScrollBars = System::Windows::Forms::ScrollBars::Vertical; this->LogBox->Size = System::Drawing::Size(544, 200); this->LogBox->TabIndex = 0; this->LogBox->Text = S""; // // StartBtn // this->StartBtn->Location = System::Drawing::Point(8, 216); this->StartBtn->Name = S"StartBtn"; this->StartBtn->TabIndex = 1; this->StartBtn->Text = S"Start"; this->StartBtn->Click += new System::EventHandler(this, StartBtn_Click); // // CloseBtn // this->CloseBtn->Location = System::Drawing::Point(480, 216); this->CloseBtn->Name = S"CloseBtn"; this->CloseBtn->TabIndex = 2; this->CloseBtn->Text = S"Exit"; this->CloseBtn->Click += new System::EventHandler(this, CloseBtn_Click); // // NextBtn // this->NextBtn->Enabled = false; this->NextBtn->Location = System::Drawing::Point(96, 216); this->NextBtn->Name = S"NextBtn"; this->NextBtn->TabIndex = 3; this->NextBtn->Text = S"Next"; this->NextBtn->Click += new System::EventHandler(this, NextBtn_Click); // // Form1 // this->AutoScaleBaseSize = System::Drawing::Size(5, 13); this->ClientSize = System::Drawing::Size(568, 246); this->Controls->Add(this->NextBtn); this->Controls->Add(this->CloseBtn); this->Controls->Add(this->StartBtn); this->Controls->Add(this->LogBox); this->MaximizeBox = false; this->Name = S"Form1"; this->Text = S"Priority Queue Testing"; this->ResumeLayout(false); } private: System::Void CloseBtn_Click(System::Object * sender, System::EventArgs * e) { Close(); } private: System::Void StartBtn_Click(System::Object * sender, System::EventArgs * e) { rq = new RobotQueue; Robot *tank; RobotQueue::iterator pos; srand( (unsigned)time( NULL ) ); for(int i=0;i<10;i++){ tank = new Robot(); rq->push(tank); LogBox->AppendText( String::Concat(S"Inserted robot with PR: ", tank->getPr().ToString(), S" and HP: ", tank->getHP().ToString(),S"\r\n")); } LogBox->AppendText(S"\r\n"); NextBtn->Enabled = true; } private: System::Void NextBtn_Click(System::Object * sender, System::EventArgs * e) { Robot *tank; RobotQueue::iterator pos; static int bnum = 1; int killed, hit; LogBox->AppendText(String::Concat(S"\r\nThere are ", rq->size().ToString(), S" robots left BEFORE battle #", bnum.ToString(), S".\r\n")); if( rq->size() > 1 ){ // the battle for(pos=rq->begin();pos!=rq->end();pos++){ tank = *pos; if( tank->alive() ){ hit = tank->fight(*rq); LogBox->AppendText( String::Concat(S" ** Robot PR:", tank->getPr().ToString(), S" hits robot PR:", hit.ToString(), S"\r\n" )); } } // remove all killed robots from the queue killed = 0; for(pos = rq->begin(); pos!=rq->end();){ tank = *pos; if( ! tank->alive() ){ LogBox->AppendText(String::Concat(S" -- removing robot: PR:", tank->getPr().ToString(), S" HP:", tank->getHP().ToString(), S".\r\n")); pos = rq->remove(pos); killed++; } else pos++; } LogBox->AppendText(String::Concat(killed.ToString(), S" robot(s) were killed in battle #", bnum.ToString(), S". ")); LogBox->AppendText(String::Concat(S"\r\nThere are ", rq->size().ToString(), S" robots left AFTER battle #", bnum.ToString(), S":\r\n")); for(pos=rq->begin();pos!=rq->end();pos++){ tank = *pos; LogBox->AppendText( String::Concat(S"Pr:", tank->getPr().ToString(), S" HP:", tank->getHP().ToString(), S"\r\n")); } bnum++; } NextBtn->Enabled = rq->size() > 1; } }; }