Im working on a tic tac toe game in visual basic C++.net. i\'ve pretty much done
ID: 3625753 • Letter: I
Question
Im working on a tic tac toe game in visual basic C++.net. i've pretty much done the coding for the X and O but im having trouble with checking the winner. the rest of the code are working. this is what im having troube with /// void checkwin();
void Form1::checkwin()
{
if((button1->Text = "X") && (button2->Text = "X") && (button3->Text = "X"));
{
MessageBox::Show("X wins");
}
else if ((button4->Text = "X") && (button5->Text = "X") && (button6->Text = "X"));
{
MessageBox::Show("X wins");
}
else if ((button7->Text = "X") && (button8->Text = "X") && (button9->Text = "X"));
} //same for the rest of buttons // this can be found at the end of the code..im not sure if i placed these in the correct place..i haven't included the code for the scores yet, in which i used labels. can anyone help me with the code for the checking who wins and keeping the score? thanks in advance...been trying to figure this out, but still having problems...
#pragma once
namespace TTT {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
bool turn = 0; //whose turn it is
///
/// Summary for Form1
///
public ref class Form1 : public System::Windows::Forms::Form
{
public:
void checkwin();
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
///
/// Clean up any resources being used.
///
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::Button^ button5;
private: System::Windows::Forms::Button^ button6;
private: System::Windows::Forms::Button^ button7;
private: System::Windows::Forms::Button^ button8;
private: System::Windows::Forms::Button^ button9;
private: System::Windows::Forms::Button^ button10;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Button^ button11;
private: System::Windows::Forms::GroupBox^ groupBox1;
private: System::Windows::Forms::Label^ label7;
private: System::Windows::Forms::Label^ label6;
private: System::Windows::Forms::Label^ label5;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::PictureBox^ pictureBox1;
protected:
private:
///
/// Required designer variable.
///
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
void InitializeComponent(void)
{
System::ComponentModel::ComponentResourceManager^ resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->button5 = (gcnew System::Windows::Forms::Button());
this->button6 = (gcnew System::Windows::Forms::Button());
this->button7 = (gcnew System::Windows::Forms::Button());
this->button8 = (gcnew System::Windows::Forms::Button());
this->button9 = (gcnew System::Windows::Forms::Button());
this->button10 = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->label2 = (gcnew System::Windows::Forms::Label());
this->button11 = (gcnew System::Windows::Forms::Button());
this->groupBox1 = (gcnew System::Windows::Forms::GroupBox());
this->label7 = (gcnew System::Windows::Forms::Label());
this->label6 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
this->groupBox1->SuspendLayout();
(cli::safe_cast(this->pictureBox1))->BeginInit();
this->SuspendLayout();
//
// button1
//
this->button1->BackColor = System::Drawing::Color::White;
this->button1->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 18, static_cast(((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)
| System::Drawing::FontStyle::Strikeout)), System::Drawing::GraphicsUnit::Point, static_cast(0)));
this->button1->ForeColor = System::Drawing::Color::Black;
this->button1->Location = System::Drawing::Point(33, 117);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(58, 53);
this->button1->TabIndex = 0;
this->button1->UseVisualStyleBackColor = false;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// button2
//
this->button2->BackColor = System::Drawing::Color::White;
this->button2->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 18, static_cast(((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)
| System::Drawing::FontStyle::Strikeout)), System::Drawing::GraphicsUnit::Point, static_cast(0)));
this->button2->ForeColor = System::Drawing::Color::Black;
this->button2->Location = System::Drawing::Point(112, 117);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(58, 53);
this->button2->TabIndex = 1;
this->button2->UseVisualStyleBackColor = false;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// button3
//
this->button3->BackColor = System::Drawing::Color::White;
this->button3->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 18, static_cast(((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)
| System::Drawing::FontStyle::Strikeout)), System::Drawing::GraphicsUnit::Point, static_cast(0)));
this->button3->ForeColor = System::Drawing::Color::Black;
this->button3->Location = System::Drawing::Point(190, 117);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(58, 53);
this->button3->TabIndex = 2;
this->button3->UseVisualStyleBackColor = false;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// button4
//
this->button4->BackColor = System::Drawing::Color::White;
this->button4->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 18, static_cast(((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)
| System::Drawing::FontStyle::Strikeout)), System::Drawing::GraphicsUnit::Point, static_cast(0)));
this->button4->ForeColor = System::Drawing::Color::Black;
this->button4->Location = System::Drawing::Point(33, 187);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(58, 53);
this->button4->TabIndex = 3;
this->button4->UseVisualStyleBackColor = false;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// button5
//
this->button5->BackColor = System::Drawing::Color::White;
this->button5->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 18, static_cast(((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)
| System::Drawing::FontStyle::Strikeout)), System::Drawing::GraphicsUnit::Point, static_cast(0)));
this->button5->ForeColor = System::Drawing::Color::Black;
this->button5->Location = System::Drawing::Point(112, 187);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(58, 53);
this->button5->TabIndex = 4;
this->button5->UseVisualStyleBackColor = false;
this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);
//
// button6
//
this->button6->BackColor = System::Drawing::Color::White;
this->button6->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 18, static_cast(((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)
| System::Drawing::FontStyle::Strikeout)), System::Drawing::GraphicsUnit::Point, static_cast(0)));
this->button6->ForeColor = System::Drawing::Color::Black;
this->button6->Location = System::Drawing::Point(190, 187);
this->button6->Name = L"button6";
this->button6->Size = System::Drawing::Size(58, 53);
this->button6->TabIndex = 5;
this->button6->UseVisualStyleBackColor = false;
this->button6->Click += gcnew System::EventHandler(this, &Form1::button6_Click);
//
// button7
//
this->button7->BackColor = System::Drawing::Color::White;
this->button7->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 18, static_cast(((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)
| System::Drawing::FontStyle::Strikeout)), System::Drawing::GraphicsUnit::Point, static_cast(0)));
this->button7->ForeColor = System::Drawing::Color::Black;
this->button7->Location = System::Drawing::Point(33, 263);
this->button7->Name = L"button7";
this->button7->Size = System::Drawing::Size(58, 53);
this->button7->TabIndex = 6;
this->button7->UseVisualStyleBackColor = false;
this->button7->Click += gcnew System::EventHandler(this, &Form1::button7_Click);
//
// button8
//
this->button8->BackColor = System::Drawing::Color::White;
this->button8->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 18, static_cast(((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)
| System::Drawing::FontStyle::Strikeout)), System::Drawing::GraphicsUnit::Point, static_cast(0)));
this->button8->ForeColor = System::Drawing::Color::Black;
this->button8->Location = System::Drawing::Point(112, 263);
this->button8->Name = L"button8";
this->button8->Size = System::Drawing::Size(58, 53);
this->button8->TabIndex = 7;
this->button8->UseVisualStyleBackColor = false;
this->button8->Click += gcnew System::EventHandler(this, &Form1::button8_Click);
//
// button9
//
this->button9->BackColor = System::Drawing::Color::White;
this->button9->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 18, static_cast(((System::Drawing::FontStyle::Bold | System::Drawing::FontStyle::Italic)
| System::Drawing::FontStyle::Strikeout)), System::Drawing::GraphicsUnit::Point, static_cast(0)));
this->button9->ForeColor = System::Drawing::Color::Black;
this->button9->Location = System::Drawing::Point(190, 263);
this->button9->Name = L"button9";
this->button9->Size = System::Drawing::Size(58, 53);
this->button9->TabIndex = 8;
this->button9->UseVisualStyleBackColor = false;
this->button9->Click += gcnew System::EventHandler(this, &Form1::button9_Click);
//
// button10
//
this->button10->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 8.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast(0)));
this->button10->Location = System::Drawing::Point(321, 294);
this->button10->Name = L"button10";
this->button10->Size = System::Drawing::Size(55, 44);
this->button10->TabIndex = 9;
this->button10->Text = L"New Game";
this->button10->UseVisualStyleBackColor = true;
this->button10->Click += gcnew System::EventHandler(this, &Form1::button10_Click);
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 14.25F, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point,
static_cast(0)));
this->label1->ForeColor = System::Drawing::Color::White;
this->label1->Location = System::Drawing::Point(305, 105);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(136, 31);
this->label1->TabIndex = 10;
this->label1->Text = L"Player Turn : ";
//
// label2
//
this->label2->AutoSize = true;
this->label2->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast(0)));
this->label2->ForeColor = System::Drawing::Color::White;
this->label2->Location = System::Drawing::Point(447, 105);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(26, 31);
this->label2->TabIndex = 11;
this->label2->Text = L"X";
this->label2->Click += gcnew System::EventHandler(this, &Form1::label2_Click);
//
// button11
//
this->button11->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 9.75F, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point,
static_cast(0)));
this->button11->Location = System::Drawing::Point(391, 293);
this->button11->Name = L"button11";
this->button11->Size = System::Drawing::Size(50, 45);
this->button11->TabIndex = 12;
this->button11->Text = L"Quit";
this->button11->UseVisualStyleBackColor = true;
this->button11->Click += gcnew System::EventHandler(this, &Form1::button11_Click);
//
// groupBox1
//
this->groupBox1->Controls->Add(this->label7);
this->groupBox1->Controls->Add(this->label6);
this->groupBox1->Controls->Add(this->label5);
this->groupBox1->Controls->Add(this->label4);
this->groupBox1->Font = (gcnew System::Drawing::Font(L"Viner Hand ITC", 14.25F, System::Drawing::FontStyle::Italic, System::Drawing::GraphicsUnit::Point,
static_cast(0)));
this->groupBox1->ForeColor = System::Drawing::Color::White;
this->groupBox1->Location = System::Drawing::Point(306, 148);
this->groupBox1->Name = L"groupBox1";
this->groupBox1->Size = System::Drawing::Size(198, 109);
this->groupBox1->TabIndex = 13;
this->groupBox1->TabStop = false;
this->groupBox1->Text = L"Scoreboard";
//
// label7
//
this->label7->AutoSize = true;
this->label7->Location = System::Drawing::Point(154, 72);
this->label7->Name = L"label7";
this->label7->Size = System::Drawing::Size(22, 31);
this->label7->TabIndex = 3;
this->label7->Text = L"0";
//
// label6
//
this->label6->AutoSize = true;
this->label6->Location = System::Drawing::Point(35, 72);
this->label6->Name = L"label6";
this->label6->Size = System::Drawing::Size(97, 31);
this->label6->TabIndex = 2;
this->label6->Text = L"Player O :";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(154, 28);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(22, 31);
this->label5->TabIndex = 1;
this->label5->Text = L"0";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(35, 28);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(100, 31);
this->label4->TabIndex = 0;
this->label4->Text = L"Player X :";
//
// pictureBox1
//
this->pictureBox1->Cursor = System::Windows::Forms::Cursors::Hand;
this->pictureBox1->Image = (cli::safe_cast(resources->GetObject(L"pictureBox1.Image")));
this->pictureBox1->Location = System::Drawing::Point(100, 12);
this->pictureBox1->Name = L"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(341, 62);
this->pictureBox1->TabIndex = 15;
this->pictureBox1->TabStop = false;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::Black;
this->ClientSize = System::Drawing::Size(571, 384);
this->Controls->Add(this->pictureBox1);
this->Controls->Add(this->groupBox1);
this->Controls->Add(this->button11);
this->Controls->Add(this->label2);
this->Controls->Add(this->label1);
this->Controls->Add(this->button10);
this->Controls->Add(this->button9);
this->Controls->Add(this->button8);
this->Controls->Add(this->button7);
this->Controls->Add(this->button6);
this->Controls->Add(this->button5);
this->Controls->Add(this->button4);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Tic Tac Toe";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->groupBox1->ResumeLayout(false);
this->groupBox1->PerformLayout();
(cli::safe_cast(this->pictureBox1))->EndInit();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
if (turn == 0)
{
button1->Text = "X";
label2->Text = "O"; //changes the label and tell whose turn it is to play
turn = 1; // changes the player
button1->Enabled= false; //disables the button so it can't be used twice
}
else
{
button1->Text="O";
label2->Text="X"; //changes the labe and tell whose turn it is to play
turn = 0; //changes the player
button1->Enabled = false; //disables the button so it can't be used twice
}
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
if (turn == 0)
{
button2->Text = "X";
label2->Text = "O";
turn = 1;
button2->Enabled= false;
}
else
{
button2->Text="O";
label2->Text = "X";
turn = 0;
button2->Enabled = false;
}
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
if (turn == 0)
{
button3->Text = "X";
label2->Text = "O";
turn = 1;
button3->Enabled= false;
}
else
{
button3->Text="O";
label2->Text = "X";
turn = 0;
button3->Enabled = false;
}
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
{
if (turn == 0)
{
button4->Text = "X";
label2->Text = "O";
turn = 1;
button4->Enabled= false;
}
else
{
button4->Text="O";
label2->Text="X";
turn = 0;
button4->Enabled = false;
}
}
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
{
if (turn == 0)
{
button5->Text = "X";
label2->Text = "O";
turn = 1;
button5->Enabled= false;
}
else
{
button5->Text="O";
label2->Text = "X";
turn = 0;
button5->Enabled = false;
}
}
private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e)
{
if (turn == 0)
{
button6->Text = "X";
label2->Text = "O";
turn = 1;
button6->Enabled= false;
}
else
{
button6->Text="O";
label2->Text = "X";
turn = 0;
button6->Enabled = false;
}
}
private: System::Void button7_Click(System::Object^ sender, System::EventArgs^ e)
{
if (turn == 0)
{
button7->Text = "X";
label2->Text = "O";
turn = 1;
button7->Enabled= false;
}
else
{
button7->Text="O";
label2->Text = "X";
turn = 0;
button7->Enabled = false;
}
}
private: System::Void button8_Click(System::Object^ sender, System::EventArgs^ e)
{
if (turn == 0)
{
button8->Text = "X";
label2->Text = "O";
turn = 1;
button8->Enabled= false;
}
else
{
button8->Text="O";
label2->Text = "X";
turn = 0;
button8->Enabled = false;
}
}
private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e)
{
if (turn == 0)
{
button9->Text = "X";
label2->Text = "O";
turn = 1;
button9->Enabled= false;
}
else
{
button9->Text="O";
label2->Text = "X";
turn = 0;
button9->Enabled = false;
}
}
private: System::Void button10_Click(System::Object^ sender, System::EventArgs^ e)
{
button1->Text= " ";
button1->Enabled= true;
button2->Text= " ";
button2->Enabled= true;
button3->Text= " ";
button3->Enabled= true; //Starts a new game or resets the game
button4->Text= " ";
button4->Enabled= true;
button5->Text= " ";
button5->Enabled= true;
button6->Text= " ";
button6->Enabled= true;
button7->Text= " ";
button7->Enabled= true;
button8->Text= " ";
button8->Enabled= true;
button9->Text= " ";
button9->Enabled= true;
}
private: System::Void button11_Click(System::Object^ sender, System::EventArgs^ e)
{
Form1::Close(); //closes the form
}
private: System::Void label2_Click(System::Object^ sender, System::EventArgs^ e) {
}
}; // end of form
void Form1::checkwin()
{
if((button1->Text = "X") && (button2->Text = "X") && (button3->Text = "X"));
{
MessageBox::Show("X wins");
}
else if ((button4->Text = "X") && (button5->Text = "X") && (button6->Text = "X"));
{
MessageBox::Show("X wins");
}
else if ((button7->Text = "X") && (button8->Text = "X") && (button9->Text = "X"));
}
}
Explanation / Answer
// TRY THIS: void Form1::checkwin() { // Check X's if((button1->Text = "X") && (button2->Text = "X") && (button3->Text = "X")) MessageBox::Show("X wins"); else if ((button4->Text = "X") && (button5->Text = "X") && (button6->Text = "X")) MessageBox::Show("X wins"); else if ((button7->Text = "X") && (button8->Text = "X") && (button9->Text = "X")) MessageBox::Show("X wins"); else if ((button1->Text = "X") && (button5->Text = "X") && (button9->Text = "X")) MessageBox::Show("X wins"); else if ((button3->Text = "X") && (button5->Text = "X") && (button6->Text = "X")) MessageBox::Show("X wins"); // Check O's if((button1->Text = "O") && (button2->Text = "O") && (button3->Text = "O")) MessageBox::Show("O wins"); else if ((button4->Text = "O") && (button5->Text = "O") && (button6->Text = "O")) MessageBox::Show("O wins"); else if ((button7->Text = "O") && (button8->Text = "O") && (button9->Text = "O")) MessageBox::Show("O wins"); else if ((button1->Text = "O") && (button5->Text = "O") && (button9->Text = "O")) MessageBox::Show("O wins"); else if ((button3->Text = "O") && (button5->Text = "O") && (button6->Text = "O")) MessageBox::Show("O wins"); }