Setelah sekian lama pembuatan game ini saya hiraukan,entah ada angin apa aku punya niat untuk menyelesaiinnya,Tic Tack Toe ini saya buat menggunakan Embarcadero XE3.
Disini saya lampirkan Source Codenya,mungkin program ini belum rapi dan efisien tapi semoga bisa membantu bagi teman-teman yang ingin buat game tic tac toe.
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#include <String.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
int Isi[5][5];
int Play;
//---------------------------------------------------------------------------
void PlayerTurn()
{
if(Play %2 ==0)
{
Form1->Label4->Caption ="Player 1 Trun";
}
else
{
Form1->Label4->Caption ="Player 2 Trun";
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button10Click(TObject *Sender)
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
Isi[i][j] =0;
}
}
Form1->Label4->Caption ="Player 1 Trun";
Form1->Label1->Caption ="";
Form1->Label2->Caption ="";
Form1->Button1->Enabled = True;
Form1->Button2->Enabled = True;
Form1->Button3->Enabled = True;
Form1->Button4->Enabled = True;
Form1->Button5->Enabled = True;
Form1->Button6->Enabled = True;
Form1->Button7->Enabled = True;
Form1->Button8->Enabled = True;
Form1->Button9->Enabled = True;
Form1->Button1->Caption ="";
Form1->Button2->Caption ="";
Form1->Button3->Caption ="";
Form1->Button4->Caption ="";
Form1->Button5->Caption ="";
Form1->Button6->Caption ="";
Form1->Button7->Caption ="";
Form1->Button8->Caption ="";
Form1->Button9->Caption ="";
Form1->Label3->Caption ="";
Play =0;
}
//---------------------------------------------------------------------------
void Win()
{
Form1->Button1->Enabled = False;
Form1->Button2->Enabled = False;
Form1->Button3->Enabled = False;
Form1->Button4->Enabled = False;
Form1->Button5->Enabled = False;
Form1->Button6->Enabled = False;
Form1->Button7->Enabled = False;
Form1->Button8->Enabled = False;
Form1->Button9->Enabled = False;
}
//---------------------------------------------------------------------------
void CheckWin()
{
int i,j,m;
int win1;
for(i=0;i<3;i++)
{
win1 = 0;
for(j=0;j<3;j++)
{
if(j==0)
{
if(Isi[i][j] == 1 || Isi[i][j] == 2)
{
m = Isi[i][j];
}
}
if(m == Isi[i][j+1])
{
win1++;
if(win1==2)
{
if(m==1)
{
Win();
Form1->Label4->Caption ="Player 1 Win";
}
else
{
Win();
Form1->Label4->Caption ="Player 2 Win";
}
}
}
}
}
int win2,n,k;
for(i=0;i<3;i++)
{
win2 = 0;
k=i;
for(j=0;j<3;j++)
{
if(Isi[0][k] == 1 || Isi[0][k] == 2)
{
n = Isi[0][k];
}
if(n == Isi[j+1][i])
{
win2++;
if(win2==2)
{
if(n==1)
{
Win();
Form1->Label4->Caption ="Player 1 Win";
}
else if(n==2)
{
Win();
Form1->Label4->Caption ="Player 2 Win";
}
}
}
}
}
int o=0,p=0;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(i==j)
{
if(Isi[i][j] == 1)
{
o++;
if(o==3)
{
Win();
Form1->Label4->Caption ="Player 1 Win";
}
}
else if(Isi[i][j]==2)
{
p++;
if(p==3)
{
Win();
Form1->Label4->Caption ="Player 2 Win";
}
}
}
}
}
int q=0,r=0,a;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(j ==2-i)
{
if(Isi[i][j] == 1)
{
q++;
if(q==3)
{
Win();
Form1->Label4->Caption ="Player 1 Win";
}
}
else if(Isi[i][j]==2)
{
r++;
if(r==3)
{
Win();
Form1->Label4->Caption ="Player 2 Win";
}
}
}
}
}
String CheckDraw,Winnya;
Winnya ="Player 2 Win";
CheckDraw = Form1->Label4->Caption;
if(Play==6)
{
if(CheckDraw!=Winnya)
{
Win();
Form1->Label4->Caption ="";
Form1->Label3->Caption ="Draw";
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Play++;
if(Play %2 != 0)
{
Button1->Caption = "X";
Isi[0][0] = 1;
}
else
{
Button1->Caption = "O";
Isi[0][0] = 2;
}
Button1->Enabled = False;
PlayerTurn();
CheckWin();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Play++;
if(Play %2 != 0)
{
Button2->Caption = "X";
Isi[0][1] = 1;
}
else
{
Button2->Caption = "O";
Isi[0][1] = 2;
}
Button2->Enabled = False;
PlayerTurn();
CheckWin();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
Play++;
if(Play %2 != 0)
{
Button3->Caption = "X";
Isi[0][2] = 1;
}
else
{
Button3->Caption = "O";
Isi[0][2] = 2;
}
Button3->Enabled = False;
PlayerTurn();
CheckWin();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
Play++;
if(Play %2 != 0)
{
Button4->Caption = "X";
Isi[1][0] = 1;
}
else
{
Button4->Caption = "O";
Isi[1][0] = 2;
}
Button4->Enabled = False;
PlayerTurn();
CheckWin();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button5Click(TObject *Sender)
{
Play++;
if(Play %2 != 0)
{
Button5->Caption = "X";
Isi[1][1] = 1;
}
else
{
Button5->Caption = "O";
Isi[1][1] = 2;
}
Button5->Enabled = False;
PlayerTurn();
CheckWin();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button6Click(TObject *Sender)
{
Play++;
if(Play %2 != 0)
{
Button6->Caption = "X";
Isi[1][2] = 1;
}
else
{
Button6->Caption = "O";
Isi[1][2] = 2;
}
Button6->Enabled = False;
PlayerTurn();
CheckWin();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button7Click(TObject *Sender)
{
Play++;
if(Play %2 != 0)
{
Button7->Caption = "X";
Isi[2][0] = 1;
}
else
{
Button7->Caption = "O";
Isi[2][0] = 2;
}
Button7->Enabled = False;
PlayerTurn();
CheckWin();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button8Click(TObject *Sender)
{
Play++;
if(Play %2 != 0)
{
Button8->Caption = "X";
Isi[2][1] = 1;
}
else
{
Button8->Caption = "O";
Isi[2][1] = 2;
}
Button8->Enabled = False;
PlayerTurn();
CheckWin();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button9Click(TObject *Sender)
{
Play++;
if(Play %2 != 0)
{
Button9->Caption = "X";
Isi[2][2] = 1;
}
else
{
Button9->Caption = "O";
Isi[2][2] = 2;
}
Button9->Enabled = False;
PlayerTurn();
CheckWin();
}
//---------------------------------------------------------------------------
Terimakasih :)
0 komentar:
Posting Komentar