Hangman
#pragma
once
namespace
HangmanGame
{
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>
/// 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.
///
</summary>
public ref
class Form1 :
public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
///
<summary>
/// Clean up any resources being used.
///
</summary>
///
<param name="disposing">"description of
the parameter"</param>
private:
System::Windows::Forms::Panel^
pnlLetters;
private:
System::Windows::Forms::TextBox^
txtLetter10;
private:
System::Windows::Forms::TextBox^
txtLetter9;
private:
System::Windows::Forms::TextBox^
txtLetter8;
private:
System::Windows::Forms::TextBox^
txtLetter7;
private:
System::Windows::Forms::TextBox^
txtLetter6;
private:
System::Windows::Forms::TextBox^
txtLetter5;
private:
System::Windows::Forms::TextBox^
txtLetter4;
private:
System::Windows::Forms::TextBox^
txtLetter3;
private:
System::Windows::Forms::TextBox^
txtLetter2;
private:
System::Windows::Forms::TextBox^
txtLetter1;
private:
System::Windows::Forms::Label^
lblInput;
private:
System::Windows::Forms::TextBox^
txtInput;
private:
System::Windows::Forms::Button^
btnSubmit;
private:
System::Windows::Forms::GroupBox^
grpStats;
private:
System::Windows::Forms::Label^
lblNoMissed;
private:
System::Windows::Forms::Label^
lblGamesLost;
private:
System::Windows::Forms::Label^
lblGamesWon;
private:
System::Windows::Forms::Label^
lblGuesses;
private:
System::Windows::Forms::TextBox^
txtGamesLost;
private:
System::Windows::Forms::TextBox^
txtGamesWon;
private:
System::Windows::Forms::TextBox^
txtGuesses;
private:
System::Windows::Forms::TextBox^
txtMisses;
private:
System::Windows::Forms::PictureBox^
pbcHangmanBmp;
private:
System::Windows::Forms::ImageList^
imlHangmanBmps;
private:
System::ComponentModel::IContainer^
components;
protected:
private:
///
<summary>
/// Required designer variable.
///
</summary>
//Array to hold game words
array<String^>^ strWordListArray;
//String for game current word
String^ strGameWord;
//String for missed guesses
String^ strWrong;
//Counter for missed guesses
Int32 intMisses;
//Counter for correct guesses
Int32 intNoRight;
//Counter for games won
Int32 intGamesWon;
//Counter for games loss
Int32 intGamesLost;
//Controls display of hangman image
Int32 intHangmanNo;
#pragma
region
Windows Form Designer generated code
///
<summary>
/// Required method for Designer support -
do not modify
/// the contents of this method with the
code editor.
///
</summary>
void InitializeComponent(void)
{
this->components = (gcnew
System::ComponentModel::Container());
System::ComponentModel::ComponentResourceManager^
resources = (gcnew
System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->pnlLetters = (gcnew
System::Windows::Forms::Panel());
this->txtLetter10 = (gcnew
System::Windows::Forms::TextBox());
this->txtLetter9 = (gcnew
System::Windows::Forms::TextBox());
this->txtLetter8 = (gcnew
System::Windows::Forms::TextBox());
this->txtLetter7 = (gcnew
System::Windows::Forms::TextBox());
this->txtLetter6 = (gcnew
System::Windows::Forms::TextBox());
this->txtLetter5 = (gcnew
System::Windows::Forms::TextBox());
this->txtLetter4 = (gcnew
System::Windows::Forms::TextBox());
this->txtLetter3 = (gcnew
System::Windows::Forms::TextBox());
this->txtLetter2 = (gcnew
System::Windows::Forms::TextBox());
this->txtLetter1 = (gcnew
System::Windows::Forms::TextBox());
this->lblInput = (gcnew
System::Windows::Forms::Label());
this->txtInput = (gcnew
System::Windows::Forms::TextBox());
this->btnSubmit = (gcnew
System::Windows::Forms::Button());
this->grpStats = (gcnew
System::Windows::Forms::GroupBox());
this->txtGamesLost = (gcnew
System::Windows::Forms::TextBox());
this->txtGamesWon = (gcnew
System::Windows::Forms::TextBox());
this->txtGuesses = (gcnew
System::Windows::Forms::TextBox());
this->txtMisses = (gcnew
System::Windows::Forms::TextBox());
this->lblGamesLost = (gcnew
System::Windows::Forms::Label());
this->lblGamesWon = (gcnew
System::Windows::Forms::Label());
this->lblGuesses = (gcnew
System::Windows::Forms::Label());
this->lblNoMissed = (gcnew
System::Windows::Forms::Label());
this->pbcHangmanBmp = (gcnew
System::Windows::Forms::PictureBox());
this->imlHangmanBmps = (gcnew
System::Windows::Forms::ImageList(this->components));
this->pnlLetters->SuspendLayout();
this->grpStats->SuspendLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
>(this->pbcHangmanBmp))->BeginInit();
this->SuspendLayout();
//
// pnlLetters
//
this->pnlLetters->BackColor =
System::Drawing::SystemColors::Menu;
this->pnlLetters->BorderStyle =
System::Windows::Forms::BorderStyle::Fixed3D;
this->pnlLetters->Controls->Add(this->txtLetter10);
this->pnlLetters->Controls->Add(this->txtLetter9);
this->pnlLetters->Controls->Add(this->txtLetter8);
this->pnlLetters->Controls->Add(this->txtLetter7);
this->pnlLetters->Controls->Add(this->txtLetter6);
this->pnlLetters->Controls->Add(this->txtLetter5);
this->pnlLetters->Controls->Add(this->txtLetter4);
this->pnlLetters->Controls->Add(this->txtLetter3);
this->pnlLetters->Controls->Add(this->txtLetter2);
this->pnlLetters->Controls->Add(this->txtLetter1);
this->pnlLetters->Location =
System::Drawing::Point(12, 12);
this->pnlLetters->Name = L"pnlLetters";
this->pnlLetters->Size =
System::Drawing::Size(506, 74);
this->pnlLetters->TabIndex = 0;
//
// txtLetter10
//
this->txtLetter10->BackColor =
System::Drawing::SystemColors::Menu;
this->txtLetter10->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtLetter10->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtLetter10->ForeColor =
System::Drawing::SystemColors::MenuHighlight;
this->txtLetter10->Location =
System::Drawing::Point(456, 12);
this->txtLetter10->Name = L"txtLetter10";
this->txtLetter10->ReadOnly =
true;
this->txtLetter10->Size =
System::Drawing::Size(50, 55);
this->txtLetter10->TabIndex = 9;
this->txtLetter10->TabStop =
false;
//
// txtLetter9
//
this->txtLetter9->BackColor =
System::Drawing::SystemColors::Menu;
this->txtLetter9->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtLetter9->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtLetter9->ForeColor =
System::Drawing::SystemColors::MenuHighlight;
this->txtLetter9->Location =
System::Drawing::Point(406, 12);
this->txtLetter9->Name = L"txtLetter9";
this->txtLetter9->ReadOnly =
true;
this->txtLetter9->Size =
System::Drawing::Size(50, 55);
this->txtLetter9->TabIndex = 8;
this->txtLetter9->TabStop =
false;
//
// txtLetter8
//
this->txtLetter8->AcceptsTab =
true;
this->txtLetter8->BackColor =
System::Drawing::SystemColors::Menu;
this->txtLetter8->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtLetter8->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtLetter8->ForeColor =
System::Drawing::SystemColors::MenuHighlight;
this->txtLetter8->Location =
System::Drawing::Point(356, 12);
this->txtLetter8->Name = L"txtLetter8";
this->txtLetter8->ReadOnly =
true;
this->txtLetter8->Size =
System::Drawing::Size(50, 55);
this->txtLetter8->TabIndex = 7;
this->txtLetter8->TabStop =
false;
//
// txtLetter7
//
this->txtLetter7->BackColor =
System::Drawing::SystemColors::Menu;
this->txtLetter7->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtLetter7->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtLetter7->ForeColor =
System::Drawing::SystemColors::MenuHighlight;
this->txtLetter7->Location =
System::Drawing::Point(306, 12);
this->txtLetter7->Name = L"txtLetter7";
this->txtLetter7->ReadOnly =
true;
this->txtLetter7->Size =
System::Drawing::Size(50, 55);
this->txtLetter7->TabIndex = 6;
this->txtLetter7->TabStop =
false;
//
// txtLetter6
//
this->txtLetter6->BackColor =
System::Drawing::SystemColors::Menu;
this->txtLetter6->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtLetter6->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtLetter6->ForeColor =
System::Drawing::SystemColors::MenuHighlight;
this->txtLetter6->Location =
System::Drawing::Point(256, 12);
this->txtLetter6->Name = L"txtLetter6";
this->txtLetter6->ReadOnly =
true;
this->txtLetter6->Size =
System::Drawing::Size(50, 55);
this->txtLetter6->TabIndex = 5;
this->txtLetter6->TabStop =
false;
//
// txtLetter5
//
this->txtLetter5->BackColor =
System::Drawing::SystemColors::Menu;
this->txtLetter5->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtLetter5->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtLetter5->ForeColor =
System::Drawing::SystemColors::MenuHighlight;
this->txtLetter5->Location =
System::Drawing::Point(206, 12);
this->txtLetter5->Name = L"txtLetter5";
this->txtLetter5->ReadOnly =
true;
this->txtLetter5->Size =
System::Drawing::Size(50, 55);
this->txtLetter5->TabIndex = 4;
this->txtLetter5->TabStop =
false;
//
// txtLetter4
//
this->txtLetter4->BackColor =
System::Drawing::SystemColors::Menu;
this->txtLetter4->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtLetter4->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtLetter4->ForeColor =
System::Drawing::SystemColors::MenuHighlight;
this->txtLetter4->Location =
System::Drawing::Point(156, 12);
this->txtLetter4->Name = L"txtLetter4";
this->txtLetter4->ReadOnly =
true;
this->txtLetter4->Size =
System::Drawing::Size(50, 55);
this->txtLetter4->TabIndex = 3;
this->txtLetter4->TabStop =
false;
//
// txtLetter3
//
this->txtLetter3->BackColor =
System::Drawing::SystemColors::Menu;
this->txtLetter3->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtLetter3->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtLetter3->ForeColor =
System::Drawing::SystemColors::MenuHighlight;
this->txtLetter3->Location =
System::Drawing::Point(106, 12);
this->txtLetter3->Name = L"txtLetter3";
this->txtLetter3->ReadOnly =
true;
this->txtLetter3->Size =
System::Drawing::Size(50, 55);
this->txtLetter3->TabIndex = 2;
this->txtLetter3->TabStop =
false;
//
// txtLetter2
//
this->txtLetter2->BackColor =
System::Drawing::SystemColors::Menu;
this->txtLetter2->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtLetter2->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtLetter2->ForeColor =
System::Drawing::SystemColors::MenuHighlight;
this->txtLetter2->Location =
System::Drawing::Point(56, 12);
this->txtLetter2->Name = L"txtLetter2";
this->txtLetter2->ReadOnly =
true;
this->txtLetter2->Size =
System::Drawing::Size(50, 55);
this->txtLetter2->TabIndex = 1;
this->txtLetter2->TabStop =
false;
//
// txtLetter1
//
this->txtLetter1->BackColor =
System::Drawing::SystemColors::Menu;
this->txtLetter1->BorderStyle =
System::Windows::Forms::BorderStyle::None;
this->txtLetter1->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
36, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtLetter1->ForeColor =
System::Drawing::SystemColors::MenuHighlight;
this->txtLetter1->Location =
System::Drawing::Point(6, 12);
this->txtLetter1->Name = L"txtLetter1";
this->txtLetter1->ReadOnly =
true;
this->txtLetter1->Size =
System::Drawing::Size(50, 55);
this->txtLetter1->TabIndex = 0;
this->txtLetter1->TabStop =
false;
//
// lblInput
//
this->lblInput->AutoSize =
true;
this->lblInput->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
8.25F, System::Drawing::FontStyle::Bold,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->lblInput->Location =
System::Drawing::Point(16, 129);
this->lblInput->Name = L"lblInput";
this->lblInput->Size =
System::Drawing::Size(125, 13);
this->lblInput->TabIndex = 1;
this->lblInput->Text = L"Enter
A Letter Guess:";
//
// txtInput
//
this->txtInput->CharacterCasing =
System::Windows::Forms::CharacterCasing::Upper;
this->txtInput->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
18, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->txtInput->Location =
System::Drawing::Point(147, 118);
this->txtInput->Name = L"txtInput";
this->txtInput->Size =
System::Drawing::Size(59, 35);
this->txtInput->TabIndex = 2;
//
// btnSubmit
//
this->btnSubmit->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
8.25F, System::Drawing::FontStyle::Bold,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->btnSubmit->Location =
System::Drawing::Point(212, 124);
this->btnSubmit->Name = L"btnSubmit";
this->btnSubmit->Size =
System::Drawing::Size(75, 23);
this->btnSubmit->TabIndex = 3;
this->btnSubmit->Text = L"Submit";
this->btnSubmit->Click +=
gcnew System::EventHandler(this,
&Form1::btnSubmit_Click);
//
// grpStats
//
this->grpStats->Controls->Add(this->txtGamesLost);
this->grpStats->Controls->Add(this->txtGamesWon);
this->grpStats->Controls->Add(this->txtGuesses);
this->grpStats->Controls->Add(this->txtMisses);
this->grpStats->Controls->Add(this->lblGamesLost);
this->grpStats->Controls->Add(this->lblGamesWon);
this->grpStats->Controls->Add(this->lblGuesses);
this->grpStats->Controls->Add(this->lblNoMissed);
this->grpStats->Location =
System::Drawing::Point(12, 173);
this->grpStats->Name = L"grpStats";
this->grpStats->Size =
System::Drawing::Size(300, 132);
this->grpStats->TabIndex = 4;
this->grpStats->TabStop =
false;
this->grpStats->Text = L"Game
Stats:";
//
// txtGamesLost
//
this->txtGamesLost->Location =
System::Drawing::Point(127, 101);
this->txtGamesLost->Name = L"txtGamesLost";
this->txtGamesLost->ReadOnly =
true;
this->txtGamesLost->Size =
System::Drawing::Size(100, 20);
this->txtGamesLost->TabIndex = 7;
this->txtGamesLost->TabStop =
false;
//
// txtGamesWon
//
this->txtGamesWon->Location =
System::Drawing::Point(127, 73);
this->txtGamesWon->Name = L"txtGamesWon";
this->txtGamesWon->ReadOnly =
true;
this->txtGamesWon->Size =
System::Drawing::Size(100, 20);
this->txtGamesWon->TabIndex = 6;
this->txtGamesWon->TabStop =
false;
//
// txtGuesses
//
this->txtGuesses->Location =
System::Drawing::Point(65, 44);
this->txtGuesses->Name = L"txtGuesses";
this->txtGuesses->ReadOnly =
true;
this->txtGuesses->Size =
System::Drawing::Size(229, 20);
this->txtGuesses->TabIndex = 5;
this->txtGuesses->TabStop =
false;
//
// txtMisses
//
this->txtMisses->Location =
System::Drawing::Point(97, 13);
this->txtMisses->Name = L"txtMisses";
this->txtMisses->ReadOnly =
true;
this->txtMisses->Size =
System::Drawing::Size(100, 20);
this->txtMisses->TabIndex = 4;
this->txtMisses->TabStop =
false;
//
// lblGamesLost
//
this->lblGamesLost->AutoSize =
true;
this->lblGamesLost->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
8.25F, System::Drawing::FontStyle::Bold,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->lblGamesLost->Location =
System::Drawing::Point(4, 101);
this->lblGamesLost->Name = L"lblGamesLost";
this->lblGamesLost->Size =
System::Drawing::Size(112, 13);
this->lblGamesLost->TabIndex = 3;
this->lblGamesLost->Text = L"No.
of Games Lost:";
//
// lblGamesWon
//
this->lblGamesWon->AutoSize =
true;
this->lblGamesWon->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
8.25F, System::Drawing::FontStyle::Bold,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->lblGamesWon->Location =
System::Drawing::Point(7, 76);
this->lblGamesWon->Name = L"lblGamesWon";
this->lblGamesWon->Size =
System::Drawing::Size(114, 13);
this->lblGamesWon->TabIndex = 2;
this->lblGamesWon->Text = L"No.
of Games Won:";
//
// lblGuesses
//
this->lblGuesses->AutoSize =
true;
this->lblGuesses->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
8.25F, System::Drawing::FontStyle::Bold,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->lblGuesses->Location =
System::Drawing::Point(7, 47);
this->lblGuesses->Name = L"lblGuesses";
this->lblGuesses->Size =
System::Drawing::Size(51, 13);
this->lblGuesses->TabIndex = 1;
this->lblGuesses->Text = L"Guesses";
//
// lblNoMissed
//
this->lblNoMissed->AutoSize =
true;
this->lblNoMissed->Font = (gcnew
System::Drawing::Font(L"Microsoft Sans Serif",
8.25F, System::Drawing::FontStyle::Bold,
System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->lblNoMissed->Location =
System::Drawing::Point(7, 16);
this->lblNoMissed->Name = L"lblNoMissed";
this->lblNoMissed->Size =
System::Drawing::Size(84, 13);
this->lblNoMissed->TabIndex = 0;
this->lblNoMissed->Text = L"No.
of Misses:";
//
// pbcHangmanBmp
//
this->pbcHangmanBmp->BorderStyle =
System::Windows::Forms::BorderStyle::Fixed3D;
this->pbcHangmanBmp->Location =
System::Drawing::Point(318, 105);
this->pbcHangmanBmp->Name = L"pbcHangmanBmp";
this->pbcHangmanBmp->Size =
System::Drawing::Size(200, 196);
this->pbcHangmanBmp->SizeMode =
System::Windows::Forms::PictureBoxSizeMode::StretchImage;
this->pbcHangmanBmp->TabIndex = 5;
this->pbcHangmanBmp->TabStop =
false;
//
// imlHangmanBmps
//
this->imlHangmanBmps->ImageStream =
(cli::safe_cast<System::Windows::Forms::ImageListStreamer^
>(resources->GetObject(L"imlHangmanBmps.ImageStream")));
this->imlHangmanBmps->Images->SetKeyName(0,
L"100.bmp");
this->imlHangmanBmps->Images->SetKeyName(1,
L"80.bmp");
this->imlHangmanBmps->Images->SetKeyName(2,
L"60.bmp");
this->imlHangmanBmps->Images->SetKeyName(3,
L"40.bmp");
this->imlHangmanBmps->Images->SetKeyName(4,
L"20.bmp");
this->imlHangmanBmps->Images->SetKeyName(5,
L"10.bmp");
this->imlHangmanBmps->Images->SetKeyName(6,
L"0.bmp");
//
// Form1
//
this->AutoScaleDimensions =
System::Drawing::SizeF(6, 13);
this->AutoScaleMode =
System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize =
System::Drawing::Size(530, 324);
this->Controls->Add(this->pbcHangmanBmp);
this->Controls->Add(this->grpStats);
this->Controls->Add(this->btnSubmit);
this->Controls->Add(this->txtInput);
this->Controls->Add(this->lblInput);
this->Controls->Add(this->pnlLetters);
this->Cursor =
System::Windows::Forms::Cursors::Hand;
this->FormBorderStyle =
System::Windows::Forms::FormBorderStyle::Fixed3D;
this->MaximizeBox =
false;
this->MinimizeBox =
false;
this->Name = L"Form1";
this->ShowIcon =
false;
this->StartPosition =
System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"Hangman";
this->Load +=
gcnew System::EventHandler(this,
&Form1::Form1_Load);
this->pnlLetters->ResumeLayout(false);
this->pnlLetters->PerformLayout();
this->grpStats->ResumeLayout(false);
this->grpStats->PerformLayout();
(cli::safe_cast<System::ComponentModel::ISupportInitialize^
>(this->pbcHangmanBmp))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma
endregion
private:
System::Void Form1_Load(System::Object^
sender, System::EventArgs^
e) {
strGameWord =
gcnew String("");
strWrong =
gcnew String("");
intGamesWon = 0;
intGamesLost = 0;
intHangmanNo = 6;
//Populate
the array with words
LoadWordArray();
//Set
up the game's starting values
InitializeGameStats();
StartTheGame();
//Start the game
}
private:
Void LoadWordArray( Void ) {
const
Int32 cintArraySize = 20;
strWordListArray = \
gcnew
array<String^>(cintArraySize);
strWordListArray->SetValue(
"ELEPHANT", 0 );
strWordListArray->SetValue(
"CARRIAGE", 1 );
strWordListArray->SetValue(
"ENVELOPE", 2 );
strWordListArray->SetValue(
"PRESIDENT", 3 );
strWordListArray->SetValue(
"ELLIPTICAL", 4 );
strWordListArray->SetValue(
"MARKER", 5 );
strWordListArray->SetValue(
"BATTLESHIP", 6 );
strWordListArray->SetValue(
"TERMINATE", 7 );
strWordListArray->SetValue(
"REJOICE", 8 );
strWordListArray->SetValue(
"LIBERTY", 9 );
strWordListArray->SetValue(
"CLASSIC", 10 );
strWordListArray->SetValue(
"REFLEX", 11 );
strWordListArray->SetValue(
"VERSION", 12 );
strWordListArray->SetValue(
"DWELLING", 13 );
strWordListArray->SetValue(
"APARTMENT", 14 );
strWordListArray->SetValue(
"MATRIX", 15 );
strWordListArray->SetValue(
"FAUCET", 16 );
strWordListArray->SetValue(
"BEDROOM", 17 );
strWordListArray->SetValue(
"VOLLEY", 18 );
strWordListArray->SetValue(
"WHISTLE", 19 );
}
private:
Void InitializeGameStats( Void ) {
//Reset
and display games won and lost
intGamesWon = 0;
intGamesLost = 0;
txtGamesWon->Text =
intGamesWon.ToString();
txtGamesLost->Text =
intGamesLost.ToString();
}
private:
Void StartTheGame( Void ) {
//Reset game settings
ResetDefaultSettings();
//Get a word for the player to guess
RetrieveWord();
//Format the display of the mystery word
FormatGameWord();
//Prepare to accept the player's guess
txtInput->Focus();
}
private:
Void ResetDefaultSettings( Void ) {
//Reset
all game settings
intMisses = 0;
intNoRight = 0;
txtGuesses->Text = "";
txtMisses->Text = intMisses.ToString();
txtInput->Text = "";
intHangmanNo = 6;
pbcHangmanBmp->Image =\
imlHangmanBmps->Images->default[intHangmanNo];
}
private:
Void RetrieveWord( Void ) {
//Seed random number generator with
current time
DateTime moment = DateTime::Now;
Random^ randNumGen = gcnew Random(
moment.Millisecond );
//Declare a variable to hold a randomly
//
generated number
Int32 intRandomNo = 0;
//Use the Random object's Next method to
retrieve
//
a number between zero
and the number
//
of words in the array
intRandomNo = randNumGen->Next( strWordListArray->Length );
//Use the random number to select a word
from
// the array
strGameWord = \
strWordListArray->GetValue(intRandomNo)->ToString();
}
private:
Void FormatGameWord( Void ) {
//Game words are limited to ten or less
characters.
//
Each letter is stored in a separate Textbox
//
control. Start formatting by setting the value
//
stored in each Textbox control to a blank character
txtLetter1->Text = "";
txtLetter2->Text = "";
txtLetter3->Text = "";
txtLetter4->Text = "";
txtLetter5->Text = "";
txtLetter6->Text = "";
txtLetter7->Text = "";
txtLetter8->Text = "";
txtLetter9->Text = "";
txtLetter10->Text = "";
//Loop once for each word in the current
letter and
//
set an underscore in the appropriate text box
for( Int32 intCounter = 0;
intCounter <= strGameWord->Length;
intCounter++ )
{
if( intCounter == 1 )
txtLetter1->Text = "_";
if( intCounter == 2 )
txtLetter2->Text = "_";
if( intCounter == 3 )
txtLetter3->Text = "_";
if( intCounter == 4 )
txtLetter4->Text = "_";
if( intCounter == 5 )
txtLetter5->Text = "_";
if( intCounter == 6 )
txtLetter6->Text = "_";
if( intCounter == 7 )
txtLetter7->Text = "_";
if( intCounter == 8 )
txtLetter8->Text = "_";
if( intCounter == 9 )
txtLetter9->Text = "_";
if( intCounter == 10 )
txtLetter10->Text = "_";
}
}
private:
System::Void btnSubmit_Click(System::Object^
sender, System::EventArgs^
e) {
//Make sure input is valid
if( !PlayerInputValid() )
return;
//Exit on invalid guess
//If letter already guessed, display error
if( txtGuesses->Text->Contains(
txtInput->Text->ToUpper() ) )
MessageBox::Show("Error:"
"That letter has already been guessed.");
else
//Check if a correct letter was guessed
{
//Add letter to list of already guessed
letters
txtGuesses->Text = String::Concat( txtGuesses->Text,
txtInput->Text );
//Look for the letter in the word
if( !strGameWord->Contains(
txtInput->Text->ToUpper() ) )
{
//The player's guess was wrong
//Append letter to the list of missed
guesses
strWrong = String::Concat( strWrong,
" ", txtInput->Text->ToUpper() );
//Update
and display missed guesses
intMisses += 1;
txtMisses->Text = intMisses.ToString();
//Reduce counter used to control hangman
pic and
//
update graphic
intHangmanNo -= 1;
pbcHangmanBmp->Image =\
imlHangmanBmps->Images->default[intHangmanNo];
//End game if six wrong guesses
if( intMisses == 6 )
{
//Update lost games and inform player
intGamesLost += 1;
txtGamesLost->Text = intGamesLost.ToString();
//Call procedure to see if player wants to
play again
MessageBox::Show( "Sorry. You have
lost." );
PromptForNewGame();
return;
}
}
//Use for loop to check each letter in the
word
for( Int32 intCounter = 0;
intCounter <=
strGameWord->Length;
intCounter++ )
{
//Select one letter at a time
if( String::Compare( strGameWord,
intCounter,
txtInput->Text, 0, true ) == 0 )
{
//Update and display guesses
intNoRight += 1;
FlipGameLetter( intCounter );
}
}
}
//See if the player has guessed every
letter in the word
if( intNoRight ==
strGameWord->Length )
{
intGamesWon += 1;
txtGamesWon->Text = intGamesWon.ToString();
//Call procedure to see if player wants to
play again
MessageBox::Show( "Congratulations. You
have Won!" );
PromptForNewGame();
}
txtInput->Text = "";
txtInput->Focus();
}
private:
Boolean PlayerInputValid( Void ) {
//Create a list of letter to compare input to
String^ strAlphabet = gcnew String(
\
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
//See if the player clicked on Submit
without
//
entering a guess
if( txtInput->Text ==
"" )
{
MessageBox::Show("Error: You must enter
a letter.");
txtInput->Text = "";
txtInput->Focus();
return
false;
}
//See if the player entered more than one
character
if( txtInput->Text->Length > 1 )
{
MessageBox::Show("Error: Please enter
only 1 letter." );
txtInput->Text = "";
txtInput->Focus();
return
false;
}
//See if the player entered a number
if( !strAlphabet->Contains(
txtInput->Text ) )
{
MessageBox::Show("Error: Please enter a
letter.");
txtInput->Text = "";
txtInput->Focus();
return
false;
}
return
true;
}
private:
Void FlipGameLetter( Int32 intLetterNumber ) {
//Match the letter to be displayed with
the
//
correct text box and show the letter
switch( intLetterNumber )
{
case 0 :
txtLetter1->Text = txtInput->Text->ToUpper();
break;
case 1 :
txtLetter2->Text = txtInput->Text->ToUpper();
break;
case 2 :
txtLetter3->Text = txtInput->Text->ToUpper();
break;
case 3 :
txtLetter4->Text = txtInput->Text->ToUpper();
break;
case 4 :
txtLetter5->Text = txtInput->Text->ToUpper();
break;
case 5 :
txtLetter6->Text = txtInput->Text->ToUpper();
break;
case 6 :
txtLetter7->Text = txtInput->Text->ToUpper();
break;
case 7 :
txtLetter8->Text = txtInput->Text->ToUpper();
break;
case 8 :
txtLetter9->Text = txtInput->Text->ToUpper();
break;
case 9 :
txtLetter10->Text = txtInput->Text->ToUpper();
break;
}
}
private:
Void PromptForNewGame( Void ) {
//Variable used to hold player response
Windows::Forms::DialogResult drPlayAgain;
//Prompt player to try again
drPlayAgain = MessageBox::Show(
"Would you like to play again?",
"Hangman",
MessageBoxButtons::YesNo, MessageBoxIcon::Question,
MessageBoxDefaultButton::Button1 );
//if( player clicks on Yes set up a new
game
if( drPlayAgain ==
Windows::Forms::DialogResult::Yes )
StartTheGame(); //Start a new game
else //
End the game
this->Close();
}
};
}