Minggu, 31 Mei 2015

Serial Transmit

  '''Greetings!'''

Kali ini saya akan menjelaskan sebuah program Serial Port,tapi kali ini sebagai Serial Transmitter,sebenarnya program yang kita buat ini tidak jauh dari program utk chatting.Pengiriman data melalui serial port sangat sederhana, cukup menggunakan method Write dengan parameter berupa string yang ingin dikirim.

1.Aplikasi Chatting Sederhana

Baiklah,kita akan membuat sebuah aplikasi chating antara dua komputer menggunakan port serial.

Yang dibutuhkan:
  1. Komputer/Laptop
  2. Visual Studio/Sharp Develop
  3. Arduino
  4. Arduino IDE
1,Buat dulu solution baru pada VS ato C# Develop

2.Susun item item yang diperlukan,contoh spt gambar:

3. Komponen Timer ada di Windows Form toolbar. Sedangkan komponen SerialPort ada di Components toolbar. Jangan lupa, set properties Enabled di komponen Timer menjadi true.Kemudian properties Interval di-set menjadi 500 ms(tapi sbenarnya terserah aja mau berapa lama waktunya :3).

4. Double click tombol (button) dengan text “kirim”. Ketikkan statemen di bawah ini di dalam fungsi ButtonClick :

serialPort1.Write(textBox1.Text);

Kode di atas tujuannya adalah untuk menuliskan isi dari textBox1 ke port serial. Sebelumnya kita harus membuka port dulu dengan statemen :

serialPort1.Open();

Double click untuk timer. Ketikkan statemen di bawah ini di dalam fungsi TimerTick :

if (serialPort1.BytesToRead != 0)
{
textBox2.Text = serialPort1.ReadExisting();
}

5. Kode di atas tujuannya adalah untuk membaca data yang ada di buffer serial port. Compile dan jalankan program.

6. Buka hyperterminal, kemudian amati data yang tampil pada hyperterminal saat anda mengirim data.

7. Buka Arduino.exe.

8. Ketikkan program dibawah ini pada Arduino IDE.

\
9. Koneksikan Arduino ke computer, pilih port dan tipe Arduino terlebih dahulu. Kemudian upload program tersebut dengan cara mengeklik tanda  "UPLOAD"(Disebelah tanda cheklist).
10. Tunggu sampai ada tulisan “Done Uploading” pada Arduino IDE.

11. Jalankan aplikasi anda, dengan mengatur port serial sesuai dengan port Arduino yang terdeteksi pada computer.
12. Coba kirim data karakter dari aplikasi yang kita buat.
13.Lihat hasilnya pada Arduino.
Dan seperti ini keseluruhan koding nya :

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 Transmit
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Open();
            serialPort1.Write(textBox1.Text);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (serialPort1.BytesToRead != 0)
            {
                textBox2.Text = serialPort1.ReadExisting();
            }
        }
    }
}

2.Kontrol 3 LED

Sekarang saya telah membuat sebuah aplikasi yang digunakan untuk menghidupkan dan mematikan 3 buah LED yang ada pada Arduino. Pengaturan komunikasi serial dapat dilakukan langsung pada aplikasi tanpa harus mengubah program aplikasi.

yang dibutuhkan:
  1. Visual Studio/Sharp Develop
  2. Arduino IDE/HTerm + Virtual Serial Port*
  3. Komputer/Laptop
  4. Kemauan
*HTerm dan Virtual Serial Port dipakai jika kita tak ada Arduino

Seperti inilah penampakan program buatan saya:
dan inilah kodingan keseluruhan 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;
using System.IO.Ports;

namespace Port
{
    public partial class Form1 : Form
    {
        Image black = Properties.Resources.niggs;
        Image gold = Properties.Resources.latest;

        public Form1()
        {
            InitializeComponent();
            foreach (String port in System.IO.Ports.SerialPort.GetPortNames())
            {
                comboBox1.Items.Add(port);
            }
            pictureBox1.BackgroundImage = black;
            pictureBox2.BackgroundImage = black;
            pictureBox3.BackgroundImage = black;

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort1.Open();
        }

        private void button3_Click(object sender, EventArgs e) //on led 1
        {
            try
            {
                serialPort1.Write("1");
                pictureBox1.BackgroundImage = gold;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button4_Click(object sender, EventArgs e) //off led 1
        {
            try
            {
                serialPort1.Write("0");
                pictureBox1.BackgroundImage = black;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button5_Click(object sender, EventArgs e) //on led 2
        {
            try
            {
                serialPort1.Write("2");
                pictureBox2.BackgroundImage = gold;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        private void button6_Click(object sender, EventArgs e) //off led 2
        {
            try
            {
                serialPort1.Write("3");
                pictureBox2.BackgroundImage = black;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button7_Click(object sender, EventArgs e) //on led 3
        {
            try
            {
                serialPort1.Write("4");
                pictureBox3.BackgroundImage = gold;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);

            }
        }

        private void button8_Click(object sender, EventArgs e) //off led 3
        {
            try
            {
                serialPort1.Write("5");
                pictureBox3.BackgroundImage = black;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.Write(textBox1.Text);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            serialPort1.Close();
            Application.Exit();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (serialPort1.BytesToRead != 0)
            {
                textBox2.Text = serialPort1.ReadExisting();
            }
        }
    }
}

Jalankan aplikasi HTerm,tapi sebelum itu,atur dulu port nya memakai Virtual Serial Port


Setelah itu jalankan aplikasi HTerm nya,ubah Baudrate nya ke 9600 terlebih dahulu

Seperti inilah jadinya program nya


Berhubung saya tak ada Arduino -_-,jadi maaf saya tak bisa memperlihatkan jalannya program dengan Arduino

Dan ini Video penjelasan nya :)

 Baiklah,saya rasa sekian dari saya.Maaf jika ada salah kata atau Typo 0:)

Compact!!

Serial Receiver

^^^Holla!^^^

Pada kesempatan kali ini,saya akan menunjukkan sebuah program Serial Receiver,tentu saja dengan port sbg jalur nya,tapi sebelum itu apa itu PORT?


1.Port









Port merupakan kumpulan jalur elektronik untuk menyalurkan data. Port ada yang untuk input saja, output saja atau input/output. Port dipakai untuk komunikasi antara CPU dengan monitor, keyboard, mouse dll. Proses pengiriman data dapat langsung sekaligus secara paralel dengan menggunakan beberapa kabel, atau satu-persatu secara berurutan (serial) dengan menggunakan sepasang kabel. Contoh transmisi data secara paralel adalah komunikasi harddisk IDE dengan CPU, komunikasi printer dengan komputer. 

2.Program Data Receiver

Kali ini kita akan membuat sebuah program yang berfungsi untuk menerima data dari mikrokontroller melalui port serial. Data yang dikirim oleh mikrokontroller adalah kondisi dari potensiometer. Nilai analog tegangan akan dikonversi oleh mikro ke nilai digital dalam range 0 - 1023. Kemudian nilai digital ini yang dikirim ke komputer melalui port serial. Program pada mikro (kita akan menggunakan arduino)

Yang dibutuhkan:
  1. Potensiometer
  2. Arduino
  3. Aplikasi Arduino
  4. Visual Studio / C# Develop

Potesiometer

Arduino

kodingan di aplkasi Arduino

void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead(A1);
delay(500);
Serial.println(sensorValue, DEC);
}

1.Buat solution baru pada. Susun form dengan item item seperti terlihat pada gambar.

2.Atur properties “Series” pada chart, sehingga muncul window baru seperti dibawah ini.
 kita bisa mengubah jenis grafik dengan mengubah ChartType nya.


3.Komponen SerialPort ada di Components toolbar, sedangkan komponen Chart ada di data.

4.Tambahkan program berikut untuk control button1:

if (button1.Text == "Connect") {
button1.Text = "Disconnect";
serialPort1.Open();
richtextbox1.text="";
a=0;
else 
{
button1.Text = "Connect";
serialPort1.Close();
}

5. Tambahkan program berikut ke event DataReceived-nya serial:

rxString = serialPort1.ReadLine();
this.Invoke(new EventHandler(DisplayText));

Kita menggunakan method Invoke karena thread untuk receive serial berbeda dengan thread program utama. Padahal object RichTextBox atau Label, tempat kita untuk menampilkan data serial, terletak di thread utama. Invoke membuat kita dapat melakukan cross-thread.Program di atas akan membuat event DataReceived memanggil event handler baru yang terletak di thread utama, yaitu DisplayText.

6.Untuk method DisplayText, tambahkan program berikut:

int a; //variable global
private void DisplayText(object sender, EventArgs e){
richTextBox1.AppendText(rxString);
richTextBox1.ScrollToCaret();
chart1.Series["Series1"].Points.AddXY(a,Convert.ToInt16(rxString));
a++;
}

7.Compile dan jalankan...

dan seperti ini kodingan keseluruhan nya

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 Nuel
{
    public partial class Form1 : Form
    {
        int a;
        string rxString;


        private void DisplayText(object sender, EventArgs e)
        {
            richTextBox1.AppendText(rxString);
            richTextBox1.ScrollToCaret();
            chart1.Series["Series1"].Points.AddXY(a, Convert.ToInt16(rxString));
            a++;
        } 
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (button1.Text == "Connect")
            {
                button1.Text = "Disconnect";
                serialPort1.Open();
                richTextBox1.Text = "";
                a = 0;
            }
            else
            {
                button1.Text = "Connect";
                serialPort1.Close();
            }

        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            rxString = serialPort1.ReadLine();
            this.Invoke(new EventHandler(DisplayText));
        }

    }
}

Ntar kalo jalan nya kayak gini:


Compact!!

Minggu, 03 Mei 2015

Tic Tac Toe di C#

[[[ SALAM ]]]
Salam hangat para blogger sekalian ! :)
Pada kesempatan kali ini saya aan menunjukkan sebuah program yang tidak biasa seperti sebelum nya,kali ini saya akan menunjukkan sebuah program Game sederhana yang bernama Tic Tac Toe,sebelum kita ke progam nya saya akan menjelaskan apa itu Tic Tac Toe...

1. Tentang Tic Tac Toe dan Sejarah nya

APA SIH TIC TAC TOE.?
     Sebelumnya saya ingin menjelaskan awal mula permainan Tic Tac Toe. Game Tic Tac Toe lebih dikenal sebagai permainan catur jawa jika di Indonesia. Bahkan dalam pengembangan permainan ini, Tic Tac Toe ada yang dalam bentuk tiga dimensi. Sebagian dari kita pasti mengenal permainan Tic Tac Toe. Permainan ini sangat unik karena menggunakan papan dengan grid 3×3 atau lebih yang dimainkan oleh dua orang pemain dan para pemain bermain dengan menggunakan bidak berbentuk “X” atau “O” membentuk satu barisan untuk memenangkan permainan. Permainan ini sangat diminati banyak orang di seluruh penjuruh dunia, bahkan dengan kepopulerannya di banyak negara memberikan nama unik seperti nama tit-tat-toe,Naughts and crosses,Exy-Ozys,Xsie-Osies,serta X’s and O’s.

SEJARAH

Menurut sejarah Tic Tac Toe berasal dari Kekaisaran Romawi sekitar abad pertama sebelum masehi. Masyarakat setempat menyebutnya Terni Lapili. Permainan ini sama seperti yang kita temukan di zaman sekarang yang dimainkan oleh dua orang  pemain dengan menggunakan tiga bidak, pemain harus membentuk satu barisan dengan bidak-bidak yang dimiiki di lembar kertas papirus. Namun ada informasi lain, menurut Claudia Zaslavsky dalam buku Tic Tac Toe: And Other Three-In-A Row Games from Ancient Egypt to the Modern Computer,  Tic-Tac-Toe berasal dari Mesir kuno. Namun bagaimana permainan ini mulai disebut dengan nama Tic Tac Toe? Pada tahun 1884, anak-anak bermain di sebuah batu tulis  dimana mereka menutup mata sambil mencoretkan batu dengan dua simbol "X" dan "O" membentuk satu barisan namun disertai suara tic dan tac dari anak-anak. Pada tahun 1952, Tic Tac Toe mulai marak diminati karena untuk pertama masuk ke dunia video game OXO (or Noughts and Crosses) untuk komputer EDSAC.  Pemain bisa memainkan Tic-Tac-Toe dengan melawan komputer bahkan dapat multyplay melawan manusia. Pada tahun 1975, Tic Tac Toe juga digunakan oleh siswa MIT  untuk menunjukkan kekuatan komputasi dari elemen Tinkertoy yang saat ini dipamerkan di Museum of Science, Boston . 
.
.
 . 
Program Tic Tac Toe dengan C#
Oke,mungkin itu saja sekilas mengenai Tic Tac Toe,sekarang kita akan beralih ke program,tapi kita lihat dulu teori dasar nya...


 Teori Dasar
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.



Jadi,untuk membuat program Tic Tac Toe kita akan lebih dominan menggunakan pictureBox.
Baiklah ,sekarang saya akan menunjukkan bagaimana program yang saya buat
 Dapat dilihat jika form program saya terdiri dari
  1. 5 buah label
  2. 2 buah groupbox
  3. 2 buah button
  4. 9 buah pircture box
  5. 1 buah menu toolstrip(diatas form program)
dan saya akan menunjukkan sedikit pembuatan program nya..
Dan yang dibutuhkan adalah:
  1. Komputer/Laptop
  2. Kemauan
  3. Visual Studio / C Sharp Develop
langkah langkah
 
1.Buka Visual Studio / C Sharp Develop
 
2.Seperti biasa,pilih WindowsFormApplication
 
3. ketika WindowsFormApllication yang akan kita edit sudah muncul,maka isilah form yang kosong dengan benda benda dari toolbox,semau anda yang penting disitu tersedia 9 picture box sebagai wadah menaruh gambar
seperti ini kira kira kita harus membuatnya

4.Kita harus memberi event klik pada tiap picture box,pada menu event pilih Click
5.Baiklah,dan mari kira lihat seluruh 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 WindowsFormsApplication18
{
    public partial class Form1 : Form
    {
        int start = 0;
        int d1 = 0, d2 = 0, d3 = 0, d4 = 0, d5 = 0, d6 = 0, d7 = 0, d8 = 0, d9 = 0;
        int type;
        int kolor;
        public Form1()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }
         // menaruh gambar pada picturebox dengan fungsi resources
        void difficult(int type)
        {
            if (type == 5)
            {
                if (d2 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox2.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d2 = 2;
                    }

                    else
                    {
                        pictureBox2.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d2 = 1;
                    }
                }
                else if (d1 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox1.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d1 = 2;
                    }

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

                else if (d3 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox3.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d3 = 2;
                    }

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

                else if (d4 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox4.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d4 = 2;
                    }

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

                else if (d6 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox6.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d6 = 2;
                    }

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

                else if (d7 == 0 && d3 == 1 && d5 == 1)
                {
                    if (start == 1)
                    {
                        pictureBox7.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d7 = 2;
                    }

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

                else if (d8 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox8.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d8 = 2;
                    }

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

                else if (d9 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox9.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d9 = 2;
                    }

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

            if (type == 1)
            {
                if (d2 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox2.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d2 = 2;
                    }

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

            }


            if (type == 2)
            {
                if (d1 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox1.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d1 = 2;
                    }

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

                else if (d3 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox3.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d3 = 2;
                    }

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


                else if (d5 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox5.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d5 = 2;
                    }

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

                else if (d8 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox8.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d8 = 2;
                    }

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

            if (type == 3)
            {
                if (d2 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox2.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d2 = 2;
                    }

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

                else if (d1 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox1.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d1 = 2;
                    }

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

                else if (d6 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox6.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d6 = 2;
                    }

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

                else if (d5 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox5.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d5 = 2;
                    }

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

                else if (d7 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox7.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d7 = 2;
                    }

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

                else if (d9 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox9.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d9 = 2;
                    }

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

            if (type == 4)
            {
                if (d1 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox1.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d1 = 2;
                    }

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

                else if (d5 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox5.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d5 = 2;
                    }

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

                else if (d6 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox6.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d6 = 2;
                    }

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

                else if (d7 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox7.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d7 = 2;
                    }

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

            if (type == 6)
            {
                if (d3 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox3.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d3 = 1;
                    }
                }
                else if (d9 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox9.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d9 = 1;
                    }
                }
                else if (d4 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox4.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d4 = 2;
                    }

                    else
                    {
                        pictureBox4.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d4 = 1;
                    }
                }
                else if (d5 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox5.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d5 = 2;
                    }

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

            if (type == 7)
            {
                if (d3 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox3.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d3 = 2;
                    }

                    else
                    {
                        pictureBox3.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d3 = 1;
                    }
                }
                else if (d5 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox5.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d5 = 2;
                    }

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

                else if (d1 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox1.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d1 = 2;
                    }

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

                else if (d4 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox4.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d4 = 2;
                    }

                    else
                    {
                        pictureBox4.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d4 = 1;
                    }
                }
                else if (d8 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox8.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d8 = 2;
                    }

                    else
                    {
                        pictureBox8.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d8 = 1;
                    }
                }
                else if (d9 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox9.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d9 = 1;
                    }
                }
            }
            if (type == 8)
            {
                if (d2 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox2.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d2 = 2;
                    }

                    else
                    {
                        pictureBox2.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d2 = 1;
                    }
                }
                else if (d5 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox5.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d5 = 1;
                    }
                }
                else if (d7 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox7.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d7 = 2;
                    }

                    else
                    {
                        pictureBox7.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d7 = 1;
                    }
                }
                else if (d9 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox9.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d9 = 2;
                    }

                    else
                    {
                        pictureBox9.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d9 = 1;
                    }
                }
            }
            if (type == 9)
            {
                if (d1 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox1.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d1 = 2;
                    }

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

                else if (d3 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox3.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d3 = 2;
                    }

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

                else if (d6 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox6.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d6 = 2;
                    }

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

                else if (d7 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox7.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d7 = 2;
                    }

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



                else if (d8 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox8.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d8 = 2;
                    }

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

                else if (d5 == 0)
                {
                    if (start == 1)
                    {
                        pictureBox5.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                        d5 = 2;
                    }

                    else
                    {
                        pictureBox5.BackgroundImage = WindowsFormsApplication18.Properties.Resources.o;
                        d5 = 1;
                    }
                }
            }
        }
        //pemilihan pihak
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (start == 0)
            {
                MessageBox.Show("Choose Your Side!"); // indikator jika pemain mengklik pictureBox jika belum memilih
            }

            else if (start > 0 && d1 == 0)
            {
                if (start % 2 == 0)
                {
                    pictureBox1.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                    d1 = 2;
                }

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

                type = 1;
                difficult(1);
            }
            Win();

        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

            if (start == 0)
            {
                MessageBox.Show("Choose Your Side!");
            }

            else if (start > 0 && d2 == 0)
            {
                if (start % 2 == 0)
                {
                    pictureBox2.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                    d2 = 2;
                }

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

                type = 2;
                difficult(2);
            }
            Win();


        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {

            if (start == 0)
            {
                MessageBox.Show("Choose Your Side!");
            }
            else if (start > 0 && d3 == 0)
            {
                if (start % 2 == 0)
                {
                    pictureBox3.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                    d3 = 2;
                }

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


                type = 3;
                difficult(3);
            }
            Win();

        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {

            if (start == 0)
            {
                MessageBox.Show("Choose Your Side!");
            }

            else if (start > 0 && d4 == 0)
            {
                if (start % 2 == 0)
                {
                    pictureBox4.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                    d4 = 2;
                }

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


                type = 4;
                difficult(4);
            }
            Win();


        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {

            if (start == 0)
            {
                MessageBox.Show("Choose Your Side first!");
            }
            else if (start > 0 && d5 == 0)
            {
                if (start % 2 == 0)
                {
                    pictureBox5.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                    d5 = 2;
                }

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

                    d5 = 1;
                }

                type = 5;
                difficult(5);


            }
            Win();


        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {

            if (start == 0)
            {
                MessageBox.Show("Choose Your Side!");
            }

            else if (start > 0 && d6 == 0)
            {
                if (start % 2 == 0)
                {
                    pictureBox6.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;

                    d6 = 1;
                }

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

                    d6 = 1;
                }


                type = 6;
                difficult(6);

            }
            Win();

        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {

            if (start == 0)
            {
                MessageBox.Show("Choose Your Side!");
            }

            else if (start > 0 && d7 == 0)
            {
                if (start % 2 == 0)
                {
                    pictureBox7.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;

                    d7 = 2;
                }

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

                    d7 = 1;
                }


                type = 7;
                difficult(7);

            }
            Win();

        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {

            if (start == 0)
            {
                MessageBox.Show("Choose Your Side!");
            }

            else if (start > 0 && d8 == 0)
            {
                if (start % 2 == 0)
                {
                    pictureBox8.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;

                    d8 = 2;
                }

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


                type = 8;
                difficult(8);
            }
            Win();

        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            k
            if (start == 0)
            {
                MessageBox.Show("Choose Your Side!");
            }

            else if (start > 0 && d9 == 0)
            {
                if (start % 2 == 0)
                {
                    pictureBox9.BackgroundImage = WindowsFormsApplication18.Properties.Resources.x;
                    d9 = 2;
                }

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


                type = 9;
                difficult(9);
            }
            Win();

        }
        //pemilihan pihak
        //if you O
        private void button3_Click(object sender, EventArgs e)
        {
            if (start == 0)
            {
                start = 1;
            }
            label4.Text = "you (O) vs Computer (X)";
        }

        //if you X
        private void button4_Click(object sender, EventArgs e)
        {
            if (start == 0)
            {
                start = 2;
            }
            label4.Text = "you (X) vs Computer (O)";
        }

        void Win() //pemilihan pihak menang
        {
            //1 ,2 ,3
            if (d1 == 2 && d2 == 2 && d3 == 2)
            {
                label5.Text = "X side win!";
            }
            else if (d1 == 1 && d2 == 1 && d3 == 1)
            {
                label5.Text = "O side win!";
            }
            //4, 5, 6

            else if (d4 == 2 && d5 == 2 && d6 == 2)
            {
                label5.Text = "O side win!";
            }
            else if (d4 == 1 && d5 == 1 && d6 == 1)
            {
                label5.Text = "X side win!";
            }
            //win 7 , 8 , 9

            else if (d7 == 2 && d8 == 2 && d9 == 2)
            {
                label5.Text = "X side win!";
            }
            else if (d7 == 1 && d8 == 1 && d9 == 1)
            {
                label5.Text = "O side win!";
            }
            //1, 4 ,7
            else if (d1 == 2 && d4 == 2 && d7 == 2)
            {
                label5.Text = "X side win!";
            }
            else if (d1 == 1 && d4 == 1 && d7 == 1)
            {
                label5.Text = "O side win!";
            }
            //2, 5, 8
            else if (d2 == 2 && d5 == 2 && d8 == 2)
            {
                label5.Text = "X side win!";
            }
            else if (d2 == 1 && d5 == 1 && d8 == 1)
            {
                label5.Text = "O side win!";
            }
            //3, 6 , 9
            else if (d3 == 2 && d6 == 2 && d9 == 2)
            {
                label5.Text = "X side win!";
            }
            else if (d3 == 1 && d6 == 1 && d9 == 1)
            {
                label5.Text = "O side win!";
            }

            //diagonal
            //1, 5 , 9
            else if (d1 == 2 && d5 == 2 && d9 == 2)
            {
                label5.Text = "X side win!";
            }
            else if (d1 == 1 && d5 == 1 && d9 == 1)
            {
                label5.Text = "O side win!";
            }
            //3, 5 , 7
            else if (d3 == 2 && d5 == 2 && d7 == 2)
            {
                label5.Text = "X side win!";
            }
            else if (d3 == 1 && d5 == 1 && d7 == 1)
            {
                label5.Text = "O side win!";
            }
        }

        private void button_Click(object sender, EventArgs e)
        {

        }

        private void Picture_Click(object sender, EventArgs e)
        {

        }
        private void exit()
        {   //message box exit
            DialogResult result = MessageBox.Show("Really?", "Hold up for a sec'", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes) this.Close();
        }

        private void Exit_click(object sender, EventArgs e)
        { //memanggil void exit()
            exit();
        }

        private void newgame_click(object sender, EventArgs e)
        { //reset semua
            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;
            d1 = d2 = d3 = d4 = d5 = d6 = d7 = d8 = d9 = 0;
            label4.Text = "";
            label1.Text = "";
            start = 0;
        }

dan ini adalah kodingan tambahan (sambungan dari atas)

private void howToPlayToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("kalahkan lawan dengan mengisi 3 buah kotak secara berurut \n \n 1.pilih simbol apa pihak kamu X atau O? \n \n 2.isi kotak yang berwarna merah secara ber-urutan vertical,horizontal,atau diagonal \n \n 3.jangan biarkan lawan mengalahkan mu! \n \n 4.HAVE FUN!! :)", "Read This");
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("TicTacToe v 1.0.0 \n \n \n Dibuat dengan penuh kesabaran,dengan compile sana sini,ngoding sana sini,tapi puji Tuhan bisa juga akhirnya :) \n \n By: Imanuel Simatupang \n \n \n Copyright 2015", "Info");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            kolor = kolor + 1;
            switch (kolor)
            {
                case 0: label1.ForeColor = Color.White; break;
                case 1: label1.ForeColor = Color.WhiteSmoke; break;
                case 2: label1.ForeColor = Color.Gainsboro; break;
                case 3: label1.ForeColor = Color.LightGray; break; //tulisan Tic
                case 4: label1.ForeColor = Color.Silver; break;
                case 5: label1.ForeColor = Color.DarkGray; break;
                case 6: label1.ForeColor = Color.Gray; break;
                case 7: label1.ForeColor = Color.DimGray; break;
                case 8: label1.ForeColor = Color.Black; break;
                case 9: label1.ForeColor = Color.Black; break;
                case 10: label1.ForeColor = Color.DimGray; break;
                case 11: label1.ForeColor = Color.Gray; break;
                case 12: label1.ForeColor = Color.DarkGray; break;
                case 13: label1.ForeColor = Color.Silver; break;
                case 14: label1.ForeColor = Color.LightGray; break;
                case 15: label1.ForeColor = Color.Gainsboro; break;
                case 16: label1.ForeColor = Color.WhiteSmoke; break;
                case 17: label1.ForeColor = Color.White; break;
            }
            if (kolor > 16)
            { kolor = 0; }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            kolor = kolor + 1;
            switch (kolor)
            {
                case 0: label2.ForeColor = Color.White; break;
                case 1: label2.ForeColor = Color.WhiteSmoke; break;
                case 2: label2.ForeColor = Color.Gainsboro; break;
                case 3: label2.ForeColor = Color.LightGray; break;
                case 4: label2.ForeColor = Color.Silver; break;
                case 5: label2.ForeColor = Color.DarkGray; break; //tulisan Tac
                case 6: label2.ForeColor = Color.Gray; break;
                case 7: label2.ForeColor = Color.DimGray; break;
                case 8: label2.ForeColor = Color.Black; break;
                case 9: label2.ForeColor = Color.Black; break;
                case 10: label2.ForeColor = Color.DimGray; break;
                case 11: label2.ForeColor = Color.Gray; break;
                case 12: label2.ForeColor = Color.DarkGray; break;
                case 13: label2.ForeColor = Color.Silver; break;
                case 14: label2.ForeColor = Color.LightGray; break;
                case 15: label2.ForeColor = Color.Gainsboro; break;
                case 16: label2.ForeColor = Color.WhiteSmoke; break;
                case 17: label2.ForeColor = Color.White; break;
            }
            if (kolor > 16)
            { kolor = 0; }
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            kolor = kolor + 1;
            switch (kolor)
            {
                case 0: label3.ForeColor = Color.White; break;
                case 1: label3.ForeColor = Color.WhiteSmoke; break;
                case 2: label3.ForeColor = Color.Gainsboro; break;
                case 3: label3.ForeColor = Color.LightGray; break;
                case 4: label3.ForeColor = Color.Silver; break;
                case 5: label3.ForeColor = Color.DarkGray; break;
                case 6: label3.ForeColor = Color.Gray; break;
                case 7: label3.ForeColor = Color.DimGray; break; //tulisan Toe
                case 8: label3.ForeColor = Color.Black; break;
                case 9: label3.ForeColor = Color.Black; break;
                case 10: label3.ForeColor = Color.DimGray; break;
                case 11: label3.ForeColor = Color.Gray; break;
                case 12: label3.ForeColor = Color.DarkGray; break;
                case 13: label3.ForeColor = Color.Silver; break;
                case 14: label3.ForeColor = Color.LightGray; break;
                case 15: label3.ForeColor = Color.Gainsboro; break;
                case 16: label3.ForeColor = Color.WhiteSmoke; break;
                case 17: label3.ForeColor = Color.White; break;
            }
            if (kolor > 16)
            { kolor = 0; }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
    }
}

Dan ini Videonya

Baiklah,mungkin sekia dari saya,maaf jika ada yang tak jelas,dan kesalahan penulisan kata
Terima Kasih :)

---SALAM KOMPAK!!!---