jester-1.0/ 40775 764 764 0 6531745121 11262 5ustar mattgmattgjester-1.0/Makefile100664 764 764 514 6531740210 12771 0ustar mattgmattg# makefile for jester CC = gcc -Wall -O2 #CC = gcc -Wall -g LIBS = -L/usr/X11R6/lib -lX11 INSTALLDIR = /usr/local jester: jester.c jester.h $(CC) -o jester jester.c $(LIBDIR) $(LIBS) install: jester jester.6 install jester $(INSTALLDIR)/bin/jester install -m 644 jester.6 $(INSTALLDIR)/man/man6/jester.6 clean: rm -f jester jester-1.0/jester.c100444 764 764 67002 6531745121 13040 0ustar mattgmattg/* jester.c */ /* an X enhanced game similar to the boardgame "Othello"(tm) Copyright (C) 1998 Matthew Grossman */ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include"jester.h" static int exit_flag = 0; static int current_player = WHITE; /* white moves first */ static char *exit_text = "EXIT"; static int game_over = 0; char *program_name = "jester"; int main(int argc, char *argv[]) { char *display_name = NULL; int rv = 0; char *s = NULL; display = NULL; gc = NULL; jester_font = 0; legal_cursor = 0; /* random numbers for the ai */ srand(time(NULL)); if(get_bool_option(argc, argv, "-help") || get_bool_option(argc, argv, "-h") || get_bool_option(argc, argv, "--help")) { print_usage(); goto DONE; } if(!(display_name = get_display_name(argc, argv))) { fprintf(stderr, "Cannot get display name\n"); goto ERROR; } screen_number = get_screen_number(display_name); if(!(display = XOpenDisplay(display_name))) { fprintf(stderr, "cannot open display.\n"); goto ERROR; } if(get_bool_option(argc, argv, "-2players")) ai = 0; else if((s = get_text_option(argc, argv, "-ai"))) { if(strcmp(s, "white") == 0) ai = WHITE; else if(strcmp(s, "black") == 0) ai = BLACK; else { fprintf(stderr, "No such color %s\n", s); goto ERROR; } } else ai = BLACK; if(set_up_stuff(argc, argv)) goto ERROR; if(ai == WHITE) make_ai_move(); if(handle_events()) goto ERROR; DONE: if(display_name) free(display_name); clean_up_stuff(); exit(rv); ERROR: rv = 1; goto DONE; } int handle_events() /* the event loop */ { XEvent event; int rv = 0; struct jester_data *jd = NULL; int (*func)(); while(!exit_flag) { XNextEvent(display, &event); if(!XFindContext(display, event.xany.window, jester_context, (XPointer *)&jd)) { func = (int (*)())proper_function(jd, &event); if(!func) continue; (*func)(&event); } } return(rv); } caddr_t proper_function(struct jester_data *jd, XEvent *event) /* return the proper function for the proper event type */ { caddr_t rv = 0; switch(event->type) { case Expose: if(event->xexpose.count == 0) rv = (caddr_t)jd->expose_function; break; case ButtonPress: rv = (caddr_t)jd->buttonpress_function; break; default: } return(rv); } int set_up_stuff(int argc, char *argv[]) /* create and display the windows, initialize gc, etc. */ { int rv = 0; set_up_colors(); set_up_pieces(); set_up_fonts(); set_up_cursors(); jester_context = XUniqueContext(); create_board_window(argc, argv); gc = XCreateGC(display, board_window, 0, NULL); create_exit_button(); create_score_window(); create_player_id_window(); create_square_windows(); mark_legal_squares(current_player); return(rv); } void set_up_fonts() { jester_font = XLoadFont(display, "fixed"); } void set_up_colors() /* set up the colors */ { XColor exact_color, real_color; Colormap colormap; colormap = DefaultColormap(display, screen_number); XAllocNamedColor(display, colormap, "green", &exact_color, &real_color); green = real_color.pixel; XAllocNamedColor(display, colormap, "black", &exact_color, &real_color); black = real_color.pixel; XAllocNamedColor(display, colormap, "white", &exact_color, &real_color); white = real_color.pixel; XAllocNamedColor(display, colormap, "Gray35", &exact_color, &real_color); black_piece = real_color.pixel; XAllocNamedColor(display, colormap, "Gray50", &exact_color, &real_color); black_highlight = real_color.pixel; XAllocNamedColor(display, colormap, "Gray20", &exact_color, &real_color); black_shadow = real_color.pixel; XAllocNamedColor(display, colormap, "ivory2", &exact_color, &real_color); white_piece = real_color.pixel; XAllocNamedColor(display, colormap, "ivory", &exact_color, &real_color); white_highlight = real_color.pixel; XAllocNamedColor(display, colormap, "ivory3", &exact_color, &real_color); white_shadow = real_color.pixel; return; } void set_up_pieces() /* set up the board */ { int i = 0, j = 0; for(i = 0; i < 8; i++) for(j = 0; j < 8; j++) pieces[i][j] = EMPTY; /* place the initial four pieces */ pieces[3][3] = WHITE; pieces[4][4] = WHITE; pieces[3][4] = BLACK; pieces[4][3] = BLACK; return; } char * get_display_name(int argc, char *argv[]) /* return a pointer to the display name, or the value of $DISPLAY, or NULL */ { char *s = NULL; char *rv = NULL; /* the "display" flags are "-d", "-display", "--display" */ if((s = get_text_option(argc, argv, "-d")) || (s = get_text_option(argc, argv, "-display")) || (s = get_text_option(argc, argv, "--display")) || (s = getenv("DISPLAY"))) { rv = (char *)malloc(strlen(s) + 1); strcpy(rv, s); goto DONE; } else { fprintf(stderr, "get_display_name: cannot get display name.\n"); goto ERROR; } DONE: #ifdef DEBUG if(rv) fprintf(stderr, "get_display_name: display = %s\n", rv); #endif return(rv); ERROR: rv = NULL; goto DONE; } int get_screen_number(char *display_name) /* get the number of the screen from the display name */ { char *s = NULL; int rv = 0; if((s = strrchr(display_name, '.')) == NULL) goto DONE; s += 1; rv = atoi(s); DONE: #ifdef DEBUG fprintf(stderr, "get_screen_number: screen number is %d\n", rv); #endif return(rv); } int square_expose(XEvent *event) /* handle an expose event in a square */ { int rv = 0; struct jester_data *jd = NULL; XFindContext(display, event->xexpose.window, jester_context, (XPointer *)&jd); if(pieces[jd->coords.x][jd->coords.y] == EMPTY) goto DONE; draw_piece(pieces[jd->coords.x][jd->coords.y], event->xexpose.window); DONE: return(rv); } void draw_piece(int player, Window window) /* draw a color piece in window */ { unsigned long color = 0, shadow = 0, highlight = 0; XPoint points[3]; int i = 0; if(player == WHITE) { color = white_piece; shadow = white_shadow; highlight = white_highlight; } else { color = black_piece; shadow = black_shadow; highlight = black_highlight; } XSetForeground(display, gc, color); XFillRectangle(display, window, gc, 0, 0, SQUARE_WIDTH, SQUARE_WIDTH); XSetForeground(display, gc, highlight); for(i = 0; i < 8; i++) { points[0].x = i; points[0].y = SQUARE_WIDTH - (i + 1); points[1].x = i; points[1].y = i; points[2].x = SQUARE_WIDTH - (i + 1); points[2].y = i; XDrawLines(display, window, gc, points, 3, CoordModeOrigin); } XSetForeground(display, gc, shadow); for(i = 0; i < 8; i++) { points[0].x = SQUARE_WIDTH - (i + 1); points[0].y = i; points[1].x = SQUARE_WIDTH - (i + 1); points[1].y = SQUARE_WIDTH - (i + 1); points[2].x = i; points[2].y = SQUARE_WIDTH - (i + 1); XDrawLines(display, window, gc, points, 3, CoordModeOrigin); } XSetForeground(display, gc, highlight); for(i = 8; i < 12; i++) { points[0].x = SQUARE_WIDTH - (i + 1); points[0].y = i; points[1].x = SQUARE_WIDTH - (i + 1); points[1].y = SQUARE_WIDTH - (i + 1); points[2].x = i; points[2].y = SQUARE_WIDTH - (i + 1); XDrawLines(display, window, gc, points, 3, CoordModeOrigin); } XSetForeground(display, gc, shadow); for(i = 8; i < 12; i++) { points[0].x = i; points[0].y = SQUARE_WIDTH - (i + 1); points[1].x = i; points[1].y = i; points[2].x = SQUARE_WIDTH - (i + 1); points[2].y = i; XDrawLines(display, window, gc, points, 3, CoordModeOrigin); } /* XSetForeground(display, gc, color); */ /* XFillArc(display, window, gc, 0, 0, SQUARE_WIDTH, SQUARE_WIDTH, 0, */ /* 360 * 64); */ return; } unsigned long color_from_player(int player) { unsigned long rv = 0; if(player == WHITE) rv = white_piece; else if(player == BLACK) rv = black_piece; else fprintf(stderr, "unknown player %d\n", player); return(rv); } int legal_move(int x, int y, int player) /* is current_player playing on x, y a legal move? and how many pieces will be flipped? */ { int rv = 0; if(pieces[x][y] == EMPTY) { if(y > 1) if((rv = flip_line(x, y, up, player, 1))) { goto SUCCESS; } if(x < 6 && y > 1) if((rv = flip_line(x, y, up_right, player, 1))) { goto SUCCESS; } if(x > 1 && y > 1) if((rv = flip_line(x, y, up_left, player, 1))) { goto SUCCESS; } if(x < 6) if((rv = flip_line(x, y, right, player, 1))) { goto SUCCESS; } if(x > 1) if((rv = flip_line(x, y, left, player, 1))) { goto SUCCESS; } if(x > 1 && y < 6) if((rv = flip_line(x, y, down_left, player, 1))) { goto SUCCESS; } if(y < 6) if((rv = flip_line(x, y, down, player, 1))) { goto SUCCESS; } if(y < 6 && x < 6) if((rv = flip_line(x, y, down_right, player, 1))) { goto SUCCESS; } } DONE: return(rv); SUCCESS: /* rv = 1; */ goto DONE; } int square_buttonpress(XEvent *event) /* handle a buttonpress in a square */ { struct jester_data *jd = NULL; int rv = 0; int x = 0, y = 0; XFindContext(display, event->xany.window, jester_context, (XPointer *)&jd); x = jd->coords.x; y = jd->coords.y; if(!legal_move(x, y, current_player)) goto DONE; if(current_player == ai) goto DONE; if(current_player == WHITE) { pieces[x][y] = WHITE; current_player = BLACK; } else { pieces[x][y] = BLACK; current_player = WHITE; } draw_piece(pieces[x][y], event->xany.window); flip_pieces(jd->coords); update_score_window("White = %d Black = %d"); show_player_id(); mark_legal_squares(current_player); XFlush(display); if(!can_player_move(current_player)) { game_over = 1; update_score_window("GAME OVER White = %d Black = %d"); show_restart(); goto DONE; } if(ai == current_player) make_ai_move(); else goto DONE; if(!can_player_move(current_player)) { game_over = 1; update_score_window("GAME OVER White = %d Black = %d"); show_restart(); goto DONE; } DONE: return(rv); } int board_expose(XEvent *event) /* handle an expose event on the board */ { return(0); } struct jester_data * new_jd() /* return a new struct jester_data */ { struct jester_data *jd = NULL; jd = (struct jester_data *)calloc(1, sizeof(struct jester_data)); return(jd); } void delete_jd(struct jester_data *jd) /* duh */ { if(jd) free(jd); return; } void clean_up_stuff() /* dispose of globals, etc. */ { struct jester_data *jd = NULL; int i = 0, j = 0; if(jester_font) XUnloadFont(display, jester_font); if(legal_cursor) XFreeCursor(display, legal_cursor); if(gc) XFreeGC(display, gc); if(display) { for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { XFindContext(display, square_windows[i][j], jester_context, (XPointer *)&jd); delete_jd(jd); } } XFindContext(display, exit_button_window, jester_context, (XPointer *)&jd); delete_jd(jd); XFindContext(display, score_window, jester_context, (XPointer *)&jd); delete_jd(jd); XFindContext(display, player_id_window, jester_context, (XPointer *)&jd); delete_jd(jd); XFindContext(display, board_window, jester_context, (XPointer *)&jd); delete_jd(jd); XDestroyWindow(display, board_window); XCloseDisplay(display); } return; } int flip_pieces(struct coords coords) /* flip over pieces that should be flipped, unless we are just testing */ { int new_x = 0, new_y = 0; int new_color = 0; int rv = 0; new_x = coords.x; new_y = coords.y; new_color = pieces[new_x][new_y]; if(new_y > 1) { if(other_color(pieces[new_x][new_y - 1], new_color)) rv = flip_line(new_x, new_y, up, new_color, 0); } if(new_x < 6 && new_y > 1) { if(other_color(pieces[new_x + 1][new_y - 1], new_color)) rv = flip_line(new_x, new_y, up_right, new_color, 0); } if(new_x > 1 && new_y > 1) { if(other_color(pieces[new_x - 1][new_y - 1], new_color)) rv = flip_line(new_x, new_y, up_left, new_color, 0); } if(new_x < 6) { if(other_color(pieces[new_x + 1][new_y], new_color)) rv = flip_line(new_x, new_y, right, new_color, 0); } if(new_x > 1) { if(other_color(pieces[new_x - 1][new_y], new_color)) rv = flip_line(new_x, new_y, left, new_color, 0); } if(new_x > 1 && new_y < 6) { if(other_color(pieces[new_x - 1][new_y + 1], new_color)) rv = flip_line(new_x, new_y, down_left, new_color, 0); } if(new_y < 6) { if(other_color(pieces[new_x][new_y + 1], new_color)) rv = flip_line(new_x, new_y, down, new_color, 0); } if(new_y < 6 && new_x < 6) { if(other_color(pieces[new_x + 1][new_y + 1], new_color)) rv = flip_line(new_x, new_y, down_right, new_color, 0); } return(rv); } int check_piece(int x, int y, int color) /* if pieces[x][y] is color, return 1, if EMPTY return -1, else 0 */ { int rv = 0; if(pieces[x][y] == color) rv = 1; else if(pieces[x][y] == EMPTY) rv = -1; return(rv); } int flip_line(int x, int y, enum directions direction, int new_color, int testp) /* follow a line of pieces, if it is terminated by a piece of the same color, flip all intervening pieces, unless we are just testing */ { int i = 0, j = 0; int rv = 0; int counter = 0; int flipp = 0; /* a flag: if it is 1, we want to flip */ switch(direction) { case up: for(i = x, j = y - 1, counter = 0; j >= 0; j--, counter++) { if((flipp = check_piece(i, j, new_color)) != 0 ) break; } break; case up_right: for(counter = 0, i = x + 1, j = y - 1; i < 8 && j >= 0; i++, j--, counter++) { if((flipp = check_piece(i, j, new_color)) != 0 ) break; } break; case right: for(counter = 0, i = x + 1, j = y; i < 8; i++, counter++) { if((flipp = check_piece(i, j, new_color)) != 0 ) break; } break; case down_right: for(counter = 0, i = x + 1, j = y + 1; i < 8 && j < 8; i++, j++, counter++) { if((flipp = check_piece(i, j, new_color)) != 0 ) break; } break; case down: for(counter = 0, i = x, j = y + 1; j < 8; j++, counter++) { if((flipp = check_piece(i, j, new_color)) != 0 ) break; } break; case down_left: for(counter = 0, i = x - 1, j = y + 1; i >= 0 && j < 8; i--, j++, counter++) { if((flipp = check_piece(i, j, new_color)) != 0 ) break; } break; case left: for(counter = 0, i = x - 1, j = y; i >= 0; i--, counter++) { if((flipp = check_piece(i, j, new_color)) != 0 ) break; } break; case up_left: for(counter = 0, i = x - 1, j = y - 1; i >= 0 && j >= 0; i--, j--, counter++) { if((flipp = check_piece(i, j, new_color)) != 0 ) break; } break; } if(!testp && flipp == 1 && counter) really_flip_line(x, y, i, j, new_color); if(flipp == 1 && counter) rv = counter; return(rv); } void really_flip_line(int start_x, int start_y, int end_x, int end_y, int new_color) /* actually do the work of flipping the line of pieces */ { int i = 0, j = 0; int x_increment = 0, y_increment = 0; if(start_x < end_x) x_increment = 1; else if(start_x == end_x) x_increment = 0; else x_increment = -1; if(start_y < end_y) y_increment = 1; else if(start_y == end_y) y_increment = 0; else y_increment = -1; for(i = start_x, j = start_y; i != end_x || j != end_y; i += x_increment, j += y_increment) { pieces[i][j] = new_color; draw_piece(new_color, square_windows[i][j]); } return; } int other_color(int s, int c) { int rv = 0; if(s != EMPTY && s != c) rv = 1; return(rv); } int count_pieces(int player) /* how many pieces does player have? */ { int rv = 0; int i = 0, j = 0; for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { if(pieces[i][j] == player) rv++; } } return(rv); } int can_player_move(int player) /* can player make a move, or must he forfeit the turn? */ { int rv = 0; int i = 0, j = 0; for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { if(legal_move(i, j, player)) { rv = 1; goto DONE; } } } DONE: return(rv); } int other_player(int player) { int rv = 0; if(player == WHITE) rv = BLACK; else rv = WHITE; return(rv); } int exit_button_expose(XEvent *event) { int rv = 0; int x = 0, y = 0; x = center_text_x(display, EXIT_BUTTON_WIDTH, exit_text, jester_font); y = center_text_y(display, BUTTON_HEIGHT, jester_font); XSetFont(display, gc, jester_font); XSetForeground(display, gc, black); XSetBackground(display, gc, green); XDrawString(display, event->xexpose.window, gc, x, y, exit_text, strlen(exit_text)); return(rv); } int center_text_x(Display *display, int width, char *text, Font font) { return((width - get_string_width(display, text, font)) / 2); } int center_text_y(Display *display, int height, Font font) { return(height - get_font_height(display, font) / 2); } int exit_button_buttonpress(XEvent *event) { int rv = 0; exit_flag = 1; return(rv); } int get_font_height(Display *display, Font font) /* returns the ascent + descent of font */ { int rv = 0; XFontStruct *fstruct = NULL; fstruct = XQueryFont(display, font); if(!fstruct) { fprintf(stderr, "font_height: font %ld does not exist.\n", font); goto ERROR; } rv = fstruct->ascent + fstruct->descent; DONE: if(fstruct) XFreeFontInfo(NULL, fstruct, 1); return(rv); ERROR: rv = 1; goto DONE; } int get_string_width(Display *display, char *s, Font font) /* width of s in font */ { XFontStruct *fstruct = NULL; int rv = 0; fstruct = XQueryFont(display, font); if(!fstruct) { fprintf(stderr, "string_width: cannot find font %d.\n", (int)font); goto ERROR; } rv = XTextWidth(fstruct, s, strlen(s)); XFreeFontInfo(NULL, fstruct, 1); DONE: return(rv); ERROR: goto DONE; } int score_window_expose(XEvent *event) { if(game_over) return(update_score_window("GAME OVER White = %d Black = %d")); else return(update_score_window("White = %d Black = %d")); } int update_score_window(char *format) /* update the score window after each move */ { int black_pieces = 0, white_pieces = 0; int rv = 0; char text[256]; int x = 0, y = 0; XClearWindow(display, score_window); black_pieces = count_pieces(BLACK); white_pieces = count_pieces(WHITE); /* sprintf(text, "White = %d Black = %d", white_pieces, black_pieces); */ sprintf(text, format, white_pieces, black_pieces); x = center_text_x(display, SCORE_WINDOW_WIDTH, text, jester_font); y = center_text_y(display, BUTTON_HEIGHT, jester_font); XSetForeground(display, gc, black); XSetFont(display, gc, jester_font); XDrawString(display, score_window, gc, x, y, text, strlen(text)); return(rv); } int player_id_window_expose(XEvent *event) /* if the game is ongoing, show the color of the current player; if it's over, offer to restart */ { int rv = 0; if(game_over == 0) show_player_id(); else show_restart(); return(rv); } void show_player_id() { unsigned long color = 0; if(current_player == WHITE) color = white; else color = black; XSetForeground(display, gc, color); XFillRectangle(display, player_id_window, gc, 0, 0, PLAYER_ID_WINDOW_WIDTH, BUTTON_HEIGHT); return; } void show_restart() /* show "RESTART" */ { char text[] = "RESTART"; int x = 0, y = 0; XClearWindow(display, player_id_window); x = center_text_x(display, PLAYER_ID_WINDOW_WIDTH, text, jester_font); y = center_text_y(display, BUTTON_HEIGHT, jester_font); XSetForeground(display, gc, black); XSetFont(display, gc, jester_font); XDrawString(display, player_id_window, gc, x, y, text, strlen(text)); } int player_id_window_buttonpress(XEvent *event) /* if the game is over, restart; else do nothing */ { int rv = 0; int i = 0, j = 0; if(game_over) { game_over = 0; current_player = WHITE; show_player_id(); set_up_pieces(); update_score_window("White = %d Black = %d"); for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { if(pieces[i][j] == EMPTY) XClearWindow(display, square_windows[i][j]); else draw_piece(pieces[i][j], square_windows[i][j]); } } mark_legal_squares(current_player); if(ai == current_player) make_ai_move(); } return(rv); } void set_up_cursors() /* set up the cursors */ { legal_cursor = XCreateFontCursor(display, XC_hand2); return; } void mark_legal_squares(int player) /* set the cursor properly for player */ { int i = 0, j = 0; for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { if(legal_move(i, j, player)) XDefineCursor(display, square_windows[i][j], legal_cursor); else XUndefineCursor(display, square_windows[i][j]); } } return; } void create_board_window(int argc, char *argv[]) { struct jester_data *jd = NULL; XSizeHints *xsh = NULL; XClassHint *xch = NULL; XWMHints *xwmh = NULL; XTextProperty window_name; XTextProperty icon_name; int x = 0, y = 0; /* location of the toplevel window */ xsh = XAllocSizeHints(); xsh->flags = (PMinSize | PMaxSize); xsh->min_height = BOARD_HEIGHT; xsh->max_height = BOARD_HEIGHT; xsh->min_width = BOARD_WIDTH; xsh->max_width = BOARD_WIDTH; if(get_us_position(argc, argv, &x, &y)) { xsh->flags |= USPosition; xsh->x = x; xsh->y = y; } xch = XAllocClassHint(); xch->res_name = program_name; xch->res_class = program_name; xwmh = XAllocWMHints(); xwmh->flags = StateHint; xwmh->initial_state = NormalState; XStringListToTextProperty(argv, argc, &window_name); XStringListToTextProperty(&program_name, 1, &icon_name); board_window = XCreateSimpleWindow(display, RootWindow(display, screen_number), x, y, BOARD_WIDTH, BOARD_HEIGHT, 0, 0, green); jd = new_jd(); jd->expose_function = board_expose; XSaveContext(display, board_window, jester_context, (XPointer)jd); XSetWMProperties(display, board_window, &window_name, &icon_name, argv, argc, xsh, xwmh, xch); XFree(xsh); XFree(xch); XFree(xwmh); XMapWindow(display, board_window); return; } void create_exit_button() { struct jester_data *jd = NULL; exit_button_window = XCreateSimpleWindow(display, board_window, BORDER_WIDTH, BORDER_WIDTH, EXIT_BUTTON_WIDTH, BUTTON_HEIGHT, 1, black, green); jd = new_jd(); jd->expose_function = exit_button_expose; jd->buttonpress_function = exit_button_buttonpress; XSaveContext(display, exit_button_window, jester_context, (XPointer)jd); XSelectInput(display, exit_button_window, ExposureMask | ButtonPressMask); XMapRaised(display, exit_button_window); return; } void create_score_window() { struct jester_data *jd = NULL; score_window = XCreateSimpleWindow(display, board_window, EXIT_BUTTON_WIDTH + (BORDER_WIDTH * 2), BORDER_WIDTH, SCORE_WINDOW_WIDTH, BUTTON_HEIGHT, 1, black, green); jd = new_jd(); jd->expose_function = score_window_expose; XSaveContext(display, score_window, jester_context, (XPointer)jd); XSelectInput(display, score_window, ExposureMask); XMapRaised(display, score_window); return; } void create_player_id_window() { struct jester_data *jd = NULL; player_id_window = XCreateSimpleWindow(display, board_window, EXIT_BUTTON_WIDTH + SCORE_WINDOW_WIDTH + (BORDER_WIDTH * 3), BORDER_WIDTH, PLAYER_ID_WINDOW_WIDTH, BUTTON_HEIGHT, 1, black, green); jd = new_jd(); jd->expose_function = player_id_window_expose; jd->buttonpress_function = player_id_window_buttonpress; XSaveContext(display, player_id_window, jester_context, (XPointer)jd); XSelectInput(display, player_id_window, ExposureMask | ButtonPressMask); XMapRaised(display, player_id_window); return; } void create_square_windows() { struct jester_data *jd = NULL; int i = 0, j = 0; for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { square_windows[i][j] = XCreateSimpleWindow(display, board_window, (i * SQUARE_WIDTH) + BORDER_WIDTH, (j * SQUARE_WIDTH) + (BORDER_WIDTH * 2) + BUTTON_HEIGHT, SQUARE_WIDTH, SQUARE_WIDTH, 1, black, green); jd = new_jd(); jd->expose_function = square_expose; jd->buttonpress_function = square_buttonpress; jd->coords.x = i; jd->coords.y = j; XSaveContext(display, square_windows[i][j], jester_context, (XPointer)jd); XSelectInput(display, square_windows[i][j], ExposureMask | ButtonPressMask); XMapRaised(display, square_windows[i][j]); } } return; } char *get_text_option(int argc, char *argv[], char *option) /* return a pointer to the option's value, or NULL */ { char *rv = NULL; int i = 0; for(i = 0; i < argc; i++) { if(strcmp(argv[i], option) == 0) { rv = argv[i + 1]; goto DONE; } } DONE: return(rv); } void make_ai_move() /* make an ai move, ai is current player */ { struct coords move; sleep(1); get_best_ai_move(&move); pieces[move.x][move.y] = current_player; if(current_player == WHITE) { current_player = BLACK; } else { current_player = WHITE; } draw_piece(pieces[move.x][move.y], square_windows[move.x][move.y]); flip_pieces(move); update_score_window("White = %d Black = %d"); show_player_id(); mark_legal_squares(current_player); return; } void get_best_ai_move(struct coords *coords) /* put the best move in coords, ai is current_player */ { int i = 0, j = 0, counter = 0, tmp = 0; for(i = 0; i < 8; i++) { for(j = 0; j < 8; j++) { tmp = legal_move(i, j, current_player); if(tmp > counter) { coords->x = i; coords->y = j; counter = tmp; } else if(tmp == counter) { if(rand() % 100 < 50) { coords->x = i; coords->y = j; } } } } return; } int get_bool_option(int argc, char *argv[], char *option) { int i = 0, rv = 0; for(i = 0; i < argc; i++) { if(strcmp(argv[i], option) == 0) { rv = 1; goto DONE; } } DONE: return(rv); } void print_usage() { printf("jester [{-display,-d,--display} display-name]\n[{-h,-help,--help}] [-2players] [-ai {white | black}]\n"); return; } int get_us_position(int argc, char *argv[], int *x, int *y) /* user specifies position through "-geometry +x+y" */ { char *s = NULL, *copy = NULL, *p1 = NULL, *p2 = NULL; int rv = 0; /* 1 if success */ if((s = get_text_option(argc, argv, "-geometry")) || (s = get_text_option(argc, argv, "-g")) || (s = get_text_option(argc, argv, "--geometry"))) { copy = (char *)malloc(strlen(s) + 1); strcpy(copy, s); p1 = copy; while(*p1 != '+') /* in case a regular geometry string has been passed */ p1++; p1++; /* skip the initial '+' */ p2 = p1; while(*p1 && isdigit(*p1)) p1++; *p1 = '\0'; *x = atoi(p2); p1++; p2 = p1; while(*p1 && isdigit(*p1)) p1++; *p1 = '\0'; *y = atoi(p2); rv = 1; } return(rv); } jester-1.0/jester.h100444 764 764 10635 6531745121 13045 0ustar mattgmattg/* jester.h */ /* header file for jester Copyright (C) 1998 Matthew Grossman */ /* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #ifndef _JESTER_H #define _JESTER_H /*********************/ /* includes */ #include #include #include #include #include #include #include #include #include #include /*********************/ /* defines */ #define EMPTY 0 #define WHITE 1 #define BLACK 2 #define SQUARE_WIDTH 50 #define BORDER_WIDTH 10 #define BUTTON_HEIGHT 20 #define EXIT_BUTTON_WIDTH 50 #define SCORE_WINDOW_WIDTH 200 #define PLAYER_ID_WINDOW_WIDTH 50 #define BOARD_WIDTH ((SQUARE_WIDTH * 8) + (BORDER_WIDTH * 2)) #define BOARD_HEIGHT ((SQUARE_WIDTH * 8) + (BORDER_WIDTH * 3) + (BUTTON_HEIGHT)) /*********************/ /* globals */ enum directions { up = 1, up_right, right, down_right, down, down_left, left, up_left }; struct coords { int x; int y; }; struct jester_data { int (*expose_function)(); int (*buttonpress_function)(); struct coords coords; }; Display *display; int screen_number; Window board_window; Window square_windows[8][8]; Window score_window; Window exit_button_window; Window player_id_window; int pieces[8][8]; GC gc; unsigned long green; unsigned long black; unsigned long white; unsigned long white_piece; unsigned long black_piece; unsigned long white_shadow; unsigned long white_highlight; unsigned long black_shadow; unsigned long black_highlight; XContext jester_context; int num_black_pieces; int num_white_pieces; Font jester_font; Cursor legal_cursor; int ai; /* which side, if any, is the computer playing? */ /*********************/ /* functions */ char *get_display_name(int argc, char *argv[]); int get_screen_number(char *display_name); int set_up_stuff(int argc, char *argv[]); int handle_events(); void set_up_fonts(); void set_up_colors(); void set_up_pieces(); caddr_t proper_function(struct jester_data *jd, XEvent *event); int square_expose(XEvent *event); void draw_piece(int player, Window window); unsigned long color_from_player(int player); int legal_move(int x, int y, int player); int square_buttonpress(XEvent *event); int board_expose(XEvent *event); struct jester_data *new_jd(); void delete_jd(struct jester_data *jd); void clean_up_stuff(); int flip_pieces(struct coords coords); int check_piece(int x, int y, int color); int flip_line(int x, int y, enum directions direction, int new_color, int testp); void really_flip_line(int start_x, int start_y, int end_x, int end_y, int new_color); int other_color(int s, int c); int count_pieces(int player); int can_player_move(int player); int other_player(int player); int exit_button_expose(XEvent *event); int exit_button_buttonpress(XEvent *event); int center_text_x(Display *display, int width, char *text, Font font); int center_text_y(Display *display, int height, Font font); int get_font_height(Display *display, Font font); int get_string_width(Display *display, char *s, Font font); int score_window_expose(XEvent *event); int update_score_window(char *format); int player_id_window_expose(XEvent *event); void show_player_id(); void show_restart(); int player_id_window_buttonpress(XEvent *event); void set_up_cursors(); void mark_legal_squares(int player); void create_board_window(int argc, char *argv[]); void create_exit_button(); void create_score_window(); void create_player_id_window(); void create_square_windows(); char *get_text_option(int argc, char *argv[], char *option); void make_ai_move(); void get_best_ai_move(struct coords *move); int get_bool_option(int argc, char *argv[], char *option); void print_usage(); int get_us_position(int argc, char *argv[], int *x, int *y); #endif /* !_JESTER_H */ jester-1.0/jester.6100664 764 764 3666 6527425647 12772 0ustar mattgmattg.TH JESTER 6 "jester 1.0" "5/16/1998" \" -*- nroff -*- .SH NAME jester \- an X-based game similar to \fIOthello\fR(tm) .SH SYNOPSIS .B jester [{\-display, \-d, \-\-display} \fIdisplay\fP] [{\-h, \-help, \-\-help}] [\-2player] [\-ai {white, black}] .SH DESCRIPTION .B jester is an X-based game similar to the board game \fIOthello\fR. \fIjester\fR allows you to waste hours of otherwise productive time that could be spent playing Solitaire instead. .SH GAME PLAY There are two players, one playing the white pieces, the other playing the black pieces. Each player takes turns placing a piece of his or her color on an 8x8 board. Each piece must be placed so as to bracket a line of the opponent's pieces between two of the current player's pieces. When this happens, the opponent's pieces are replaced by pieces of the current player's color. .PP When the cursor is over a square that it is legal for the current player to play in, the cursor will change to a pointing hand. A small rectangle in the upper right side of the window shows the color of the current player. .PP The game ends when a player cannot make a move, either because the board is full or because the position of the pieces makes it impossible to make a move. At this point, the player with the most pieces on the board is the winner. You will be offered the option of starting a new game. .PP You may exit at any time by clicking the \(lqExit\(rq button. .SS OPTIONS .TP .I \-d, \-display, \-\-display display Display on \fIdisplay\fR. .TP .I \-h, \-help, \-\-help Print a help message and exit. .TP .I \-2player Play against another person at the same console. .TP .I \-ai {white, black} Tell the computer which side to play. The default behavior is equivalent to \fI\-ai black\fR. .SH ENVIRONMENT .B jester uses the environment variable \(lqDISPLAY\(rq as the default display. This can be overridden on the command line. .SH BUGS None known... .SH AUTHOR Matt Grossman \(la\fImattg@oz.net\fR\(rajester-1.0/COPYING100664 764 764 43105 6410627225 12435 0ustar mattgmattg GNU GENERAL PUBLIC LICENSE Version 2, June 1991 Copyright (C) 1989, 1991 Free Software Foundation, Inc. 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Preamble The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Library General Public License instead.) You can apply it to your programs, too. When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things. To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it. For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights. We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software. Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations. Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all. The precise terms and conditions for copying, distribution and modification follow. GNU GENERAL PUBLIC LICENSE TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you". Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does. 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program. You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee. 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions: a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change. b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License. c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.) These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it. Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program. In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License. 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following: a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or, c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.) The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable. If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code. 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance. 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it. 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License. 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program. If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances. It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice. This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License. 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License. 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns. Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation. 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally. NO WARRANTY 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. END OF TERMS AND CONDITIONS How to Apply These Terms to Your New Programs If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms. To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. Copyright (C) 19yy This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA Also add information on how to contact you by electronic and paper mail. If the program is interactive, make it output a short notice like this when it starts in an interactive mode: Gnomovision version 69, Copyright (C) 19yy name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details. The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program. You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names: Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker. , 1 April 1989 Ty Coon, President of Vice This General Public License does not permit incorporating your program into proprietary programs. If your program is a subroutine library, you may consider it more useful to permit linking proprietary applications with the library. If this is what you want to do, use the GNU Library General Public License instead of this License. jester-1.0/README100664 764 764 2103 6531744220 12232 0ustar mattgmattgREADME file for jester 1.0 jester is an X-based board game similar to Othello. It can be played head-to-head on the console or by a single player against the computer. jester allows you to waste valuable time that could otherwise be spent playing Solitaire. REQUIREMENTS jester requires only the basic X libraries (libX11); it does not use any toolkits. If you want to build jester you need an ANSI C compiler, such as gcc. INSTALLATION Untar the distribution. It should create the directory jester-1.0 and create the files: COPYING README Makefile jester.6 jester.c jester.h Edit jester-1.0/Makefile for your site. Type "make". You should produce an executable named "jester". Verify that the program is working and then type "make install" as root. This will install jester in /usr/local/bin/ and the man page in /usr/local/man/man6. If you have any problems or comments please mail mattg@oz.net jester 1.0 is Copyright (C) 1998 by Matthew Grossman and is distributed under the terms of the GNU General Public License. See the file COPYING for further details. Enjoy!