Friday, October 7, 2011

How to write tic tac toe on C# (C sharp)







Hi, now I will show you how to write tic tac toe on C# (C sharp).
First of all you need to add on Form 10 buttons and 4 labels.













The Form should look like this:








After that we will write some C# code:






using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Tic_tac
{
    public partial class Form1 : Form
    {
        bool Player1 = true;
        bool Player2 = false;
        int X = 0;
        int O = 0;


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button10_Click(object sender, EventArgs e)
        {
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = "";
            
            Player1 = true;
            Player2 = false;
           

        }

       

        private void button1_Click(object sender, EventArgs e)
        {
            if (Player1 == true)
            {
                //Player1 = true;
                button1.Text = "X";
                Player1 = false;
            }
            else
            {
                Player2 = true;
                button1.Text = "O";
                Player2 = false;
                Player1 = true;
            }  
            Wincombination();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (Player1 == true)
            {
                //Player1 = true;
                button2.Text = "X";
                Player1 = false;
            }
            else
            {
                Player2 = true;
                button2.Text = "O";
                Player2 = false;
                Player1 = true;
            }
            Wincombination();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (Player1 == true)
            {
                //Player1 = true;
                button3.Text = "X";
                Player1 = false;
            }
            else
            {
                Player2 = true;
                button3.Text = "O";
                Player2 = false;
                Player1 = true;
            }

            Wincombination();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            if (Player1 == true)
            {
                //Player1 = true;
                button4.Text = "X";
                Player1 = false;
            }
            else
            {
                Player2 = true;
                button4.Text = "O";
                Player2 = false;
                Player1 = true;
            }

            Wincombination();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (Player1 == true)
            {
                //Player1 = true;
                button5.Text = "X";
                Player1 = false;
            }
            else
            {
                Player2 = true;
                button5.Text = "O";
                Player2 = false;
                Player1 = true;
            }

            Wincombination();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            if (Player1 == true)
            {
               // Player1 = true;
                button6.Text = "X";
                Player1 = false;
            }
            else
            {
                Player2 = true;
                button6.Text = "O";
                Player2 = false;
                Player1 = true;
            }

            Wincombination();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            if (Player1 == true)
            {
               // Player1 = true;
                button7.Text = "X";
                Player1 = false;
            }
            else
            {
                Player2 = true;
                button7.Text = "O";
                Player2 = false;
                Player1 = true;
            }

            Wincombination();
        }

        private void button8_Click(object sender, EventArgs e)
        {
            if (Player1 == true)
            {
             //   Player1 = true;
                button8.Text = "X";
                Player1 = false;
            }
            else
            {
                Player2 = true;
                button8.Text = "O";
                Player2 = false;
                Player1 = true;
            }

            Wincombination();
        }

        private void button9_Click(object sender, EventArgs e)
        {
            if (Player1 == true)
            {
             //   Player1 = true;
                button9.Text = "X";
                Player1 = false;
            }
            else
            {
                Player2 = true;
                button9.Text = "O";
                Player2 = false;
                Player1 = true;
            }

            Wincombination();
        }

        private void Wincombination()
        {
            if (button1.Text == "X" && button2.Text == "X" && button3.Text == "X")
            {
                MessageBox.Show("X Win!", "Game over", MessageBoxButtons.OK);
                X = X + 1;
                label1.Text = X.ToString("");
            }
            if (button4.Text == "X" && button5.Text == "X" && button6.Text == "X")
            {
                MessageBox.Show("X Win!", "Game over", MessageBoxButtons.OK);
                X = X + 1;
                label1.Text = X.ToString("");
            }
            if (button7.Text == "X" && button8.Text == "X" && button9.Text == "X")
            {
                MessageBox.Show("X Win!", "Game over", MessageBoxButtons.OK);
                X = X + 1;
                label1.Text = X.ToString("");
            }

            if (button1.Text == "X" && button5.Text == "X" && button9.Text == "X")
            {
                MessageBox.Show("X Win!", "Game over", MessageBoxButtons.OK);
                X = X + 1;
                label1.Text = X.ToString("");
            }
            if (button3.Text == "X" && button5.Text == "X" && button7.Text == "X")
            {
                MessageBox.Show("X Win!", "Game over", MessageBoxButtons.OK);
                X = X + 1;
                label1.Text = X.ToString("");
            }

            if (button1.Text == "X" && button4.Text == "X" && button7.Text == "X")
            {
                MessageBox.Show("X Win!", "Game over", MessageBoxButtons.OK);
                X = X + 1;
                label1.Text = X.ToString("");
            }
            if (button2.Text == "X" && button5.Text == "X" && button8.Text == "X")
            {
                MessageBox.Show("X Win!", "Game over", MessageBoxButtons.OK);
                X = X + 1;
                label1.Text = X.ToString("");
            }
            if (button3.Text == "X" && button6.Text == "X" && button9.Text == "X")
            {
                MessageBox.Show("X Win!", "Game over", MessageBoxButtons.OK);
                X = X + 1;
                label1.Text = X.ToString("");
            }

            // for O

            if (button1.Text == "O" && button2.Text == "O" && button3.Text == "O")
            {
                MessageBox.Show("O Win!", "Game over", MessageBoxButtons.OK);
                O = O + 1;
                label2.Text = O.ToString("");
            }
            if (button4.Text == "O" && button5.Text == "O" && button6.Text == "O")
            {
                MessageBox.Show("O Win!", "Game over", MessageBoxButtons.OK);
                O = O + 1;
                label2.Text = O.ToString("");
            }
            if (button7.Text == "O" && button8.Text == "O" && button9.Text == "O")
            {
                MessageBox.Show("O Win!", "Game over", MessageBoxButtons.OK);
                O = O + 1;
                label2.Text = O.ToString("");
            }

            if (button1.Text == "O" && button5.Text == "O" && button9.Text == "O")
            {
                MessageBox.Show("O Win!", "Game over", MessageBoxButtons.OK);
                O = O + 1;
                label2.Text = O.ToString("");
            }
            if (button3.Text == "O" && button5.Text == "O" && button7.Text == "O")
            {
                MessageBox.Show("O Win!", "Game over", MessageBoxButtons.OK);
                O = O + 1;
                label2.Text = O.ToString("");
            }

            if (button1.Text == "O" && button4.Text == "O" && button7.Text == "O")
            {
                MessageBox.Show("O Win!", "Game over", MessageBoxButtons.OK);
                O = O + 1;
                label2.Text = O.ToString("");
            }
            if (button2.Text == "O" && button5.Text == "O" && button8.Text == "O")
            {
                MessageBox.Show("O Win!", "Game over", MessageBoxButtons.OK);
                O = O + 1;
                label2.Text = O.ToString("");
            }
            if (button3.Text == "O" && button6.Text == "O" && button9.Text == "O")
            {
                MessageBox.Show("O Win!", "Game over", MessageBoxButtons.OK);
                O = O + 1;
                label2.Text = O.ToString("");
            }


        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button1.Text = "";
            button2.Text = "";
            button3.Text = "";
            button4.Text = "";
            button5.Text = "";
            button6.Text = "";
            button7.Text = "";
            button8.Text = "";
            button9.Text = "";

            Player1 = true;
            Player2 = false;
            X = 0;
            O = 0;
            label1.Text = "0";
            label2.Text = "0";
        }

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

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Tic-Tac-Toe 1.0 by Bogdan Ustyak");
        }
    }
}

No comments:

Post a Comment