Minggu, 03 Mei 2015

Membuat Game Tic Tac Toe menggunakan Ms. visual c# 2010 Express

Assalamualaikum wr.wb ..
kembali lagi di blog saya mengenai pemograman c# pada Ms. visual c# 2010 Express ..
Setelah Beberapa kali membuat aplikasi berbasis c#, maka kali ini kita akan memcoba membuat sebuah game sederhana berbasis C#. Game tersebut adalah Tic Tac Toe. game ini adalah game yang sangat simple dan termasuk game yang lama
pertama- tama siapkan terlebih dahulu:
1. Microsoft Visual Studio. bisa diganti dengan aplikasi sejenis yang memiliki bahasa pemograman c#.
2.Niat dan jangan lupa baca bismillah
langkah – langkah awalnya sama seperti post sebelumnya yaitu membuka aplikasi visual studio dan membuat project baru . setelah itu dapat mengikuti langkah-langkah dibawah ini:

1. Desainlah tampilan form menjadi seperti ini
2.klik kanan lalu klik view code dan buat program seperti dibawah ini tetapi nama properties dari setiap komponen di sesuaikan dengan yang teman-teman buat.

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int play = 0;
        int p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0, p6 = 0, p7 = 0, p8 = 0, p9 = 0;
        int input;

        public Form1()
        {
            InitializeComponent();
        }
        void com_expert(int masukan)
        {
            if (masukan == 5)                    // ketika player memilih pictureBox 5
            {
                if (p2 == 0)              // akan di tahan di pictureBox 2
                {
                    if (play == 1)
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.x;
                        p2 = 2;             //tampilan sesuai pilihan
                    }

                    else
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.o;
                        p2 = 1;
                    }
                }

                else if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p3 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.x;
                        p3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.o;
                        p3 = 1;
                    }
                }

                else if (p4 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.x;
                        p4 = 2;
                    }

                    else
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.o;
                        p4 = 1;
                    }
                }

                else if (p6 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.x;
                        p6 = 2;
                    }

                    else
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.o;
                        p6 = 1;
                    }
                }

                else if (p7 == 0 && p3 == 1 && p5 == 1)
                {
                    if (play == 1)
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.x;
                        p7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.o;
                        p7 = 1;
                    }
                }

                else if (p8 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.x;
                        p8 = 2;
                    }

                    else
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.o;
                        p8 = 1;
                    }
                }

                else if (p9 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.x;
                        p9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.o;
                        p9 = 1;
                    }
                }
            }

            if (masukan == 1)
            {
                if (p2 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.x;
                        p2 = 2;
                    }

                    else
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.o;
                        p2 = 1;
                    }
                }

            }


            if (masukan == 2)
            {
                if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p3 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.x;
                        p3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.o;
                        p3 = 1;
                    }
                }


                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }

                else if (p8 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.x;
                        p8 = 2;
                    }

                    else
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.o;
                        p8 = 1;
                    }
                }
            }

            if (masukan == 3)
            {
                if (p2 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.x;
                        p2 = 2;
                    }

                    else
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.o;
                        p2 = 1;
                    }
                }

                else if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p6 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.x;
                        p6 = 2;
                    }

                    else
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.o;
                        p6 = 1;
                    }
                }

                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }

                else if (p7 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.x;
                        p7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.o;
                        p7 = 1;
                    }
                }

                else if (p9 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.x;
                        p9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.o;
                        p9 = 1;
                    }
                }
            }

            if (masukan == 4)
            {
                if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }

                else if (p6 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.x;
                        p6 = 2;
                    }

                    else
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.o;
                        p6 = 1;
                    }
                }

                else if (p7 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.x;
                        p7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.o;
                        p7 = 1;
                    }
                }
            }

            if (masukan == 6)
            {
                if (p3 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.x;
                        p3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.o;
                        p3 = 1;
                    }
                }
                else if (p9 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.x;
                        p9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.o;
                        p9 = 1;
                    }
                }
                else if (p4 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.x;
                        p4 = 2;
                    }

                    else
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.o;
                        p4 = 1;
                    }
                }
                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }
            }

            if (masukan == 7)
            {
                if (p3 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.x;
                        p3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.o;
                        p3 = 1;
                    }
                }
                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }

                else if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p4 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.x;
                        p4 = 2;
                    }

                    else
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.o;
                        p4 = 1;
                    }
                }
                else if (p8 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.x;
                        p8 = 2;
                    }

                    else
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.o;
                        p8 = 1;
                    }
                }
                else if (p9 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.x;
                        p9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.o;
                        p9 = 1;
                    }
                }
            }
            if (masukan == 8)
            {
                if (p2 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.x;
                        p2 = 2;
                    }

                    else
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.o;
                        p2 = 1;
                    }
                }
                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }
                else if (p7 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.x;
                        p7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.o;
                        p7 = 1;
                    }
                }
                else if (p9 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.x;
                        p9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.o;
                        p9 = 1;
                    }
                }
            }
            if (masukan == 9)
            {
                if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p3 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.x;
                        p3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.o;
                        p3 = 1;
                    }
                }

                else if (p6 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.x;
                        p6 = 2;
                    }

                    else
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.o;
                        p6 = 1;
                    }
                }

                else if (p7 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.x;
                        p7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.o;
                        p7 = 1;
                    }
                }



                else if (p8 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.x;
                        p8 = 2;
                    }

                    else
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.o;
                        p8 = 1;
                    }
                }

                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda"); // indikator jika pemain mengklik pictureBox jika belum memilih
            }

            else if (play > 0 && p1 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox1.BackgroundImage = Properties.Resources.x;
                    p1 = 2;
                }

                else
                {
                    pictureBox1.BackgroundImage = Properties.Resources.o;
                    p1 = 1;
                }
                //com();
                input = 1;
                com_expert(1);
            }

            menang();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p2 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox2.BackgroundImage = Properties.Resources.x;
                    p2 = 2;
                }

                else
                {
                    pictureBox2.BackgroundImage = Properties.Resources.o;
                    p2 = 1;
                }
                //com();
                input = 2;
                com_expert(2);
            }

            menang();

        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }
            else if (play > 0 && p3 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox3.BackgroundImage = Properties.Resources.x;
                    p3 = 2;
                }

                else
                {
                    pictureBox3.BackgroundImage = Properties.Resources.o;
                    p3 = 1;
                }

                //com();
                input = 3;
                com_expert(3);
            }
            menang();

        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p4 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox4.BackgroundImage = Properties.Resources.x;
                    p4 = 2;
                }

                else
                {
                    pictureBox4.BackgroundImage = Properties.Resources.o;
                    p4 = 1;
                }

                //com();
                input = 4;
                com_expert(4);
            }

            menang();

        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }
            else if (play > 0 && p5 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox5.BackgroundImage = Properties.Resources.x;
                    p5 = 2;
                }

                else
                {
                    pictureBox5.BackgroundImage = Properties.Resources.o;

                    p5 = 1;
                }

                input = 5;
                com_expert(5);
                //com();

            }
            menang();


        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p6 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox6.BackgroundImage = Properties.Resources.x;

                    p6 = 1;
                }

                else
                {
                    pictureBox6.BackgroundImage = Properties.Resources.o;

                    p6 = 1;
                }

                //com();
                input = 6;
                com_expert(6);

            }
            menang();

        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p7 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox7.BackgroundImage = Properties.Resources.x;
                    p7 = 2;
                }

                else
                {
                    pictureBox7.BackgroundImage = Properties.Resources.o;

                    p7 = 1;
                }

                //com();
                input = 7;
                com_expert(7);

            }
            menang();

        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p8 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox8.BackgroundImage = Properties.Resources.x;

                    p8 = 2;
                }

                else
                {
                    pictureBox8.BackgroundImage = Properties.Resources.o;
                    p8 = 1;
                }

                //com();
                input = 8;
                com_expert(8);
            }
            menang();
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p9 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox9.BackgroundImage = Properties.Resources.x;
                    p9 = 2;
                }

                else
                {
                    pictureBox9.BackgroundImage = Properties.Resources.o;
                    p9 = 1;
                }

                //com();
                input = 9;
                com_expert(9);
            }
            menang();

        }

        void menang()
        {
            //1 ,2 ,3
            if (p1 == 2 && p2 == 2 && p3 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p1 == 1 && p2 == 1 && p3 == 1)
            {
                textBox1.Text = "O WON";
            }

            //4, 5, 6

            else if (p4 == 2 && p5 == 2 && p6 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p4 == 1 && p5 == 1 && p6 == 1)
            {
                textBox1.Text = "O WON";
            }
            //win 7 , 8 , 9

            else if (p7 == 2 && p8 == 2 && p9 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p7 == 1 && p8 == 1 && p9 == 1)
            {
                textBox1.Text = "O WON ";
            }
            //1, 4 ,7
            else if (p1 == 2 && p4 == 2 && p7 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p1 == 1 && p4 == 1 && p7 == 1)
            {
                textBox1.Text = "O WON";
            }
            //2, 5, 8
            else if (p2 == 2 && p5 == 2 && p8 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p2 == 1 && p5 == 1 && p8 == 1)
            {
                textBox1.Text = "O WON";
            }
            //3, 6 , 9
            else if (p3 == 2 && p6 == 2 && p9 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p3 == 1 && p6 == 1 && p9 == 1)
            {
                textBox1.Text = "O WON";
            }

            //diagonal
            //1, 5 , 9
            else if (p1 == 2 && p5 == 2 && p9 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p1 == 1 && p5 == 1 && p9 == 1)
            {
                textBox1.Text = "O WON";
            }
            //3, 5 , 7
            else if (p3 == 2 && p5 == 2 && p7 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p3 == 1 && p5 == 1 && p7 == 1)
            {
                textBox1.Text = "O WON";
            }
        }

        private void radioButton2_CheckedChanged_1(object sender, EventArgs e)
        {
            if (play == 0)
            {
                play = 2;
            }
            label1.Text = " YOU X dan COM O";
        }

        private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
        {
            if (play == 0)
            {
                play = 1;
            }
            label1.Text = " YOU O dan COM X";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            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;
            p1 = p2 = p3 = p4 = p5 = p6 = p7 = p8 = p9 = 0;
            label1.Text = "";
            textBox1.Text = "";
            play = 0;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

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

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int play = 0;
        int p1 = 0, p2 = 0, p3 = 0, p4 = 0, p5 = 0, p6 = 0, p7 = 0, p8 = 0, p9 = 0;
        int input;

        public Form1()
        {
            InitializeComponent();
        }
        void com_expert(int masukan)
        {
            if (masukan == 5)                    // ketika player memilih pictureBox 5
            {
                if (p2 == 0)              // akan di tahan di pictureBox 2
                {
                    if (play == 1)
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.x;
                        p2 = 2;             //tampilan sesuai pilihan
                    }

                    else
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.o;
                        p2 = 1;
                    }
                }

                else if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p3 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.x;
                        p3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.o;
                        p3 = 1;
                    }
                }

                else if (p4 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.x;
                        p4 = 2;
                    }

                    else
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.o;
                        p4 = 1;
                    }
                }

                else if (p6 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.x;
                        p6 = 2;
                    }

                    else
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.o;
                        p6 = 1;
                    }
                }

                else if (p7 == 0 && p3 == 1 && p5 == 1)
                {
                    if (play == 1)
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.x;
                        p7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.o;
                        p7 = 1;
                    }
                }

                else if (p8 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.x;
                        p8 = 2;
                    }

                    else
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.o;
                        p8 = 1;
                    }
                }

                else if (p9 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.x;
                        p9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.o;
                        p9 = 1;
                    }
                }
            }

            if (masukan == 1)
            {
                if (p2 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.x;
                        p2 = 2;
                    }

                    else
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.o;
                        p2 = 1;
                    }
                }

            }


            if (masukan == 2)
            {
                if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p3 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.x;
                        p3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.o;
                        p3 = 1;
                    }
                }


                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }

                else if (p8 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.x;
                        p8 = 2;
                    }

                    else
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.o;
                        p8 = 1;
                    }
                }
            }

            if (masukan == 3)
            {
                if (p2 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.x;
                        p2 = 2;
                    }

                    else
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.o;
                        p2 = 1;
                    }
                }

                else if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p6 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.x;
                        p6 = 2;
                    }

                    else
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.o;
                        p6 = 1;
                    }
                }

                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }

                else if (p7 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.x;
                        p7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.o;
                        p7 = 1;
                    }
                }

                else if (p9 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.x;
                        p9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.o;
                        p9 = 1;
                    }
                }
            }

            if (masukan == 4)
            {
                if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }

                else if (p6 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.x;
                        p6 = 2;
                    }

                    else
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.o;
                        p6 = 1;
                    }
                }

                else if (p7 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.x;
                        p7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.o;
                        p7 = 1;
                    }
                }
            }

            if (masukan == 6)
            {
                if (p3 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.x;
                        p3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.o;
                        p3 = 1;
                    }
                }
                else if (p9 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.x;
                        p9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.o;
                        p9 = 1;
                    }
                }
                else if (p4 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.x;
                        p4 = 2;
                    }

                    else
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.o;
                        p4 = 1;
                    }
                }
                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }
            }

            if (masukan == 7)
            {
                if (p3 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.x;
                        p3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.o;
                        p3 = 1;
                    }
                }
                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }

                else if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p4 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.x;
                        p4 = 2;
                    }

                    else
                    {
                        pictureBox4.BackgroundImage = Properties.Resources.o;
                        p4 = 1;
                    }
                }
                else if (p8 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.x;
                        p8 = 2;
                    }

                    else
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.o;
                        p8 = 1;
                    }
                }
                else if (p9 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.x;
                        p9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.o;
                        p9 = 1;
                    }
                }
            }
            if (masukan == 8)
            {
                if (p2 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.x;
                        p2 = 2;
                    }

                    else
                    {
                        pictureBox2.BackgroundImage = Properties.Resources.o;
                        p2 = 1;
                    }
                }
                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }
                else if (p7 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.x;
                        p7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.o;
                        p7 = 1;
                    }
                }
                else if (p9 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.x;
                        p9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = Properties.Resources.o;
                        p9 = 1;
                    }
                }
            }
            if (masukan == 9)
            {
                if (p1 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.x;
                        p1 = 2;
                    }

                    else
                    {
                        pictureBox1.BackgroundImage = Properties.Resources.o;
                        p1 = 1;
                    }
                }

                else if (p3 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.x;
                        p3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = Properties.Resources.o;
                        p3 = 1;
                    }
                }

                else if (p6 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.x;
                        p6 = 2;
                    }

                    else
                    {
                        pictureBox6.BackgroundImage = Properties.Resources.o;
                        p6 = 1;
                    }
                }

                else if (p7 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.x;
                        p7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = Properties.Resources.o;
                        p7 = 1;
                    }
                }



                else if (p8 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.x;
                        p8 = 2;
                    }

                    else
                    {
                        pictureBox8.BackgroundImage = Properties.Resources.o;
                        p8 = 1;
                    }
                }

                else if (p5 == 0)
                {
                    if (play == 1)
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.x;
                        p5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = Properties.Resources.o;
                        p5 = 1;
                    }
                }
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {

        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda"); // indikator jika pemain mengklik pictureBox jika belum memilih
            }

            else if (play > 0 && p1 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox1.BackgroundImage = Properties.Resources.x;
                    p1 = 2;
                }

                else
                {
                    pictureBox1.BackgroundImage = Properties.Resources.o;
                    p1 = 1;
                }
                //com();
                input = 1;
                com_expert(1);
            }

            menang();
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p2 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox2.BackgroundImage = Properties.Resources.x;
                    p2 = 2;
                }

                else
                {
                    pictureBox2.BackgroundImage = Properties.Resources.o;
                    p2 = 1;
                }
                //com();
                input = 2;
                com_expert(2);
            }

            menang();

        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }
            else if (play > 0 && p3 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox3.BackgroundImage = Properties.Resources.x;
                    p3 = 2;
                }

                else
                {
                    pictureBox3.BackgroundImage = Properties.Resources.o;
                    p3 = 1;
                }

                //com();
                input = 3;
                com_expert(3);
            }
            menang();

        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p4 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox4.BackgroundImage = Properties.Resources.x;
                    p4 = 2;
                }

                else
                {
                    pictureBox4.BackgroundImage = Properties.Resources.o;
                    p4 = 1;
                }

                //com();
                input = 4;
                com_expert(4);
            }

            menang();

        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }
            else if (play > 0 && p5 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox5.BackgroundImage = Properties.Resources.x;
                    p5 = 2;
                }

                else
                {
                    pictureBox5.BackgroundImage = Properties.Resources.o;

                    p5 = 1;
                }

                input = 5;
                com_expert(5);
                //com();

            }
            menang();


        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p6 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox6.BackgroundImage = Properties.Resources.x;

                    p6 = 1;
                }

                else
                {
                    pictureBox6.BackgroundImage = Properties.Resources.o;

                    p6 = 1;
                }

                //com();
                input = 6;
                com_expert(6);

            }
            menang();

        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p7 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox7.BackgroundImage = Properties.Resources.x;
                    p7 = 2;
                }

                else
                {
                    pictureBox7.BackgroundImage = Properties.Resources.o;

                    p7 = 1;
                }

                //com();
                input = 7;
                com_expert(7);

            }
            menang();

        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p8 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox8.BackgroundImage = Properties.Resources.x;

                    p8 = 2;
                }

                else
                {
                    pictureBox8.BackgroundImage = Properties.Resources.o;
                    p8 = 1;
                }

                //com();
                input = 8;
                com_expert(8);
            }
            menang();
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {

            if (play == 0)
            {
                MessageBox.Show("Silahkan Masukkan Pilihan anda");
            }

            else if (play > 0 && p9 == 0)
            {
                if (play % 2 == 0)
                {
                    pictureBox9.BackgroundImage = Properties.Resources.x;
                    p9 = 2;
                }

                else
                {
                    pictureBox9.BackgroundImage = Properties.Resources.o;
                    p9 = 1;
                }

                //com();
                input = 9;
                com_expert(9);
            }
            menang();

        }

        void menang()
        {
            //1 ,2 ,3
            if (p1 == 2 && p2 == 2 && p3 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p1 == 1 && p2 == 1 && p3 == 1)
            {
                textBox1.Text = "O WON";
            }

            //4, 5, 6

            else if (p4 == 2 && p5 == 2 && p6 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p4 == 1 && p5 == 1 && p6 == 1)
            {
                textBox1.Text = "O WON";
            }
            //win 7 , 8 , 9

            else if (p7 == 2 && p8 == 2 && p9 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p7 == 1 && p8 == 1 && p9 == 1)
            {
                textBox1.Text = "O WON ";
            }
            //1, 4 ,7
            else if (p1 == 2 && p4 == 2 && p7 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p1 == 1 && p4 == 1 && p7 == 1)
            {
                textBox1.Text = "O WON";
            }
            //2, 5, 8
            else if (p2 == 2 && p5 == 2 && p8 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p2 == 1 && p5 == 1 && p8 == 1)
            {
                textBox1.Text = "O WON";
            }
            //3, 6 , 9
            else if (p3 == 2 && p6 == 2 && p9 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p3 == 1 && p6 == 1 && p9 == 1)
            {
                textBox1.Text = "O WON";
            }

            //diagonal
            //1, 5 , 9
            else if (p1 == 2 && p5 == 2 && p9 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p1 == 1 && p5 == 1 && p9 == 1)
            {
                textBox1.Text = "O WON";
            }
            //3, 5 , 7
            else if (p3 == 2 && p5 == 2 && p7 == 2)
            {
                textBox1.Text = "X WON";
            }
            else if (p3 == 1 && p5 == 1 && p7 == 1)
            {
                textBox1.Text = "O WON";
            }
        }

        private void radioButton2_CheckedChanged_1(object sender, EventArgs e)
        {
            if (play == 0)
            {
                play = 2;
            }
            label1.Text = " YOU X dan COM O";
        }

        private void radioButton1_CheckedChanged_1(object sender, EventArgs e)
        {
            if (play == 0)
            {
                play = 1;
            }
            label1.Text = " YOU O dan COM X";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            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;
            p1 = p2 = p3 = p4 = p5 = p6 = p7 = p8 = p9 = 0;
            label1.Text = "";
            textBox1.Text = "";
            play = 0;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

      
    }
}

lalu coba run dengan menekan tombol play atau tombol F5 pada keyboard.

gambar diatas adalah hasil dari program yang saya buat. semoga teman-teman bisa juga untuk membuatnya.untuk penjelasan yang tidak dapat saya jelaskan pada post ini akan coba saya jelaskan pada video di bawah ini.

Tidak ada komentar:

Posting Komentar