Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Link Soal : https://www.dropbox.com/s/sse2lot7e6ohdl9/O1-T0316-RG01-09.pdf?dl=0
- #include <stdio.h>
- #include <sys/sem.h>
- #include <sys/shm.h>
- #include <sys/ipc.h>
- #include <sys/types.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- struct data{
- char pilih[10];
- };
- int main(){
- int lawan=0;
- int skor=0;
- struct sembuf s;
- int h=shmget(123456,sizeof(struct data),IPC_CREAT|0666);
- int e=semget(123456,1,IPC_CREAT|0666);
- struct data *suit=(struct data*)shmat(h,NULL,0666);
- semctl(e,0,SETVAL,0);
- do{
- s.sem_num=0;
- s.sem_op=-1;
- s.sem_flg=0;
- semop(e,&s,1);
- srand(time(NULL));
- lawan=rand()%3;
- if(strcasecmp(suit->pilih,"exit")!=0){
- printf("You Choose : %s\n",suit->pilih);
- if(lawan==2)
- printf("Enemy Choose : Gunting\n");
- else if(lawan==1)
- printf("Enemy Choose : Batu\n");
- else if(lawan==0)
- printf("Enemy Choose : Kertas\n");
- if((lawan==2&&strcasecmp(suit->pilih,"Batu")==0)||(lawan==1&&strcasecmp(suit->pilih,"Kertas")==0)||(lawan==0&&strcasecmp(suit->pilih,"Gunting")==0)){
- printf("You Win\n");
- skor+=5;
- }
- else if((lawan==2&&strcasecmp(suit->pilih,"Gunting")==0)||(lawan==1&&strcasecmp(suit->pilih,"Batu")==0)||(lawan==0&&strcasecmp(suit->pilih,"Kertas")==0))
- printf("You Draw\n");
- else{
- printf("You Lose\n");
- skor-=3;
- }
- printf("Total Score is : %d\n",skor);
- }
- else
- break;
- printf("\n");
- }while(1);
- shmdt(&s);
- shmctl(h,IPC_RMID,0);
- semctl(e,0,IPC_RMID);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement