Minggu, 03 Mei 2015

MEMBUAT GAME TIC TAC TOE DI SHARPDEVELOP C#

Haii semuanyaaa !!! :)

Kali ini saya akan memberikan salah satu tutorial penjelasan pemrograman bahasa C.
Berikut merupakan tutorial singkat Pemrograman Bahasa C membuat game TIC TAC TOE dengan Sharpdevelop.
langsung saja di coba yaa...!!

Cara Membuat Permainan Tic Tac Toe dengan menggunakan Sharpdevelop:


1. Jalankan SharpDevelop.


2. Buat project baru dengan cara meng-klik tombol "New Solution".


3. Pilih untuk membuat "Windows Application" , beri nama project anda dan klik "Create".


4. Akan muncul dinding baru untuk kita memulai membuat aplikasinya. Pilih tab bertuliskan “Design” untuk membuat desain aplikasi yang diinginkan.


5. Maka akan muncul form template aplikasi windowsnya, desain sesuai keinginan anda, ini contoh desain saya. Praktek kali ini menggunakan 1 button, 9 picturebox, dan 4 label.



6. Buatlah source code untuk desain yang telah anda buat. Pilih tab bertuliskan “Source”.




*MY SOURCE CODE*




using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace game_tictactoe_devi
{
  
    public partial class MainForm : Form
    {
      
        Image pictureX = game_tictactoe_devi.Resource1.tic1;
        Image pictureO = game_tictactoe_devi.Resource1.tic2;
        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;
      
        public MainForm()
        {
          
            InitializeComponent();
          
        }
      
        private void disablepicturebox()
        {
            List<PictureBox> pctList = new List<PictureBox>();
          
        }
      
        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 (playercpuToolStripMenuItem.Checked == true)
                    easycomputer_play(pic);
                else if (hardToolStripMenuItem.Checked == true && easy==false)
                    hardcomputer_play(pic);
                else if (hardToolStripMenuItem.Checked == true && easy == true)
                    easycomputer_play(pic);
        }
      
        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; }
        }
      
        private void hardcomputer_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;
            }
        }
      
        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;
            }
          
            resultO();
        }
      
        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 (You) has WON !!!", "Congratulation"); checkO = true;
            }
            checkdraw();
        }
      
        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");
        }

        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 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 || hardToolStripMenuItem.Checked == true)
            {
                pctr = pictureX;
                if (checkX == false && checkO == false) wasclickedbefore(pic);
            }
            else if (player2ToolStripMenuItem.Checked==true)
            {
            if (count % 2 == 0)
            pctr = pictureX;
            else
            pctr = pictureO;
            }
        }

        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>();
          
        }

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

        void easy_click(object sender, EventArgs e)
        {
            hardToolStripMenuItem.Checked = false; easyToolStripMenuItem.Checked = true;
            playercpuToolStripMenuItem.Checked = true; reset(); player2ToolStripMenuItem.Checked = false;
            XScore.Text = OScore.Text = "0";
            MessageBox.Show(" \rMode Selected Player (VS) CPU \r\n\tEasy Level ", "Created by: Devi Tinambunan");
        }

        void hard_click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false; hardToolStripMenuItem.Checked = true;
            playercpuToolStripMenuItem.Checked = true; player2ToolStripMenuItem.Checked = false;
            reset(); XScore.Text = OScore.Text = "0";
            MessageBox.Show(" \rMode Selected Player (VS) CPU \r\n\tHard Level ", "Created by: Devi Tinambunan");

        }

        void player2_click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false; hardToolStripMenuItem.Checked = false;
            playercpuToolStripMenuItem.Checked = false; player2ToolStripMenuItem.Checked = true;
            reset(); XScore.Text = OScore.Text = "0";
            MessageBox.Show(" \rMode Selected Player vs Player ", "Created by: Devi Tinambunan");
        }

        void about_click(object sender, EventArgs e)
        {
            MessageBox.Show("Created by: Devi Tinambunan \n\n Powered by :www.devigusn.blogspot.com", "Warning");
         }
    }
}

7. Jalankan program yang telah anda buat dengan menekan tombol "Run".


inilah hasil dari program yang telah saya buat :



Okayy sekian penjelasannya cara membuat game tic tac toe, maaf ya kalo banyak yang salah :))
Untuk tutorialnya dapat dilihat di: https://www.youtube.com/watch?v=778rMCb6-3Q
Terima kasihh {}

Tidak ada komentar:

Posting Komentar