#include #include #include #include #include #include #include #include #include #include #include #include #include #include "kalaha.h" #include "kalaha_io.h" void readmove (int player) { char buf[100]; int c; while (1) { flushinp (); sprintf (buf, "Player %c: Your Move? ", (player==PLAYERA)?'A':'B'); mvaddstr (20, 0, buf); refresh (); c = getch (); if (c == KEY_MOUSE) { MEVENT event; if (getmouse (&event) != OK) continue; if ((event.bstate & BUTTON1_DOUBLE_CLICKED) == 0) continue; if ((player == PLAYERA) && ((event.y >18) || (event.y <= 11))) continue; if ((player == PLAYERB) && ((event.y <4) || (event.y >= 11))) continue; if ((event.x % 10 < 1) || (event.x % 10 > 8)) continue; c = (event.x / 10); if ((c < 1) || (c > 6)) continue; if (player == PLAYERB) c = 7-c; c += '0'; } if ((c < '1') || (c > '6')) continue; sprintf (buf, " Your Move: %c%c ", (player==PLAYERA)?'A':'B', c); mvaddstr (0, 40-strlen(buf)/2, buf); refresh (); return; } } /* Signal handler for some signals that will flush the input queue and * cleanup the ncurses library before exiting. */ void handler (sig) { flushinp (); endwin (); exit (1); } int main () { int i, sock, lastmove; kalaha_state * state = kalaha_create (PLAYERA); FILE * file = fopen ("input", "r"); char line[100]; struct sigaction act; /* Install signal handlers */ sigemptyset (&act.sa_mask); act.sa_flags = 0; act.sa_handler = handler; sigaction (SIGTERM, &act, NULL); sigaction (SIGINT, &act, NULL); sigaction (SIGQUIT, &act, NULL); assert (state); state->player = PLAYERNONE; assert (file); /* Initialize ncurses. */ initscr (); cbreak (); noecho (); nonl (); intrflush (stdscr, FALSE); keypad (stdscr, TRUE); /* Enable mouse events. */ mousemask (ALL_MOUSE_EVENTS, NULL); lastmove = -1; /* Handle conversation with server. */ while (1) { /* Read a line until we reach EOF. */ if (fgets (line, 100, file) == NULL) break; if (strncasecmp (line, "STATE", 5) == 0) { break; } /* Parse a line that tells us the last move. */ if (strncasecmp (line, "MOVE ", 5) == 0) { char * p = line+4; while ((*p) && isspace ((int)*p)) p++; switch (*p) { case 'a': case 'A': lastmove = A1; p++; break; case 'b': case 'B': lastmove = B1; p++; break; default: lastmove = -1; break; } if ((lastmove >= 0) && ('1' <= (*p)) && ((*p) <= '6')) { lastmove += ((*p) - '1'); p++; } else { lastmove = -1; } while (isspace ((int)*p)) p++; if (*p) { lastmove = -1; } continue; } } if (readstate (file, state)) { drawstate (state, lastmove); readmove (state->player); } mvaddstr (20, 0, "Press return to terminate "); refresh (); fclose (file); flushinp (); getch (); endwin (); return 0; }