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 :





Tidak ada komentar:

Posting Komentar