Minggu, 31 Mei 2015

Serial Transmit

Assalamu'alaikum WR.WB
Hallo saya Akhbar Ferdeansyah, kali ini saya akan berbagi sedikit ilmu tentang Serial Transmit, Pengiriman data melalui serial port sangat sederhana, cukup menggunakan method Write dengan parameter berupa string yang ingin dikirim.

1. Tujuannya yaitu membuat aplikasi chating antara dua komputer menggunakan port serial.

2. Buat solution baru. Susun form dengan control-control seperti terlihat pada 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.

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.

Ini adalah program yang full:

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

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

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

6. Compile dan jalankan program.



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


Sekian tentang Serial Transmit dari saya, kurang lebihnya mohon maaf, Assalamu'alaikum WR.WB



Serial Receive

Assalamu'alaikum WR.WB

Hallo saya Akhbar Ferdeansyah, dalam kesempatan kali ini saya akan berbagi ilmu tentang Serial Port di C# untuk akusisi data.

Sebelumnya saya akan menjelaskan tentang Port, apa itu 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. Contoh transmisi data secara serial adalah komunikasi harddisk SATA dengan CPU, komunikasi USB dll.
Mikrokontroller umumnya menggunakan standar komunikasi serial RS232 untuk koneksi dengan perangkat lain. Komputer juga memiliki port standar serialport, paralelport, USB, VGA dan PS/2. Pada praktikum ini kita akan mempelajari penggunaan serial port.

1. Tujuan praktikum ini adalah 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) cukup sederhana:


2. Buat solution baru. Susun form dengan control-control seperti
terlihat pada gambar.


Atur properties “Series” pada chart, sehingga muncul window baru
seperti dibawah ini.

Anda 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:


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



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:



7. Compile dan jalankan program.

Sekian tentang Serial port dari saya, kurang lebihnya mohon maaf, Assalamu'alaikum WR.WB.





Selasa, 26 Mei 2015

Serial Port Communication visual studio C#

Assalamu'alakum WR.WB

Hallo, Kali ini saya akan berbagi ilmu tentang Serial Port Communication di visual studio C# tatapi tidak menggunakan hardware (antar aplikasi, dengan menggunakan aplikasi tambahan"Virtual Port").
kida dapat mengirim dan menerima data sederhana (berupa text) antar serialport yang kemudian kita beri fungsi untuk mengontrol beberapa lampu, kita cukup menggunakan method write dengan parameter berupa string yang akan di kirim dan diterima dala bentuk string juga.

Pertama kita buat design seperti di bawah ini ;


Tambahkan komponen "timer" dan komponen "serialport" yang terdapat pada toolbox kemudia tambahkan file picture pada picturebox.


Samakan semua fungsi dan even (source code) anda dengan di bawah ini ;

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 Serial_port_communication
{
    public partial class Form1 : Form
    {
        Image ON = Properties.Resources.LampuOn1;
        Image OFF = Properties.Resources.LampuOff;

        public Form1()
        {
            InitializeComponent();
            foreach (String PortKu in System.IO.Ports.SerialPort.GetPortNames())
            {
                comboBox1.Items.Add(PortKu);
            }
        }

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

                if (textBox2.Text.ToUpper() == "LAMPU 1 HIDUP" || textBox2.Text.ToUpper() == "HIDUP LAMPU 1")
                {
                    lampu1.Image = ON;
                }

                if (textBox2.Text.ToUpper() == "LAMPU 2 HIDUP" || textBox2.Text.ToUpper() == "HIDUP LAMPU 2")
                {
                    lampu2.Image = ON;
                }
                
                if (textBox2.Text.ToUpper() == "LAMPU 3 HIDUP" || textBox2.Text.ToUpper() == "HIDUP LAMPU 3")
                {
                    lampu3.Image = ON;
                }

                if (textBox2.Text.ToUpper() == "LAMPU 4 HIDUP" || textBox2.Text.ToUpper() == "HIDUP LAMPU 4")
                {
                    lampu4.Image = ON;
                }

                if (textBox2.Text.ToUpper() == "LAMPU 5 HIDUP" || textBox2.Text.ToUpper() == "HIDUP LAMPU 5")
                {
                    lampu5.Image = ON;
                }

                ///...........................................................................................


                if (textBox2.Text.ToUpper() == "LAMPU 1 MATI" || textBox2.Text.ToUpper() == "MATI LAMPU 1")
                {
                    lampu1.Image = OFF;
                }

                if (textBox2.Text.ToUpper() == "LAMPU 2 MATI" || textBox2.Text.ToUpper() == "MATI LAMPU 2")
                {
                    lampu2.Image = OFF;
                }

                if (textBox2.Text.ToUpper() == "LAMPU 3 MATI" || textBox2.Text.ToUpper() == "MATI LAMPU 3")
                {
                    lampu3.Image = OFF;
                }

                if (textBox2.Text.ToUpper() == "LAMPU 4 MATI" || textBox2.Text.ToUpper() == "MATI LAMPU 4")
                {
                    lampu4.Image = OFF;
                }

                if (textBox2.Text.ToUpper() == "LAMPU 5 MATI" || textBox2.Text.ToUpper() == "MATI LAMPU 5")
                {
                    lampu5.Image = OFF;
                }
            }

            else { }
        }

        private void button_Open_Click(object sender, EventArgs e)
        {
            try
            {
                serialPort1.PortName = comboBox1.Text;
                if (comboBox1.Text == "COM PORTS")
                {
                    
                }

                else if (!serialPort1.IsOpen)
                {
                    button_Open.BackColor = Color.Orange;
                    button_Open.Enabled = false;
                    button_Close.BackColor = Color.Gainsboro;
                    button_Close.Enabled = true;
                    serialPort1.Open();
                }
                else
                {

                }
            }
            catch (Exception)
            {
                MessageBox.Show("Pilih Com Port terlebeih dahulu", "Warning",MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }


        private void button_Close_Click(object sender, EventArgs e)
        {
            if (serialPort1.IsOpen)
            {
                button_Close.BackColor = Color.Red;
                button_Close.Enabled = false;
                button_Open.BackColor = Color.Gainsboro;
                button_Open.Enabled = true;
                serialPort1.Close();
            }

            else { }
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                if (serialPort1.IsOpen)
                {
                    serialPort1.Write(textBox1.Text + "   ");
                }

                else { }
            }
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "Kirim Data")
            {
                textBox1.Text = "";
            }

            else { }

            textBox1.ForeColor = Color.Black;
        }

        private void lampu4_Click(object sender, EventArgs e)
        {

        }
    }
}

Install Virtual Port terlebih dahulu, kemudian buatlah setidaknya satu pasang koneksi (COM1 dan COM2) dengan cara "add pair".


Lalu jalankan aplikasi HTERM seperti ini, lalu setting COM berbeda dengann COM yan terdapat pada visual lalu klik "connect"


Jalankan Program 


Contoh : Input melalui HTerm "Lampu 3 hidup"



Demikian tutorial singkat tentang Serial Port Communication di visual studio C#. Kurang lebihnya mohon maaf, Assalamu'alaikum WR.WB.

Minggu, 03 Mei 2015

Program Game TicTacToe ( C# Windows Application )

Hallo, jumpa lagi dengan saya Akhbar Ferdeansyah. Kali ini saya akan berbagi ilmu tentang cara membuat game TicTacToe di Microsoft Visual Studio.
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.


Gambar pada PictureBox bisa saling dicopy. Artinya gambar pada instance pictureBox1 dapat kita copy-kan ke instance pictureBox2. Di sini kita akan membuat permainan TicTacToe. Kita susun terlebih dahulu gambar kotak kosong sebanyak 3x3 sebagai papan permainan. Kemudian tambahkan gambar lingkaran dan gambar silang sebagai pemainnya. Gambar ini kita set properties Visible-nya menjadi false. Semua gambar kita letakkan di PictureBox.


Inti dari program ini adalah menunggu pemain untuk menempatkan pilihannya. Tiap PictureBox kita tambahkan event onClick. Apabila salah satu di-klik, akan dicek terlebih dahulu, apakah masih kosong atau tidak. Apabila kosong, maka pemain boleh memilih kotak tersebut, artinya kita copy-kan simbol silang ke kotak tersebut. Kemudian tinggal di-cek, apakah sudah ada yang berhasil membuat 3 segaris atau belum. Lalu giliran komputer. Komputer akan memilih secara acak kotak yang kosong, kemudian copy-kan simbol lingkaran ke kotak tersebut. Demikian terus bergantian antara pemain dan komputer sampai semuanya terisi. Tapi apabila ada yang berhasil membuat 3 segaris, maka dia dinyatakan sebagai pemenang.


Berikut ini adalah desain dari game tic tac toe yang saya buat :


Berikut ini adalah programnya :

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

namespace B
{
    public partial class Form1 : Form
    {
        public Form1()
        
        {
            InitializeComponent();
        }

        Image pictureX = Properties.Resources._1;
        Image pictureO = Properties.Resources._3;
        Image pctr; int count = 0;
        Boolean checkX = false, checkO = false, easy = false;
        int pict1 = 0, pict5 = 0, pict9 = 0,
            pict2 = 0, pict6 = 0,
            pict3 = 0, pict7 = 0,
            pict4 = 0, pict8 = 0;


    //fungsi reset 

    private void reset()
    {
        pict1 = pict2 = pict3 = pict4 = pict5 = pict6 = pict7 = pict8 = pict9 = count = 0; //mereset nilai int pict1-pict9
        easy = checkX = checkO = false;

        pictureBox1.BackgroundImage = pictureBox2.BackgroundImage =
        pictureBox3.BackgroundImage = pictureBox4.BackgroundImage =
        pictureBox5.BackgroundImage = pictureBox6.BackgroundImage =
        pictureBox7.BackgroundImage = pictureBox8.BackgroundImage =
        pictureBox9.BackgroundImage = null;


        List<PictureBox> pctList = new List<PictureBox>();
        foreach (PictureBox pct in this.Controls.OfType<PictureBox>())
        {
            pctList.Add(pct);
            pct.Enabled = true;
        }
    }



     //menonaktifkan atau disable semua picture box ketika hasi score tampil

    private void disablepicturebox()
    {
        List<PictureBox> pctList = new List<PictureBox>();
        foreach (PictureBox pct in this.Controls.OfType<PictureBox>())
        {
            pctList.Add(pct);
            pct.Enabled = false;
        }
    }

     //menonaktifkan atau disable picture box yang sudah diclick

    private void wasclickedbefore(int pic)
    {
        switch (pic)
    {
        case 1: { if (pict1 == 1) pict1++; break; }
        case 2: { if (pict2 == 1) pict2++; break; }
        case 3: { if (pict3 == 1) pict3++; break; }
        case 4: { if (pict4 == 1) pict4++; break; }
        case 5: { if (pict5 == 1) pict5++; break; }
        case 6: { if (pict6 == 1) pict6++; break; }
        case 7: { if (pict7 == 1) pict7++; break; }
        case 8: { if (pict8 == 1) pict8++; break; }
        case 9: { if (pict9 == 1) pict9++; break; }
        default: break;
    }

    if (easyToolStripMenuItem.Checked == true)
    easycomputer_play(pic);
    else if (normalToolStripMenuItem.Checked == true && easy==false)
    normalcomputer_play(pic);
    else if (normalToolStripMenuItem.Checked == true && easy == true)
    easycomputer_play(pic);
//else if (hardToolStripMenuItem.Checked == true && easy == false)
//    normalcomputer_play(pic);
//else if (hardToolStripMenuItem.Checked == true && easy == true)
//    easycomputer_play(pic);
    }

    //logika fungsi easy mode

    private void easycomputer_play(int pic)
    {
        if (pict1 == 0) { pictureBox1.BackgroundImage = pictureO; pict1 = 2; }
        else if (pict2 == 0) { pictureBox2.BackgroundImage = pictureO; pict2 = 2; }
        else if (pict3 == 0) { pictureBox3.BackgroundImage = pictureO; pict3 = 2; }
        else if (pict4 == 0) { pictureBox4.BackgroundImage = pictureO; pict4 = 2; }
        else if (pict5 == 0) { pictureBox5.BackgroundImage = pictureO; pict5 = 2; }
        else if (pict6 == 0) { pictureBox6.BackgroundImage = pictureO; pict6 = 2; }
        else if (pict7 == 0) { pictureBox7.BackgroundImage = pictureO; pict7 = 2; }
        else if (pict8 == 0) { pictureBox8.BackgroundImage = pictureO; pict8 = 2; }
        else if (pict9 == 0) { pictureBox9.BackgroundImage = pictureO; pict9 = 2; }
    }

    //logika fungsi normal mode

    private void normalcomputer_play(int pic)
    {
        if ((pict1==2 || pict2==2 || pict3==2 || pict4==2 || pict6==2 || pict7==2 || pict8==2 || pict9==2) &&
        pict5 == 0) { pictureBox5.BackgroundImage = pictureO; pict5 = 2; }

        else if (pict5 == 2 && (pict1 == 0 || pict3 == 0 || pict7 == 0 || pict9 == 0))
    { 
        Random Rnd = new Random();
        int rnd = Rnd.Next(1,5);
        switch (rnd)
        {
        case 1: { if (pict1 == 0) pictureBox1.BackgroundImage = pictureO; pict1 = 2; break; }
        case 2: { if (pict3 == 0) pictureBox3.BackgroundImage = pictureO; pict3 = 2; break; }
        case 3: { if (pict7 == 0) pictureBox7.BackgroundImage = pictureO; pict7 = 2; break; }
        case 4: { if (pict9 == 0) pictureBox9.BackgroundImage = pictureO; pict9 = 2; break; }
        default: break;
    }
        easy = true;
    }
    }


    //logika fungsi hard mode

    private void hardcomputer_play(int pic)
    {
        if ((pict1 == 4 || pict2 == 2 || pict3 == 4 || pict4 == 2 || pict6 == 4 || pict7 == 2 || pict8 == 4 || pict9 == 2) &&
        pict5 == 0) { pictureBox5.BackgroundImage = pictureO; pict5 = 2; }

        else if (pict5 == 2 && (pict1 == 0 || pict3 == 0 || pict7 == 0 || pict9 == 0))
        {
            Random Rnd = new Random();
            int rnd = Rnd.Next(1, 5);
            switch (rnd)
            {
                case 1: { if (pict1 == 0) pictureBox1.BackgroundImage = pictureO; pict1 = 2; break; }
                case 2: { if (pict3 == 0) pictureBox3.BackgroundImage = pictureO; pict3 = 2; break; }
                case 3: { if (pict7 == 0) pictureBox7.BackgroundImage = pictureO; pict7 = 2; break; }
                case 4: { if (pict9 == 0) pictureBox9.BackgroundImage = pictureO; pict9 = 2; break; }
                default: break;
            }
            easy = true;
        }
    }

    //fungsi memeriksa ketika User (X) menang

    private void result()
    { 
        if (pictureBox1.BackgroundImage == pictureX && 
        pictureBox2.BackgroundImage == pictureX && 
        pictureBox3.BackgroundImage == pictureX)
    {
        MessageBox.Show("Player X has WON !!!", "Congratulation"); checkX = true;
    }

        else if (pictureBox4.BackgroundImage == pictureX &&
        pictureBox5.BackgroundImage == pictureX &&
        pictureBox6.BackgroundImage == pictureX)
    {
        MessageBox.Show("Player X has WON !!!", "Congratulation"); checkX = true;
    }

        else if (pictureBox7.BackgroundImage == pictureX &&
        pictureBox8.BackgroundImage == pictureX &&
        pictureBox9.BackgroundImage == pictureX)
    {
        MessageBox.Show("Player X has WON !!!", "Congratulation"); checkX = true;
    }

        else if (pictureBox1.BackgroundImage == pictureX &&
        pictureBox4.BackgroundImage == pictureX &&
        pictureBox7.BackgroundImage == pictureX)
    {
        MessageBox.Show("Player X has WON !!!", "Congratulation"); checkX = true;
    }

        else if (pictureBox2.BackgroundImage == pictureX &&
        pictureBox5.BackgroundImage == pictureX &&
        pictureBox8.BackgroundImage == pictureX)
    {
        MessageBox.Show("Player X has WON !!!", "Congratulation"); checkX = true;
    }

        else if (pictureBox3.BackgroundImage == pictureX &&
        pictureBox6.BackgroundImage == pictureX &&
        pictureBox9.BackgroundImage == pictureX)
    {
        MessageBox.Show("Player X has WON !!!", "Congratulation"); checkX = true;
    }

        else if (pictureBox3.BackgroundImage == pictureX &&
        pictureBox5.BackgroundImage == pictureX &&
        pictureBox7.BackgroundImage == pictureX)
    {
        MessageBox.Show("Player X has WON !!!", "Congratulation"); checkX = true;
    }

        else if (pictureBox1.BackgroundImage == pictureX &&
        pictureBox5.BackgroundImage == pictureX &&
        pictureBox9.BackgroundImage == pictureX)
    {
        MessageBox.Show("Player X has WON !!!", "Congratulation"); checkX = true;
    }
        resultO();

    }

    //fungsi memeriksa ketika computer (O) menang

    private void resultO()
    {
        if (pictureBox1.BackgroundImage == pictureO &&
        pictureBox2.BackgroundImage == pictureO &&
        pictureBox3.BackgroundImage == pictureO)
    {
        MessageBox.Show("Player O has WON !!!", "Congratulation"); checkO = true;
    }

        else if (pictureBox4.BackgroundImage == pictureO &&
        pictureBox5.BackgroundImage == pictureO &&
        pictureBox6.BackgroundImage == pictureO)
    {
        MessageBox.Show("Player O has WON !!!", "Congratulation"); checkO = true;
    }

        else if (pictureBox7.BackgroundImage == pictureO &&
        pictureBox8.BackgroundImage == pictureO &&
        pictureBox9.BackgroundImage == pictureO)
    {
        MessageBox.Show("Player O has WON !!!", "Congratulation"); checkO = true;
    }

        else if (pictureBox1.BackgroundImage == pictureO &&
        pictureBox4.BackgroundImage == pictureO &&
        pictureBox7.BackgroundImage == pictureO)
    {
        MessageBox.Show("Player O has WON !!!", "Congratulation"); checkO = true;
    }

        else if (pictureBox2.BackgroundImage == pictureO &&
        pictureBox5.BackgroundImage == pictureO &&
        pictureBox8.BackgroundImage == pictureO)
    {
        MessageBox.Show("Player O has WON !!!", "Congratulation"); checkO = true;
    }

        else if (pictureBox3.BackgroundImage == pictureO &&
        pictureBox6.BackgroundImage == pictureO &&
        pictureBox9.BackgroundImage == pictureO)
    {
        MessageBox.Show("Player O has WON !!!", "Congratulation"); checkO = true;
    }

        else if (pictureBox3.BackgroundImage == pictureO &&
        pictureBox5.BackgroundImage == pictureO &&
        pictureBox7.BackgroundImage == pictureO)
    {
        MessageBox.Show("Player  O has WON !!!", "Congratulation"); checkO = true;
    }

        else if (pictureBox1.BackgroundImage == pictureO &&
        pictureBox5.BackgroundImage == pictureO &&
        pictureBox9.BackgroundImage == pictureO)
    {
        MessageBox.Show("Player O has WON !!!", "Congratulation"); checkO = true;
    }

        checkdraw(); 
    }

    //fungsi memeriksa ketika game seri

    private void checkdraw()
    {
        if (pictureBox1.BackgroundImage !=null && pictureBox2.BackgroundImage !=null &&
        pictureBox3.BackgroundImage !=null && pictureBox4.BackgroundImage !=null &&
        pictureBox5.BackgroundImage !=null && pictureBox6.BackgroundImage !=null &&
        pictureBox7.BackgroundImage !=null && pictureBox8.BackgroundImage !=null &&
        pictureBox9.BackgroundImage !=null ) 
        MessageBox.Show("Game is DRAW !!!", "Congratulation"); 
    }

    //update hasil score

    private void score()
    {
        if (checkX)
    {
        disablepicturebox();
        XScore.Text = (int.Parse(XScore.Text) + 1).ToString();
    }
        else if (checkO)
    {
        disablepicturebox();
        OScore.Text = (int.Parse(OScore.Text) + 1).ToString();
    }
    }

    private void pictureBox1_Click(object sender, EventArgs e)
    {
     //MessageBox.Show("Select level before play game", "Warning !!!", MessageBoxButtons.OK);
        PictureBox piCTure = (PictureBox)sender; 
        int pic = Convert.ToInt32((piCTure.Name).Substring(10,1));

    count++;
    // switch kondisi masing-masing picture box
    switch (pic)
    {
    case 1:
    { if (pict1 == 0) { pict1++; pictureBox1.BackgroundImage = pctr; }
    else if (pict1 == 2) { pictureBox1.Enabled = false; } break; }

    case 2:
    { if (pict2 == 0) { pict2++; pictureBox2.BackgroundImage = pctr; }
    else if (pict2 == 2) { pictureBox2.Enabled = false; } break; }

    case 3:
    { if (pict3 == 0) { pict3++; pictureBox3.BackgroundImage = pctr; }
    else if (pict3 == 2) { pictureBox3.Enabled = false; } break; }

    case 4:
    { if (pict4 == 0) { pict4++; pictureBox4.BackgroundImage = pctr; }
    else if (pict4 == 2) { pictureBox4.Enabled = false; } break; }

    case 5:
    { if (pict5 == 0) { pict5++; pictureBox5.BackgroundImage = pctr; }
    else if (pict5 == 2) { pictureBox5.Enabled = false; } break; }

    case 6:
    { if (pict6 == 0) { pict6++; pictureBox6.BackgroundImage = pctr; }
    else if (pict6 == 2) { pictureBox6.Enabled = false; } break; }

    case 7:
    { if (pict7 == 0) { pict7++; pictureBox7.BackgroundImage = pctr; }
    else if (pict7 == 2) { pictureBox7.Enabled = false; } break; }

    case 8:
    { if (pict8 == 0) { pict1++; pictureBox8.BackgroundImage = pctr; }
  (pict9 == 0) { pict9++; pictureBox9.BackgroundImage = pctr; }
    else if (pict9 == 2) { pictureBox9.Enabled = false; } break; }

    default: break;
    }

    result(); score();
    if (easyToolStripMenuItem.Checked == true || normalToolStripMenuItem.Checked == true)
    {
    pctr  else if (pict8 == 2) { pictureBox8.Enabled = false; } break; }

    case 9:
    { if = pictureX;
    if (checkX == false && checkO == false) wasclickedbefore(pic);
    }

    else if (multiPlayerToolStripMenuItem.Checked == true)
    {
    if (count % 2 == 0)
    pctr = pictureX;
    else
    pctr = pictureO;
    }
    }

        private void play_again_Click(object sender, EventArgs e)
        {
            reset();
        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            reset(); XScore.Text = OScore.Text = "0";
        
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult result = MessageBox.Show("Do you really want to exit ?", "Warning !!!", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes) this.Close();
        
        }

        private void easyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            normalToolStripMenuItem.Checked = false; easyToolStripMenuItem.Checked = true;
            levelToolStripMenuItem.Checked = true; reset(); multiPlayerToolStripMenuItem.Checked = false;
            XScore.Text = OScore.Text = "0";
            MessageBox.Show(" \rMode Selected Player (VS) CPU \r\n\tEasy Level ", "tic tac toe");
        
        }

        private void normalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false; normalToolStripMenuItem.Checked = true;
            levelToolStripMenuItem.Checked = true; multiPlayerToolStripMenuItem.Checked = false;
            reset(); XScore.Text = OScore.Text = "0";
            MessageBox.Show(" \rMode Selected Player (VS) CPU \r\n\tNormal Level ", "tic tac toe");
        
        }

        private void multiPlayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            normalToolStripMenuItem.Checked = false; easyToolStripMenuItem.Checked = false;
            levelToolStripMenuItem.Checked = false; multiPlayerToolStripMenuItem.Checked = true;
            reset(); XScore.Text = OScore.Text = "0";
            MessageBox.Show(" \rMode Selected MultiPlayer ", "tic tac toe");
        
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
     
        }     
    }

Untuk lebih jelasnya lihat video berikut :