Changeset 5569:ce502c95f097 in roaraudio


Ignore:
Timestamp:
07/17/12 00:34:59 (12 years ago)
Author:
phi
Branch:
default
Phase:
public
Message:

Improved tic-tac-toe plugin.

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • ChangeLog

    r5567 r5569  
    33        * Renamed protocol plugins for roard to protocol-* (Closes: #258) 
    44        * roard now tries to auto load missing protocols as plugins (Closes: #275) 
     5        * Improved tic-tac-toe plugin. 
    56 
    67v. 1.0beta3 - Sun Jul 15 2012 26:08 CEST 
  • plugins/universal/tic-tac-toe.c

    r5461 r5569  
    5050#define _DISPLAY_BUFFER_LEN 1024 
    5151 
    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 --" 
    5373#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 --" 
    5778 
    5879typedef char game_state[9]; 
     
    190211} 
    191212 
     213static 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 
    192233static void draw_game(int client, game_state * state, const char * info) { 
    193234 struct roar_buffer * buf; 
     
    235276} 
    236277 
    237 static char input2id(char c) { 
     278static 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 
    238298 if ( c > '9' ) 
    239299  return c; 
     
    271331 if ( check_won(cs->protoinst) ) { 
    272332  for (i = 0; i < len; i++) { 
    273    if ( buf[i] == 'q' ) { 
     333   if ( map_input(buf[i]) == 'q' ) { 
    274334    clients_delete(client); 
    275335    return -1; 
     
    283343 
    284344 for (i = 0; i < len; i++) { 
    285   c = input2id(buf[i]); 
     345  c = map_input(buf[i]); 
    286346  switch (c) { 
    287347   case 'q': 
    288348     clients_delete(client); 
    289349     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; 
    290363    break; 
    291364   case '0': case '1': case '2': case '3': case '4': 
Note: See TracChangeset for help on using the changeset viewer.