Membuat Program game Tic Tac Toe dengan C#
I. Tujuan
• Meningkatkan kemampuan berpikir logis
• Mengetahui kegunaan PictureBox di SharpDevelop
• Dapat mengunakan PictureBox dalam aplikasi windows
sederhana
II. Dasar Teori
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.
![]() |
| Add caption |
III. Praktikum
Berikut merupakan desain game tictactoe yang saya buat
Berikut adala source code program diatas
#region Using Statements
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Media;
#endregion
namespace TicTacToe
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
# region Deklarasi
// Mendeklarasikan variabel dalam proyek
// Indikator gilirannya , untuk mode multi player
private bool playerTurn = false;
//Gambar Untuk X dan O
private Image x = Properties.Resources.X1;
private Image o = Properties.Resources.O1;
#endregion
#region Reset
private void _Reset()
{
// Ini me-reset permainan (permainan baru)
button1.BackgroundImage = null;
button2.BackgroundImage = null;
button3.BackgroundImage = null;
button4.BackgroundImage = null;
button5.BackgroundImage = null;
button6.BackgroundImage = null;
button7.BackgroundImage = null;
button8.BackgroundImage = null;
button9.BackgroundImage = null;
playerTurn = false;
}
#endregion
#region Check
// Ini adalah logika untuk memeriksa apakah seseorang telah memenangkan
private void _Check()
{
//In every check there is a sound to be played //To modi123_1, I suggest you delete them
if (button3.BackgroundImage == x)
{
if (button5.BackgroundImage == x)
{
if (button7.BackgroundImage == x)
{
//Untuk Memberi tahu Pemenangnya
MessageBox.Show("X MENANG!");
return;
}
}
}
if (button4.BackgroundImage == x)
{
if (button5.BackgroundImage == x)
{
if (button6.BackgroundImage == x)
{
MessageBox.Show("X MENANG!");
return;
}
}
}
if (button7.BackgroundImage == x)
{
if (button8.BackgroundImage == x)
{
if (button9.BackgroundImage == x)
{
MessageBox.Show("X MENANG!");
return;
}
}
}
if (button2.BackgroundImage == x)
{
if (button5.BackgroundImage == x)
{
if (button8.BackgroundImage == x)
{
MessageBox.Show("X MENANG!");
return;
}
}
}
if (button3.BackgroundImage == x)
{
if (button6.BackgroundImage == x)
{
if (button9.BackgroundImage == x)
{
MessageBox.Show("X MENANG!");
return;
}
}
}
if (button1.BackgroundImage == x)
{
if (button2.BackgroundImage == x)
{
if (button3.BackgroundImage == x)
{
MessageBox.Show("X MENANG!");
return;
}
}
}
if (button1.BackgroundImage == x)
{
if (button5.BackgroundImage == x)
{
if (button9.BackgroundImage == x)
{
MessageBox.Show("X MENANG!");
return;
}
}
}
if (button1.BackgroundImage == x)
{
if (button4.BackgroundImage == x)
{
if (button7.BackgroundImage == x)
{
MessageBox.Show("X MENANG!");
return;
}
}
}
if (button1.BackgroundImage == o)
{
if (button2.BackgroundImage == o)
{
if (button3.BackgroundImage == o)
{
MessageBox.Show("O MENANG!");
return;
}
}
}
if (button1.BackgroundImage == o)
{
if (button5.BackgroundImage == o)
{
if (button9.BackgroundImage == o)
{
MessageBox.Show("O MENANG!");
return;
}
}
}
if (button1.BackgroundImage == o)
{
if (button4.BackgroundImage == o)
{
if (button7.BackgroundImage == o)
{
MessageBox.Show("O MENANG!");
return;
}
}
}
if (button3.BackgroundImage == o)
{
if (button5.BackgroundImage == o)
{
if (button7.BackgroundImage == o)
{
MessageBox.Show("O MENANG!");
return;
}
}
}
if (button4.BackgroundImage == o)
{
if (button5.BackgroundImage == o)
{
if (button6.BackgroundImage == o)
{
MessageBox.Show("O MENANG!");
return;
}
}
}
if (button7.BackgroundImage == o)
{
if (button8.BackgroundImage == o)
{
if (button9.BackgroundImage == o)
{
MessageBox.Show("O MENANG!");
return;
}
}
}
if (button2.BackgroundImage == o)
{
if (button5.BackgroundImage == o)
{
if (button8.BackgroundImage == o)
{
MessageBox.Show("O MENANG!");
return;
}
}
}
if (button3.BackgroundImage == o)
{
if (button6.BackgroundImage == o)
{
if (button9.BackgroundImage == o)
{
MessageBox.Show("O MENANG!");
return;
}
}
}
if (button1.BackgroundImage != null && button2.BackgroundImage != null && button3.BackgroundImage != null && button4.BackgroundImage != null && button5.BackgroundImage != null && button6.BackgroundImage != null && button7.BackgroundImage != null && button8.BackgroundImage != null && button9.BackgroundImage != null)
{
MessageBox.Show("Permainan Imbang!");
return;
}
}
#endregion
#region Computer Play
// Ini adalah logika komputer
// Ia memeriksa tiga hal dalam urutan ini :
// 1 . Jika bisa menang itu akan memainkan bergerak menang
// 2 . Jika bisa mempertahankan diri dari kehilangan itu akan bermain langkah yang
// 3 . Bergerak jika tidak ada pilihan lain
private void _ComputerPlay()
{
if (button1.BackgroundImage == o && button2.BackgroundImage == o)
{
if (button3.BackgroundImage == null)
{
button3.BackgroundImage = o;
_Check();
return;
}
}
if (button2.BackgroundImage == o && button3.BackgroundImage == o)
{
if (button1.BackgroundImage == null)
{
button1.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == o && button3.BackgroundImage == o)
{
if (button2.BackgroundImage == null)
{
button2.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == o && button7.BackgroundImage == o)
{
if (button4.BackgroundImage == null)
{
button4.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == o && button4.BackgroundImage == o)
{
if (button7.BackgroundImage == null)
{
button7.BackgroundImage = o;
_Check();
return;
}
}
if (button4.BackgroundImage == o && button7.BackgroundImage == o)
{
if (button1.BackgroundImage == null)
{
button1.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == o && button5.BackgroundImage == o)
{
if (button9.BackgroundImage == null)
{
button9.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == o && button9.BackgroundImage == o)
{
if (button1.BackgroundImage == null)
{
button1.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == o && button9.BackgroundImage == o)
{
if (button5.BackgroundImage == null)
{
button5.BackgroundImage = o;
_Check();
return;
}
}
if (button2.BackgroundImage == o && button5.BackgroundImage == o)
{
if (button8.BackgroundImage == null)
{
button8.BackgroundImage = o;
_Check();
return;
}
}
if (button2.BackgroundImage == o && button8.BackgroundImage == o)
{
if (button5.BackgroundImage == null)
{
button5.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == o && button8.BackgroundImage == o)
{
if (button2.BackgroundImage == null)
{
button2.BackgroundImage = o;
_Check();
return;
}
}
if (button3.BackgroundImage == o && button6.BackgroundImage == o)
{
if (button9.BackgroundImage == null)
{
button9.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == o && button8.BackgroundImage == o)
{
if (button2.BackgroundImage == null)
{
button2.BackgroundImage = o;
_Check();
return;
}
}
if (button3.BackgroundImage == o && button9.BackgroundImage == o)
{
if (button6.BackgroundImage == null)
{
button6.BackgroundImage = o;
_Check();
return;
}
}
if (button6.BackgroundImage == o && button9.BackgroundImage == o)
{
if (button3.BackgroundImage == null)
{
button3.BackgroundImage = o;
_Check();
return;
}
}
if (button4.BackgroundImage == o && button5.BackgroundImage == o)
{
if (button4.BackgroundImage == null)
{
button6.BackgroundImage = o;
_Check();
return;
}
}
if (button4.BackgroundImage == o && button6.BackgroundImage == o)
{
if (button5.BackgroundImage == null)
{
button5.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == o && button6.BackgroundImage == o)
{
if (button4.BackgroundImage == null)
{
button4.BackgroundImage = o;
_Check();
return;
}
}
if (button7.BackgroundImage == o && button9.BackgroundImage == o)
{
if (button8.BackgroundImage == null)
{
button8.BackgroundImage = o;
_Check();
return;
}
}
if (button8.BackgroundImage == o && button9.BackgroundImage == o)
{
if (button7.BackgroundImage == null)
{
button7.BackgroundImage = o;
_Check();
return;
}
}
if (button7.BackgroundImage == o && button8.BackgroundImage == o)
{
if (button9.BackgroundImage == null)
{
button9.BackgroundImage = o;
_Check();
return;
}
}
if (button3.BackgroundImage == o && button5.BackgroundImage == o)
{
if (button7.BackgroundImage == null)
{
button7.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == o && button7.BackgroundImage == o)
{
if (button3.BackgroundImage == null)
{
button3.BackgroundImage = o;
_Check();
return;
}
}
if (button3.BackgroundImage == o && button7.BackgroundImage == o)
{
if (button5.BackgroundImage == null)
{
button5.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == x && button2.BackgroundImage == x)
{
if (button3.BackgroundImage == null)
{
button3.BackgroundImage = o;
_Check();
return;
}
}
if (button2.BackgroundImage == x && button3.BackgroundImage == x)
{
if (button1.BackgroundImage == null)
{
button1.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == x && button3.BackgroundImage == x)
{
if (button2.BackgroundImage == null)
{
button2.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == x && button4.BackgroundImage == x)
{
if (button7.BackgroundImage == null)
{
button7.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == x && button7.BackgroundImage == x)
{
if (button4.BackgroundImage == null)
{
button4.BackgroundImage = o;
_Check();
return;
}
}
if (button4.BackgroundImage == x && button7.BackgroundImage == x)
{
if (button1.BackgroundImage == null)
{
button1.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == x && button9.BackgroundImage == x)
{
if (button5.BackgroundImage == null)
{
button5.BackgroundImage = o;
_Check();
return;
}
}
if (button1.BackgroundImage == x && button5.BackgroundImage == x)
{
if (button9.BackgroundImage == null)
{
button9.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == x && button9.BackgroundImage == x)
{
if (button5.BackgroundImage == null)
{
button1.BackgroundImage = o;
_Check();
return;
}
}
if (button4.BackgroundImage == x && button5.BackgroundImage == x)
{
if (button6.BackgroundImage == null)
{
button6.BackgroundImage = o;
_Check();
return;
}
}
if (button4.BackgroundImage == x && button6.BackgroundImage == x)
{
if (button5.BackgroundImage == null)
{
button5.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == x && button6.BackgroundImage == x)
{
if (button4.BackgroundImage == null)
{
button4.BackgroundImage = o;
_Check();
return;
}
}
if (button7.BackgroundImage == x && button9.BackgroundImage == x)
{
if (button8.BackgroundImage == null)
{
button8.BackgroundImage = o;
_Check();
return;
}
}
if (button7.BackgroundImage == x && button8.BackgroundImage == x)
{
if (button9.BackgroundImage == null)
{
button9.BackgroundImage = o;
_Check();
return;
}
}
if (button8.BackgroundImage == x && button9.BackgroundImage == x)
{
if (button7.BackgroundImage == null)
{
button7.BackgroundImage = o;
_Check();
return;
}
}
if (button2.BackgroundImage == x && button5.BackgroundImage == x)
{
if (button8.BackgroundImage == null)
{
button8.BackgroundImage = o;
_Check();
return;
}
}
if (button3.BackgroundImage == x && button6.BackgroundImage == x)
{
if (button9.BackgroundImage == null)
{
button9.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == x && button7.BackgroundImage == x)
{
if (button3.BackgroundImage == null)
{
button3.BackgroundImage = o;
_Check();
return;
}
}
if (button2.BackgroundImage == x && button7.BackgroundImage == x)
{
if (button5.BackgroundImage == null)
{
button5.BackgroundImage = o;
_Check();
return;
}
}
if (button3.BackgroundImage == x && button9.BackgroundImage == x)
{
if (button6.BackgroundImage == null)
{
button6.BackgroundImage = o;
_Check();
return;
}
}
if (button6.BackgroundImage == x && button9.BackgroundImage == x)
{
if (button3.BackgroundImage == null)
{
button3.BackgroundImage = o;
_Check();
return;
}
}
if (button3.BackgroundImage == x && button9.BackgroundImage == x)
{
if (button6.BackgroundImage == null)
{
button6.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == x && button8.BackgroundImage == x)
{
if (button2.BackgroundImage == null)
{
button2.BackgroundImage = o;
_Check();
return;
}
}
if (button3.BackgroundImage == x && button5.BackgroundImage == x)
{
if (button7.BackgroundImage == null)
{
button7.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == x && button7.BackgroundImage == x)
{
if (button3.BackgroundImage == null)
{
button3.BackgroundImage = o;
_Check();
return;
}
}
if (button3.BackgroundImage == x && button7.BackgroundImage == x)
{
if (button5.BackgroundImage == null)
{
button5.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == null)
{
button5.BackgroundImage = o;
_Check();
return;
}
if (button1.BackgroundImage == x && button9.BackgroundImage == x && button5.BackgroundImage == o)
{
if (button2.BackgroundImage == null)
{
button2.BackgroundImage = o;
_Check();
return;
}
}
if (button3.BackgroundImage == x && button7.BackgroundImage == x && button5.BackgroundImage == o)
{
if (button2.BackgroundImage == null)
{
button2.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == o && button2.BackgroundImage == x)
{
if (button3.BackgroundImage == null)
{
button3.BackgroundImage = o;
_Check();
return;
}
}
if (button5.BackgroundImage == o && button8.BackgroundImage == x)
{
if (button9.BackgroundImage == null)
{
button9.BackgroundImage = o;
_Check();
return;
}
}
// Pilihan terakhir ... jika tidak ada di pilihan adalah salah
if (button1.BackgroundImage == null)
{
button1.BackgroundImage = o;
_Check();
return;
}
if (button3.BackgroundImage == null)
{
button3.BackgroundImage = o;
_Check();
return;
}
if (button9.BackgroundImage == null)
{
button9.BackgroundImage = o;
_Check();
return;
}
if (button6.BackgroundImage == null)
{
button6.BackgroundImage = o;
_Check();
return;
}
}
#endregion
#region ButtonClickLogic
private void _ButtonClick(ref Button theButton)
{
// yang terjadi pada button
if (radioButton1.Checked)
{
if (theButton.BackgroundImage == null)
{
// Mengatur gambar latar belakang dari tombol untuk x
theButton.BackgroundImage = x;
//Memeriksa jika permainan berakhir
_Check();
// Giliran Komputer
_ComputerPlay();
}
else
MessageBox.Show("Tombol yang diinginkan sudah dipilih!");
}
else if (radioButton2.Checked)
{
if (theButton.BackgroundImage == null)
{
if (playerTurn == false)
{
theButton.BackgroundImage = x;
playerTurn = true;
_Check();
}
else
{
theButton.BackgroundImage = o;
playerTurn = false;
_Check();
}
}
else
{
// Menunjukkan pesan untuk memberitahu bahwa tombol sudah dipilih
MessageBox.Show("Tombol yang diinginkan sudah dipilih!");
}
}
else
{
//memilih mode permainan
MessageBox.Show("Silahkan Pilih Mode Permainan . . . .");
}
}
#endregion
#region ButtonClicks
private void button1_Click(object sender, EventArgs e)
{
_ButtonClick(ref button1);
}
private void button2_Click(object sender, EventArgs e)
{
_ButtonClick(ref button2);
}
private void button3_Click(object sender, EventArgs e)
{
_ButtonClick(ref button3);
}
private void button4_Click(object sender, EventArgs e)
{
_ButtonClick(ref button4);
}
private void button5_Click(object sender, EventArgs e)
{
_ButtonClick(ref button5);
}
private void button6_Click(object sender, EventArgs e)
{
_ButtonClick(ref button6);
}
private void button7_Click(object sender, EventArgs e)
{
_ButtonClick(ref button7);
}
private void button8_Click(object sender, EventArgs e)
{
_ButtonClick(ref button8);
}
private void button9_Click(object sender, EventArgs e)
{
_ButtonClick(ref button9);
}
private void button10_Click(object sender, EventArgs e)
{
//Calls the reset method to initiate a new game
_Reset();
}
private void button11_Click(object sender, EventArgs e)
{
//Closes the application
Close();
}
#endregion
}
}



Tidak ada komentar:
Posting Komentar