Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# windows form app Tic-Tac-Toe Simulator Create an application that simulates a

ID: 3850674 • Letter: C

Question

C# windows form app Tic-Tac-Toe Simulator Create an application that simulates a game of tic-tac-toe. Figure 7-45 shows an example of the application's form. The form shown in the figure uses eight large Label controls to display the Xs and Os. The application should use a two-dimensional int array to simulate the game board in memory. When the user clicks the New Game button, the application should step through the array, storing a random number in the range of 0 through 1 in each element. The number 0 represents the letter O, and the number 1 represents the letter X. The form should then be updated to display the game board. The application should display a message indicating whether player X won, player Y won, or the game was a tie.

Explanation / Answer

C# program Code For the TicTac Toe Game as given:

Project Name: WindowsFormsApplication

File Name: Form1.Designer.cs

namespace WindowsFormsApplication2
{
    partial class Form1
    {

        private System.ComponentModel.IContainer components = null;


        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code


        private void InitializeComponent()
        {
            this.tictactoeBox1 = new System.Windows.Forms.Button();
            this.tictactoeBox2 = new System.Windows.Forms.Button();
            this.tictactoeBox3 = new System.Windows.Forms.Button();
            this.tictactoeBox4 = new System.Windows.Forms.Button();
            this.tictactoeBox5 = new System.Windows.Forms.Button();
            this.tictactoeBox6 = new System.Windows.Forms.Button();
            this.tictactoeBox7 = new System.Windows.Forms.Button();
            this.tictactoeBox8 = new System.Windows.Forms.Button();
            this.tictactoeBox9 = new System.Windows.Forms.Button();
            this.textTheresult = new System.Windows.Forms.TextBox();
            this.ressetButton = new System.Windows.Forms.Button();
            this.gameExitButton = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // design the first box for tic tac toe
            //
            this.tictactoeBox1.Location = new System.Drawing.Point(65, 68);
            this.tictactoeBox1.Name = "tictactoeBox1";
            this.tictactoeBox1.Size = new System.Drawing.Size(114, 90);
            this.tictactoeBox1.TabIndex = 0;
            this.tictactoeBox1.UseVisualStyleBackColor = true;
            this.tictactoeBox1.Click += new System.EventHandler(this.click_Box1);
            //
            // design the second box for tic tac toe
            //
            this.tictactoeBox2.Location = new System.Drawing.Point(191, 68);
            this.tictactoeBox2.Name = "tictactoeBox2";
            this.tictactoeBox2.Size = new System.Drawing.Size(114, 90);
            this.tictactoeBox2.TabIndex = 1;
            this.tictactoeBox2.UseVisualStyleBackColor = true;
            this.tictactoeBox2.Click += new System.EventHandler(this.click_Box2);
            //
            // design the third box for tic tac toe
            //
            this.tictactoeBox3.Location = new System.Drawing.Point(311, 68);
            this.tictactoeBox3.Name = "tictactoeBox3";
            this.tictactoeBox3.Size = new System.Drawing.Size(114, 90);
            this.tictactoeBox3.TabIndex = 2;
            this.tictactoeBox3.UseVisualStyleBackColor = true;
            this.tictactoeBox3.Click += new System.EventHandler(this.click_Box3);
            //
            // design the fourth box for tic tac toe
            //
            this.tictactoeBox4.Location = new System.Drawing.Point(65, 167);
            this.tictactoeBox4.Name = "tictactoeBox4";
            this.tictactoeBox4.Size = new System.Drawing.Size(114, 90);
            this.tictactoeBox4.TabIndex = 3;
            this.tictactoeBox4.UseVisualStyleBackColor = true;
            this.tictactoeBox4.Click += new System.EventHandler(this.click_Box4);
            //
            // design the fifth box for tic tac toe
            //
            this.tictactoeBox5.Location = new System.Drawing.Point(191, 164);
            this.tictactoeBox5.Name = "tictactoeBox5";
            this.tictactoeBox5.Size = new System.Drawing.Size(114, 90);
            this.tictactoeBox5.TabIndex = 4;
            this.tictactoeBox5.UseVisualStyleBackColor = true;
            this.tictactoeBox5.Click += new System.EventHandler(this.click_Box5);
            //
            // design the sixth box for tic tac toe
            //
            this.tictactoeBox6.Location = new System.Drawing.Point(311, 164);
            this.tictactoeBox6.Name = "tictactoeBox6";
            this.tictactoeBox6.Size = new System.Drawing.Size(114, 90);
            this.tictactoeBox6.TabIndex = 5;
            this.tictactoeBox6.UseVisualStyleBackColor = true;
            this.tictactoeBox6.Click += new System.EventHandler(this.button6_Click);
            //
            // design the seventh box for tic tac toe
            //
            this.tictactoeBox7.Location = new System.Drawing.Point(65, 263);
            this.tictactoeBox7.Name = "tictactoeBox7";
            this.tictactoeBox7.Size = new System.Drawing.Size(114, 90);
            this.tictactoeBox7.TabIndex = 6;
            this.tictactoeBox7.UseVisualStyleBackColor = true;
            this.tictactoeBox7.Click += new System.EventHandler(this.button7_Click);
            //
            // design the eighth box for tic tac toe
            //
            this.tictactoeBox8.Location = new System.Drawing.Point(191, 263);
            this.tictactoeBox8.Name = "tictactoeBox8";
            this.tictactoeBox8.Size = new System.Drawing.Size(114, 90);
            this.tictactoeBox8.TabIndex = 7;
            this.tictactoeBox8.UseVisualStyleBackColor = true;
            this.tictactoeBox8.Click += new System.EventHandler(this.button8_Click_1);
            //
            // design the nineth box for tic tac toe
            //
            this.tictactoeBox9.Location = new System.Drawing.Point(311, 263);
            this.tictactoeBox9.Name = "tictactoeBox9";
            this.tictactoeBox9.Size = new System.Drawing.Size(114, 90);
            this.tictactoeBox9.TabIndex = 8;
            this.tictactoeBox9.UseVisualStyleBackColor = true;
            this.tictactoeBox9.Click += new System.EventHandler(this.button9_Click);
            //
            // design the tenth box for tic tac toe
            //
            this.textTheresult.Location = new System.Drawing.Point(65, 372);
            this.textTheresult.Name = "textTheresult";
            this.textTheresult.Size = new System.Drawing.Size(360, 20);
            this.textTheresult.TabIndex = 9;
            //
            // design the fnew game button
            //
            this.ressetButton.Location = new System.Drawing.Point(65, 421);
            this.ressetButton.Name = "ressetButton";
            this.ressetButton.Size = new System.Drawing.Size(138, 34);
            this.ressetButton.TabIndex = 10;
            this.ressetButton.Text = "New Game";
            this.ressetButton.UseVisualStyleBackColor = true;
            this.ressetButton.Click += new System.EventHandler(this.button10_Click);
            //
            // design the ExitButton
            //
            this.gameExitButton.Location = new System.Drawing.Point(291, 421);
            this.gameExitButton.Name = "gameExitButton";
            this.gameExitButton.Size = new System.Drawing.Size(127, 34);
            this.gameExitButton.TabIndex = 11;
            this.gameExitButton.Text = "Exit";
            this.gameExitButton.UseVisualStyleBackColor = true;
            this.gameExitButton.Click += new System.EventHandler(this.exitButton_Click);
            //
            //design form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(498, 532);
            this.Controls.Add(this.gameExitButton);
            this.Controls.Add(this.ressetButton);
            this.Controls.Add(this.textTheresult);
            this.Controls.Add(this.tictactoeBox9);
            this.Controls.Add(this.tictactoeBox8);
            this.Controls.Add(this.tictactoeBox7);
            this.Controls.Add(this.tictactoeBox6);
            this.Controls.Add(this.tictactoeBox5);
            this.Controls.Add(this.tictactoeBox4);
            this.Controls.Add(this.tictactoeBox3);
            this.Controls.Add(this.tictactoeBox2);
            this.Controls.Add(this.tictactoeBox1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button tictactoeBox1;
        private System.Windows.Forms.Button tictactoeBox2;
        private System.Windows.Forms.Button tictactoeBox3;
        private System.Windows.Forms.Button tictactoeBox4;
        private System.Windows.Forms.Button tictactoeBox5;
        private System.Windows.Forms.Button tictactoeBox6;
        private System.Windows.Forms.Button tictactoeBox7;
        private System.Windows.Forms.Button tictactoeBox8;
        private System.Windows.Forms.Button tictactoeBox9;
        private System.Windows.Forms.TextBox textTheresult;
        private System.Windows.Forms.Button ressetButton;
        private System.Windows.Forms.Button gameExitButton;
    }
}

File Name: Form1.cs

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

namespace WindowsFormsApplication2
{

    public partial class Form1 : Form
    {

        //To change the player
        int playerChange = 0;

        public Form1()
        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void click_Box1(object sender, EventArgs e)
        {


            if (playerChange == 0)
            {

                tictactoeBox1.Text = "X";

                playerChange++;

            }

            else if (playerChange == 1)
            {

                tictactoeBox1.Text = "O";

                playerChange--;

            }


            tictactoeBox1.Enabled = false;


            winCheck();

        }

        //to perform operation for box2 click
        private void click_Box2(object sender, EventArgs e)
        {


            if (playerChange == 0)
            {

                tictactoeBox2.Text = "X";

                playerChange++;

            }

            else if (playerChange == 1)
            {

                tictactoeBox2.Text = "O";

                playerChange--;

            }


            tictactoeBox2.Enabled = false;

            //call the mthod to check winner
            winCheck();

        }


        //to perform operation for box2 click
        private void click_Box3(object sender, EventArgs e)
        {

            //test the winners

            if (playerChange == 0)
            {

                tictactoeBox3.Text = "X";

                playerChange++;

            }

            else if (playerChange == 1)
            {

                tictactoeBox3.Text = "O";

                playerChange--;

            }

            tictactoeBox3.Enabled = false;


            winCheck();

        }

        //to perform operation for box 4 click
        private void click_Box4(object sender, EventArgs e)
        {


            if (playerChange == 0)
            {

                tictactoeBox4.Text = "X";

                playerChange++;

            }

            else if (playerChange == 1)
            {

                tictactoeBox4.Text = "O";

                playerChange--;

            }


            tictactoeBox4.Enabled = false;


            winCheck();

        }

        //to perform operation for box 5 click
        private void click_Box5(object sender, EventArgs e)
        {


            if (playerChange == 0)
            {

                tictactoeBox5.Text = "X";

                playerChange++;

            }

            else if (playerChange == 1)
            {

                tictactoeBox5.Text = "O";

                playerChange--;

            }


            tictactoeBox5.Enabled = false;


            winCheck();

        }


        private void button6_Click(object sender, EventArgs e)
        {

            if (playerChange == 0)
            {

                tictactoeBox6.Text = "X";

                playerChange++;

            }

            else if (playerChange == 1)
            {

                tictactoeBox6.Text = "O";

                playerChange--;

            }


            tictactoeBox6.Enabled = false;


            winCheck();

        }

        private void button7_Click(object sender, EventArgs e)
        {

            if (playerChange == 0)
            {

                tictactoeBox7.Text = "X";

                playerChange++;

            }

            else if (playerChange == 1)
            {

                tictactoeBox7.Text = "O";

                playerChange--;

            }


            tictactoeBox7.Enabled = false;


            winCheck();

        }


        private void button8_Click_1(object sender, EventArgs e)
        {

            if (playerChange == 0)
            {

                tictactoeBox8.Text = "X";

                playerChange++;

            }

            else if (playerChange == 1)
            {

                tictactoeBox8.Text = "O";

                playerChange--;

            }

            tictactoeBox8.Enabled = false;


            winCheck();

        }

        private void button9_Click(object sender, EventArgs e)
        {


            if (playerChange == 0)
            {

                tictactoeBox9.Text = "X";

                playerChange++;

            }

            else if (playerChange == 1)
            {

                tictactoeBox9.Text = "O";

                playerChange--;

            }


            tictactoeBox9.Enabled = false;


            winCheck();

        }

        void winCheck()
        {


            if (tictactoeBox1.Text != "" && tictactoeBox2.Text != "" && tictactoeBox3.Text != "" &&

            tictactoeBox4.Text != "" && tictactoeBox5.Text != "" && tictactoeBox6.Text != "" &&

            tictactoeBox7.Text != "" && tictactoeBox8.Text != "" && tictactoeBox9.Text != "")
            {

                textTheresult.Text = "Game Tied";

            }


            if (tictactoeBox1.Text == "X" && tictactoeBox5.Text == "X" && tictactoeBox9.Text == "X")
            {

                textTheresult.Text = "X wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            if (tictactoeBox3.Text == "X" && tictactoeBox5.Text == "X" && tictactoeBox7.Text == "X")
            {

                textTheresult.Text = "X wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }


            if (tictactoeBox1.Text == "X" && tictactoeBox2.Text == "X" && tictactoeBox3.Text == "X")
            {

                textTheresult.Text = "X wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            if (tictactoeBox4.Text == "X" && tictactoeBox5.Text == "X" && tictactoeBox6.Text == "X")
            {

                textTheresult.Text = "X wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            if (tictactoeBox7.Text == "X" && tictactoeBox8.Text == "X" && tictactoeBox9.Text == "X")
            {

                textTheresult.Text = "X wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            //winCheck columns for X

            if (tictactoeBox1.Text == "X" && tictactoeBox4.Text == "X" && tictactoeBox7.Text == "X")
            {

                textTheresult.Text = "X wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            if (tictactoeBox2.Text == "X" && tictactoeBox5.Text == "X" && tictactoeBox8.Text == "X")
            {

                textTheresult.Text = "X wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            if (tictactoeBox3.Text == "X" && tictactoeBox6.Text == "X" && tictactoeBox9.Text == "X")
            {

                textTheresult.Text = "X wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            //check whether 0 wins

            if (tictactoeBox1.Text == "O" && tictactoeBox5.Text == "O" && tictactoeBox9.Text == "O")
            {

                textTheresult.Text = "O wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            if (tictactoeBox3.Text == "O" && tictactoeBox5.Text == "O" && tictactoeBox7.Text == "O")
            {

                textTheresult.Text = "O Wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            //winCheck rows for O

            if (tictactoeBox1.Text == "O" && tictactoeBox2.Text == "O" && tictactoeBox3.Text == "O")
            {

                textTheresult.Text = "O Wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            if (tictactoeBox4.Text == "O" && tictactoeBox5.Text == "O" && tictactoeBox6.Text == "O")
            {

                textTheresult.Text = "O Wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            if (tictactoeBox7.Text == "O" && tictactoeBox8.Text == "O" && tictactoeBox9.Text == "O")
            {

                textTheresult.Text = "O Wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            //check for winner 0

            if (tictactoeBox1.Text == "O" && tictactoeBox4.Text == "O" && tictactoeBox7.Text == "O")
            {

                textTheresult.Text = "O Wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            if (tictactoeBox2.Text == "O" && tictactoeBox5.Text == "O" && tictactoeBox8.Text == "O")
            {

                textTheresult.Text = "O Wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

            if (tictactoeBox3.Text == "O" && tictactoeBox6.Text == "O" && tictactoeBox9.Text == "O")
            {

                textTheresult.Text = "O Wins";

                tictactoeBox1.Enabled = false;

                tictactoeBox2.Enabled = false;

                tictactoeBox3.Enabled = false;

                tictactoeBox4.Enabled = false;

                tictactoeBox5.Enabled = false;

                tictactoeBox6.Enabled = false;

                tictactoeBox7.Enabled = false;

                tictactoeBox8.Enabled = false;

                tictactoeBox9.Enabled = false;

            }

        }

        private void button10_Click(object sender, EventArgs e)
        {

            tictactoeBox1.Text = "";

            tictactoeBox1.Enabled = true;

            tictactoeBox2.Text = "";

            tictactoeBox2.Enabled = true;

            tictactoeBox3.Text = "";

            tictactoeBox3.Enabled = true;

            tictactoeBox4.Text = "";

            tictactoeBox4.Enabled = true;

            tictactoeBox5.Text = "";

            tictactoeBox5.Enabled = true;

            tictactoeBox6.Text = "";

            tictactoeBox6.Enabled = true;

            tictactoeBox7.Text = "";

            tictactoeBox7.Enabled = true;

            tictactoeBox8.Text = "";

            tictactoeBox8.Enabled = true;

            tictactoeBox9.Text = "";

            tictactoeBox9.Enabled = true;

            textTheresult.Text = "";

            playerChange = 0;

        }

        private void button11_Click(object sender, EventArgs e)
        {

            this.Close();

        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }

}

Note: Since upload character limit reached i can't upload the output screen shots.