Nama saya Yosua Renol S dari politeknik Negeri Batam
Disini saya akan membuat sebuah aplikasi TicTacToe atau
XOX dengan lvl easy menggunaka aplikasi Visual studio
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.
Pertama buka
aplikasi visual studio
Kedua klik
new project
Lalu klik
windows form aplication lalu ok
Kluar form
seperti ini
Lalu buat
form dengan toolBox : 2 textBox, 3 Button, 9 pictureBox
pastikan sudah ada gambar O dan X dalam komputer anda.
lalu buat codingan seperti ini :
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TicTacToe
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TicTacToe
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
Random r = new Random();
Image x = Image.FromFile("x.png");
Image o = Image.FromFile("o.png");
PictureBox [] gbr = new PictureBox[8];
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void ExitToolStripMenuItemClick(object sender, EventArgs e)
{
Application.Exit();
}
void menang ()
{
//123
if ( gbr[0].Image == x && gbr[1].Image == x && gbr[2].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[0].Image == o && gbr[1].Image == o && gbr[2].Image == o)
{
MessageBox.Show("team o win");
}
//456
else if ( gbr[3].Image == x && gbr[4].Image == x && gbr[5].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[3].Image == o && gbr[4].Image == o && gbr[5].Image == o)
{
MessageBox.Show("team o win");
}
//789
else if ( gbr[6].Image == x && gbr[7].Image == x && gbr[8].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[6].Image == o && gbr[7].Image == o && gbr[8].Image == o)
{
MessageBox.Show("team o win");
}
//147
else if ( gbr[0].Image == x && gbr[3].Image == x && gbr[6].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[0].Image == o && gbr[3].Image == o && gbr[6].Image == o)
{
MessageBox.Show("team o win");
}
//258
else if ( gbr[1].Image == x && gbr[4].Image == x && gbr[7].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[1].Image == o && gbr[4].Image == o && gbr[7].Image == o)
{
MessageBox.Show("team o win");
}
//369
else if ( gbr[2].Image == x && gbr[5].Image == x && gbr[8].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[2].Image == o && gbr[5].Image == o && gbr[8].Image == o)
{
MessageBox.Show("team o win");
}
//159
else if ( gbr[0].Image == x && gbr[4].Image == x && gbr[8].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[0].Image == o && gbr[4].Image == o && gbr[8].Image == o)
{
MessageBox.Show("team o win");
}
//357
else if ( gbr[2].Image == x && gbr[4].Image == x && gbr[6].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[2].Image == o && gbr[4].Image == o && gbr[7].Image == o)
{
MessageBox.Show("team o win");
}
}
void random()
{
int e = r.Next(8);
if (gbr[e].Image == null)
{
gbr[e].Image = Image.FromFile("o.png");
}
else
{
random();
}
}
void MainFormLoad(object sender, EventArgs e)
{
gbr = new PictureBox[] {pictureBox1,pictureBox2,pictureBox3,pictureBox4,pictureBox5,pictureBox6,pictureBox7,pictureBox8,pictureBox9};
for (int z=0;z<9;z++)
{
gbr[z].Click += new EventHandler(array);
}
}
void array(Object sender, EventArgs e)
{
PictureBox baru = (PictureBox) sender;
if(baru.Image == null)
{
baru.Image = x;
//menang();
random ();
menang();
}
//random();
}
}
}
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
Random r = new Random();
Image x = Image.FromFile("x.png");
Image o = Image.FromFile("o.png");
PictureBox [] gbr = new PictureBox[8];
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
void ExitToolStripMenuItemClick(object sender, EventArgs e)
{
Application.Exit();
}
void menang ()
{
//123
if ( gbr[0].Image == x && gbr[1].Image == x && gbr[2].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[0].Image == o && gbr[1].Image == o && gbr[2].Image == o)
{
MessageBox.Show("team o win");
}
//456
else if ( gbr[3].Image == x && gbr[4].Image == x && gbr[5].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[3].Image == o && gbr[4].Image == o && gbr[5].Image == o)
{
MessageBox.Show("team o win");
}
//789
else if ( gbr[6].Image == x && gbr[7].Image == x && gbr[8].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[6].Image == o && gbr[7].Image == o && gbr[8].Image == o)
{
MessageBox.Show("team o win");
}
//147
else if ( gbr[0].Image == x && gbr[3].Image == x && gbr[6].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[0].Image == o && gbr[3].Image == o && gbr[6].Image == o)
{
MessageBox.Show("team o win");
}
//258
else if ( gbr[1].Image == x && gbr[4].Image == x && gbr[7].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[1].Image == o && gbr[4].Image == o && gbr[7].Image == o)
{
MessageBox.Show("team o win");
}
//369
else if ( gbr[2].Image == x && gbr[5].Image == x && gbr[8].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[2].Image == o && gbr[5].Image == o && gbr[8].Image == o)
{
MessageBox.Show("team o win");
}
//159
else if ( gbr[0].Image == x && gbr[4].Image == x && gbr[8].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[0].Image == o && gbr[4].Image == o && gbr[8].Image == o)
{
MessageBox.Show("team o win");
}
//357
else if ( gbr[2].Image == x && gbr[4].Image == x && gbr[6].Image == x)
{
MessageBox.Show("team x win");
}
else if ( gbr[2].Image == o && gbr[4].Image == o && gbr[7].Image == o)
{
MessageBox.Show("team o win");
}
}
void random()
{
int e = r.Next(8);
if (gbr[e].Image == null)
{
gbr[e].Image = Image.FromFile("o.png");
}
else
{
random();
}
}
void MainFormLoad(object sender, EventArgs e)
{
gbr = new PictureBox[] {pictureBox1,pictureBox2,pictureBox3,pictureBox4,pictureBox5,pictureBox6,pictureBox7,pictureBox8,pictureBox9};
for (int z=0;z<9;z++)
{
gbr[z].Click += new EventHandler(array);
}
}
void array(Object sender, EventArgs e)
{
PictureBox baru = (PictureBox) sender;
if(baru.Image == null)
{
baru.Image = x;
//menang();
random ();
menang();
}
//random();
}
}
}
Tidak ada komentar:
Posting Komentar