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 !!!"<
cout<<"^_^_^_^_^_^_LOADING_^_^_^_^_^_^";
gotoxy(25,10);
cout<<"[";
gotoxy(55,10);
cout<<"]";
gotoxy(25,11);
cout<<"^_^_^_^_^_^_LOADING_^_^_^_^_^_^";
gotoxy(25,15);
cout<<"Loading....."<
{
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<
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-------------------------------------------"<
cout<<"\t\t|\t\t\t\7\1\t\t |"<
cout<<"\t\t|Tri Wibowo NIM : DBC 106 039 |"<
cout<<"\t\t-------------------------------------------"<
delay(15000);
cout<<"\tTekan sembarang tombol untuk kembali ke Menu!!!"<
textcolor(GREEN);
clrscr();
goto pil;
case '4':
_exit(0);
break;
default:
cout<<"\n";
cout<<"Mohon Maaf......."<
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<
for (int i = 0; i < 9 ; i++)
{
board_info[i] = ' ';
}
getch();
clrscr();
menu();
}
while(counter>=9)
{
clrscr();
papan();
cout<
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<
//ini untuk membersihkan papan kalo habis main
for (int i = 0; i < 9 ; i++)
{
board_info[i] = ' ';
}
getch();
clrscr();
menu();
}
}
}
void papan()
{
cout<
{
cout<<" | "<
cout<
}
void pemain()
{
int pilihan;
cout<<"Pilihan anda(1-9):";
cin>>pilihan ;
while( pilihan<=0 || pilihan>=10)
{
cout<<"Pilihan anda salah"<
cin>>pilihan;
}
while(board_info[(pilihan-1)]!=' ')
{
cout<<"Pilihan telah ditempati"<
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<
cout<<"\tPenjelasan program "<
cout<<"\tTekan angka sesuai dengan kolom yang anda pilih"<
cout<<"\tTekan sembarang tombol untuk kembali ke menu !!!";
}