Hello ENGINEER, Welcome to Electrical's Corner

Mei 03, 2015

Tutorial Membuat Game Tic Tac Toe Sederhana C#

Assalamualaikum Wr. Wb

hai temen-temen..udah lama nih gak posting lagi. dan sekarang laras ada tugas lagi nih tentang game yeeeey :D
kali ini laras mau membuat game tic tac toe sederhana guys!! pada tau kan tic tac toe?? itu loh permainan waktu di SD dan SMP yang namanya SOS, kadang-kadang juga ada di SMA/K (kalau lagi bosen aja) hihihi
game ini asik loh guys! ada player1 dan player 2 guys, kalau dulu kan seringnya mainnya di kertas yakan nah sekarang laras buat di komputer nih :D

program ini memakai banyak kemungkinan, jadi laras memakai beberapa case disini.
begini langkah-langkahnya :
1. Buka new project di Visual Studio 2012, jangan lupa diberi nama dan pilih windows application
2. Pilih Label, Button, Picture Box dan Menu Script di toolbox kemudian susun sesuai keinginan anda.



untuk memakai menu strip kita harus mengubah event click semuanya, mulai dari file sampai help. seperti dibawah ini. 


3. Kemudian tambahkan gambar X dan O untuk permainannya di resource. seperti ini :

4. Masukkan Codingannya, pertama-tama inisialisasi dahulu variabel globalnya.
        //Inisialisasi Variabel Global
        Image pictureX = Properties.Resources.Capture; // lokasi gambar X
        Image pictureO = Properties.Resources.fg; // lokasi gambar O
        Image pctr; int count = 0;
        Boolean checkX = false, checkO = false, easy = false;
        public int pict1 = 0, pict5 = 0, pict9 = 0,
        pict2 = 0, pict6 = 0,
        pict3 = 0, pict7 = 0,
        pict4 = 0, pict8 = 0;

5. Buat fungsi reset untuk mereset aplikasinya.
   private void reset()
        {
            pict1 = pict2 = pict3 = pict4 = pict5 = pict6 = pict7 = pict8 = pict9 = count = 0; //mereset nilai int pict1-pict9
            easy = checkX = checkO = false;
            // mengembalikan ke gambar awal picture box / kosong
            pictureBox1.BackgroundImage = pictureBox2.BackgroundImage =
            pictureBox3.BackgroundImage = pictureBox4.BackgroundImage =
            pictureBox5.BackgroundImage = pictureBox6.BackgroundImage =
            pictureBox7.BackgroundImage = pictureBox8.BackgroundImage =
            pictureBox9.BackgroundImage = null;
            label1.Text = label2.Text = "0";
            // enable semua picture box
            List<PictureBox> pctlist = new List<PictureBox>();
            foreach (PictureBox pct in this.Controls.OfType<PictureBox>())
            {
                pctlist.Add(pct);
                pct.Enabled = true;
            }
        }

6. Nonaktifkan atau disable semua picture box ketika hasil 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;
            }
        }

7. Nonaktifkan atau disable picture box yang sudah diclick agar player yang lain tidak dapat menggunakan picture box yang sudah dipakai.
        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);
        }

8. Jika memilih level easy seperti ini logika fungsi easy modenya
        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; }
        }

9. dan juga 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;
            }
        }

10. Kemungkinan Player 1 atau X menang, beginilah fungsi memeriksa ketika User (X) menang:
        private void result()
        {
            if (pictureBox1.BackgroundImage == pictureX &&
            pictureBox2.BackgroundImage == pictureX &&
            pictureBox3.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }
            else if (pictureBox4.BackgroundImage == pictureX &&
            pictureBox5.BackgroundImage == pictureX &&
            pictureBox6.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }
            else if (pictureBox7.BackgroundImage == pictureX &&
            pictureBox8.BackgroundImage == pictureX &&
            pictureBox9.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }
            else if (pictureBox1.BackgroundImage == pictureX &&
            pictureBox4.BackgroundImage == pictureX &&
            pictureBox7.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }
            else if (pictureBox2.BackgroundImage == pictureX &&
            pictureBox5.BackgroundImage == pictureX &&
            pictureBox8.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }
            else if (pictureBox3.BackgroundImage == pictureX &&
            pictureBox6.BackgroundImage == pictureX &&
            pictureBox9.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }
            else if (pictureBox3.BackgroundImage == pictureX &&
            pictureBox5.BackgroundImage == pictureX &&
            pictureBox7.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }
            else if (pictureBox1.BackgroundImage == pictureX &&
            pictureBox5.BackgroundImage == pictureX &&
            pictureBox9.BackgroundImage == pictureX)
            {
                MessageBox.Show("X (You) has WON !!!", "Congratulation"); checkX = true;
            }
            else resultO();
        }

11. Begitu juga dengan fungsi memeriksa ketika computer (O) menang
        private void resultO()
        {
            if (pictureBox1.BackgroundImage == pictureO &&
            pictureBox2.BackgroundImage == pictureO &&
            pictureBox3.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO =
                true;
            }
            else if (pictureBox4.BackgroundImage == pictureO &&
            pictureBox5.BackgroundImage == pictureO &&
            pictureBox6.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO =
                true;
            }
            else if (pictureBox7.BackgroundImage == pictureO &&
            pictureBox8.BackgroundImage == pictureO &&
            pictureBox9.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO =
                true;
            }
            else if (pictureBox1.BackgroundImage == pictureO &&
            pictureBox4.BackgroundImage == pictureO &&
            pictureBox7.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO =
                true;
            }
            else if (pictureBox2.BackgroundImage == pictureO &&
            pictureBox5.BackgroundImage == pictureO &&
            pictureBox8.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO =
                true;
            }
            else if (pictureBox3.BackgroundImage == pictureO &&
            pictureBox6.BackgroundImage == pictureO &&
            pictureBox9.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO =
                true;
            }
            else if (pictureBox3.BackgroundImage == pictureO &&
            pictureBox5.BackgroundImage == pictureO &&
            pictureBox7.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO =
                true;
            }
            else if (pictureBox1.BackgroundImage == pictureO &&
            pictureBox5.BackgroundImage == pictureO &&
            pictureBox9.BackgroundImage == pictureO)
            {
                MessageBox.Show("O (Computer) has WON !!!", "Congratulation"); checkO =
                true;
            }
            checkdraw();
        }

12. Kemungkinan ketika game seri. beginilah 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");
        }

13. Buat fungsi score untuk menampilkan score pada label 1 dan 2:
    private void score()
        {
            if (checkX)
            {
                disablepicturebox();
                label1.Text = (int.Parse(label1.Text) + 1).ToString();
            }
            else if (checkO)
            {
                disablepicturebox();
                label2.Text = (int.Parse(label2.Text) + 1).ToString();
            }
        }

14. Inilah inti permainannya, event ketika picture box diklik.
        private void Picture_Click(object sender, EventArgs e)
        {
            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; }
                        else if (pict8 == 2) { pictureBox8.Enabled = false; } break;
                    }
                case 9:
                    {
                        if (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 = pictureX;
                if (checkX == false && checkO == false) wasclickedbefore(pic);
            }
         
        }

15. Ketika user memilih new game pada menu strip, begini programnya:
   private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            reset(); label1.Text = label2.Text = "0";
        }

16. Exit Mode
// exit mode
        private void exit()
        {
            DialogResult result = MessageBox.Show("Yakin Mau Keluar???", "Peringatan!", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes) this.Close();
        }
        private void Exit_Click(object sender, EventArgs e)
        {
            exit();
        }
        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            exit();
        }

17. Fungsi ketika user memilih level normal :
    private void normalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false; normalToolStripMenuItem.Checked = true;
            reset(); label1.Text = label2.Text = "0";
            MessageBox.Show(" \rMode Selected Player (VS) CPU \r\n\tNormal Level ","Created by: Laras Hanisaputri");
        }

18. Fungsi ketika user memilih level easy :
private void easyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            normalToolStripMenuItem.Checked = false; easyToolStripMenuItem.Checked =true;
            reset();
            label1.Text = label2.Text = "0";
            MessageBox.Show(" \rMode Selected Player (VS) CPU \r\n\tEasy Level ","Created by: Laras Hanisaputri");
        }

19. Fungsi Ketika user memilih mode multi player:
private void multiPlayerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            normalToolStripMenuItem.Checked = false; easyToolStripMenuItem.Checked = false;
            reset(); label1.Text = label2.Text = "0";
            MessageBox.Show(" \rMode Selected MultiPlayer ", "Created by: Laras Hanisaputri");
        }

20. Menu strip help > about:
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
                MessageBox.Show("Created by: Laras Hanisaputri \n\n Powered by : larashanisaa.blogspot.com", "Warning");
        }

21. Button reset dan play again
private void button1_Click(object sender, EventArgs e)
        {
            reset();
        }

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

22. Jika tidak ada error, jalankan programnya seperti ini:




jika masih ada yang belum mengerti dapat mengunjungu youtube laras :

Wassalamualaikum Wr. Wb

Tidak ada komentar:

Posting Komentar