]> git.somenet.org - pub/jan/sysprog.git/blob - ttt/ttt-client.c
GITOLITE.txt
[pub/jan/sysprog.git] / ttt / ttt-client.c
1 /**************\r
2 * Name:         ttt-client.c\r
3 * Author:       Jan Vales (e0726236@student.tuwien.ac.at)\r
4 * Date:         3.12.2009\r
5 * Purpose:      Gameclient. Handles userinput.\r
6 * Usage:        ttt-client\r
7 **************/\r
8 \r
9 \r
10 #include "globals.h"\r
11 \r
12 static int nSharedMemID = -1; /* ID for shared memory */\r
13 static int semIDa = -1; /* semaphore 1 ID */\r
14 static int semIDb = -1; /* semaphore 2 ID */\r
15 static sharedmem_t *pSharedMem = (sharedmem_t *) -1;  /* pointer on sharedmem_t */\r
16 \r
17 char *szCommand = "";\r
18 void bailOut(); /* Forward declaration */\r
19 \r
20 \r
21 /* prints error messages if an error has occoured */\r
22 void bailOut(char *szMessage) {\r
23     if(errno!=0) {\r
24         (void)fprintf(stderr,"%s: %s - %s\n", szCommand,szMessage,strerror(errno));\r
25     }else{\r
26         (void)fprintf(stderr,"%s: %s\n", szCommand,szMessage);\r
27     }\r
28     exit(EXIT_FAILURE);\r
29 }\r
30 \r
31 /* handles signals */\r
32 void handler(int nSignal) {\r
33     exit(EXIT_SUCCESS);\r
34 }\r
35 \r
36 \r
37 \r
38 /* Game stuff starts here */\r
39 \r
40 void restartGame(){\r
41         int i,j;\r
42         (void) fprintf(stdout,"Ursprüngliches Feld\n");\r
43         for(i=0; i < 3; i++) {\r
44                 (void) fprintf(stdout,"%c, %c, %c\n", pSharedMem->field[i][0], pSharedMem->field[i][1],pSharedMem->field[i][2]);\r
45         }\r
46         for(i=0;i<3;i++) {\r
47                 for(j=0;j<3;j++) {\r
48                         pSharedMem->field[i][j]=0;\r
49                 }\r
50         }\r
51         (void) fprintf(stdout,"Feld zurückgesetzt\n");\r
52         pSharedMem->status = -1;\r
53 }\r
54 \r
55 int main(int argc, char **argv) {\r
56         char line[MAX_CHAR] = "";\r
57         int i;\r
58         szCommand = argv[0];\r
59 \r
60         if(argc > 1){\r
61                 (void) fprintf(stderr,"USAGE: %s\n", szCommand);\r
62                 exit(EXIT_FAILURE);\r
63         }\r
64 \r
65         /* INIT SEM + SHM*/     \r
66         if ((nSharedMemID = shmget(SHM_KEY,sizeof(sharedmem_t),PERM))==-1) {\r
67                 bailOut("Konnte nicht zum Server verbinden!");\r
68         }\r
69         if ((pSharedMem = (sharedmem_t *) shmat(nSharedMemID,(const void *) 0,0))==(sharedmem_t *) -1) {\r
70                 bailOut("Konnte nicht zum Server verbinden!");\r
71         }\r
72         if ((semIDa=semgrab(SEM_KEY_A))==-1) {\r
73                 bailOut("Konnte nicht zum Server verbinden!");\r
74         }\r
75         if ((semIDb=semgrab(SEM_KEY_B))==-1) {\r
76                 bailOut("Konnte nicht zum Server verbinden!");\r
77         }\r
78 \r
79         /* register handler */\r
80         (void) signal(SIGINT, handler);\r
81         (void) signal(SIGTERM, handler);\r
82         (void) signal(SIGQUIT, handler);\r
83 \r
84         /* the game routine */\r
85         (void) fprintf(stderr,"Connected to server. idle.\n");\r
86 \r
87         while(1) {\r
88                 if((P(semIDa))==-1)bailOut("Could not P semaphoreA");\r
89                 switch(pSharedMem->status) {\r
90                         case 0:\r
91                                 fprintf(stdout,"Du bist am zug!\n");\r
92                                 for(i=0; i < 3; i++) {\r
93                                         fprintf(stdout,"%c, %c, %c\n", pSharedMem->field[i][0], pSharedMem->field[i][1],pSharedMem->field[i][2]);\r
94                                 }\r
95                                 break;\r
96                         case -1:\r
97                                 fprintf(stdout,"Wegen des cheatens wurde des Spiel neu gestartet!\n");\r
98                                 restartGame();\r
99                                 break;\r
100                         case 2:\r
101                                 fprintf(stdout,"Du hast gewonnen!\n");\r
102                                 restartGame();\r
103                                 break;\r
104                         case 3:\r
105                                 fprintf(stdout,"Server hat gewonnen!\n");\r
106                                 restartGame();\r
107                                 break;\r
108                         case 4:\r
109                                 fprintf(stdout,"Unentsschieden!\n");\r
110                                 restartGame();\r
111                                 break;\r
112                 }\r
113                 fgets(line,MAX_CHAR-1,stdin);\r
114                 if(ferror(stdin))bailOut("Error: while reading from stdin!\n");\r
115                 switch(line[0]) {\r
116                         case 's': \r
117                                 for(i=0; i < 3; i++) {\r
118                                         fprintf(stdout,"%c, %c, %c\n", pSharedMem->field[i][0], pSharedMem->field[i][1],pSharedMem->field[i][2]);\r
119                                 }\r
120                                 break;\r
121 \r
122                         case 'p':\r
123                                 if((line[2]<49) | (line[2]>51) | (line[3]<49) | (line[3]>51)){\r
124                                         (void) fprintf(stdout,"Ungültige eingabe Werte 1-3 eingeben!\n");\r
125                                         break;\r
126                                 }\r
127                                 if(pSharedMem->field[(line[2]-49)][(line[3]-49)] == 0){\r
128                                         pSharedMem->field[(line[2]-49)][(line[3]-49)]='X';\r
129                                         pSharedMem->status = 1;\r
130                                 }else{\r
131                                         (void) fprintf(stdout,"Feld belegt!\n");\r
132                                 }\r
133                                 break;\r
134                         case 'c':\r
135                                 if((line[2]<49) | (line[2]>51) | (line[3]<49) | (line[3]>51) | (line[5]<49) | (line[5]>51) | (line[6]<49) | (line[6]>51)){\r
136                                         (void) fprintf(stdout,"Ungültige eingabe Werte 1-3 eingeben!\n");\r
137                                         break;\r
138                                 }\r
139                                 if((pSharedMem->field[(line[2]-49)][(line[3]-49)] == 0) & (pSharedMem->field[(line[5]-49)][(line[6]-49)] == 0)){\r
140                                         pSharedMem->field[(line[2]-49)][(line[3]-49)]='X';\r
141                                         pSharedMem->field[(line[5]-49)][(line[6]-49)]='X';\r
142                                         pSharedMem->status = 1;\r
143                                 }else{\r
144                                         (void) fprintf(stdout,"Mindestens 1 Feld belegt!\n");\r
145                                 }\r
146                                 break;\r
147                         case 'n':\r
148                                 restartGame();\r
149                                 break;\r
150                         case 'q':\r
151                                 if((V(semIDb))==-1)bailOut("Can not V semaphoreB\n");\r
152                                 exit (EXIT_SUCCESS);\r
153                         default: printf("befehl nicht erkannt\n");\r
154                         case 'h':\r
155                                 printf("s - Spielfeld anzeigen (dies wird nach jedem Zug automatisch gemacht)\n");\r
156                                 printf("p <xy> - Symbol platzieren auf Position xy (wobei (1,1) die Ecke links oben bezeichnet\n");\r
157                                 printf("c <xy> <xy> - zwei Symbole platzieren (Cheat-Modus)\n");\r
158                                 printf("n - Neues Spiel starten\n");\r
159                                 printf("q - Spiel beenden (Position bleibt weiterhin bestehen!)\n");\r
160                 }\r
161                 if((V(semIDb))==-1)bailOut("Can not V semaphoreB");\r
162         }\r
163         /* return 1337 for the guy who reached this impossible state!*/\r
164         return 1337; \r
165 }\r
166 \r