Assalammualaikum Wr.Wb
Welcome to my blog……
Kembali lagi dengan
saya Gobar di Catatan Mungil. Pada kesempatan kali ini saya ingin
memberikan sedikit tutorial mengenai pembuatan sebuah aplikasi Game
Tic-Tac-Toe menggunakan bahasa pemrograman C#.
Pada program game Tic-Tac-Toe saya kali
ini, saya menggunakan 4 buah windows Form, yaitu :
> Form_Loading : Loading Tampilan Awal
> Form_Play : Untuk Game Running
> Form_Option1 : Setting saat bermain one
player
> Form_Option2 : Setting saat bermain two
player
Berikut ini adalah tampilan awal dari
program saya saat loading awal (Form_Loading) :
<div class="hovergallery">
</div>
Ini
adalah bagian menu utama (Form_Play) :
Ini
adalah bagian menu Option (Form_Play) :
Tampilan
saat memilih “One Player” (Form_Play), maka akan tampil Form_Option1 seperti
gambar dibawah :
Tampilan
saat memilih “Two Player” (Form_Play), maka akan tampil Form_Option2 seperti
gambar dibawah :
Coding
Program saya seperti dibawah ini :
Form_Loading :
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 Tic_Tac_Toe
{
public partial class Form_Loading : Form
{
public Form_Loading()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBar1.Value <= 100)
{
progressBar1.Value += 1;
}
if (progressBar1.Value == 14)
{
B2.Visible = true;
}
if (progressBar1.Value == 28)
{
B1.Visible = true;
}
if (progressBar1.Value == 42)
{
A3.Visible = true;
}
if (progressBar1.Value == 56)
{
C1.Visible = true;
}
if (progressBar1.Value == 70)
{
A1.Visible = true;
}
if (progressBar1.Value == 84)
{
A2.Visible = true;
}
if (progressBar1.Value == 96)
{
C3.Visible = true;
}
if (progressBar1.Value == 100)
{
timer1.Stop(); //Menonaktifkan timer1
Form_Play Gobar = new Form_Play(); //Memanggil Form2
Gobar.Show(); //Menampilkan ilkan objek baru
this.Hide(); //Menyembunyikan foam2
}
}
}
}
Form_Option1 :
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 Tic_Tac_Toe
{
public partial class Form_Option1 : Form
{
public Form_Option1()
{
InitializeComponent();
}
private void label_Start_Click_1(object sender, EventArgs e)
{
if (textBox_name1.Text == "")
{
MessageBox.Show("Lengkapi Nama Anda Terlebih Dahulu !!!", "My System");
}
else
{
Form_Play.SetNamaPemain(textBox_name1.Text, textBox_name2.Text); //mengeset nama ke Form1 dari textBox_name
this.Close();
}
}
private void label_Start_MouseMove(object sender, MouseEventArgs e)
{
label_Start.ForeColor = Color.Maroon;
}
private void label_Start_MouseLeave(object sender, EventArgs e)
{
label_Start.ForeColor = Color.Black;
}
}
}
Form_Option2 :
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 Tic_Tac_Toe
{
public partial class Form_Option2 : Form
{
public Form_Option2()
{
InitializeComponent();
}
private void label_Start_Click(object sender, EventArgs e)
{
if (textBox_name1.Text == "" || textBox_name2.Text == "")
{
MessageBox.Show("Lengkapi Nama Player Terlebih Dahulu !!!", "My System");
}
else
{
Form_Play.SetNamaPemain(textBox_name1.Text, textBox_name2.Text); //mengeset nama ke Form1 dari textBox_name
this.Close();
}
}
private void label_Start_MouseMove(object sender, MouseEventArgs e)
{
label_Start.ForeColor = Color.Maroon;
}
private void label_Start_MouseLeave(object sender, EventArgs e)
{
label_Start.ForeColor = Color.Black;
}
}
}
Form_Play :
//Keterangan :
//pictureBox1 = A1
//pictureBox2 = A2
//pictureBox3 = A3
//pictureBox4 = B1
//pictureBox5 = B2
//pictureBox6 = B3
//pictureBox7 = C1
//pictureBox8 = C2
//pictureBox9 = C3
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 Tic_Tac_Toe
{
public partial class Form_Play : Form
{
bool Giliran = true; //Jika true = Giliran X ; jika false = Giliran O
bool Computer = false; //untuk Computer
int Jumlah_Giliran = 0;
Image PictureX = Properties.Resources.Pict_X;
Image PictureO = Properties.Resources.Pict_O;
static String player1, player2; //deklarasi untuk Form_Option
public Form_Play()
{
InitializeComponent();
A1.Image = A2.Image = A3.Image = B1.Image = B2.Image = B3.Image = C1.Image = C2.Image = C3.Image = null;
ShowMenuUtama();
}
public static void SetNamaPemain(String nama1, String nama2) //menginputkan kesini, dari Form_Option tadi
{
player1 = nama1;
player2 = nama2;
}
private void Langkah_Random()
{
Random angka = new Random();
int langkah = angka.Next(1, 9);
switch (langkah)
{
case 1: if (A1.Image == null) { A1.Image = PictureO; A1.Enabled = false; } else { Langkah_Random(); } break;
case 2: if (A2.Image == null) { A2.Image = PictureO; A2.Enabled = false; } else { Langkah_Random(); } break;
case 3: if (A3.Image == null) { A3.Image = PictureO; A3.Enabled = false; } else { Langkah_Random(); } break;
case 4: if (B1.Image == null) { B1.Image = PictureO; B1.Enabled = false; } else { Langkah_Random(); } break;
case 5: if (B2.Image == null) { B2.Image = PictureO; B2.Enabled = false; } else { Langkah_Random(); } break;
case 6: if (B3.Image == null) { B3.Image = PictureO; B3.Enabled = false; } else { Langkah_Random(); } break;
case 7: if (C1.Image == null) { C1.Image = PictureO; C1.Enabled = false; } else { Langkah_Random(); } break;
case 8: if (C2.Image == null) { C2.Image = PictureO; C2.Enabled = false; } else { Langkah_Random(); } break;
case 9: if (C3.Image == null) { C3.Image = PictureO; C3.Enabled = false; } else { Langkah_Random(); } break;
}
}
private void pilihan_terbaik() //mode Hard
{
//Metode melihat peluang Menang
//Horizontal
if ((A1.Image == PictureO) && (A2.Image == PictureO) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
else if ((A1.Image == PictureO) && (A3.Image == PictureO) && (A2.Image == null)) { A2.Image = PictureO; A2.Enabled = false; }
else if ((A2.Image == PictureO) && (A3.Image == PictureO) && (A1.Image == null)) { A1.Image = PictureO; A1.Enabled = false; }
else if ((B1.Image == PictureO) && (B2.Image == PictureO) && (B3.Image == null)) { B3.Image = PictureO; B3.Enabled = false; }
else if ((B1.Image == PictureO) && (B3.Image == PictureO) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureO) && (B3.Image == PictureO) && (B1.Image == null)) { B1.Image = PictureO; B1.Enabled = false; }
else if ((C1.Image == PictureO) && (C2.Image == PictureO) && (C3.Image == null)) { C3.Image = PictureO; C3.Enabled = false; }
else if ((C1.Image == PictureO) && (C3.Image == PictureO) && (C2.Image == null)) { C2.Image = PictureO; C2.Enabled = false; }
else if ((C2.Image == PictureO) && (C3.Image == PictureO) && (C1.Image == null)) { C1.Image = PictureO; C1.Enabled = false; }
//Vertikal
else if ((A1.Image == PictureO) && (B1.Image == PictureO) && (C1.Image == null)) { C1.Image = PictureO; C1.Enabled = false; }
else if ((A1.Image == PictureO) && (C1.Image == PictureO) && (B1.Image == null)) { B1.Image = PictureO; B1.Enabled = false; }
else if ((B1.Image == PictureO) && (C1.Image == PictureO) && (A1.Image == null)) { A1.Image = PictureO; }
else if ((A2.Image == PictureO) && (B2.Image == PictureO) && (C2.Image == null)) { C2.Image = PictureO; C2.Enabled = false; }
else if ((A2.Image == PictureO) && (C2.Image == PictureO) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureO) && (C2.Image == PictureO) && (A2.Image == null)) { A2.Image = PictureO; A2.Enabled = false; }
else if ((A3.Image == PictureO) && (B3.Image == PictureO) && (C3.Image == null)) { C3.Image = PictureO; C3.Enabled = false; }
else if ((A3.Image == PictureO) && (C3.Image == PictureO) && (B3.Image == null)) { B3.Image = PictureO; B3.Enabled = false; }
else if ((B3.Image == PictureO) && (C3.Image == PictureO) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
//Diagonal
else if ((A1.Image == PictureO) && (B2.Image == PictureO) && (C3.Image == null)) { C3.Image = PictureO; C3.Enabled = false; }
else if ((A1.Image == PictureO) && (C3.Image == PictureO) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureO) && (C3.Image == PictureO) && (A1.Image == null)) { A1.Image = PictureO; A1.Enabled = false; }
else if ((A3.Image == PictureO) && (B2.Image == PictureO) && (C1.Image == null)) { C1.Image = PictureO; C1.Enabled = false; }
else if ((A3.Image == PictureO) && (C1.Image == PictureO) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureO) && (C1.Image == PictureO) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
//Metode Block lawan
//Horizontal
else if ((A1.Image == PictureX) && (A2.Image == PictureX) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
else if ((A1.Image == PictureX) && (A3.Image == PictureX) && (A2.Image == null)) { A2.Image = PictureO; A2.Enabled = false; }
else if ((A2.Image == PictureX) && (A3.Image == PictureX) && (A1.Image == null)) { A1.Image = PictureO; A1.Enabled = false; }
else if ((B1.Image == PictureX) && (B2.Image == PictureX) && (B3.Image == null)) { B3.Image = PictureO; B3.Enabled = false; }
else if ((B1.Image == PictureX) && (B3.Image == PictureX) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureX) && (B3.Image == PictureX) && (B1.Image == null)) { B1.Image = PictureO; B1.Enabled = false; }
else if ((C1.Image == PictureX) && (C2.Image == PictureX) && (C3.Image == null)) { C3.Image = PictureO; C3.Enabled = false; }
else if ((C1.Image == PictureX) && (C3.Image == PictureX) && (C2.Image == null)) { C2.Image = PictureO; C2.Enabled = false; }
else if ((C2.Image == PictureX) && (C3.Image == PictureX) && (C1.Image == null)) { C1.Image = PictureO; C1.Enabled = false; }
//Vertikal
else if ((A1.Image == PictureX) && (B1.Image == PictureX) && (C1.Image == null)) { C1.Image = PictureO; C1.Enabled = false; }
else if ((A1.Image == PictureX) && (C1.Image == PictureX) && (B1.Image == null)) { B1.Image = PictureO; B1.Enabled = false; }
else if ((B1.Image == PictureX) && (C1.Image == PictureX) && (A1.Image == null)) { A1.Image = PictureO; A1.Enabled = false; }
else if ((A2.Image == PictureX) && (B2.Image == PictureX) && (C2.Image == null)) { C2.Image = PictureO; C2.Enabled = false; }
else if ((A2.Image == PictureX) && (C2.Image == PictureX) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureX) && (C2.Image == PictureX) && (A2.Image == null)) { A2.Image = PictureO; A2.Enabled = false; }
else if ((A3.Image == PictureX) && (B3.Image == PictureX) && (C3.Image == null)) { C3.Image = PictureO; C3.Enabled = false; }
else if ((A3.Image == PictureX) && (C3.Image == PictureX) && (B3.Image == null)) { B3.Image = PictureO; B3.Enabled = false; }
else if ((B3.Image == PictureX) && (C3.Image == PictureX) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
//Diagonal
else if ((A1.Image == PictureX) && (B2.Image == PictureX) && (C3.Image == null)) { C3.Image = PictureO; C3.Enabled = false; }
else if ((A1.Image == PictureX) && (C3.Image == PictureX) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureX) && (C3.Image == PictureX) && (A1.Image == null)) { A1.Image = PictureO; A1.Enabled = false; }
else if ((A3.Image == PictureX) && (B2.Image == PictureX) && (C1.Image == null)) { C1.Image = PictureO; A1.Enabled = false; }
else if ((A3.Image == PictureX) && (C1.Image == PictureX) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureX) && (C1.Image == PictureX) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
//Metode jika tidak ada pilihan terbaik
else if (B2.Image == null) { B2.Image = PictureO; B2.Enabled = false; }
else if (B2.Image == PictureO)
{
if (A3.Image == null) { A3.Image = PictureO; A3.Enabled = false; }
else if (C3.Image == null) { C3.Image = PictureO; C3.Enabled = false; }
else if (A1.Image == null) { A1.Image = PictureO; A1.Enabled = false; }
else if (C1.Image == null) { C1.Image = PictureO; C1.Enabled = false; }
else if (B1.Image == null) { B1.Image = PictureO; B1.Enabled = false; }
else if (B3.Image == null) { B3.Image = PictureO; B3.Enabled = false; }
else if (C2.Image == null) { C2.Image = PictureO; C2.Enabled = false; }
else if (A2.Image == null) { A2.Image = PictureO; A2.Enabled = false; }
}
else if (A3.Image == PictureO)
{
if (B3.Image == null) { B3.Image = PictureO; B3.Enabled = false; }
else if (C3.Image == null) { C3.Image = PictureO; C3.Enabled = false; }
else if (A2.Image == null) { A2.Image = PictureO; A2.Enabled = false; }
else if (A1.Image == null) { A1.Image = PictureO; A1.Enabled = false; }
else if (C1.Image == null) { C1.Image = PictureO; C1.Enabled = false; }
else if (B2.Image == null) { B2.Image = PictureO; B2.Enabled = false; }
}
else if (C3.Image == PictureO)
{
if (B3.Image == null) { B3.Image = PictureO; B3.Enabled = false; }
else if (A3.Image == null) { A3.Image = PictureO; A3.Enabled = false; }
else if (C2.Image == null) { C2.Image = PictureO; C2.Enabled = false; }
else if (C1.Image == null) { C1.Image = PictureO; C1.Enabled = false; }
else if (A1.Image == null) { A1.Image = PictureO; A1.Enabled = false; }
else if (B2.Image == null) { B2.Image = PictureO; B2.Enabled = false; }
}
else if (A1.Image == PictureO)
{
if (B1.Image == null) { B1.Image = PictureO; B1.Enabled = false; }
else if (C1.Image == null) { C1.Image = PictureO; C1.Enabled = false; }
else if (A2.Image == null) { A2.Image = PictureO; A2.Enabled = false; }
else if (A3.Image == null) { A3.Image = PictureO; A3.Enabled = false; }
else if (C3.Image == null) { C3.Image = PictureO; C3.Enabled = false; }
else if (B2.Image == null) { B2.Image = PictureO; B2.Enabled = false; }
}
else if (C1.Image == PictureO)
{
if (B1.Image == null) { B1.Image = PictureO; B1.Enabled = false; }
else if (A1.Image == null) { A1.Image = PictureO; A1.Enabled = false; }
else if (C2.Image == null) { C2.Image = PictureO; C2.Enabled = false; }
else if (C3.Image == null) { C3.Image = PictureO; C3.Enabled = false; }
else if (A3.Image == null) { A3.Image = PictureO; A3.Enabled = false; }
else if (B2.Image == null) { B2.Image = PictureO; B2.Enabled = false; }
}
else if (A2.Image == PictureO)
{
if (A1.Image == null) { A1.Image = PictureO; A1.Enabled = false; }
else if (A3.Image == null) { A3.Image = PictureO; A3.Enabled = false; }
else if (B2.Image == null) { B2.Image = PictureO; B2.Enabled = false; }
else if (C2.Image == null) { C2.Image = PictureO; C2.Enabled = false; }
}
else if (B1.Image == PictureO)
{
if (A1.Image == null) { A1.Image = PictureO; A1.Enabled = false; }
else if (C1.Image == null) { C1.Image = PictureO; C1.Enabled = false; }
else if (B2.Image == null) { B2.Image = PictureO; B2.Enabled = false; }
else if (B3.Image == null) { B3.Image = PictureO; B3.Enabled = false; }
}
else if (B3.Image == PictureO)
{
if (A3.Image == null) { A3.Image = PictureO; A3.Enabled = false; }
else if (C3.Image == null) { C3.Image = PictureO; C3.Enabled = false; }
else if (B2.Image == null) { B2.Image = PictureO; B2.Enabled = false; }
else if (B1.Image == null) { B1.Image = PictureO; B1.Enabled = false; }
}
else if (C2.Image == PictureO)
{
if (C1.Image == null) { C1.Image = PictureO; C1.Enabled = false; }
else if (C3.Image == null) { C3.Image = PictureO; C3.Enabled = false; }
else if (B2.Image == null) { B2.Image = PictureO; B2.Enabled = false; }
else if (A2.Image == null) { A2.Image = PictureO; A2.Enabled = false; }
}
else if (A2.Image == null) { A2.Image = PictureO; A2.Enabled = false; }
else if (B1.Image == null) { B1.Image = PictureO; B1.Enabled = false; }
else if (B3.Image == null) { B3.Image = PictureO; B3.Enabled = false; }
else if (C2.Image == null) { C2.Image = PictureO; C2.Enabled = false; }
else if (A1.Image == null) { A1.Image = PictureO; A1.Enabled = false; }
else if (C1.Image == null) { C1.Image = PictureO; C1.Enabled = false; }
else if (A3.Image == null) { A3.Image = PictureO; A3.Enabled = false; }
else if (C3.Image == null) { C3.Image = PictureO; C3.Enabled = false; }
Jumlah_Giliran++; //menghitung jumlah kotak yang sudah diklik
Giliran = true; //Giliran X kembali
}
private void pilihan_normal() //mode normal
{
//Metode melihat peluang Menang
//Horizontal
if ((A1.Image == PictureO) && (A2.Image == PictureO) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
else if ((A1.Image == PictureO) && (A3.Image == PictureO) && (A2.Image == null)) { A2.Image = PictureO; A2.Enabled = false; }
else if ((A2.Image == PictureO) && (A3.Image == PictureO) && (A1.Image == null)) { A1.Image = PictureO; A1.Enabled = false; }
else if ((B1.Image == PictureO) && (B2.Image == PictureO) && (B3.Image == null)) { B3.Image = PictureO; B3.Enabled = false; }
else if ((B1.Image == PictureO) && (B3.Image == PictureO) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureO) && (B3.Image == PictureO) && (B1.Image == null)) { B1.Image = PictureO; B1.Enabled = false; }
//Vertikal
else if ((A1.Image == PictureO) && (B1.Image == PictureO) && (C1.Image == null)) { C1.Image = PictureO; C1.Enabled = false; }
else if ((A1.Image == PictureO) && (C1.Image == PictureO) && (B1.Image == null)) { B1.Image = PictureO; B1.Enabled = false; }
else if ((B1.Image == PictureO) && (C1.Image == PictureO) && (A1.Image == null)) { A1.Image = PictureO; }
else if ((A3.Image == PictureO) && (B3.Image == PictureO) && (C3.Image == null)) { C3.Image = PictureO; C3.Enabled = false; }
else if ((A3.Image == PictureO) && (C3.Image == PictureO) && (B3.Image == null)) { B3.Image = PictureO; B3.Enabled = false; }
else if ((B3.Image == PictureO) && (C3.Image == PictureO) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
//Diagonal
else if ((A3.Image == PictureO) && (B2.Image == PictureO) && (C1.Image == null)) { C1.Image = PictureO; C1.Enabled = false; }
else if ((A3.Image == PictureO) && (C1.Image == PictureO) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureO) && (C1.Image == PictureO) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
//Metode Block lawan
//Horizontal
else if ((A1.Image == PictureX) && (A2.Image == PictureX) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
else if ((A1.Image == PictureX) && (A3.Image == PictureX) && (A2.Image == null)) { A2.Image = PictureO; A2.Enabled = false; }
else if ((A2.Image == PictureX) && (A3.Image == PictureX) && (A1.Image == null)) { A1.Image = PictureO; A1.Enabled = false; }
//Vertikal
else if ((A2.Image == PictureX) && (B2.Image == PictureX) && (C2.Image == null)) { C2.Image = PictureO; C2.Enabled = false; }
else if ((A2.Image == PictureX) && (C2.Image == PictureX) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureX) && (C2.Image == PictureX) && (A2.Image == null)) { A2.Image = PictureO; A2.Enabled = false; }
//Diagonal
else if ((A3.Image == PictureX) && (B2.Image == PictureX) && (C1.Image == null)) { C1.Image = PictureO; A1.Enabled = false; }
else if ((A3.Image == PictureX) && (C1.Image == PictureX) && (B2.Image == null)) { B2.Image = PictureO; B2.Enabled = false; }
else if ((B2.Image == PictureX) && (C1.Image == PictureX) && (A3.Image == null)) { A3.Image = PictureO; A3.Enabled = false; }
else Langkah_Random(); //mode acak
Jumlah_Giliran++; //menghitung jumlah kotak yang sudah diklik
Giliran = true; //Giliran X kembali
}
private void pilihan_random() //mode easy
{
Langkah_Random(); //mode acak
Jumlah_Giliran++; //menghitung jumlah kotak yang sudah diklik
Giliran = true; //Giliran X kembali
}
private void Giliran_Computer()
{
if (radioButton_Hard.Checked == true) { pilihan_terbaik(); } //hard mode
else if (radioButton_Medium.Checked == true) { pilihan_normal(); } //medium mode
else if (radioButton_Easy.Checked == true) { pilihan_random(); } //easy mode
CheckPemenang();
}
private void HideMenuUtama()
{
label_Oneplayer.Visible = label_Twoplayer.Visible = label_Option.Visible = label_Exit.Visible = label_Me.Visible = false;
}
private void HideStart()
{
pictureBox_Palang.Visible = false;
A1.Visible = A2.Visible = A3.Visible = B1.Visible = B2.Visible = B3.Visible = C1.Visible = C2.Visible = C3.Visible = false;
}
private void HideOption()
{
label_Level.Visible = false;
radioButton_Easy.Visible = radioButton_Medium.Visible = radioButton_Hard.Visible = false;
}
private void ShowMenuUtama()
{
HideStart();
HideOption();
pictureBox_Palang.SendToBack();
label_Oneplayer.Visible = label_Twoplayer.Visible = label_Option.Visible = label_Exit.Visible = label_Me.Visible = true;
label_Back.Visible = label_New.Visible = false;
label_Oscore.Text = label_Xscore.Text = label_Dscore.Text = "0";
label_Oscore.Visible = label_Xscore.Visible = label_Dscore.Visible = false;
label_NameO.Visible = label_NameX.Visible = label_NameD.Visible = false;
}
private void ShowStart()
{
HideMenuUtama();
HideOption();
pictureBox_Palang.Visible = true;
A1.Visible = A2.Visible = A3.Visible = B1.Visible = B2.Visible = B3.Visible = C1.Visible = C2.Visible = C3.Visible = true;
label_Back.Visible = label_New.Visible = true;
label_Oscore.Visible = label_Xscore.Visible = label_Dscore.Visible = true;
label_NameO.Visible = label_NameX.Visible = label_NameD.Visible = true;
}
private void ShowOption()
{
HideMenuUtama();
HideStart();
label_Level.Visible = true;
radioButton_Easy.Visible = radioButton_Medium.Visible = radioButton_Hard.Visible = true;
label_Back.Visible = true;
label_New.Visible = false;
}
private void PlayAgain()
{
Giliran = true;
Jumlah_Giliran = 0;
A1.Image = A2.Image = A3.Image = B1.Image = B2.Image = B3.Image = C1.Image = C2.Image = C3.Image = null;
A1.Enabled = A2.Enabled = A3.Enabled = B1.Enabled = B2.Enabled = B3.Enabled = C1.Enabled = C2.Enabled = C3.Enabled = true;
}
private void pictureBox_MouseMove(object sender, MouseEventArgs e)
{
PictureBox Pilih = (PictureBox)sender; //memilih daerah gambar yg diklik
if (Pilih.Enabled)
{
if (Giliran) { Pilih.Image = PictureX; }
else { Pilih.Image = PictureO; }
}
}
private void pictureBox_MouseLeave(object sender, EventArgs e)
{
PictureBox Pilih = (PictureBox)sender; //memilih daerah gambar yg diklik
if (Pilih.Enabled)
{
Pilih.Image = null;
}
}
private void pictureBox_Click(object sender, EventArgs e)
{
PictureBox Pilih = (PictureBox)sender; //memilih daerah gambar
if (Giliran) { Pilih.Image = PictureX; }
else { Pilih.Image = PictureO; }
Giliran = !Giliran; //merubah Giliran
Pilih.Enabled = false; //menonaktifkan gambar yg telah diklik
Jumlah_Giliran++; //menghitung jumlah kotak yang sudah diklik
CheckPemenang(); //mengecheck kondisi permainan
//mengecek apakah sekarang saatnya Giliran O, dan apakah Mode Computer "Aktif"
if ((!Giliran) && (Computer))
{
Giliran_Computer(); //Computer yang jalan
}
}
private void CheckPemenang()
{
bool Pemenang = false;
//Horizontal
if ((A1.Image == A2.Image) && (A2.Image == A3.Image) && (!A1.Enabled)) { Pemenang = true; }
else if ((B1.Image == B2.Image) && (B2.Image == B3.Image) && (!B1.Enabled)) { Pemenang = true; }
else if ((C1.Image == C2.Image) && (C2.Image == C3.Image) && (!C1.Enabled)) { Pemenang = true; }
//Vertikal
else if ((A1.Image == B1.Image) && (B1.Image == C1.Image) && (!A1.Enabled)) { Pemenang = true; }
else if ((A2.Image == B2.Image) && (B2.Image == C2.Image) && (!A2.Enabled)) { Pemenang = true; }
else if ((A3.Image == B3.Image) && (B3.Image == C3.Image) && (!A3.Enabled)) { Pemenang = true; }
//Diagonal
else if ((A1.Image == B2.Image) && (B2.Image == C3.Image) && (!A1.Enabled)) { Pemenang = true; }
else if ((A3.Image == B2.Image) && (B2.Image == C1.Image) && (!A3.Enabled)) { Pemenang = true; }
//jika permainan Draw
else if (Jumlah_Giliran == 9)
{
label_Dscore.Text = (Int32.Parse(label_Dscore.Text) + 1).ToString();
MessageBox.Show("Game is DRAW !!!", "GameOver");
}
if (Pemenang)
{
string Winner = "";
A1.Enabled = A2.Enabled = A3.Enabled = B1.Enabled = B2.Enabled = B3.Enabled = C1.Enabled = C2.Enabled = C3.Enabled = false;
//jika diakhiri dengan Giliran "X" maka yang menang adalah giliran sebelumnya
if (Giliran) { Winner = player2; label_Oscore.Text = (Int32.Parse(label_Oscore.Text) + 1).ToString(); }
//jika diakhiri dengan Giliran "O" maka yang menang adalah giliran sebelumnya
else { Winner = player1; label_Xscore.Text = (Int32.Parse(label_Xscore.Text) + 1).ToString(); }
MessageBox.Show(Winner + " Wins !!!", "GameOver");
}
}
private void label_New_Click(object sender, EventArgs e)
{
PlayAgain();
}
private void label_Exit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void label_Back_Click(object sender, EventArgs e)
{
ShowMenuUtama();
HideStart();
}
private void label_Oneplayer_Click(object sender, EventArgs e)
{
Form_Option1 Gobar = new Form_Option1();
Gobar.ShowDialog(); //menampilkan Form_Option
label_NameX.Text = player1;
label_NameO.Text = player2;
PlayAgain();
ShowStart();
Computer = true;
}
private void label_Twoplayer_Click(object sender, EventArgs e)
{
Form_Option2 Gobar = new Form_Option2();
Gobar.ShowDialog(); //menampilkan Form_Option
label_NameX.Text = player1;
label_NameO.Text = player2;
PlayAgain();
ShowStart();
Computer = false;
}
private void label_Option_Click(object sender, EventArgs e)
{
ShowOption();
}
private void label_Oneplayer_MouseMove(object sender, MouseEventArgs e)
{
Label choose = (Label)sender;
choose.ForeColor = Color.Maroon;
}
private void label_Oneplayer_MouseLeave(object sender, EventArgs e)
{
label_Oneplayer.ForeColor = Color.Black;
label_Twoplayer.ForeColor = Color.Black;
label_Option.ForeColor = Color.Black;
label_Exit.ForeColor = Color.Black;
}
private void radioButton_MouseMove(object sender, MouseEventArgs e)
{
RadioButton choose = (RadioButton)sender;
choose.ForeColor = Color.Maroon;
}
private void radioButton_Easy_MouseLeave(object sender, EventArgs e)
{
radioButton_Easy.ForeColor = Color.Black;
radioButton_Medium.ForeColor = Color.Black;
radioButton_Hard.ForeColor = Color.Black;
}
}
}
Ketika
program dijalankan :
Player1 VS Computer
Player1 VS Player2
Baiklah…,, Demikian sedeikit
penjelasan yang dapat saya sampaikan, jika ada kesalahan saya mohon
dimaafkan. semoga ini bermanfaat :)
Untuk lebih jelasnya, silahkan klik
video tutorial dibawah ini :
Nantikan terus postingan-postingan saya
selanjutnya di
Wassalammualaikum Wr.Wb
Terima Kasih
Tidak ada komentar:
Posting Komentar