Minggu, 03 Mei 2015

membuat game tic tac toe menggunakan program C# (visual studio 2012)



assalamu`alaikum wr wb, 
salam sejahtera buat temen2, kakak-kakak, abang-abang, adik-adik, bapak-bapak serta ibu-ibu sekalian. pada kesempatan kali ini saya ingin menshare atau lebih tepatnya membagikan ilmu yg telah saya dapatkan dan pelajari dalam membuat game TIC TAC TOE menggunakan program C#. 


langkah paling pertama dalam membuat program game ini adalah adanya software visual studio, yang saya gunakan adalah microsoft visual studio 2012.

selanjutnya buka file microsoft visual studio seperti langkah2 yg ada di bawah ini.
1.      Kita klik new project


2.      Setelah itu akan muncul menu seperti yg dibawah ini. Kita pilih Windows Form Application lalu kita tulis nama project kita dan klik OK.

3.      Lalu akan muncul Form seperti Berikut.

4.   Design form seperti berikut menggunakan picture box.


5.    Masukkan program yg telah saya buat atau program yg telah saudara2 buat.

      namespace tictactoefix
{
    public partial class Form1 : Form
    {

        public bool playerTurn = true;
        Image o = Properties.Resources.o;
        Image x = Properties.Resources.x;

        static string h, j, k;
        int a,b,c,d,r,index;

        public Form1()
        {
            InitializeComponent();
        }

        #region score
        private void scoreO()
        {
            c = c + 1;
            label1.Text = Convert.ToString(c);
        }

        private void scoreX()
        {
            d=d+1;
            label2.Text=Convert.ToString(d);
        }
        #endregion

        #region check kemenangan
        private void check()
        {
          
            // PEMEMNANG X
            if (pictureBox1.BackgroundImage == x && pictureBox5.BackgroundImage == x && pictureBox9.BackgroundImage == x)
            {

                MessageBox.Show("x menang");
                scoreX();
                reset();
                return;
                
            }

            if (pictureBox1.BackgroundImage == x && pictureBox4.BackgroundImage == x && pictureBox7.BackgroundImage == x)
            {
                MessageBox.Show("x menang");
                scoreX();
                reset();
                return;

            }

            if (pictureBox1.BackgroundImage == x && pictureBox2.BackgroundImage == x && pictureBox3.BackgroundImage == x)
            {
                MessageBox.Show("x menang");
                scoreX();
                reset();
                return;

            }

            if (pictureBox3.BackgroundImage == x && pictureBox5.BackgroundImage == x && pictureBox7.BackgroundImage == x)
            {

                MessageBox.Show("x menang");
                scoreX();
                reset();
                return;

            }

            if (pictureBox4.BackgroundImage == x && pictureBox5.BackgroundImage == x && pictureBox6.BackgroundImage == x)
            {

                MessageBox.Show("x menang");
                scoreX();
                reset();
                return;

            }

            if (pictureBox7.BackgroundImage == x && pictureBox8.BackgroundImage == x && pictureBox9.BackgroundImage == x)
            {

                MessageBox.Show("x menang");
                scoreX();
                reset();
                return;

            }

            if (pictureBox2.BackgroundImage == x && pictureBox5.BackgroundImage == x && pictureBox8.BackgroundImage == x)
            {

                MessageBox.Show("x menang");
                scoreX();
                reset();
                return;

            }

            if (pictureBox3.BackgroundImage == x && pictureBox6.BackgroundImage == x && pictureBox9.BackgroundImage == x)
            {

                MessageBox.Show("x menang");
                scoreX();
                reset();
                return;

            }

            if (pictureBox1.BackgroundImage == o && pictureBox5.BackgroundImage == o && pictureBox9.BackgroundImage == o)
            {

                MessageBox.Show("o menang");
                scoreO();
                reset();
                return;


            }

            if (pictureBox1.BackgroundImage == o && pictureBox4.BackgroundImage == o && pictureBox7.BackgroundImage == o)
            {

                MessageBox.Show("o menang");
                scoreO();
                reset();
                return;

            }

            if (pictureBox1.BackgroundImage == o && pictureBox2.BackgroundImage == o && pictureBox3.BackgroundImage == o)
            {

                MessageBox.Show("o menang");
                scoreO();
                reset();
                return;

            }

            if (pictureBox3.BackgroundImage == o && pictureBox5.BackgroundImage == o && pictureBox7.BackgroundImage == o)
            {

                MessageBox.Show("o menang");
                scoreO();
                reset();
                return;

            }

            if (pictureBox4.BackgroundImage == o && pictureBox5.BackgroundImage == o && pictureBox6.BackgroundImage == o)
            {

                MessageBox.Show("o menang");
                scoreO();
                reset();
                return;

            }

            if (pictureBox7.BackgroundImage == o && pictureBox8.BackgroundImage == o && pictureBox9.BackgroundImage == o)
            {

                MessageBox.Show("o menang");
                scoreO();
                reset();
                return;

            }

            if (pictureBox2.BackgroundImage == o && pictureBox5.BackgroundImage == o && pictureBox8.BackgroundImage == o)
            {

                MessageBox.Show("o menang");
                scoreO();
                reset();
                return;

            }

            if (pictureBox3.BackgroundImage == o && pictureBox6.BackgroundImage == o && pictureBox9.BackgroundImage == o)
            {

                MessageBox.Show("o menang");
                scoreO();
                reset();
                return;

            }

            if (pictureBox1.BackgroundImage != null && pictureBox2.BackgroundImage != null && pictureBox3.BackgroundImage != null &&
               pictureBox4.BackgroundImage != null && pictureBox5.BackgroundImage != null && pictureBox6.BackgroundImage != null &&
               pictureBox7.BackgroundImage != null && pictureBox8.BackgroundImage != null && pictureBox9.BackgroundImage != null)
            {
                MessageBox.Show("draw");// jika tidak ada yang menang akan menampilkan message ini
                reset();
                return;
            }

        }

        #endregion

        #region comeasy
        private void computerplayeasy()
        {
            
                if (pictureBox1.BackgroundImage == o && pictureBox2.BackgroundImage == o)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();//DIPERIKSA DI CLASS CHECK APAK SUDAH ADA ADA PEMEMENANG ATAU BELUM
                        return;

                    }
                }


                if (pictureBox2.BackgroundImage == o && pictureBox3.BackgroundImage == o)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == o && pictureBox3.BackgroundImage == o)
                {
                    if (pictureBox7.BackgroundImage == null)
                    {
                        pictureBox7.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == o && pictureBox7.BackgroundImage == o)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == o && pictureBox4.BackgroundImage == o)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox4.BackgroundImage == o && pictureBox7.BackgroundImage == o)
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }
                

                if (pictureBox1.BackgroundImage == o && pictureBox5.BackgroundImage == o)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox8.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox2.BackgroundImage == o && pictureBox8.BackgroundImage == o)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox8.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == o && pictureBox6.BackgroundImage == o)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox6.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox4.BackgroundImage == o && pictureBox5.BackgroundImage == o)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox4.BackgroundImage == o && pictureBox6.BackgroundImage == o)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox6.BackgroundImage == o)
                {
                    if (pictureBox4.BackgroundImage == null)
                    {
                        pictureBox4.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox7.BackgroundImage == o && pictureBox8.BackgroundImage == o)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;
                    }
                }


                if (pictureBox7.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;
                    }
                }


                if (pictureBox8.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == o && pictureBox5.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == o && pictureBox7.BackgroundImage == o)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;
                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox7.BackgroundImage == o)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;
                    }
                }


                if (pictureBox3.BackgroundImage == o && pictureBox7.BackgroundImage == o)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                //  x

                if (pictureBox1.BackgroundImage == x && pictureBox2.BackgroundImage == x)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox2.BackgroundImage == x && pictureBox3.BackgroundImage == x)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == x && pictureBox3.BackgroundImage == x)
                {
                    if (pictureBox7.BackgroundImage == null)
                    {
                        pictureBox7.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == x && pictureBox4.BackgroundImage == x)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox4.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == x && pictureBox5.BackgroundImage == x)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;

                    }
                }

                if (pictureBox2.BackgroundImage == x && pictureBox5.BackgroundImage == x)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;

                    }
                }

                if (pictureBox5.BackgroundImage == x && pictureBox8.BackgroundImage == x)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == x && pictureBox6.BackgroundImage == x)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;

                    }
                }

                if (pictureBox4.BackgroundImage == x && pictureBox5.BackgroundImage == x)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;

                    }
                }

                if (pictureBox4.BackgroundImage == x && pictureBox6.BackgroundImage == x)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == x && pictureBox6.BackgroundImage == x)
                {
                    if (pictureBox4.BackgroundImage == null)
                    {
                        pictureBox4.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox7.BackgroundImage == x && pictureBox8.BackgroundImage == x)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox7.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox8.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == x && pictureBox5.BackgroundImage == x)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox6.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;

                    }
                }

                if (pictureBox5.BackgroundImage == null)
                {
                    pictureBox5.BackgroundImage = o;
                    check();
                    return;
                }

                //3

                if (pictureBox1.BackgroundImage == x && pictureBox9.BackgroundImage == x && pictureBox5.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                if (pictureBox3.BackgroundImage == x && pictureBox7.BackgroundImage == x && pictureBox5.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;
                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox8.BackgroundImage == x)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                if (pictureBox5.BackgroundImage == o && pictureBox2.BackgroundImage == x)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                if (pictureBox2.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                if (pictureBox5.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                if (pictureBox5.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                //jika tidak ada yg terpilih yg di atas


                if (pictureBox1.BackgroundImage == null)
                {
                    pictureBox1.BackgroundImage = o;
                    check();
                    return;

                }

                if (pictureBox3.BackgroundImage == null)
                {
                    pictureBox3.BackgroundImage = o;
                    check();
                    return;

                }

                if (pictureBox9.BackgroundImage == null)
                {
                    pictureBox9.BackgroundImage = o;
                    check();
                    return;

                }

                if (pictureBox6.BackgroundImage == null)
                {
                    pictureBox6.BackgroundImage = o;
                    check();
                    return;

                
            
            }
        }

        #endregion

        #region comhard
        private void computerplayhard()
        {
            
                if (pictureBox1.BackgroundImage == o && pictureBox2.BackgroundImage == o)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();//DIPERIKSA DI CLASS CHECK APAK SUDAH ADA ADA PEMEMENANG ATAU BELUM
                        return;
                    }
                }


                if (pictureBox2.BackgroundImage == o && pictureBox3.BackgroundImage == o)
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = o;
                        check();
                        return;
                    }
                }


                if (pictureBox1.BackgroundImage == o && pictureBox3.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;
                    }
                }


                if (pictureBox1.BackgroundImage == o && pictureBox7.BackgroundImage == o)
                {
                    if (pictureBox4.BackgroundImage == null)
                    {
                        pictureBox4.BackgroundImage = o;
                        check();
                        return;
                    }
                }


                if (pictureBox1.BackgroundImage == o && pictureBox4.BackgroundImage == o)
                {
                    if (pictureBox7.BackgroundImage == null)
                    {
                        pictureBox7.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox4.BackgroundImage == o && pictureBox7.BackgroundImage == o)
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == o && pictureBox5.BackgroundImage == o)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox8.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox2.BackgroundImage == o && pictureBox8.BackgroundImage == o)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox8.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == o && pictureBox6.BackgroundImage == o)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox6.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox4.BackgroundImage == o && pictureBox5.BackgroundImage == o)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox4.BackgroundImage == o && pictureBox6.BackgroundImage == o)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox6.BackgroundImage == o)
                {
                    if (pictureBox4.BackgroundImage == null)
                    {
                        pictureBox4.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox7.BackgroundImage == o && pictureBox8.BackgroundImage == o)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox7.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox8.BackgroundImage == o && pictureBox9.BackgroundImage == o)
                {
                    if (pictureBox7.BackgroundImage == null)
                    {
                        pictureBox7.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == o && pictureBox5.BackgroundImage == o)
                {
                    if (pictureBox7.BackgroundImage == null)
                    {
                        pictureBox7.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == o && pictureBox7.BackgroundImage == o)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox7.BackgroundImage == o)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == o && pictureBox7.BackgroundImage == o)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }

                //  x

                if (pictureBox1.BackgroundImage == x && pictureBox2.BackgroundImage == x)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox2.BackgroundImage == x && pictureBox3.BackgroundImage == x)
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == x && pictureBox3.BackgroundImage == x)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox4.BackgroundImage == null)
                    {
                        pictureBox4.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == x && pictureBox4.BackgroundImage == x)
                {
                    if (pictureBox7.BackgroundImage == null)
                    {
                        pictureBox7.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox4.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox1.BackgroundImage == null)
                    {
                        pictureBox1.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox1.BackgroundImage == x && pictureBox5.BackgroundImage == x)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }





                if (pictureBox2.BackgroundImage == x && pictureBox5.BackgroundImage == x)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;

                    }
                }





                if (pictureBox5.BackgroundImage == x && pictureBox8.BackgroundImage == x)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == x && pictureBox6.BackgroundImage == x)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;

                    }
                }




                if (pictureBox4.BackgroundImage == x && pictureBox5.BackgroundImage == x)
                {
                    if (pictureBox6.BackgroundImage == null)
                    {
                        pictureBox6.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox4.BackgroundImage == x && pictureBox6.BackgroundImage == x)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == x && pictureBox6.BackgroundImage == x)
                {
                    if (pictureBox4.BackgroundImage == null)
                    {
                        pictureBox4.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox7.BackgroundImage == x && pictureBox8.BackgroundImage == x)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox7.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox8.BackgroundImage == null)
                    {
                        pictureBox8.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox8.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox7.BackgroundImage == null)
                    {
                        pictureBox7.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == x && pictureBox5.BackgroundImage == x)
                {
                    if (pictureBox7.BackgroundImage == null)
                    {
                        pictureBox7.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox3.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox5.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;

                    }
                }


                if (pictureBox6.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;

                    }
                }

                if (pictureBox5.BackgroundImage == null)
                {
                    pictureBox5.BackgroundImage = o;
                    check();
                    return;
                }

                //3

                if (pictureBox1.BackgroundImage == x && pictureBox9.BackgroundImage == x && pictureBox5.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                if (pictureBox3.BackgroundImage == x && pictureBox7.BackgroundImage == x && pictureBox5.BackgroundImage == o)
                {
                    if (pictureBox2.BackgroundImage == null)
                    {
                        pictureBox2.BackgroundImage = o;
                        check();
                        return;
                    }
                }


                if (pictureBox5.BackgroundImage == o && pictureBox8.BackgroundImage == x)
                {
                    if (pictureBox9.BackgroundImage == null)
                    {
                        pictureBox9.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                if (pictureBox5.BackgroundImage == o && pictureBox2.BackgroundImage == x)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                if (pictureBox2.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox5.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                if (pictureBox5.BackgroundImage == x && pictureBox7.BackgroundImage == x)
                {
                    if (pictureBox3.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                if (pictureBox5.BackgroundImage == x && pictureBox9.BackgroundImage == x)
                {
                    if (pictureBox5.BackgroundImage == null)
                    {
                        pictureBox3.BackgroundImage = o;
                        check();
                        return;
                    }
                }

                //jika tidak ada yg terpilih yg di atas


                if (pictureBox1.BackgroundImage == null)
                {
                    pictureBox1.BackgroundImage = o;
                    check();
                    return;

                }

                if (pictureBox3.BackgroundImage == null)
                {
                    pictureBox3.BackgroundImage = o;
                    check();
                    return;

                }

                if (pictureBox9.BackgroundImage == null)
                {
                    pictureBox9.BackgroundImage = o;
                    check();
                    return;

                }

                if (pictureBox6.BackgroundImage == null)
                {
                    pictureBox6.BackgroundImage = o;
                    check();
                    return;

                
            
            }
        }
        #endregion

        #region pictureclick
        private void pictureclik(ref PictureBox thePictureBox)// THE PICTURE ITU SUDAH MEMBAWA SEMUA PICTUREBOX ADA
        {
            if (a==1)//UNTUK 1 PALYER ATAU MELAWAN KOMPUTER hard
            {
                if (thePictureBox.BackgroundImage == null)//NULL BAEARTI KOSONG
                {
                    thePictureBox.BackgroundImage = x;//UNTUK PEMILIHAN AWAL//GILIRAN PEMAIN
                    check();// MELAKUKAN PENGECEKAN PEMENANG
                    computerplayhard();//GILRAN KOMPUTER BERMAIN

                }
                else
                    MessageBox.Show(" picture box sudah terisi");//MENAMILKAN PESAN JIKA BOX YANG DIKLIK SUDAH TERISI
            }

            else if (r == 1)//UNTUK 1 PALYER ATAU MELAWAN KOMPUTER easy
            {
                if (thePictureBox.BackgroundImage == null)//NULL BAEARTI KOSONG
                {
                    thePictureBox.BackgroundImage = x;//UNTUK PEMILIHAN AWAL//GILIRAN PEMAIN
                    check();// MELAKUKAN PENGECEKAN PEMENANG
                    computerplayeasy();//GILRAN KOMPUTER BERMAIN

                }
                else
                    MessageBox.Show(" picture box sudah terisi");//MENAMILKAN PESAN JIKA BOX YANG DIKLIK SUDAH TERISI
            }

            else if (b==1)//UNTUK 2 PALYER
            {
                if (thePictureBox.BackgroundImage == null)
                { 
                    if (playerTurn == false)
                    {
                        thePictureBox.BackgroundImage = x;//GILIRAN PEMAIN PERTAMA
                        playerTurn = true;
                        check();
                    }

                    else
                    {
                        thePictureBox.BackgroundImage = o;//GILIRAN PEMAIN KEDUA
                        playerTurn = false;
                        check();
                    }
                }
                else
                {
                    MessageBox.Show("picture box sudah terisi");

                }
            }

            else
            {
                MessageBox.Show("pilih tipe permainan");
            }

        }
        #endregion

        #region reset
        private void reset()
        {
            pictureBox1.BackgroundImage = null;
            pictureBox2.BackgroundImage = null;
            pictureBox3.BackgroundImage = null;
            pictureBox4.BackgroundImage = null;
            pictureBox5.BackgroundImage = null;
            pictureBox6.BackgroundImage = null;
            pictureBox7.BackgroundImage = null;
            pictureBox8.BackgroundImage = null;
            pictureBox9.BackgroundImage = null;
        }
        #endregion

        #region pictureboxclick
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            pictureclik(ref pictureBox1);
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            pictureclik(ref pictureBox2);
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            pictureclik(ref pictureBox3);
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            pictureclik(ref pictureBox4);
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            pictureclik(ref pictureBox5);
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            pictureclik(ref pictureBox6);
        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            pictureclik(ref pictureBox7);
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            pictureclik(ref pictureBox8);
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            pictureclik(ref pictureBox9);
        }
        #endregion

        #region menustrip
        private void playerVsPlayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            b = 1;
        }

        private void hardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            a = 1;
        }

        private void easyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            r = 1;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult tanya = MessageBox.Show("do you want to exit", "Warning", MessageBoxButtons.YesNo);
            if (tanya == DialogResult.Yes)
            { Close(); }

        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            reset();
        }
        #endregion

        #region btnplayagain
        private void playagainbtn_Click(object sender, EventArgs e)
        {
            
            a = 0;
            b = 0;
            c = +1;
            d = +1;
            r = 0;
        }
        #endregion

        #region btnreset
        private void resetbtn_Click(object sender, EventArgs e)
        {
            label1.Text = "0";
            label2.Text = "0";
            reset();
            a = 0;
            b = 0;
            c = 0;
            d = 0;
            r = 0;
        }

        #endregion

        #region menustripabout

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Created by: Gusti Amasta \n\n Visit My Blog : www.gustiamasta.blogspot.com", "Warning");
        }
        #endregion

    }
}

6. Kemudian klik start

                                                      

7. Berikut adalah hasilnya


Mungkin ini saja yg dapat saya sampaikan, jika ada kesalahan atau kekurangan saya minta maaf karena saya manusia yg tidak luput dari kesalahan :D
Terimakasih semoga bermanfaat.
Video tutorial : https://www.youtube.com/watch?v=DgXb9Qyk09Y&feature=youtu.be

Wassalamu`alaikum Wr Wb.



  

Tidak ada komentar:

Posting Komentar