Sunday, May 3, 2015

minggu7: Visual studio C#- Game tic tac toe


I. Dasar Teori
Control PictureBox digunakan untuk menampilkan gambar dan memanipulasinya. PictureBox dapat
menangani berbagai macam format file gambar. Dalam praktikum ini kita menggunakan PNG. Untuk
mengisi PictureBox dengan gambar, kita tinggal load gambarnya dengan menge-klik Properties
Image-nya. Kemudian sesuaikan ukuran Width dan Height nya sehingga gambar tidak terpotong.

II. Praktikum
Gambar pada PictureBox bisa saling dicopy. Artinya gambar pada instance pictureBox1 dapat kita copy-kan ke instance pictureBox2. Di sini kita akan membuat permainan TicTacToe. Kita susun terlebih dahulu gambar kotak kosong sebanyak 3x3 sebagai papan permainan. Kemudian tambahkan
gambar lingkaran dan gambar silang sebagai pemainnya. Gambar ini kita set properties Visible-nya menjadi false. Semua gambar kita letakkan di PictureBox. Inti dari program ini adalah menunggu pemain untuk menempatkan pilihannya. Tiap PictureBox kita tambahkan event onClick. Apabila salah satu di-klik, akan dicek terlebih dahulu, apakah masih kosong atau tidak. Apabila kosong, maka pemain boleh memilih kotak tersebut, artinya kita copy-kan simbol silang ke kotak tersebut. Kemudian tinggal di-cek, apakah sudah ada yang berhasil membuat 3 segaris atau belum.
Lalu giliran komputer. Komputer akan memilih secara acak kotak yang kosong, kemudian copy-kan simbol lingkaran ke kotak tersebut. Demikian terus bergantian antara pemain dan komputer sampai
semuanya terisi. Tapi apabila ada yang berhasil membuat 3 segaris, maka dia dinyatakan sebagai pemenang.

III. Tugas
Tambahkan kemampuan pada program tersebut sehingga komputer lebih sulit untuk dikalahkan. Komputer tidak lagi memilih kotak kosong secara acak, namun terlebih dahulu mengecek apakah dia ada peluang untuk membentuk 3 segaris (=menang) atau tidak. Apabila tidak ada peluang tersebut, maka komputer harus menge-blok kemungkinan user menang.

ini adalah tampilan pada bagian loading ketika awal masuk program (form2)

ini adalah koding programnya!

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 tictactoefix
{
    public partial class Form2 : Form
    {
        static string a;
        int colour;

        public Form2()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            a = label1.Text;

            colour = colour + 1;

            switch (colour)
            {
                //memilih warna menggunakan fungsi switch case
                case 0: label1.ForeColor = Color.BlueViolet; break;
                case 1: label1.ForeColor = Color.LightBlue; break;
                case 2: label1.ForeColor = Color.Cyan; break;
                case 3: label1.ForeColor = Color.Red; break;
                case 4: label1.ForeColor = Color.Yellow; break;
                case 5: label1.ForeColor = Color.PowderBlue; break;
                case 6: label1.ForeColor = Color.Purple; break;
                case 7: label1.ForeColor = Color.RoyalBlue; break;
                case 8: label1.ForeColor = Color.SkyBlue; break;
                case 9: label1.ForeColor = Color.SteelBlue; break;
            }
            if (colour > 9)
                colour = 0;
            if (progressBar1.Value < 100)
            {
                progressBar1.Value += 1;
            }
            else if (progressBar1.Value == 100)
            {
                timer1.Stop();
                Form1 Form = new Form1();
                Form.Show();
                this.Hide();
            }
        }
    }
}


ini adalah program bagian inti ketika di running (form1)


ini kodingan nya
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 tictactoefix
{
    public partial class Form1 : Form
    {

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

        static string h;
        int a,b,c,d,r,colour;

        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 WIN !");
                scoreX();
                reset();
                return;
                
            }

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

            }

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

            }

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

                MessageBox.Show("X WIN !");
                scoreX();
                reset();
                return;

            }

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

                MessageBox.Show("X WIN !");
                scoreX();
                reset();
                return;

            }

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

                MessageBox.Show("X WIN !");
                scoreX();
                reset();
                return;

            }

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

                MessageBox.Show("X WIN !");
                scoreX();
                reset();
                return;

            }

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

                MessageBox.Show("X WIN !");
                scoreX();
                reset();
                return;

            }

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

                MessageBox.Show("O WIN !");
                scoreO();
                reset();
                return;


            }

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

                MessageBox.Show("O WIN !");
                scoreO();
                reset();
                return;

            }

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

                MessageBox.Show("O WIN !");
                scoreO();
                reset();
                return;

            }

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

                MessageBox.Show("O WIN !");
                scoreO();
                reset();
                return;

            }

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

                MessageBox.Show("O WIN !");
                scoreO();
                reset();
                return;

            }

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

                MessageBox.Show("O WIN !");
                scoreO();
                reset();
                return;

            }

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

                MessageBox.Show("O WIN !");
                scoreO();
                reset();
                return;

            }

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

                MessageBox.Show("O WIN !");
                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 newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            reset();
        }
        #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

        private void timer1_Tick(object sender, EventArgs e)
        {
            h = label3.Text;
           
            colour = colour + 1;

            switch (colour)
            {
                //memilih warna menggunakan fungsi switch case
                case 0: label3.ForeColor = Color.BlueViolet; break;
                case 1: label3.ForeColor = Color.LightBlue; break;
                case 2: label3.ForeColor = Color.Cyan; break;
                case 3: label3.ForeColor = Color.Red; break;
                case 4: label3.ForeColor = Color.Yellow; break;
                case 5: label3.ForeColor = Color.PowderBlue; break;
                case 6: label3.ForeColor = Color.Purple; break;
                case 7: label3.ForeColor = Color.RoyalBlue; break;
                case 8: label3.ForeColor = Color.SkyBlue; break;
                case 9: label3.ForeColor = Color.SteelBlue; break;
            }
            if (colour > 9)
                colour = 0;
        }

        private void exitToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            Close();
        }

        
     
    }
}

terimakasih sudah mengunjungi :) semoga bermanfaat

No comments:

Post a Comment