Advertisement
akevintg

(SERVER) CLIENT-SERVER (Session 9)

Jan 5th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.50 KB | None | 0 0
  1. //Link Soal : https://www.dropbox.com/s/sse2lot7e6ohdl9/O1-T0316-RG01-09.pdf?dl=0
  2.  
  3. #include <stdio.h>
  4. #include <sys/sem.h>
  5. #include <sys/shm.h>
  6. #include <sys/ipc.h>
  7. #include <sys/types.h>
  8.  
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <time.h>
  12.  
  13. struct data{
  14.     char pilih[10];
  15. };
  16.  
  17. int main(){
  18.     int lawan=0;
  19.     int skor=0;
  20.     struct sembuf s;
  21.     int h=shmget(123456,sizeof(struct data),IPC_CREAT|0666);
  22.     int e=semget(123456,1,IPC_CREAT|0666);
  23.     struct data *suit=(struct data*)shmat(h,NULL,0666);
  24.     semctl(e,0,SETVAL,0);
  25.     do{
  26.         s.sem_num=0;
  27.         s.sem_op=-1;
  28.         s.sem_flg=0;
  29.         semop(e,&s,1);
  30.         srand(time(NULL));
  31.         lawan=rand()%3;
  32.         if(strcasecmp(suit->pilih,"exit")!=0){
  33.             printf("You Choose : %s\n",suit->pilih);
  34.             if(lawan==2)
  35.                 printf("Enemy Choose : Gunting\n");
  36.             else if(lawan==1)
  37.                 printf("Enemy Choose : Batu\n");
  38.             else if(lawan==0)  
  39.                 printf("Enemy Choose : Kertas\n");
  40.             if((lawan==2&&strcasecmp(suit->pilih,"Batu")==0)||(lawan==1&&strcasecmp(suit->pilih,"Kertas")==0)||(lawan==0&&strcasecmp(suit->pilih,"Gunting")==0)){
  41.                 printf("You Win\n");
  42.                 skor+=5;
  43.             }
  44.             else if((lawan==2&&strcasecmp(suit->pilih,"Gunting")==0)||(lawan==1&&strcasecmp(suit->pilih,"Batu")==0)||(lawan==0&&strcasecmp(suit->pilih,"Kertas")==0))
  45.                 printf("You Draw\n");
  46.             else{
  47.                 printf("You Lose\n");
  48.                 skor-=3;
  49.             }
  50.             printf("Total Score is : %d\n",skor);
  51.         }
  52.         else
  53.             break;
  54.         printf("\n");
  55.     }while(1);
  56.     shmdt(&s);
  57.     shmctl(h,IPC_RMID,0);
  58.     semctl(e,0,IPC_RMID);
  59.     return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement