Tomorrow is Future

Pengikut : "Live is War "

Algoritma dan Pemrograman II (Game Tic Tac Toe)

Minggu, 21 Juni 2009

Untuk memenuhi salah satu tugas dari Mata Kuliah Internet dan Intranet yaitu membuat blog dengan menyertakan sebuah tulisan mengenai 1 Mata Kuliah yang dikuasi. Dalam hal ini saya akan membuat sebuah tulisan tentang Mata Kuliah Algoritma dan Pemrograman II yang menggunakan bahasa C++. Dimana dengan menggunakan bahasa pemrograman tersebut akan membuat sebuah aplikasi game sederhana, yaitu Permainan Tic Tac Toe. Untuk itu saya akan memberikan sebuah script mengenai pembuatan game tersebut. Algoritma yang digunakan dalam program ini yaitu :
- Perulangan
- Array
- Percabangan (If-else, Switch-Case)
- Perulangan

Yang paling penting adalah bila ingin membuat game tersebut haruslah terlebih dahulu memiliki Program C++ yang telah terintergrasi dengan komputer. Sehingga pengguna bisa langsung menerapkan script tersebut kedalah C++ yang dimiliki. Berikut ini merupakan script dari Game Tic Tac Toe :

#include
#include
#include
#include
#include

void time();
void petunjuk();
void loading();
void proses();
void menu();
void papan();
void pemain();
void computer_movement();
int chek_pemenang(char player);
void delay(int time);

int select;
char board_info[9]= {'1','2','3','4','5','6','7','8','9'};
int i;

void main()
{
loading();
menu();
}
void time()
{ textcolor(GREEN);
char datebuf[9];
char timebuf[9];

_strdate(datebuf);
_strtime(timebuf);
printf("Date: %s Time: %s\n\n\n\n",datebuf,timebuf);
textcolor(GREEN);
}
void loading()
{ textcolor(LIGHTRED);
for(int b=0;b<=1;b++)
{
clrscr();
gotoxy(25,1);
time();
gotoxy(5,5);
cout<<"Access To Main Program !!!"< gotoxy(25,9);
cout<<"^_^_^_^_^_^_LOADING_^_^_^_^_^_^";
gotoxy(25,10);
cout<<"[";
gotoxy(55,10);
cout<<"]";
gotoxy(25,11);
cout<<"^_^_^_^_^_^_LOADING_^_^_^_^_^_^";
gotoxy(25,15);
cout<<"Loading....."< for(int a=0;a<=28;a++)
{
gotoxy(26+a,10);
cout<<'\x1';
delay(10000);
}
for(int b=0;b<=28;b++)
{
gotoxy(26+b,10);
cout<<'\x4';
delay(10000);
}
clrscr();
textcolor(LIGHTBLUE);
}
}
void delay(int time)
{
for(int a=0;a<=time;a++)
for(int b=0;b<=time;b++);
}
void menu()
{
pil:
gotoxy(25,1);
time();
cout< cout<<"\t\t\tæææææææææææææææææææææææææææææææææ"< cout<<"\t\t\tö Menu Utama Game ö"< cout<<"\t\t\tæææææææææææææææææææææææææææææææææ"< cout<<"\t\t\tö\t\t\t\tö"< cout<<"\t\t\tö 1. Tic Tac Toe ö"< cout<<"\t\t\tö 2. Petunjuk Program ö"< cout<<"\t\t\tö 3. Nama Pembuat Program ö"< cout<<"\t\t\tö 4. Exit ö"< cout<<"\t\t\tö\t\t\t\tö"< cout<<"\t\t\tæææææææææææææææææææææææææææææææææ"< cout<<"\t\t\tö Tekan angka(1-4)\t\tö"< cout<<"\t\t\tö Sesuai Pilihan !!!\tö"< cout<<"\t\t\tæææææææææææææææææææææææææææææææææ"< cout<<"\n\n\n\t\t\tMasukkan Pilihan Anda : ";
select=getch();
switch(select)
{
case '1':
clrscr();
proses();
case '2':
textcolor(CYAN);
clrscr();
petunjuk();
getch();
textcolor(GREEN);
clrscr();
goto pil;
case'3':
textcolor(YELLOW);
clrscr();
cout< cout<<"\t\t\tProgram Ini Dipublis Oleh :"< cout<<"\7"< delay(14500);
cout<<"\t\t-------------------------------------------"< delay(14500);
cout<<"\t\t|\t\t\t\7\1\t\t |"< delay(14500);
cout<<"\t\t|Tri Wibowo NIM : DBC 106 039 |"< cout<<"\t\t|\t\t\t\7\2\t\t |"< delay(14500);
cout<<"\t\t-------------------------------------------"< cout<<"\7"< cout<<"\t\t\t\1\1\1 TERIMA KASIH \1\1\1"< cout< delay(15000);
delay(15000);
cout<<"\tTekan sembarang tombol untuk kembali ke Menu!!!"< getch();
textcolor(GREEN);
clrscr();
goto pil;
case '4':
_exit(0);
break;
default:
cout<<"\n";
cout<<"Mohon Maaf......."< cout<<"Pilihan Tidak Ada"< delay(60000);
cout<<"Tekan sembarang tombol";
getch();
clrscr();
goto pil;
}
}
void proses()
{
papan();
int pemenang=0 ;
int counter=0;
for (int i = 0; i < 9 ; i++)
{
board_info[i] = ' ';
}
while(1)
{
pemain();

pemenang=chek_pemenang('\x2');
counter++;
while(pemenang)
{
clrscr();
papan();
cout< cout<<"\t\4\5\6 HORE ANDA MENANG \6\5\4"<//alt+0254 //ini untuk membersihkan papan kalo habis main
for (int i = 0; i < 9 ; i++)
{
board_info[i] = ' ';
}
getch();
clrscr();
menu();
}
while(counter>=9)
{
clrscr();
papan();
cout< cout<<"\t\1\2\3 Upss...Draw \3\2\1"<//ini untuk membersihkan papan kalo habis main
for (int i = 0; i < 9 ; i++)
{
board_info[i] = ' ';
}
getch();
clrscr();
menu();
}
getch();
clrscr();
papan();
computer_movement();
delay(10000);
clrscr();
papan();
pemenang=chek_pemenang('\x5');
counter++;
while(pemenang)
{
clrscr();
papan();
cout< cout<<"\t\7\6\5 Computer yang menang \5\6\7";
//ini untuk membersihkan papan kalo habis main
for (int i = 0; i < 9 ; i++)
{
board_info[i] = ' ';
}
getch();
clrscr();
menu();
}
}
}
void papan()
{
cout<cout<<"-------------"<for( int i=0;i<9;i++)
{
cout<<" | "< if((i+1)%3==0)
cout< }
}
void pemain()
{
int pilihan;
cout<<"Pilihan anda(1-9):";
cin>>pilihan ;

while( pilihan<=0 || pilihan>=10)
{
cout<<"Pilihan anda salah"< cout<<"Pilih lagi : ";
cin>>pilihan;
}

while(board_info[(pilihan-1)]!=' ')
{
cout<<"Pilihan telah ditempati"< cout<<"Pilih lagi : ";
cin>>pilihan;
}
board_info[pilihan-1]='\x2';
}
void computer_movement()
{
if ((int(board_info[0]) + int(board_info[1]) + int(board_info[2])) == (2*int(char('\x2')))+int(char(' ')))
{
if (board_info[0] == ' ') board_info[0] = '\x5';
else if (board_info[1] == ' ') board_info[1] = '\x5';
else if (board_info[2] == ' ') board_info[2] = '\x5';
}
else if ((int(board_info[3]) + int(board_info[4]) + int(board_info[5])) == (2*int(char('\x2')))+int(char(' ')))
{
if (board_info[3] == ' ') board_info[3] = '\x5';
else if (board_info[4] == ' ') board_info[4] = '\x5';
else if (board_info[5] == ' ') board_info[5] = '\x5';
}
else if ((int(board_info[6]) + int(board_info[7]) + int(board_info[8])) == (2*int(char('\x2')))+int(char(' ')))
{
if (board_info[6] == ' ') board_info[6] = '\x5';
else if (board_info[7] == ' ') board_info[7] = '\x5';
else if (board_info[8] == ' ') board_info[8] = '\x5';
}
else if ((int(board_info[0]) + int(board_info[3]) + int(board_info[6])) == (2*int(char('\x2')))+int(char(' ')))
{
if (board_info[0] == ' ') board_info[0] = '\x5';
else if (board_info[3] == ' ') board_info[3] = '\x5';
else if (board_info[6] == ' ') board_info[6] = '\x5';
}
else if ((int(board_info[1]) + int(board_info[4]) + int(board_info[7])) == (2*int(char('\x2')))+int(char(' ')))
{
if (board_info[1] == ' ') board_info[1] = '\x5';
else if (board_info[4] == ' ') board_info[4] = '\x5';
else if (board_info[7] == ' ') board_info[7] = '\x5';
}
else if ((int(board_info[2]) + int(board_info[5]) + int(board_info[8])) == (2*int(char('\x2')))+int(char(' ')))
{
if (board_info[2] == ' ') board_info[2] = '\x5';
else if (board_info[5] == ' ') board_info[5] = '\x5';
else if (board_info[8] == ' ') board_info[8] = '\x5';
}
else if ((int(board_info[0]) + int(board_info[4]) + int(board_info[8])) == (2*int(char('\x2')))+int(char(' ')))
{
if (board_info[0] == ' ') board_info[0] = '\x5';
else if (board_info[4] == ' ') board_info[4] = '\x5';
else if (board_info[8] == ' ') board_info[8] = '\x5';
}
else if ((int(board_info[2]) + int(board_info[4]) + int(board_info[6])) == (2*int(char('\x2')))+int(char(' ')))
{
if (board_info[2] == ' ') board_info[2] = '\x5';
else if (board_info[4] == ' ') board_info[4] = '\x5';
else if (board_info[6] == ' ') board_info[6] = '\x5';
}
else
{
int number = rand()%9;
while (board_info[number] != ' ')
number = rand()%9;
board_info[number] = '\x5';
}
}
int chek_pemenang (char player)
{
if ((board_info[0] == player && board_info[1] == player) && (board_info[2] == player))
return 1;
if ((board_info[3] == player && board_info[4] == player) && (board_info[5] == player))
return 1;
if ((board_info[6] == player && board_info[7] == player) && (board_info[8] == player))
return 1;
if ((board_info[0] == player && board_info[3] == player) && (board_info[6] == player))
return 1;
if ((board_info[1] == player && board_info[4] == player) && (board_info[7] == player))
return 1;
if ((board_info[2] == player && board_info[5] == player) && (board_info[8] == player))
return 1;
if ((board_info[0] == player && board_info[4] == player) && (board_info[8] == player))
return 1;
if ((board_info[2] == player && board_info[4] == player) && (board_info[6] == player))
return 1;
else return 0;
}
void petunjuk()
{
cout<delay(14500);
cout<<"\tPenjelasan program "<cout<<"\t------------- -------------"<cout<<"\t| 1 | 2 | 3 | | |\x2| |"<cout<<"\t------------- -------------"<cout<<"\t| 4 | 5 | 6 | | |\x2| |"<cout<<"\t------------- -------------"<cout<<"\t| 7 | 8 | 9 | | |\x2| |"<cout<<"\t------------- -------------" <delay(14500);
cout<<"\tTekan angka sesuai dengan kolom yang anda pilih"<cout<<"\tCara bermain :"<cout<<"\t Anda menggunakan '\x2' sedangkan komputer ,menggunakan '\x5'"<cout<<"\t Kemenangan diperoleh apabila posisi anda 3 sejajar"<cout<<"\t bisa vertikal, horisontal, maupun miring"<cout<delay(14500);
cout<<"\tTekan sembarang tombol untuk kembali ke menu !!!";
}


Diposting oleh Life is War di 10.42  
0 komentar

Posting Komentar