- Back to Home »
- Membuat Game Benteng Takeshi pada Terminal Menggunakan Pipes
Posted by : Unknown
Jumat, 06 November 2015
Buat game gak harus susah susah. Melalui bahasa C dan metode pipes kita sudah bisa membuat game sederhana dan memainkannya melalui terminal pada linux.
Oke diatas adalah contoh ketika game tersebut dijalankan. Game tersebut bertema kan benteng takeshi. Jadi setiap player akan menaruh ranjau pada lubang 1 - 16. Lalu player satunya akan menebak, dan jika tebakan benar maka poin akan menjadi miliknya jika tidak poin akan menjadi milik lawan. Easy? Right Then, berikut adalah source code nya.
Nah anda bisa copy kode diatas dan di paste kan ke file berbeda dan di compile di terminal berbeda sehingga bisa memainkannya bersama.Oke diatas adalah contoh ketika game tersebut dijalankan. Game tersebut bertema kan benteng takeshi. Jadi setiap player akan menaruh ranjau pada lubang 1 - 16. Lalu player satunya akan menebak, dan jika tebakan benar maka poin akan menjadi miliknya jika tidak poin akan menjadi milik lawan. Easy? Right Then, berikut adalah source code nya.
Judul Spoiler:
Player1
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
int fd,fs,fsc,ffe1,ffe2;
int fc,ranjau[5],tebak[4];
int entry[16];
int counterentry=0,counterentry2=0;;
int flagentry=0;
int flagentry2=0;
char *myfifo = "/tmp/fifo1";
char *fifo2="/tmp/fifo2";
char *fifoscore="/tmp/fifoscore";
int flag;
mkfifo(myfifo,0666);
mkfifo(fifoscore,0666);
fd = open(myfifo, O_WRONLY);
fs = open(fifo2,O_RDONLY);
fsc = open(fifoscore,O_RDWR);
int i,j,counter=0;
int scorePlayer[3];
scorePlayer[1]=0;
scorePlayer[2]=0;
//-----------------------------------------------------------------------------------------------
while(counterentry+counterentry2<32){
for(i=0;i<4;i++)
{
ranjau[i]=0;
}
if(counterentry<16){
system("clear");
for(i=0;i<4;i++)
{
ranjau[i]=0;
}
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTER = %d %d\n\n",counterentry,counterentry2);
for(i=0;i<4;i++)
{
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
while(ranjau[i]<0 || ranjau[i]>16)
{
printf("Lubang tersebut tidak tersedia\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
for(j=0;j<counterentry;j++)
{
while(ranjau[i]==entry[j])
{
if(ranjau[i]==0) break;
printf("Anda telah memasukkan ranjau ke lubang tersebut\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
}
if(ranjau[i]!=0)
{
entry[counterentry]=ranjau[i];
counterentry++;
}
if(counterentry>=16) break;
}
}
printf("%d %d %d %d = %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3], counterentry);
ranjau[4]=counterentry;
write(fd,ranjau,sizeof(ranjau));
if(counterentry+counterentry2>=32) break;
//-----------------------------------------------------------------------------------------------
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTERINI = %d %d\n\n",counterentry,counterentry2);
read(fsc,scorePlayer,sizeof(scorePlayer));
read(fs, ranjau, sizeof(ranjau));
counterentry2=ranjau[4];
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTERTEBAK = %d %d\n\n",counterentry,counterentry2);
printf("READ : %d %d %d %d = %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3],counter);
for(i=0;i<4;i++)
{
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
while(tebak[i]<=0 || tebak[i]>16)
{ printf("Lubang tersebut tidak tersedia\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
for(j=0;j<i;j++)
{
while(tebak[i]==tebak[j])
{
printf("Anda telah menebak lubang tersebut\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
}
}
for(j=0;j<4;j++){
flag=0;
for(i=0;i<4;i++)
{
if(ranjau[j]==tebak[i]) flag=1;
if(ranjau[j]==0) flag=2;
}
if(flag==1) scorePlayer[1]=scorePlayer[1]+1;
else if(flag==0) scorePlayer[2]=scorePlayer[2]+1;
}
write(fsc,scorePlayer,sizeof(scorePlayer));
if(counterentry+counterentry2>=32) break;
}
//-----------------------------------------------------------------------------------------------
read(fsc,scorePlayer,sizeof(scorePlayer));
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
if(scorePlayer[1]>scorePlayer[2])
system("zenity --error --title=GAMEOVER --text=PLAYER1WIN");
else if(scorePlayer[1]==scorePlayer[2])
system("zenity --error --title=GAMEOVER --text=DRAW!!");
else
system("zenity --error --title=GAMEOVER --text=PLAYER2WIN");
close (fd);
close (fc);
close (fs);
unlink(myfifo);
return 0;
}
Judul Spoiler:
Player 2
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main(){
int fd,fc,fs,fsc,ffe1,ffe2;
char * myfifo = "/tmp/fifo1";
char *fifo2="/tmp/fifo2";
char *fifoscore="/tmp/fifoscore";
int entry[16];
int counterentry=0,flagentry=0;
int flagentry2=0;
int counterentry2=0;
mkfifo(fifo2,0666);
mkfifo(fifoscore,0666);
int ranjau[5];
int tebak[4];
int i,j;
int scorePlayer[3];
int counterplayer2=0;
int flag=0;
scorePlayer[1]=0;
scorePlayer[2]=0;
fd = open(myfifo, O_RDONLY);
fs = open(fifo2,O_WRONLY);
fsc = open(fifoscore,O_RDWR);
//-----------RRREEEEAAADDDD
while(counterentry+counterentry2<32){
read(fd, ranjau, sizeof(ranjau));
int flagcounter=0;
if(ranjau[0]==0 && ranjau[1]==0 && ranjau[2]==0 && ranjau[3]==0) flagcounter=1;
if(flagcounter==0)
{
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTER = %d %d\n\n",counterentry,counterentry2);
counterentry2=ranjau[4];
printf("READ : %d %d %d %d = %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3],counterentry);
for(i=0;i<4;i++)
{
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
while(tebak[i]<=0 || tebak[i]>16)
{ printf("Lubang tersebut tidak tersedia\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
for(j=0;j<i;j++)
{
while(tebak[i]==tebak[j])
{
printf("Anda telah menebak lubang tersebut\n");
printf("masukkan tebakan ke-%d : ",i+1);
scanf("%d",&tebak[i]);
}
}
}
for(j=0;j<4;j++){
flag=0;
for(i=0;i<4;i++)
{
if(ranjau[j]==tebak[i]) flag=1;
if(ranjau[j]==0) flag=2;
}
if(flag==1) scorePlayer[2]=scorePlayer[2]+1;
else if(flag==0) scorePlayer[1]=scorePlayer[1]+1;
}
}
write(fsc,scorePlayer,sizeof(scorePlayer));
//-------------------------------------------------------------------------------------
if(counterentry<16){
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
//printf("COUNTER = %d %d\n\n",counterentry,counterentry2);
for(i=0;i<4;i++)
{
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
while(ranjau[i]<0 || ranjau[i]>16)
{
printf("Lubang tersebut tidak tersedia\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
for(j=0;j<counterentry;j++)
{
while(ranjau[i]==entry[j])
{
if(ranjau[i]==0) break;
printf("Anda telah memasukkan ranjau ke lubang tersebut\n");
printf("masukkan ranjau ke-%d : ",i+1);
scanf("%d",&ranjau[i]);
}
}
if(ranjau[i]!=0)
{
entry[counterentry]=ranjau[i];
counterentry++;
}
if(counterentry>=16) break;
}
printf("%d %d %d %d %d= %d\n",ranjau[0],ranjau[1],ranjau[2],ranjau[3], ranjau[4], counterentry);
ranjau[4]=counterentry;
write(fs,ranjau,sizeof(ranjau));
read(fsc,scorePlayer,sizeof(scorePlayer));
}
}
write(fsc,scorePlayer,sizeof(scorePlayer));
system("clear");
printf("----------------------------\n");
printf(" \n");
printf(" PLAYER 1 PLAYER 2 \n");
printf(" %d %d \n",scorePlayer[1],scorePlayer[2]);
printf(" \n");
printf("----------------------------\n");
close (fd);
close (fc);
close (fs);
unlink (fifo2);
return 0;
}