Changeset 5569:ce502c95f097 in roaraudio for plugins/universal/tic-tac-toe.c
- Timestamp:
- 07/17/12 00:34:59 (12 years ago)
- Branch:
- default
- Phase:
- public
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
plugins/universal/tic-tac-toe.c
r5461 r5569 50 50 #define _DISPLAY_BUFFER_LEN 1024 51 51 52 #define MSG__YOUR_TURN "It's your turn. -- press any ID or q to quit --" 52 #define _HELP \ 53 "\e[2J\e[H" \ 54 " Tic Tac Toe\r\n" \ 55 " (running: roard " ROAR_VERSION_STRING ")\r\n" \ 56 "\r\n" \ 57 "IDs: Alternative IDs:\r\n" \ 58 " 7 | 8 | 9 a | s | d\r\n" \ 59 "---+---+--- ---+---+---\r\n" \ 60 " 4 | 5 | 6 f |SPC| h\r\n" \ 61 "---+---+--- ---+---+---\r\n" \ 62 " 1 | 2 | 3 j | k | l\r\n" \ 63 "\r\n" \ 64 "Other keys:\r\n" \ 65 " q g , . - Quit\r\n" \ 66 " n + - New game\r\n" \ 67 " H 0 - Help\r\n" \ 68 " e - Exit help / Redraw game\r\n" \ 69 "\r\n" \ 70 "%s\r\n" 71 72 #define MSG__YOUR_TURN "It's your turn. -- press any ID or H for help, q to quit --" 53 73 #define MSG__CANNOT_PUT_HERE "You can not put your mark here. -- try again another position --" 54 #define MSG__WON_PLAYER "You won. -- press any key to continue or q to quit --" 55 #define MSG__WON_COMPUTER "I won. -- press any key to continue or q to quit --" 56 #define MSG__WON_NOBODY "Nobody won. -- press any key to continue or q to quit --" 74 #define MSG__WON_PLAYER "You won. -- press any key to continue or H for help, q to quit --" 75 #define MSG__WON_COMPUTER "I won. -- press any key to continue or H for help, q to quit --" 76 #define MSG__WON_NOBODY "Nobody won. -- press any key to continue or H for help, q to quit --" 77 #define MSG__HELP "-- press e to continue or q to quit --" 57 78 58 79 typedef char game_state[9]; … … 190 211 } 191 212 213 static void draw_help(int client) { 214 struct roar_buffer * buf; 215 ssize_t len = 0; 216 void * data; 217 218 if ( roar_buffer_new_data(&buf, _DISPLAY_BUFFER_LEN, &data) == -1 ) 219 return; 220 221 snprintf(data, _DISPLAY_BUFFER_LEN, _HELP, MSG__HELP); 222 223 len = roar_mm_strlen(data); 224 225 if ( roar_buffer_set_len(buf, len) == -1 ) { 226 roar_buffer_free(buf); 227 clients_delete(client); 228 } 229 230 clients_add_output(client, &buf); 231 } 232 192 233 static void draw_game(int client, game_state * state, const char * info) { 193 234 struct roar_buffer * buf; … … 235 276 } 236 277 237 static char input2id(char c) { 278 static char map_input(char c) { 279 switch (c) { 280 case ',': 281 case '.': 282 case 'g': 283 return 'q'; 284 break; 285 case 'a': return '0'; break; 286 case 's': return '1'; break; 287 case 'd': return '2'; break; 288 case 'f': return '3'; break; 289 case ' ': return '4'; break; 290 case 'h': return '5'; break; 291 case 'j': return '6'; break; 292 case 'k': return '7'; break; 293 case 'l': return '8'; break; 294 case '+': return 'n'; break; 295 case '0': return 'H'; break; 296 } 297 238 298 if ( c > '9' ) 239 299 return c; … … 271 331 if ( check_won(cs->protoinst) ) { 272 332 for (i = 0; i < len; i++) { 273 if ( buf[i]== 'q' ) {333 if ( map_input(buf[i]) == 'q' ) { 274 334 clients_delete(client); 275 335 return -1; … … 283 343 284 344 for (i = 0; i < len; i++) { 285 c = input2id(buf[i]);345 c = map_input(buf[i]); 286 346 switch (c) { 287 347 case 'q': 288 348 clients_delete(client); 289 349 return -1; 350 break; 351 case 'n': 352 new_game(cs->protoinst); 353 draw_game(client, cs->protoinst, MSG__YOUR_TURN); 354 return 0; 355 break; 356 case 'H': 357 draw_help(client); 358 return 0; 359 break; 360 case 'e': 361 draw_game(client, cs->protoinst, MSG__YOUR_TURN); 362 return 0; 290 363 break; 291 364 case '0': case '1': case '2': case '3': case '4':
Note: See TracChangeset
for help on using the changeset viewer.